newton and max2D

BlitzMax Forums/BlitzMax Programming/newton and max2D

It's not perfect but it works for the begining.
Copy paste the code below.
Then save it anywhere and save the top right image of this page(Blitz RESEARCH) to the folder you saved the code.
Then just run it.
edit:As I am not seeing any posts i will change the code as I find more compartibility


Strict
Framework BRL.Blitz
Import BRL.StandardIO
Import BRL.Max2d
Import BRL.GLMax2D
Import BRL.PNGLoader
Import PUB.Newton

Type ImageNode
	Field Image:TImage
	Field x:Int , y:Int , z:Int = 0
	Field rotX:Float , rotY:Float , rotZ:Float = 0.0
	Field mass:Float
	
	Function Create:ImageNode(ImgStr:String , tmpX:Int , tmpY:Int , tmpRot:Int)
		Local tmpN:ImageNode = New ImageNode
		tmpN.Image = LoadImage(ImgStr)
		tmpN.x = tmpX
		tmpN.y = tmpY
		tmpN.rotZ = tmpRot
		MidHandleImage tmpN.Image
		Return tmpN
	End Function
	
	Method draw()
		SetRotation rotZ
		DrawImage image , x , y
		SetRotation 0
	End Method
	
End Type



Rem '''''''''''''''''''''''''''''''''''/////////////////////////////////////   The Convertion Matrix Functions
		Max2DGetMatrix is the function to get the matrix from the Max2D Image And pass it
	to an array to be used by Newton.
		Max2DSetMatrix is the function to pass the Matrix from Newton to the Max2D Image 
EndRem

Function Max2DGetMatrix(NImageNode:ImageNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	Euler[0] = NImageNode.rotX * (Pi / 180.0)
	Euler[1] = NImageNode.rotY * (Pi / 180.0)
	Euler[2] = NImageNode.rotZ * (Pi / 180.0)
	NewtonSetEulerAngle(Varptr Euler[0] , Varptr newtonMat[0])
	Newtonmat[12] = NImageNode.X
	newtonMat[13] = NImageNode.Y
	NewtonMat[14] = NImageNode.Z	
End Function

Function Max2DSetMatrix(NImageNode:ImageNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	NewtonGetEulerAngle(Varptr NewtonMat[0] , Varptr Euler[0])
	NImageNode.x = NewtonMat[12]
	NImageNode.y = NewtonMat[13]
	NImageNode.z = NewtonMat[14]
	NImageNode.rotX  =  Euler[0] * (180 / Pi)
	NImageNode.rotY  =  Euler[1] * (180 / Pi)
	NImageNode.rotZ  =  Euler[2] * (180 / Pi)
End Function
'////////////////////////////////////////////////////////// Newton
SetGraphicsDriver GLMax2DDriver()
Graphics 640 , 480 , 0 , 0

Local Mat:Float[16]
Local Test:ImageNode = ImageNode.create("brllogo-thin.png" , 200 , 300 , 0)
Local nWorld:Byte Ptr = newtonCreate(Null , Null)
Local collision:Byte Ptr = newtonCreateBox(nWorld , ImageWidth(Test.Image) , ImageHeight(Test.Image) , 10.0  ,Null)
Local NNode:Byte Ptr = newtonCreateBody(nWorld , collision)
Max2DGetMatrix(Test , Varptr Mat[0])
newtonBodySetMassMatrix(NNode , 10.0 , 1.0 , 1.0 , 1.0)
newtonBodySetMatrix(NNode , Varptr Mat[0])
newtonBodySetUserData(NNode ,Varptr Test)
newtonBodySetTransformCallBack(NNode , PhysicsSetTransform)
newtonBodySetForceandTorqueCallback(NNode , PhysicsApplyForceAndTorque)
'''/////////////	The UP VECTOR 
Local UpVector:Float[] = [0.0 , 0.0 , 1.0]
newtonConstraintCreateUpVector(nWorld , Varptr UpVector[0] , NNode) 'this line makes all the work to lock x , y rotation axis
'///////////////////// For omega i puted rotation only to the z axis couse in 2D rotation hapens only in z.
Local Omega:Float[] = [5.0 , 5.0 , 5.0]
newtonBodySetOmega(NNode , Varptr omega[0])

