[solved] Porting Old Newton 2.33 Code to ver 3.14

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

[solved] Porting Old Newton 2.33 Code to ver 3.14

Postby Corpsman » Mon Oct 02, 2017 3:28 pm

Hello to all,

i have written a program using newton 2.33, linux 32-Bit and everything worked fine.

Now i changed my linux to 64-Bit and needed to recompile all my projects in order to use them.

unfortunatunely i was not able to get a linux 64-Bit version from the old newton ver 2.33 so i had to switch to the newest 3.14 version.

As i am writing in Free Pascal i implemented a wrapper class that handles all my newton relevant stuff (to better support all that changes)

In Basic i only use the primitives (points, boxes, cones, spheres ..) and combine them to compound collision, as far as i found out until now this has to be done different now. Hoping that someone of you will help me to reimplement the specific part (and funny thing only these 100 lines needs to be changed) i post them here. Some hints / tutorials how to make this would also help

All relevant method names are the same as in the c++ version so i think even if you do not program Pascal you can get the idea of the code.

In short i do this

1. Create a lot of Newtoncollision
2. Combine them with "newtoncreateCompoundCollision" to a single one
3. give it a mass if needed
4. Set it to a special material

To make things clearer i added a few comments in english, the original code was commented in german

Code: Select all
Procedure TNewtonCompoundCollision.AddtoNewtonworld(Const Parent: Pointer;
  Const Newtonworld: newtonworld; Mass_, Material: Integer; Origin: TVector3;
  callback: NewtonApplyForceAndTorque);
// in pascal you need to declare all your variables at the beginning
Var
  Collision: Array Of NewtonCollision; // TMP Variablen für die Collider
  rescollider: NewtonCollision; // Der Resultierende Collider
  i, j: integer;
  id: TMatrix4x4;
  Inertia: TVector3;
  b: Boolean;
Begin
  If (high(Fpoints) = -1) And (high(FSPheres) = -1) And
    (high(fCylinders) = -1) And (high(fCones) = -1) Then exit;
  fcallback := callback;
  fparent := Parent;
  fmaterial := Material;
  FnewtonWorld := Newtonworld;
  fmass := Mass_;
  fOrigin := Origin;
  // Create a array that holds all the single objects that later on needs to be combined to one big compound collision
  setlength(Collision, high(Fpoints) + high(FSPheres) + high(fCylinders) + high(fCones) + 4);
  // Hinzufügen der Convexen Hüllen
  j := 0;
  For i := 0 To high(Fpoints) Do Begin
    Collision[j] := NewtonCreateConvexHull(newtonworld, high(Fpoints[i]) + 1, @fpoints[i][0], 12, 0, 0, Nil);
    inc(j);
  End;
  // Hinzufügen der Spheres
  For i := 0 To high(FSPheres) Do Begin
    // Die Position der Sphere mit einrechnen
    If (FSPheres[i].Point) = v3(0, 0, 0) Then Begin
      Collision[j] := NewtonCreateSphere(newtonworld, FSPheres[i].Radius, 0, Nil);
    End
    Else Begin
      id := IdentityMatrix4x4;
      id[3, 0] := FSPheres[i].Point.x;
      id[3, 1] := FSPheres[i].Point.y;
      id[3, 2] := FSPheres[i].Point.z;
      Collision[j] := NewtonCreateSphere(newtonworld, FSPheres[i].Radius, 0, @id[0, 0]);
    End;
    inc(j);
  End;
  // Hinzufügen der Cylinder
  For i := 0 To high(fCylinders) Do Begin
    id := fCylinders[i].RenderData.Matrix;
    id[3, 0] := fCylinders[i].Center.x;
    id[3, 1] := fCylinders[i].Center.y;
    id[3, 2] := fCylinders[i].Center.z;
    Collision[j] := NewtonCreateCylinder(Newtonworld, fCylinders[i].Radius, fCylinders[i].Radius, fCylinders[i].Height, 0, @id[0, 0]);
    inc(j);
  End;
  // Hinzufügen der Cones
  For i := 0 To high(fCones) Do Begin
    id := fCones[i].RenderData.Matrix;
    id[3, 0] := fCones[i].Center.x;
    id[3, 1] := fCones[i].Center.y;
    id[3, 2] := fCones[i].Center.z;
    Collision[j] := NewtonCreateCone(Newtonworld, fCones[i].Radius, fCones[i].Height, 0, @id[0, 0]);
    inc(j);
  End;
  // Erzeugen des Gesamtkolliders
  If High(Collision) = 0 Then Begin
    rescollider := Collision[0];
  End
  Else Begin
