NewtonBodyTransformCallback problem

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: NewtonBodyTransformCallback problem

Postby JoeJ » Wed Aug 03, 2011 3:27 pm

Henry00 wrote:It's all 0, just declaring 16 floats in an array, helpfile:
Dim is used to create new arrays (the initial value of each element will be zero).


So that will drive newton really crazy and can cause crashes too.
You can get far without knowledge about quaternions, but Transformation Matrices are essential for both Newton and any Kind of 3D Graphics.
There was a similar situation with a guy called Gibbon, look for his posts, we've eplained some of the basics to him, at the end he was successful :)

For now, something like this should fix it:

Dim testmatrix.f(15)
testmatrix(0) = 1
testmatrix(5) = 1
testmatrix(10) = 1
testmatrix(15) = 1
*testcollision = NewtonCreateBox(*physics_nWorld, 5, 5, 5, 0, @testmatrix)

EDIT: Oh already done... crossposting wars... :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBodyTransformCallback problem

Postby Henry00 » Wed Aug 03, 2011 3:31 pm

I did that in my other post, thanks I will try reading some see if it helps, also:


------------ post I wanted to send but I got informed about yours --------------


Ok since the physics is yet under construction and nothing special yet, here is the entire code so you can take a look more globally, questions can be awnsered:

Code: Select all
;  -------------------------------
;  Physics manager is a newton game dynamics wrapper
;  -------------------------------

ImportC "newton.lib"
  NewtonCreate( *NewtonAllocMemoryProcedure, *NewtonFreeMemoryProcedure )
  NewtonSetSolverModel( *NewtonWorld, mode.i)
  NewtonSetPlatformArchitecture( *NewtonWorld, mode.i)
  NewtonSetWorldSize(*NewtonWorld, *minPoint.Vector3D, *maxPoint.Vector3D)
  NewtonUpdate( *NewtonWorld, timestep.f)
  NewtonBodySetDestructorCallback( *NewtonBody, *NewtonBodyDestructorProcedure)
  NewtonCreateBody( *NewtonWorld, *NewtonCollision, *matrix)
  NewtonBodySetUserData( *NewtonBody, *userDataPtr)
  NewtonConvexCollisionCalculateInertialMatrix( *NewtonCollision, *inertia.Vector3D, *origin.Vector3D)
  NewtonBodySetMassMatrix( *NewtonBody, mass.f, Ixx.f, Iyy.f, Izz.f)
  NewtonBodySetCentreOfMass( *NewtonBody, *comPtr.Vector3D)
  NewtonBodySetForceAndTorqueCallback( *NewtonBody, *NewtonApplyForceAndTorqueProcedure)
  NewtonBodySetTransformCallback( *NewtonBody, *NewtonSetTransformProcedure)
  NewtonBodySetMatrix( *NewtonBody, *matrixPtr)
  NewtonBodyGetRotation( *NewtonBody, *rotation.Vector3D)
  NewtonBodyGetUserData( *NewtonBody )
  NewtonBodyGetMassMatrix( *NewtonBody, *mass.f, *Ixx.f, *Iyy.f, *Izz.f)
  NewtonBodySetForce( *NewtonBody, *Force)
  NewtonBodyGetMatrix( *NewtonBody, *matrixPtr)
  NewtonCreateBox( *NewtonWorld, dx.f, dy.f, dz.f, shapeID.i, *offsetMatrix)
  NewtonReleaseCollision( *NewtonWorld, *NewtonCollision)
  NewtonCreateTreeCollision( *NewtonWorld, shapeID.i)
  NewtonTreeCollisionBeginBuild( *NewtonTreeCollision)
  NewtonTreeCollisionEndBuild( *NewtonTreeCollision, optimize.i)
  NewtonTreeCollisionAddFace ( *NewtonTreeCollision, vertexCount.i, *vertexPtr, strideInBytes.i, faceAttribute.i)
  NewtonCollisionForEachPolygonDo ( *NewtonCollision, *matrix, *NewtonCollisionIterator, *userData)
  NewtonWorldGetFirstBody (*NewtonWorld)
  NewtonWorldGetNextBody(*NewtonWorld, *CurrentBody)
  NewtonBodyGetCollision(*NewtonBody)
  NewtonGetMemoryUsed()
