Matrices

BlitzMax Forums/MiniB3D Module/Matrices

Warner(Posted 2009) [#1]
Resulting code is in archive, see here:
http://www.blitzmax.com/codearcs/codearcs.php?code=2498

Original post:
---------------------------------------------------------------------------
I'm working on a set of functions that use matrices in minib3d, since I need a global TurnEntity, and an improved parenting system.
It is not complete, but it has Move, Turn and Parent commands.
The idea is that this module can later on be intergrated with minib3d's TEntity and TMatrix.

Because I saw other people working on this as well, and Simon announced he wanted to include quaternions in minib3d, I thought it might be a good idea to post this here. Hopefully someone can jump in with improvements/additions.
So if you can contribute anything to it, please do so. On the other hand, if this is not necessary, please tell me, since I find this matrices/quats stuff somewhat difficult to understand.

To run this, you need Noel's math3d lib from the archive:
http://www.blitzmax.com/codearcs/codearcs.php?code=1463
It is very helpful, well-written and complete. Thanks Noel.

It simulates Blitz3D, however the Yaw is reverted somehow.


slenkar(Posted 2009) [#2]
thanks, should be useful


Warner(Posted 2009) [#3]
Updated: adding 'global' flag for TurnEntity2, and added RotateEntity2.
Updated2: added ScaleEntity2


slenkar(Posted 2009) [#4]
could you run this code and see what happens?



In blitz3d when you parent something to something else,
there is an option to have the child have the position and rotation of the parent

I tried to copy the ships matrix to the bullets matrix but it didnt work.

also rotateentity needs global and local
need entityyaw entitypitch entityroll
please compile in strict mode
when you parent an entity to NULL it crashes the program
thanks


basically i cant get the bullets to have the same rotation as the ship


Warner(Posted 2009) [#5]
That example is still a bit too advanced for these simple routines.
Indeed it is missing the stuff you mention, so that should be a to-do list:
1. Global parameter for Rotate/Position
2. Global parameter for EntityParent
3. Make compatible with Blitz3D
   - invert Yaw ?

As soon as I can work one of these out, I'll post it here.
Any suggestions welcome offcourse.


slenkar(Posted 2009) [#6]
for entityrotate you seem to be doing it globally.

for local rotation you could multiply the matrix from q1.tomatrix() with the parent's matrix. if parent =null just return the matrix from q1.tomatrix()
same goes for position


Warner(Posted 2009) [#7]
EntityRotate2 acts locally. You can see that if you try the following:

So I need to do the opposite: calculate what the rotation/position/scaling should be in order to maintain the original position when the entity is attached to another.
For now, I'm trying to create TFormPoint. Hopefully if I understand that, I can also create the other stuff.

The way it works now, is that each Entity has a matrix. In the method "Update" this matrix is multiplied by each parent matrix. So the scale/rotation/position of each child entity is local.


Warner(Posted 2009) [#8]
Update:TFormPoint2



slenkar(Posted 2009) [#9]
tformpoint works well,

about rotateentity ,if i rotate an entity locally to 0,0,0 shouldnt it face the same direction as its parent?
It does in your example but not mine:




becuase its a sprite?


Warner(Posted 2009) [#10]
Update: EntityParent has a 'global' parameter. Also, improved these global routines. Still not completely complete.
This example turns a 'spaceship' and fires 'bullets' in the same dir as the ship using the 'global' flag:



Warner(Posted 2009) [#11]
tformpoint works well,

about rotateentity ,if i rotate an entity locally to 0,0,0 shouldnt it face the same direction as its parent?
It does in your example but not mine:

Ow excuse me, missed your posted while typing mine. I'll look into that. Global rotations I find difficult, but maybe I can do something.
In the meanwhile, look at the sample above, it fires bullet from a spaceship. (In the opposite direction, but still)

Edit: you're right. There is something strange going on there.
Wait, no, it does seem to work: as long as there is a parent, RotateEntity will be relative to it. I'll try your example.


Warner(Posted 2009) [#12]
Update: added 'global' flag to PositionEntity and RotateEntity:

The orientation is still reversed, but it seems more and more complete.
Todo:
-Keep global scaling as well as rot/pos
-EntityPitch/Yaw/Roll  <-- it is a problem to convert from matrix to euler
-Direction is reversed



slenkar(Posted 2009) [#13]
I tried it with the latest code and it works, thanks


Warner(Posted 2009) [#14]
In the meanwhile, I looked at your code.
If it allready works, it was not needed, but still I'd like to post it:



slenkar(Posted 2009) [#15]
its works with meshes but sprites dont work, the bullet i was using was a sprite,


Warner(Posted 2009) [#16]
Wait .. <edit> .. But it seems to work with sprites as well?



slenkar(Posted 2009) [#17]
i just tried it with sprites and it works! thanks for all that hard work you did


Warner(Posted 2009) [#18]
I now tweaked the wrapper routines so, that the result will match miniB3D/Blitz3D's. Basically, Z is mirrored.



Warner(Posted 2009) [#19]
I'll put this code&updates in the archive.


simonh(Posted 2009) [#20]
Nice work. Beware using vector objects in this kind of code though - the large number of small objects that are generated can clog the GC and slow things down considerably.


Warner(Posted 2009) [#21]
Thank you Simon! I rewrote the math functions it uses so they require no external types. The Methods are Methods for the TMatrix as used in minib3d. Some were allready there, so i've copied them into here for completion:

Now I want to convert the other code so it uses these functions, but it should first be complete.

That brings me down to the following question:

How should I incorporate EntityPitch/EntityYaw/EntityRoll? If I look for conversion routines from matrix to euler, they all seem to suffer from gimbal lock (or worse).
Alternatively, I could add three fields that represent pitch/yaw/roll, that are just used for reading this info, but that would only give the local angles.
Anybody knows what to do?


simonh(Posted 2009) [#22]
I think Mark used some sort of quat -> euler -> pitch/yaw/roll method. I'm not sure on that though...my memory is somewhat hazy. Did you ever downloaded the B3D source code? It was available via svn for Blitz3D SDK owners for a while.


Warner(Posted 2009) [#23]
Ah, I see. Thanks. No, I never got round to downloading the sdk source.
I can imagine it would be a great read.
[edit] hold that thought .. routine seems not right.


Warner(Posted 2009) [#24]
I've now converted everything. Hopefully it still works, I only did a very short test.
This is an extended version of 'TMatrix.bmx':


And this is the code I tried:

The routines for EntityPitch/Yaw/Roll are still missing. I just can't seem to get that right somehow. If this works okay, I will later on update the code archive entry with it.

Todo:
V-EntityPitch/Yaw/Roll
V-further intergration with minib3d
V-Strict compiling (keep forgetting that, sorry)



slenkar(Posted 2009) [#25]
so there's no longer a dependency on cower.math3d?


Warner(Posted 2009) [#26]
Yes, indeed, but it does require a minib3d rebuild.
I now tried to avoid creating a big number of objects, as Simon suggested.
Hopefully later on, these commands can be intergrated with minib3d's common commandset. But I first need to see if and how that would be suitable.
I mean, I have no idea if collisions would still work, how b3d animations are done and if everything works allright.


Warner(Posted 2009) [#27]
Here is an updated version of TEntity.bmx.
It includes the new commands.
Again, not tested thoroughly, but a quick implementation to see it working.

TEntity.bmx:


TMatrix.bmx:


Example program:



Warner(Posted 2009) [#28]
Got it! Just a stupid type in MQ_Move. Instead of transforming the vector, the position of the entity was transformed by the rotation matrix. It was a good idea to have some sleep in the meanwhile. :) Well, updated the above code, and going further with testing it.


Warner(Posted 2009) [#29]
I've now included EntityPitch/Yaw/Roll routines. I translated them from the math code that is posted in the bb.com thread. They seem to work good so far, better than any routine I could find before.

TEntity.bmx


TMatrix.bmx


Example:


It seems pretty complete so far. There are two commands missing that I'm aware of: TranslateEntity, but using the 'global' parameter on MoveEntity2 will do the same, and TFormNormal2, which is TFormVector2, only normalized. It won't be a big hassle to add them.
I now wanted to see if it would be possible to create a TFormRotation command. If possible, that could be handy.


Warner(Posted 2009) [#30]
TFormRotation .. skip that for now. I've updated the code in the archives. Hopefully everything is working correctly now.
http://www.blitzmax.com/codearcs/codearcs.php?code=2498


slenkar(Posted 2009) [#31]
There might be a problem with the version in the code archives.

The Aligntovector2 function doesnt work when I use all the code from the code archives. The cone dances about instead of pointing at the cube.


Warner(Posted 2009) [#32]
Oops .. you're right. I have updated it.


slenkar(Posted 2009) [#33]
yes that fixed that demo,thanks

There still might be something wrong with the code archive though:



Warner(Posted 2009) [#34]
?! They go upside down .. :( Strange. Well, inverting EntityPitch does help, I'll look into it:



slenkar(Posted 2009) [#35]
glad to know its something simple :) thanks


Warner(Posted 2009) [#36]
I updated the TMatrix.bmx code in the archive to invert the outcome of Pitch.
Thanks for letting me know!