Local Test2:ImageNode = ImageNode.create("brllogo-thin.png" , 200 , 400 , 0)
NNode = newtonCreateBody(nWorld , collision)
Max2DGetMatrix(Test2 , Varptr Mat[0])
newtonBodySetMassMatrix(NNode , 0.0 , 1.0 , 1.0 , 1.0)
newtonBodySetMatrix(NNode , Varptr Mat[0])
newtonBodySetUserData(NNode ,Varptr Test2)
newtonBodySetTransformCallBack(NNode , PhysicsSetTransform)
'newtonBodySetForceandTorqueCallback(NNode , PhysicsApplyForceAndTorque)

newtonReleaseCollision(nWorld , collision)

'//////////////////////////////////////// Newton CallBack Functions
Function PhysicsSetTransform(tmpNNode:Byte Ptr , tmpMat:Float Ptr)
	Local tmpImageNode:ImageNode = Var (ImageNode) Ptr (newtonBodyGetUserData(tmpNNode))
	newtonBodyGetMatrix(tmpNNode , Varptr tmpMat[0])
	Local Euler:Float[] = [0.0 , 0.0 , tmpImageNode.rotZ]
	Max2DSetMatrix(tmpImageNode , Varptr tmpMat[0])
End Function

Function PhysicsApplyForceAndTorque(tmpNNode:Byte Ptr)
	Local Mass:Float
	Local Ixx:Float
	Local Iyy:Float
	Local Izz:Float

	newtonBodyGetMassMatrix(tmpNNode , Varptr Mass , Varptr Ixx , Varptr Iyy , Varptr Izz)
	Local force:Float[] = [0.0 , mass * 9.8 , 0.0]
	newtonBodySetForce(tmpNNode , Varptr force[0])
End Function


'////////////////////////////////////	Main Loop
While Not KeyDown(key_Escape)
	Cls
	newtonUpdate(nWorld , FPS.Calc())
	test.draw()
	test2.draw()	
	newtonBodyGetMatrix(NNode , Varptr Mat[0])
	For Local i:Int = 0 To 15
		DrawText mat[i] , 10 , i * 10
	Next
	DrawText Test.rotZ , 10 , 200
	
	fps.display()
	Flip
	FlushMem
Wend

Type FPS
	' FPS_Counter <> Runs and displays the FPS
	Global Counter, Time, TFPS
	Function Calc%()
		Counter:+1
		If Time < MilliSecs()
			TFPS = Counter' <- Frames/Sec
			Time = MilliSecs() + 1000'Update
			Counter = 0
		EndIf
		Return TFPS
	EndFunction
	Function Display()
			DrawText "FPS: "+FPS.Calc(),10,220
	End Function
EndType



I think to be 100% compartible we will have to do some changes. I am not sure right now.

Any help and any comments are wellcome.

If anyone knows how to lock the z axi of movement pls post some code.

To lock to Z axis couldnt you just pass the same variable to it each pass of the main loop, therefore locking it to the same number?

oops i forgot that. There is a newton function that I used to solve that.(pointed out from a newton user in the newton forum. this function is NewtonWorldForEachBodyDo(NewtonWorld , callbackFunction). And so all the newtonBodies pass from tha callbackFunction and get fixed their z value. I just found out the result and never updated the topic.

Hmmm. I tried this.
The program compiled but didn't do anything (i.e. no graphical window opened etc).
I have the newton.mod in pub.mod, libnewton.a in
my blitzmax/lib directory and I'm using the brllog.png image.
Any ideas?

I also have the newton.dll in my system directory

I want to see more of what newton can do in 2D!

Can't compile due to syntax errors.