EndImport

Procedure physics_allocMemory(SizeInBytes.i)
  ProcedureReturn AllocateMemory(SizeInBytes.i)
EndProcedure

Procedure physics_freeMemory(*memory,SizeInBytes.i)
  ProcedureReturn FreeMemory(*memory)
EndProcedure

Procedure physics_destroyBodyCallback(*NewtonBody)
 
EndProcedure

Procedure physics_ApplyForceAndTorqueCallback(*body, timestep.f, threadIndex.i)
  Ixx.f
   Iyy.f
   Izz.f
   mass.f
 
   ; Add the external Gravity force
   NewtonBodyGetMassMatrix (*body, @mass.f, @Ixx.f, @Iyy.f, @Izz.f)
 
   *gravityForce.Vector3D = Vector3D(0, 0, mass.f * -9.8*3) ; slow..
   NewtonBodySetForce(*body, *gravityForce)
   
EndProcedure

Procedure physics_NewtonBodyTransformCallback(*body, Array *matrix.f(16), threadIndex.i)
;    ; Get the position from the matrix
;    ;*posit.Vector3D = Vector3D(*matrix(12), *matrix(13), *matrix(14))
;    Dim test.f(15)
;    NewtonBodyGetMatrix(*body, @test())
;    *posit.Vector3D = Vector3D(test(12),test(13),test(14))
;    
;    *rotation.Vector3D = Vector3D(0,0,0)

;    ; we will ignore the Rotation part of matrix And use the quaternion rotation stored in the body
;    ;NewtonBodyGetRotation(*body, *rotation)

;    ; get the entity associated With this rigid body
;    *ent.gameobject = NewtonBodyGetUserData(*body)

;    ;*ent\rotation\x = *rotation\x
;    ;*ent\rotation\y = *rotation\y
;    ;*ent\rotation\z = *rotation\z
;    *ent\position\x = *posit\x
;    *ent\position\y = *posit\y
;    *ent\position\z = *posit\z
;
;    Debug "NEW POSITION:"+Str(*ent\position\x)+","+Str(*ent\position\y)+","+Str(*ent\position\z)
;    Debug "A TRANSFORM CALLBACK"

EndProcedure ; -> crash?




; >> Create the Newton World
Global *physics_nWorld = NewtonCreate(@physics_allocMemory(), @physics_freeMemory())

; >> Set solver model
; 0 is the exact solver (default). This is a one pass solver that calculates regular and frictional forces at the same time. Use this model if you require a high degree of accuracy in your simulation.
; n ( > 0 ) is the linear solver. This is a complete iterative solver which peform n iterations per update. Increasing the number of iterations will improve accuracy at the cost of solver speed. Use this model if you require speed over accuracy in your simulation.
NewtonSetSolverModel(*physics_nWorld, 1)

; >> This function allows the application to configure the Newton to take advantage for specific hardware architecture in the same platform.
; 0 - force the hardware lower common denominator for the running platform.
; 1 - will try to use common floating point enhancement like spacial instruction set on the specific architecture. This mode made lead to result that differ from mode 1 and 2 as the accumulation round off errors maybe different.
; 2 - the engine will try to use the best possible hardware setting found in the current platform this is the default configuration. This mode made lead to result that differ from mode 1 and 2 as the accumulation round off errors maybe different.
; the only hardware mode guarantee to work is mode 0. all other are only hints to the engine, for example setting mode 1 will take not effect on CPUs without specially floating point instructions set.
NewtonSetPlatformArchitecture(*physics_nWorld, 1)

; >> Sets the limits for the Newton World. Setting the world size properly helps improve the performance of the engine
NewtonSetWorldSize(*physics_nWorld, Vector3D(-500,-500,-500), Vector3D(500,500,500))



