Code archives/3D Graphics - Maths/Matrices in minib3d

This code has been declared by its author to be Public Domain code.

Download source code

Matrices in minib3d by Warner2009
Last update: 25 jun 2009

These routines are intended to extend/replace minib3d commands for rotation/position. They use matrices and quaternions.
It has a working AlignToVector!
Example code is provided below, but first you need to update TMatrix.bmx, TEntity.bmx, TGlobal.bmx and TAnimation.bmx in the minib3d mod folder, and then rebuild.
(!)I've posted these files in the comments below, because there wasn't enough room here. (post #3, #4, #5 and #7)
TGlobal is mostly the same. I added the command "UpdateEntities" to RenderWorld.

Has:
PositionEntity (ent, x#,y#,z#, glob)
TurnEntity (ent, p#,y#,r#, glob)
MoveEntity (ent, x#,y#,z#, glob)
RotateEntity (ent, p#,y#,r#, glob)
EntityParent (ent1, ent2, glob)
ScaleEntity (ent, w#,h#,d#)
TFormPoint (x#,y#,z#, ent1, ent2)
TFormVector (x#,y#,z#, ent1, ent2)
EntityPitch (ent1, glob)
EntityYaw (ent1, glob)
EntityRoll (ent1, glob)
AlignToVector (ent1, x#,y#,z#, ang, rate#)
PointEntity (ent1, ent2) <- was allready present

Update History:
00 - posted
01 - TFormVector2/Global MoveEntity2 flag 
02 - Somehow retaining global scaling was allready present, so skip that one.
03 - Revised structure to fit minib3d/Added EntityPitch,Yaw,Roll functions
04 - solved GetMatrix scaling issue
05 - Retaining global scaling was present due to a conincidence. 
     I changed it so scaling isn't retained when using EntityParent's 
     global flag.
06 - Repaired scaling issue
07 - Revised EntityPitch/Yaw/Roll to match RotateEntity.
08 - Added experimental routines AlignToVector2 and PointEntity2
09 - Replaced AlignToVector2, and removed PointEntity2 (replaced 
     by the original minib3d PointEntity) Also, replaced original 
     commands by their counterparts.
10 - The z-axis of PositionEntity2 was inverted.
     Added a 'rate' parameter to AlignToVector2
     Further improvement of the intergration with minib3d
11 - Updated all matrix routines to match the original minib3d 
     routines. It is now compatible with minib3d examples/demos
12 - Whoops, forgot to include TGlobal. Instead of updating entities every 
     time they are moved, an update flag is set. It is processed on 
     RenderWorld or when the matrix is requested.
13   Added TAnimation.bmx so bones are now compatible with the rest.
14   AlignToVector2 and EntityPitch were affected by the latest update. They now seem to be restored.
15   Fixed Global ScaleEntity flag.

Todo:
* TFormRotation ? .. Not sure if that could work
* General testing/comparing to Blitz3D

Most of the code is based on other people's code. For instance the math3d routines from Nilum. Thanks!
Import sidesign.minib3d

Graphics3D 800, 600, 0, 2

CreateLight()

Local cam:TCamera = CreateCamera()
MoveEntity cam, 0, 0, -15

e1:TEntity = CreateCone()
PositionEntity2 e1, -8,  8, 0

e2:TEntity = CreateCube()
PositionEntity2 e2, -2, 8, 0

e3:TEntity = CreateCube()
PositionEntity2 e3,  3, 8, 0

e4:TEntity = CreateCube()
PositionEntity e4, -8,  2, 0

e5:TEntity = CreateCube()
e6:TEntity = CreateCube()
MoveEntity2 e6, 2, 2, 0
EntityParent2 e6, e5
PositionEntity e5, -2, 2, 4

e7:TEntity = CreateCube()
ScaleEntity2 e7, 1, 2, 1
PositionEntity e7, 4, 2, 0 

e8:TEntity  = CreateCube()
e9:TEntity = CreateCube()
ScaleMesh TMesh(e9), 0.1, 0.1, 0.1
EntityColor e9, 255, 0, 0
PositionEntity2 e8, -8, -4, 0

e10:TEntity = CreateCone()
PositionEntity2 e10, -2, -4, 0

e11:TEntity = CreateCone()
PositionEntity2 e11, 2, -4, 0

Repeat

	TurnEntity2 e2, 1, 2, 3
	
	t = t + 4
	MoveEntity2 e3, Sin(t)*0.1, 0, 0
	
	RotateEntity2 e4, 0, 0, t
	
	TurnEntity2 e5, 0, 1, 0
	
	TurnEntity2 e8, 1, 2, 3
	x#=0 y#=0 z#=3
	TFormPoint2 x#,y#,z#, e8, Null
	PositionEntity2 e9, x, y, z
	
	UpdateWorld
	RenderWorld
			
	BeginMax2D
	
	DrawText "PositionEntity2", 120, 140
	DrawText "TurnEntity2", 300, 140
	DrawText "MoveEntity2", 460, 140
	
	DrawText "RotateEntity2", 120, 300
	DrawText "EntityParent2", 300, 300
	DrawText "ScaleEntity2", 460, 300

	DrawText "TFormPoint2", 120, 460
	DrawText "TFormVector2", 120, 480

	TurnEntity2 e10, 0, 0, 1
	
	pt# = EntityPitch2(e10)
	yw# = EntityYaw2(e10)
	rl# = EntityRoll2(e10)
	
	RotateEntity2 e11, pt, yw, rl
		
	DrawText "EntityPitch2:" + pt, 300, 460
	DrawText "EntityYaw2:  " + yw, 300, 480
	DrawText "EntityRoll2: " + rl, 300, 500

	EndMax2D

	Flip
	
Until KeyHit(key_escape)

End

Comments

slenkar2009
GREAT!


jkrankie2009
Yeah, this will be really useful once you've got those last two features added, nice work :)

Cheers
Charlie


Warner2009
TMatrix.bmx



Warner2009
TEntity.bmx



Warner2009
TGlobal.bmx:



slenkar2009
Tmatrix and Tentity are the same above


Warner2009
TAnimation.bmx



slenkar2009
it seems to work pretty good
how would you get the vector between 2 entities?


Warner2009
I think it should be the difference between their positions:
dx=x2-x1
dy=y2-y1
dz=z2-z1


slenkar2009
oh ok,thanks
what is the difference between axis 1,2, or 3
i looked at the source but still cant work it out

EDIT:

oh axis 3 is z axis 2 is y etc.



this makes the cone point at a cube


Warner2009
Haha .. that's quite nice indeed. Here, I've made the cube move:



slenkar2009
Im installing the new matrix and quat code on a new computer.

the cone seems to flicker instead of pointing at the cube, is there something wrong with my minib3d code?

I thought I installed all the files above properly.


Warner2009
Ow .. sorry, it seems I have messed up AlignToVector in the last update. I forgot to mirror the z-axis, giving this strange result.
I've updated TEntity.bmx, so it fits again to the rest.
Hopefully it works allright again.


FreetimeCoder2009
Heyho,

its me again :D

I really enjoyed working with this, but since Sprites mess up the framerate I would prefer the original idea with TurnEntity2 etc.. I tried to implement the "old" transformationcommands in the new TEntity.bmx etc but with little success. It doesen't crash my app anymore but it just produces weird nonsese :(

Could you tell me which functions I have to exchange/modify?

greetings


Warner2009
Well, I don't remember exactly what I changed. I'm not at home atm, and I can't open the source, so I have to go by memory, which is bad. I believe PositionEntity/RotateEntity/TurnEntity etc and UpdateMat were changed.


FreetimeCoder2009
yes, that is what I changed (plus TFormPoint etc) everything with MQ_Update or something like that in it), but still the screen just shows notihg (just black)

I guess there is still something wrong.

greetings


*2009
Has the code been fixed?


Warner2009
Fixed in the sense of .. ? I didn't really changed anything big in the meanwhile. Just a few minor tweaks.


*2009
Ah from the posts above there seems to be something that broke, just asking if thats been fixed :)


Warner2009
Well, here is my latest mod. I've been using it myself for a few months:
http://www.yousendit.com/transfer.php?action=batch_download&batch_id=TzY1K2VsUnJ6NEtGa1E9PQ
It contains TSceneGraph (for static scenery meshes) TTerrain (roam-based) and the tweaked TEntity.bmx etc.
I also made a basic animation system: RecordFrame, PlayFrame, AddFrame and TweenFrame.


*2009
Thanks Warner :D


Romanski2009
could you upload it again, warner? pls. the link is broken!!


Warner2009
Okay, here it is:
http://www.yousendit.com/transfer.php?action=batch_download&batch_id=MVNkanZxa0RxRTNIRGc9PQ


Romanski2010
damnit. I am too late, again. sorry. link expired


jkrankie2010
@warner, if you send me the zip i can host it permanently. i'm charlie@...

Cheers
Charlie


Panno2010
i need it also please


BlitzSupport2010
I downloaded it from the most recent post, though I haven't played with it at all yet:

http://www.hi-toro.com/blitz/sidesign.zip [3.27 MB]

@Warner, I assume this is OK, but just let me know if you want it removed.


Warner2010
Ah, thanks!


Romanski2010
ok. thanks for the upload. I tried it but it doesn't work for me. some things are strange/reversed/misplaced. I'm looking for a aligntovector-function that works.

and this one works best for me(on top of the standard minib3d-mod):

Function AlignToVector2(ent:TEntity,vx#,vy#,vz#,axis:Int,rate#)

Local pitch#=Abs(EntityPitch(ent))
Local yaw#=Abs(EntityYaw(ent))
Local roll#=Abs(EntityRoll(ent))

Select axis

Case 1

Local dest_yaw#=Abs(ATan2(vz#,vx#))
Local dest_roll#=Abs(ATan2(vy#,vx#))

yaw#=UpdateValue#(yaw#,dest_yaw#,rate#)
roll#=UpdateValue#(roll#,dest_roll#,rate#)

Case 2

Local dest_pitch#=Abs(ATan2(vz#,vy#))
Local dest_roll#=Abs(-ATan2(vx#,vy#))

pitch#=UpdateValue#(pitch#,dest_pitch#,rate#)
roll#=UpdateValue#(roll#,dest_roll#,rate#)

Case 3

Local dest_pitch#=Abs(-ATan2(vy#,vz#))
Local dest_yaw#=Abs(-ATan2(vx#,vz#))

pitch#=UpdateValue#(pitch#,dest_pitch#,rate#)
yaw#=UpdateValue#(yaw#,dest_yaw#,rate#)

End Select

RotateEntity ent,pitch#,yaw#,roll#
TurnEntity ent, 0, 180, 0

End Function



but this is not 100% working either. there's something about this 160/180 degree thing that I haven't figured out yet. maybe someone will fix it someday...


Robert Cummings2010
Same problem here, I need a native aligntovector that works, without any of that matrix stuff because I need to ensure compatibility with C++ version of minib3d...


slenkar2010
we need that bloke from wulfire games with his linear algebra skillz


Warner2010
I've posted a non-matrix version of AlignToVector in the archive.


Krischan2011
For some reason I can't explain there seems to be a bug with the mipmap flag here. I'm currently porting my milkyway background to minib3d and the star background textures are very blurry with your modification. So I switched back to the original 0.53 and without a change at my source the stars are sharp again. But I couldn't find a different between the sources, the textures are loaded with flag 8 and I added ClearTextureFilter() after Graphics3D. Any idea?

Original (as it should look)


Warner


Source+Media+Demos: Download


Code Archives Forum