(*
 *  This routine is not available anymore with ver 3.14
 *)
    rescollider := newtoncreateCompoundCollision(newtonworld, High(Collision) + 1, @Collision[0], 0);
  End;
  // Übernehmen dieses Pointers in die Newtonworld
  id := IdentityMatrix4x4;
(*
 *  This routine is not available anymore with ver 3.14
 *)
  fNewtonBody := NewtonCreateBody(NewtonWorld, rescollider, @id[0]);

  Inertia := ZeroV3();
  // Der Inertia wird vorberechnet, falls das Objekt später noch Beweglich wird.
  NewtonConvexCollisionCalculateInertialMatrix(rescollider, @Inertia, @Origin);
  finertia := Inertia; // Speichern falls das Object später noch beweglich wird.
  If Mass_ = 0 Then
    NewtonBodySetMassMatrix(fNewtonBody, 0, 0, 0, 0)
  Else Begin
    NewtonBodySetMassMatrix(fNewtonBody, Mass_, inertia.x * Mass_, inertia.y * Mass_, inertia.z * Mass_)
  End;
  // Remove the collider, we don't need it anymore
  If High(Collision) = 0 Then Begin
(*
 *  This routine is not available anymore with ver 3.14
 *)
    NewtonReleaseCollision(NewtonWorld, Collision[0]);
  End
  Else Begin
(*
 *  This routine is not available anymore with ver 3.14
 *)
    NewtonReleaseCollision(NewtonWorld, rescollider);
    For i := 0 To high(Collision) Do Begin
(*
 *  This routine is not available anymore with ver 3.14
 *)
      NewtonReleaseCollision(NewtonWorld, Collision[i]);
    End;
  End;
  Setlength(Collision, 0);

  // Todo : Das hier ist der Totale hack, aber nur so lassen sich Wippe und Barrier gleichzeitig "gefühlt" richtig Simulieren !
  //b := (high(FSPheres) = -1) And (high(fCylinders) = -1) And (high(fCones) = -1) And (mass <> 0);
  //If b Then
  NewtonBodySetCentreOfMass(FNewtonBody, @Origin); // Setzen des Masseschwerpunktes, dieser sollte Eigentlich (0/0/0) sein !

  // Now set the position of the body's matrix
  NewtonBodyGetMatrix(fNewtonBody, @id[0, 0]);
  id[3, 0] := fPosition.x;
  id[3, 1] := fPosition.y;
  id[3, 2] := fPosition.z;
  NewtonBodySetMatrix(fNewtonBody, @id[0, 0]);
  // Set the Material Group
  NewtonBodySetMaterialGroupID(fnewtonbody, Material);
  // Set the User Data = Pointer to Parent Class
  NewtonBodySetUserData(fNewtonBody, parent);
  // Set ForceCallback if Needed
  If Fmass <> 0 Then Begin
    NewtonBodySetForceAndTorqueCallBack(fNewtonBody, callback);
    // Dafür sorgen das die Engine unseren Player nie Blockiert !!
    (*
     Removed for 2.0
    *)
    //NewtonWorldUnfreezeBody(NewtonWorld, fNewtonBody);
    //NewtonBodySetAutoFreeze(fNewtonBody, 0);
    // *)}
  End;
End;


Furthermore it seems that the routines
NewtonSetBodyLeaveWorldEvent
and
NewtonSetWorldSize
where gone too, are they not needed anymore ?
Last edited by Corpsman on Tue Oct 03, 2017 10:11 am, edited 1 time in total.
--
You want to Know more about me and my projects visit http://www.Corpsman.de
I use Newton in Balanced2
Corpsman
 
Posts: 38
Joined: Mon May 01, 2006 11:42 am

Re: Porting Old Newton 2.33 Code to ver 3.14

Postby Julio Jerez » Mon Oct 02, 2017 3:43 pm

yes, fora long time the engine does not have world limit anymore.
in the pass the world size determined the amount of layers on an octree, but now we do not use octrees we use adaptive AABB which do not requires limits.

that's not to say that you can place object at huge distance from the origin, there will have round error problems, but you can use double and then you can set very long distance from the origin with the cost of a huge broadphase octree.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting Old Newton 2.33 Code to ver 3.14

Postby Corpsman » Tue Oct 03, 2017 1:41 am