; TEST SETUP
Procedure physics_initialize(*scene)
  Global *obj1.gameobject = GameObject_New()
  Scene_AttachGameObject(*scene,*obj1)
  *mesh1.Mesh = Mesh_New("game/textures/barrel.jpg")
  Moonshine_Import(*mesh1,"game/models/barrel.ms")
  GameObject_AttachMesh(*obj1,*mesh1)
 
  Dim testmatrix.f(15)
  testmatrix(0) = 1
  testmatrix(1) = 0
  testmatrix(2) = 0
  testmatrix(3) = 0
  testmatrix(4) = 0
  testmatrix(5) = 1
  testmatrix(6) = 0
  testmatrix(7) = 0
  testmatrix(8) = 0
  testmatrix(9) = 0
  testmatrix(10) = 1
  testmatrix(11) = 0
  testmatrix(12) = 0
  testmatrix(13) = 0
  testmatrix(14) = 0
  testmatrix(15) = 1
  *testcollision = NewtonCreateBox(*physics_nWorld, 5, 5, 5, 0, @testmatrix)
  Global *testbody = NewtonCreateBody(*physics_nWorld, *testcollision, @testmatrix)
  NewtonReleaseCollision(*physics_nWorld, *testcollision)
  NewtonBodySetMassMatrix(*testbody, 10, 5, 5, 5)
  ;NewtonBodySetUserData(*testbody, *obj1)
  NewtonBodySetForceAndTorqueCallback( *testbody, @physics_ApplyForceAndTorqueCallback() )
  NewtonBodySetTransformCallback( *testbody, @physics_NewtonBodyTransformCallback() )
  NewtonBodySetDestructorCallback( *testbody, @physics_destroyBodyCallback() )
EndProcedure

Procedure physics_keyDown(key)
  If key = #SDLK_F5
    MessageRequester("what the..","Why did you press F5, are you an idiot?! DON'T PRESS IT! IMAGINE WHAT COULD HAPPEN DUDE! WHAT AN IDIOT YOU ARE, FUUUUU!!! DUUDE COME ON!!!")
  EndIf
EndProcedure

Procedure physics_update(time)
  ; >> This is the heart of the engine. This command updates the entire Newton world, by the amount of time passed (in seconds). This will collect (by calling ForceAndTorqueCallback) and apply any forces to NewtonBodies in the NewtonWorld, detect collisions, and eventually come up with the new positions and orientations for all bodies in the system (and again call the appropriate callbacks).
  NewtonUpdate(*physics_nWorld, time)
 
  ; Why is the callback failing anyway?
  Dim matrix.f(15)
  NewtonBodyGetMatrix(*testbody, @matrix())
  ;
 
  *obj1.gameobject\position\x = matrix(12)
  *obj1.gameobject\position\y = matrix(13)
  *obj1.gameobject\position\z = matrix(14)
  ; << END OF DEBUG
EndProcedure
00Laboratories
Solutions for Developers
http://00laboratories.com/
Henry00
 
Posts: 37
Joined: Mon Aug 01, 2011 7:29 pm
Location: Germany

Re: NewtonBodyTransformCallback problem

Postby JoeJ » Wed Aug 03, 2011 3:41 pm

;NewtonBodySetUserData(*testbody, *obj1)


commenting this out causes a crash in the TransformCallback, because it tries to get your object from it.
I'm sure everything works fine if you make it active again :?
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBodyTransformCallback problem

Postby Henry00 » Wed Aug 03, 2011 3:46 pm

