Problem controlling Newton.

BlitzMax Forums/BlitzMax Programming/Problem controlling Newton.

I need to create a space sim movement in Newton. I started using a falling box tutorial by Haramanai http://www.blitzmax.com/Community/posts.php?topic=51140.
When I change the values for force, Newton does'nt use the new values. Maybe I need to call a function somewhere.
Here's the Newton functions
'===============================================
'------------------- Physics -------------------
'===============================================

Function T_irrGetMatrix(NSceneNode:T_irrISceneNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	Euler[0] = NSceneNode.GetRotation().getX() * (Pi / 180.0)
	Euler[1] = NSceneNode.GetRotation().getY() * (Pi / 180.0)
	Euler[2] = NSceneNode.GetRotation().getZ() * (Pi / 180.0)
	NewtonSetEulerAngle(Varptr Euler[0] , Varptr NewtonMat[0])
	NewtonMat[12] = NSceneNode.getPosition().getX()
	newtonMat[13] = NSceneNode.getPosition().getY()
	NewtonMat[14] = NSceneNode.getPosition().getZ()	
End Function

Function T_irrSetMatrix(NSceneNode:T_irrISceneNode , NewtonMat:Float Ptr)
	Local Euler:Float[3]
	NewtonGetEulerAngle(Varptr NewtonMat[0] , Varptr Euler[0])
	NSceneNode.setPosition(T_irrVector3df.createFromVals(NewtonMat[12] , NewtonMat[13] , NewtonMat[14]))
	NSceneNode.SetRotation(T_irrVector3df.createFromVals(Euler[0] * (180 / Pi) , Euler[1] * (180 / Pi) , Euler[2] * (180 / Pi)))
End Function

Rem
	THE FUNCTIONS FROM TUTORIAL 2
endrem

Function	PhysicsAlloc:Byte Ptr(SizeInBytes:Int)
	Return MemAlloc(SizeInBytes)
End Function

Function    PhysicsFree( MemPtr:Byte Ptr)
	MemFree (MemPtr)
End Function

Function  PhysicsApplyForceAndTorque (Body:Byte Ptr)
	Local Mass:Float
	Local IXX:Float
	Local IYY:Float
	Local IZZ:Float
	'I change the Force values with player input
	'Newton only uses the values if they are preset.
	Global ForceX:Float
	Global ForceY:Float
	Global ForceZ:Float
	Local Force:Float[] =  [-mass * ForceX, -mass * ForceY, -mass * ForceZ]
	Local Torque:Float[] = [TorqueX, TorqueY, TorqueZ]
	NewtonBodyGetMassMatrix (Body,Varptr Mass, Varptr IXX, Varptr IYY, Varptr IZZ);
	NewtonBodyAddForce (Body, Varptr Force[0])
	NewtonBodyAddTorque (Body, Varptr Torque[0])
End Function

Function  PhysicsSetTransform (Body:Byte Ptr, NewtonMat:Float Ptr)
	Local Primitive:T_irrISceneNode
	Primitive = T_irrISceneNode.createFromHandle(Int(NewtonBodyGetUserData(Body)),False)
	
	NewtonBodyGetMatrix(Body , Varptr NewtonMat[0])
	T_irrSetMatrix(Primitive , Varptr NewtonMat[0])
	
End Function

Function PhysicsBodyDestructor(Body:Byte Ptr)
	Local Primitive:T_irrISceneNode
	Primitive = T_irrISceneNode.createFromHandle(Int(NewtonBodyGetUserData(Body)),False)
	Primitive.Remove()
	
End Function

Function CleanUp ()
	'destroy the Newton world
	NewtonDestroy (nWorld);
	
End Function

Function InitScene()
	Global BoxBody:Byte Ptr
	Global Collision:Byte Ptr
	Global mat:Float[16]
	'create the newton world
	
	'set the linear solver model For faster speed 
	NewtonSetSolverModel(nWorld, 8)
	'set the adpative friction model For faster speed 
	NewtonSetFrictionModel(nWorld,
	
	'Create PlayerNode
	Collision = NewtonCreateBox(nWorld, 0.5 , 0.5, 0.5 , Null)
	Local Location:Float[] = [0.0 , 0.0 , 0.0]
	PlayerNode.setPosition(T_irrVector3df.createFromVals(Location[0] ,Location[1] , Location[2]))
	BoxBody = NewtonCreateBody(nWorld , Collision)				
	' GG: changed to store the scenenode C++ object handle instead of a reference to the BMAX object with falls out of scope
	NewtonBodySetUserData(BoxBody ,Byte Ptr(PlayerNode.handle))		
	'set a destrutor For this rigid body
	NewtonBodySetDestructorCallback(BoxBody, PhysicsBodyDestructor)
	'set the tranform call back Function
	NewtonBodySetTransformCallback(BoxBody, PhysicsSetTransform)
	'set the force And torque call back funtion
	NewtonBodySetForceAndTorqueCallback(BoxBody, PhysicsApplyForceAndTorque)
	'set the mass matrix
'	NewtonBodySetMassMatrix (BoxBody, 1.0F, 1.0F / 6.0F, 1.0F / 6.0F, 1.0F  / 6.0F);
	NewtonBodySetMassMatrix(BoxBody, 1.0, 1.0, 1.0, 1.0)				
	'set the matrix For tboth the rigid body and the graphic body
	T_irrGetMatrix(PlayerNode , Varptr mat[0])
	NewtonBodySetMatrix(BoxBody,Varptr mat[0])
	PhysicsSetTransform(BoxBody, Varptr mat[0])
'	Location[0] :+ 0.5 * 2.0

	'Release the collsion geometry when Not needed
	NewtonReleaseCollision(nWorld, Collision)

EndFunction


I found a post in the newton forums about changing the force values during gameplay. http://newtondynamics.com/forum/viewtopic.php?t=2627
I was wondering if someone could help me interpret this code.
This newton wrapper is confussing me.

Move the line
NewtonBodyGetMassMatrix (Body,Varptr Mass, Varptr IXX, Varptr IYY, Varptr IZZ);

to the line above the part there you multiply the mass with the forces.
In the current state you multiply the forcevalues with a zero mass.

Ok, the force was added when I set a value to the force at the begining of the game. My input code changes the values for force.
The mesh still does'nt move with input.
The post I found was about the same problem. They solved it with NewtonbodySetFreeze and NewtonWorldSetAutoFreeze. The post was in C++, I don't know where and how to implement it.

Here's my force and torque function.
Function  PhysicsApplyForceAndTorque (Body:Byte Ptr)
	Local Mass:Float
	Local IXX:Float
	Local IYY:Float
	Local IZZ:Float
	Global ForceX:Float = 0.0
	Global ForceY:Float = 0.0
	Global ForceZ:Float = 10.0
	
	'The mesh only moves if the values if they are preset.
	Global ForceX:Float
	Global ForceY:Float
	Global ForceZ:Float
	Local Force:Float[] =  [-mass * ForceX, -mass * ForceY, -mass * ForceZ]
	Local Torque:Float[] = [TorqueX, TorqueY, TorqueZ]
	NewtonBodyGetMassMatrix (Body,Varptr Mass, Varptr IXX, Varptr IYY, Varptr IZZ);
	NewtonBodyAddForce (Body, Varptr Force[0])
	NewtonBodyAddTorque (Body, Varptr Torque[0])
End Function


You still haven't move that line...

change this...
Local Force:Float[] =  [-mass * ForceX, -mass * ForceY, -mass * ForceZ]
Local Torque:Float[] = [TorqueX, TorqueY, TorqueZ]
NewtonBodyGetMassMatrix (Body,Varptr Mass, Varptr IXX, Varptr IYY, Varptr IZZ);


to this...
NewtonBodyGetMassMatrix (Body,Varptr Mass, Varptr IXX, Varptr IYY, Varptr IZZ);
Local Force:Float[] =  [-mass * ForceX, -mass * ForceY, -mass * ForceZ]
Local Torque:Float[] = [TorqueX, TorqueY, TorqueZ]


When you declare the mass variable as local in the beginning it will be set to 0.
NewtonBodyGetMassMatrix retrieves the mass and inertia values for the body which you when use to apply the correct force.

To make sure the body is always responding to input even if it's gone to sleep you can trigger the function NewtonWorldUnfreezeBody at the same time you press any of the keys.

Using NewtonBodySetAutoFreeze to make a body active all the time works as well, but defeats the purpose of the autosleep function which is important for performance, especially if you have a large amount of bodies.

If you still want to use it you just call it right after the creation of the body.

sorry, this was my code.
Function  PhysicsApplyForceAndTorque (Body:Byte Ptr)
	Local Mass:Float
	Local IXX:Float
	Local IYY:Float
	Local IZZ:Float
	Global ForceX:Float = 0.0
	Global ForceY:Float = 0.0
	Global ForceZ:Float = 10.0
	
	NewtonBodyGetMassMatrix (Body, VarPtr Mass, VarPtr IXX, VarPtr IYY, VarPtr IZZ)
	
	'The mesh only moves if the values if they are preset.
	Global Force:Float[] =  [-mass * ForceX, -mass * ForceY, -mass * ForceZ]
	Global Torque:Float[] = [TorqueX, TorqueY, TorqueZ]
	NewtonBodyAddForce (Body, VarPtr Force[0])
	NewtonBodyAddTorque (Body, VarPtr Torque[0])
End Function


Same problem.
But, how do I setup a newton command out side the newton function, in the input method?
When I added the NewtonWorldUnfreezeBody(nWorld, body) command in my input method I get the error "Identifier 'body' not found". These wrapper modules are confusing.
Thank you.

The PhysicsApplyForceAndTorque (Body:Byte Ptr) is a callback function that the physicsengine will call every time it is updating the physics for every body in the simulation.
The passed parameter is the pointer to the Body it's currently updating.

If you want to affect a body outside this callback function you will have to use your own pointer to this body. In your case I believe it's the BoxBody variable?