Do you have any examples for my other problem (creating compound collisions out of primitives)?
--
You want to Know more about me and my projects visit http://www.Corpsman.de
I use Newton in Balanced2
Corpsman
 
Posts: 38
Joined: Mon May 01, 2006 11:42 am

Re: Porting Old Newton 2.33 Code to ver 3.14

Postby Sweenie » Tue Oct 03, 2017 3:15 am

Corpsman wrote:Do you have any examples for my other problem (creating compound collisions out of primitives)?


You would only need to do some slight adjustments to get it working.

NewtonCreateCompoundCollision still exist but have less params.
Code: Select all
NEWTON_API NewtonCollision* NewtonCreateCompoundCollision (const NewtonWorld* const newtonWorld, int shapeID);


Creating bodies are basically the same but has been split into two different body types.
Dynamic Bodies and Kinematic Bodies. The dynamic body is the same as the old body you used before.

Code: Select all
   NEWTON_API NewtonBody* NewtonCreateDynamicBody (const NewtonWorld* const newtonWorld, const NewtonCollision* const collision, const dFloat* const matrix);
   NEWTON_API NewtonBody* NewtonCreateKinematicBody (const NewtonWorld* const newtonWorld, const NewtonCollision* const collision, const dFloat* const matrix);


and NewtonReleaseCollision is now replace by
Code: Select all
NEWTON_API void NewtonDestroyCollision (const NewtonCollision* const collision);


If you check the file Newton.h under sdk\dgNewton folder you can search for API-calls that you used before and see if the parameters changed. Everything is nicely grouped together in sections so the file is pretty easy to read.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Porting Old Newton 2.33 Code to ver 3.14

Postby Corpsman » Tue Oct 03, 2017 10:11 am

Hello Sweenie,

thanks for your help, according to your hints i changed the routine names.

For the rescollider i had to add some lines (as seen below), but now it seems to work.

Code: Select all
  rescollider := newtoncreateCompoundCollision(newtonworld, 0);
    NewtonCompoundCollisionBeginAddRemove(rescollider);
    For i := 0 To high(Collision) Do Begin
      NewtonCompoundCollisionAddSubCollision(rescollider, Collision[i]);
    End;
    NewtonCompoundCollisionEndAddRemove(rescollider);


Thank you for your help.
--
You want to Know more about me and my projects visit http://www.Corpsman.de
I use Newton in Balanced2
Corpsman
 
Posts: 38
Joined: Mon May 01, 2006 11:42 am

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby JernejL » Fri Oct 06, 2017 3:57 pm

Hi, i'm having another - related, but highly specific issue for converting my old code, i did figure out this is a elementary list of changes:

NewtonCreateBody -> NewtonCreateDynamicBody
NewtonReleaseCollision -> NewtonDestroyCollision
NewtonSetWorldSize -> removed
NewtonSetBodyLeaveWorldEvent -> removed

I can't seem to figure out where the NewtonMaterialGetBodyCollisionID went tho..?

I specify a shapeID when i create convex hull pieces, which i then need to get when processing collision contacts to specify which parts of the compound should in some conditions collide with other objects and which not.

How do i get the shapeid when processing contacts? I cannot seem to figure it out where this went, it is really important for me to know which shape in the compound was hit.

Original function was:
http://www.newtondynamics.com/wiki/inde ... ollisionID

Edit: is there any offical changes list i could follow while converting code?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby Julio Jerez » Fri Oct 06, 2017 5:21 pm

Oh long time did no hear for your, welcome back

yes NewtonMaterialGetBodyCollisionID is no longer part for the SDK instead collision are now instances
you can get the collision from the body and the use this interface to get user data.
Code: Select all
   // for the end user
   void NewtonCollisionSetUserData (const NewtonCollision* const collision, void* const userData);
   void* NewtonCollisionGetUserData (const NewtonCollision* const collision);
   
   // this is used data is used by the joint library
   void NewtonCollisionSetUserID (const NewtonCollision* const collision, unsigned id);
   unsigned NewtonCollisionGetUserID (const NewtonCollision* const collision);


this allow you for example to set the same collision to many bodies, and each collision shape can have unique user data while sharing the heavy constant data.

also in the call back you should always use the collision that is passed in the callback not the one the is in the body, collision instance are like smart pointers that do operation on collision shapes.
for example to collision shape can have different scales, and the are still the same shape.
in the old newton this was awkward to do. because of this the collision you get in a callback maybe be a instance not the collision that is assigned to the body.