The entire callback is commented out as well, currently it should just do nothing on the callback instead of giving me an error with no explaination, the few object lines I have all work except the callback which errors on any kind of return, ether by ending the procedure, or returning 1 , true, 0, body matrix I tried multiple things already :(

Compiler would see it like this at the moment:

Procedure physics_NewtonBodyTransformCallback(*body, Array *matrix.f(16), threadIndex.i)
; AGAIN COMMENTS, damn programmers.. I wasted my time here!!! :evil:
EndProcedure
00Laboratories
Solutions for Developers
http://00laboratories.com/
Henry00
 
Posts: 37
Joined: Mon Aug 01, 2011 7:29 pm
Location: Germany

Re: NewtonBodyTransformCallback problem

Postby JoeJ » Wed Aug 03, 2011 3:52 pm

Oh sorry, missed that.
What if you comment out any Callbacks?

NewtonBodySetForceAndTorqueCallback( *testbody, @physics_ApplyForceAndTorqueCallback() )
NewtonBodySetTransformCallback( *testbody, @physics_NewtonBodyTransformCallback() )
NewtonBodySetDestructorCallback( *testbody, @physics_destroyBodyCallback() )

And next, what if you don't create a body and simulate an empty world?

EDIT: Your code works if comment out only the NewtonBodySetTransformCallback ???
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBodyTransformCallback problem

Postby Henry00 » Wed Aug 03, 2011 3:58 pm

Code: Select all
NewtonBodySetForceAndTorqueCallback( *testbody, @physics_ApplyForceAndTorqueCallback() )


That one gets called, empty world also works ( since I had that to begin with ) and the only two functions failing so far would be:

Code: Select all
NewtonBodySetTransformCallback( *testbody, @physics_NewtonBodyTransformCallback() )
foreachpolygondo ( thecallbackinhere )


both use the Array *matrix.f(16) ( I think ) I also tried 15 that's why I wondered if it could be an issue with the arguments, on ending telling me something went wrong and there is an invalid "write". Then again I also tried pure *matrix


EDIT: Your code works if comment out only the NewtonBodySetTransformCallback ???

Yes

lol @ Julio Jerez, why would the callback even bother about matrix if I can get it over NewtonBodyGetMatrix, I think that's the crash reason, well who knows.. :?
00Laboratories
Solutions for Developers
http://00laboratories.com/
Henry00
 
Posts: 37
Joined: Mon Aug 01, 2011 7:29 pm
Location: Germany

Re: NewtonBodyTransformCallback problem

Postby JoeJ » Wed Aug 03, 2011 4:29 pm

Now i get you, sorry that took a while.

I guess it's something like this:

You have:
physics_NewtonBodyTransformCallback(*body, Array *matrix.f(16), threadIndex.i)

Newton specifys:
typedef void (*NewtonSetTransform) (const NewtonBody* body, const dFloat* matrix, int threadIndex);

const dFloat* matrix means the memory adress of the first of 16 floating point values.
But Array *matrix.f(16) maybe gives the adress of an object that keeps track of the array, but not the first float itself?
In C such things can happen and the compiler does not warn us about that.
So is it simply a syntax problem?

But if i would be right, it shouldn't crash. Because TransformCallback doesn't write to the array.
It only gives you an adress from that you can read the matrix, but you don't do that.
Maybe you need to post both function headers in a forum with programmers capable of both C and Basic.

I can't help about that. But you can avoid the TransformCallback for now.
I guess you want to get position and orientation for rendering?
So all you need to do is, after the NewtonUpdate, loop through all bodies and get the information with NewtonBodyGetMatrix, as you allready do in your TransformCallback.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBodyTransformCallback problem

Postby Henry00 » Wed Aug 03, 2011 4:33 pm

Thanks alot JoeJ, I will do just that, it probably is some kind of incompatibility between the languages, as far as I understand the matrix is unnecesary anyway since you have the body to get it from, but for compatibility reasons it can't be changed anymore.

At least there is a workaround, time to keep working on WindEngine, thanks for your time!!

Greets, Henry00 from 00Laboratories

Time to walk in here..
Image
map+texures copyright Epic Games ( for now, testmap )
00Laboratories
Solutions for Developers
http://00laboratories.com/
Henry00
 
Posts: 37
Joined: Mon Aug 01, 2011 7:29 pm
Location: Germany

Re: NewtonBodyTransformCallback problem

Postby Julio Jerez » Wed Aug 03, 2011 5:35 pm

for most case teh matrix passed to teh call back in he same as teh matrix in the body,
However ther are, or at leat there were cases in wich the Matrix was not the one in the body.

for example in continue collision mode the engine call transform call back with a matrix that is tem varible on the stack, if you read the Body matrix you will get a the one at the begining of the Frame.
That funtionality is use on the player controller
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonBodyTransformCallback problem

Postby Henry00 » Sat Aug 06, 2011 1:38 pm

And I fixed it thanks to me co-programmer who came back from a short break!
The system architecture of newton is C, what I had to do was instead of:

Procedure

Use:

ProcedureC which is specially made for C because C is scary!

Sorry for all the trouble, and definitly thanks for all the help given, I doubt it will be long until more issues pop up, but thanks!
00Laboratories
Solutions for Developers
http://00laboratories.com/
Henry00
 
Posts: 37
Joined: Mon Aug 01, 2011 7:29 pm
Location: Germany

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests