Newton Rotation Help needed

BlitzMax Forums/MiniB3D Module/Newton Rotation Help needed

klepto2(Posted 2007) [#1]
Hi, bad news, the newton mod release will be delayed due some rotation problems I have.

I think this is a thing only simon can answer.

My problem is I'm using these 2 function to generate the rotation in eulers for updating the newtonobjects:

[code]
Function Max3DGetMatrix(Node:TNewtonObject , NewtonMat:Float Ptr)
Local Euler:Float[3]
Euler[0] = Node.rotX * Pi / 180.0
Euler[1] = Node.rotY * Pi / 180.0
Euler[2] = Node.rotZ * Pi / 180.0
NewtonSetEulerAngle(Varptr Euler[0] , Varptr newtonMat[0])
Newtonmat[12] = Node.PosX
newtonMat[13] = Node.PosY
NewtonMat[14] = Node.PosZ
End Function

Function Max3DSetMatrix(Node:TNewtonObject , NewtonMat:Float Ptr)
Local Euler:Float[3]
NewtonGetEulerAngle(Varptr NewtonMat[0] , Varptr Euler[0])
Node.Posx = NewtonMat[12]
Node.Posy = NewtonMat[13]
Node.Posz = NewtonMat[14]
Node.rotX = Euler[0] * 180.0 / Pi
Node.rotY = Euler[1] * 180.0 / Pi
Node.rotZ = Euler[2] * 180.0 / Pi
End Function
[/codebox]

I was thinking everything works fine until ihave implemented the debug routines (viewing the real hull rotation ), but the rotation is slightly wrong, sometimes the rotation works, sometimes not. espacially after some time the rotation seems to swap complete the direction when it shouldn't. (You will notice this in my latest video). Any ideas why this is happening?
If it is needed I will post more code or will release the not properly working mod. But I wish someone could point me to the right direction.

thx klepto2


simonh(Posted 2007) [#2]
Need more info if possible - feel free to send me the source code files.


Chris C(Posted 2007) [#3]
I've seen a problem very similar to this before, theres often several possible solutions to a single matrix when converting to eular angles

From memory the newton matrix is laid out at 90 degrees to an opengl one 0=0 1=4 2=8....

matrix to eular is a potential source of problems and computationally not cheap

and the same for eular to matrix

I'd have a go at directly converting from a newton matrix to a minib3d one.

the newton matrix will be missing the scale data from the minib3d one and it might only be a rotation matrix

Let me know if you need any more help.


klepto2(Posted 2007) [#4]
@Chris: yes, something similar I have thought also but I haven't the skill in matrix calculations etc. so it would be nice if you could help me.

@both: I have send you the current module state and a sample app.


Chris C(Posted 2007) [#5]
got it, I'll have to get it running in Linux first as I dont own windows anymore, updates slowed my laptop to a crawl so I binned it!

I'll let you know as soon as I have somthing


klepto2(Posted 2007) [#6]
Oh, on linux you have to download this:
http://www.physicsengine.com/downloads/newtonLinux-1.53.tar.gz

and copy the newton.lib (in the sdk directory) to the minib3d mod folder and recompile it. Then it should work.


Chris C(Posted 2007) [#7]
Wheres it doing stuff like this
Force.Length : 1574.07715

the ide doesnt like lots of console spew sometimes and I cant spot this one off hand

theres a seq fault in the material handling that I haven't quite tracked down

But the rotation thing looks like it might be an axis problem as well

Its getting late so I'll have a look tomorrow if I get chance


Chris C(Posted 2007) [#8]
ok! got the rotation fixed (was actually easier than I thought!) I have cylinders working ok, fixed cubes, but cone and probably some other prims are still way out of wack, gotta trott off, will look at it later


klepto2(Posted 2007) [#9]
thats very good news, could you email me the fix?
the cone problem seems more to be a problem with the orientation of the mesh on startup, try to replace the Rotatemesh cone,0,90,0 with Rotatemesh cone,0,-90,0 or similar.


Chris C(Posted 2007) [#10]
rotate mesh wont work cause it effects the matrix not the actual vert data...

i mailed you what i have so far, scale needs work!

there seems to be a pointer problem as small changes in the example code can intermittently induce seg faults

You'll have to look into creating some custom create primitive code to avoid the bodge-o-matic thats in there for testing purposes

2 of the newton functions dont exist in the newton lib
I'd advise using winmerge or similar to check each file for changes

got to dash, email me directly if you get stuck!


klepto2(Posted 2007) [#11]
You're wrong with RotateMesh, RotateMesh rotates the whole Vertex data. RotateEntity just changes the matrix.

I know of 2 Functions in the lib so I have declared them currently windows only and put them into a ?win32 ? block.

Btw, I have received your mail but there was no attachment with the email. Could you try to resend it?


Chris C(Posted 2007) [#12]
	Method RotateMesh(pitch#,yaw#,roll#)
	
		pitch#=-pitch#
		
		Local mat:TMatrix=New TMatrix
		mat.LoadIdentity()
		mat.Rotate(pitch#,yaw#,roll#)
		TransformMesh(mat)
		
		' set new_bounds to true so MeshWidth/Height/Depth will get new width/height/depth values next time it's called
		new_bounds=True
		
		UpdateBuffer()
	End Method


damn always doing that will resend once finished trawling the forum


klepto2(Posted 2007) [#13]
thx chris for your help :) , Its working now :)

for those who are interrested, this is the new update function:

Method Update()
		newtonBodyGetMatrix(Node , Mat)
		NewtonBodyGetVelocity(Node,Velocity)
		NewtonBodyGetOmega(Node,Omega)
		Max3DSetMatrix(Self,Mat)
		Local mmp:Float Ptr=Mesh.Mat.grid
		mmp[0]=mat[0]
		mmp[1]=mat[1]
		mmp[2]=-mat[2]
		mmp[4]=mat[4]
		mmp[5]=mat[5]
		mmp[6]=-mat[6]
		mmp[8]=mat[8]
		mmp[9]=mat[9]
		mmp[10]=-mat[10]
		mmp[12]=mat[12]'posx
		mmp[13]=mat[13]'posy
		mmp[14]=-mat[14]'-posz
		Mesh.Mat.Scale(-mesh.sx,-mesh.sy,-mesh.sz)
	End Method



simonh(Posted 2007) [#14]
	Method RotateMesh(pitch#,yaw#,roll#)
	
		pitch#=-pitch#
		
		Local mat:TMatrix=New TMatrix
		mat.LoadIdentity()
		mat.Rotate(pitch#,yaw#,roll#)
		TransformMesh(mat)
		
		' set new_bounds to true so MeshWidth/Height/Depth will get new width/height/depth values next time it's called
		new_bounds=True
		
		UpdateBuffer()
	End Method


The local matrix is only used within the function to transform the mesh's vertices, the entity's own matrix is unaffected.