basically Newton in 3.xx collision user data is like the same as rigid body user data.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby JernejL » Sat Oct 07, 2017 4:49 am

Indeed, long time haven't worked with newton, i was so busy, but now i have some time :)

I think i am still a bit confused, let me show from newton.h:

NEWTON_API NewtonCollision* NewtonCreateConvexHull (const NewtonWorld* const newtonWorld, int count, const dFloat* const vertexCloud, int strideInBytes, dFloat tolerance, int shapeID, const dFloat* const offsetMatrix);

This shapeid still exists as parameter, I think the correct replacement for the NewtonMaterialGetBodyCollisionID function is now NewtonMaterialGetBodyCollidingShape, it is in exact same spot as previous function was in header, if i do a compare with a few years old header:

Image
Full image: https://i.imgur.com/dR2sIF7.png

This is in one of demos, it's used in exact same place inbetween ( NewtonContactJointGetFirstContact / NewtonContactJointGetNextContact / NewtonContactGetMaterial ) contacts & material iterator:

: Newton\applications\demosSandbox\sdkDemos\demos\CompoundCollision.cpp
Image
Full image: https://i.imgur.com/hPNQxes.png

Where exactly is this shapeid assigned to then? does it do nothing and we should use NewtonCollisionSetUserData + NewtonCollisionGetUserData and this will return correct userdata for correct collision even in compount collision contacts processing?

Or does NewtonMaterialGetBodyCollidingShape return collision shape instance and NewtonCollisionGetUserData will return the SHAPEID specified in NewtonCreateConvexHull?

so.. is shapeid parameter automaticly assigned to m_userData0 and returned via NewtonMaterialGetBodyCollidingShape + NewtonCollisionGetUserData?

Maybe then shapeid parameters should be renamed to UserData, this would make sense.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby Julio Jerez » Sat Oct 07, 2017 10:02 am

the material ID on the body work as it did before.
what is new is the used dat on a collision shape, here is a example from the ragdoll demo.
imagine two rag dolls, her the demo used the collision id to save information about the complet rag doll.

Code: Select all
   static int OnBoneAABBOverlap (const NewtonMaterial* const material, const NewtonBody* const body0, const NewtonBody* const body1, int threadIndex)
   {
      NewtonCollision* const collision0 = NewtonBodyGetCollision(body0);
      NewtonCollision* const collision1 = NewtonBodyGetCollision(body1);
      dCustomArticulatedTransformController::dSkeletonBone* const bone0 = (dCustomArticulatedTransformController::dSkeletonBone*)NewtonCollisionGetUserData (collision0);
      dCustomArticulatedTransformController::dSkeletonBone* const bone1 = (dCustomArticulatedTransformController::dSkeletonBone*)NewtonCollisionGetUserData (collision1);

      dAssert (bone0);
      dAssert (bone1);
      if (bone0->m_myController && bone1->m_myController) {
         return bone0->m_myController->SelfCollisionTest (bone0, bone1) ? 1 : 0;
      }

      return 1;
   }


remember that a compound has its own aabb callback where you get the body an dthe subshape that is been tested.
Code: Select all
int  (*NewtonOnCompoundSubCollisionAABBOverlap) (const NewtonMaterial* const material, const NewtonBody* const body0, const void* const collsionNode0, const NewtonBody* const body1, const void* const collsionNode1, int threadIndex);
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby JernejL » Mon Oct 09, 2017 1:44 am

To my knowledge, the material contact callback was called in its own right, so does each material pair is now preceded by a NewtonOnCompoundSubCollisionAABBOverlap callback?

So.. collision's shapeid is not the same as collision's userdata?

How do i get a collision's shapeid that i set back then?

Also.. i'm having difficulties compiling or obtaining latest newton in dll form, is there any kind of instructions on how to compile a dll with vs2013?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby Julio Jerez » Mon Oct 09, 2017 3:23 pm

I made a test to explained and when I when to post the internet kicked out, I will do it again tonight.
but in the mean time look at the demo friction coefficients.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby JernejL » Wed Oct 18, 2017 3:54 pm

Ok, i got some things done.. but i have further questions, all of this is really important and relevant for me as i have no idea how to convert the callbacks and some other parts, i will really appreciate if you can help me out:

NewtonGetEulerAngle used to have 2 parameters, now it has 3:

http://newtondynamics.com/wiki/index.ph ... EulerAngle

What are now eulersAngles0 and eulersAngles1? how does this relate to old function? NewtonSetEulerAngle still has only 2 parameters like before.

NewtonWorldForEachBodyInAABBDo callback appears to have changed, now it has to return a value, what does the returned value do? it's not documented anywhere.. wiki page: http://newtondynamics.com/wiki/index.ph ... dyIterator

NewtonDestroyBody and NewtonReleaseCollision no longer takes world parameter? this is ok?

NewtonMaterialSetContactFrictionCoef now has extra parameter:

How do staticFrictionCoef & kineticFrictionCoef relate to old "coef" parameter? how do i use this with old code?

what do i do with threadindex in NewtonWorldRayCast?

NewtonCreateCompoundCollision now has much less parameters, while i THINK i got this working correctly, some documentation about this would be much appreciated.

Edit:

NewtonTreeCollisionEndBuild was giving me a spectacular crash, it was caused by not adding any faces to the mesh - i guess that's dumb on my side, but still maybe you can work around this?

Edit #2: all my bodies that fall on ground from gravity that have a upvector joint (actors) are now for some reason super bouncy and for some reason movement of bodies seems much faster now.. any idea as to cause of this?

Edit #3:

Code: Select all
void dgWorld::SerializeCollision(dgCollisionInstance* const shape, dgSerialize serialization, void* const userData) const
{
   dgAssert (0);
   dgSerializeMarker(serialization, userData);
   shape->Serialize(serialization, userData);
}


This assert is surely there in error? it breaks using debug build completely.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby Julio Jerez » Thu Oct 19, 2017 9:22 am

-NewtonGetEulerAngle take threr parameter because of the following.

the function that map a matrix to a quaternion, has two solution
you can have m = f (q) and also m = f(-q)

this mean there are two sequence of rotation that generate the same matrix, so there must be two sequence of eulers angle, hence the function now provide the two solution
you can pass the two vectors pointe and the ignore either one.

this is similar to the equation y = x^2 which has tow solution, x0, and -x0

-NewtonWorldForEachBodyInAABBDo so far I know has not changed.

-NewtonDestroyBody and NewtonReleaseCollision no longer takes world parameter? this is ok?
yes this object are now instances and the instance contain the pointer to the world that the belong

-NewtonMaterialSetContactFrictionCoef take kinetic and static friction coefficient an also the friction row, since every contact point has two perpendicular friction direction.

-threadindex you can ignored, the thread index is for application that do multithreading and use global memory

I will remove the assert, in next commit, you can temporarily comment it out
to store extra information. and example of the Is the height field collisions
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby JernejL » Fri Oct 20, 2017 2:30 am

Thank you for the answers, i got a bit further with converting my project to newton3:

NewtonBodyIterator (NewtonWorldForEachBodyInAABBDo, etc..) actually has changed, i checked code and now it's a function and return value if it's false means "stop searching", this was changed since 2.x

For NewtonGetEulerAngle i'd suggest ability to specify each of 2 pointer as null, so you can choose which one you need.

I'm still debugging my code and haven't gotten it all to work (getting strange crashes) - i'm working on this and trying to get newton3 working, i hope the experience will help me work on inproving the wiki.

It seems as if some things related to setvelocity have changed in relation with my timestep perhaps? has anything changed about this? my materials are set up same as in 2.x but bodies react with much more response for same forces, i'm not sure what is causing this, i'll probably need additional help :(

I'm currently debugging with a debug build of 32 bit newton - \Newton\packages\projects\visualStudio_2013_dll

I'm having some crashes inside forcetorque callback, is it possible that calls to NewtonBodyGetFirstContactJoint, NewtonContactJointGetFirstContact, NewtonContactGetMaterial, NewtonMaterialGetContactPositionAndNormal, NewtonContactJointGetNextContact, NewtonBodyGetNextContactJoint are unsafe to call within the forcetorque callback?

This is the crash that i get, it crashes in code for searching for contacts:

Image

In my code this was a simple NewtonBodyAddForce call, and a few lines above i've called NewtonBodySetVelocity without any problems:

Image

I also encountered a crash in newton's second thread, do i have to specify something to make it not threaded? i'm not sure how to debug this or what could cause this:

Image

Edit: i'm working ofcourse with release https://github.com/MADEAPPS/newton-dyna ... ewton-3.13 - should i use some other version?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [solved] Porting Old Newton 2.33 Code to ver 3.14

Postby JernejL » Fri Jan 05, 2018 7:45 am

This problem was solved with julio's help, the problem was i was destroying bodies and joints within forceandtorque callback, this no longer works with newton 3.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 25 guests

cron