how to use spheres, convexhulls in a compound collision

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

how to use spheres, convexhulls in a compound collision

Postby Corpsman » Mon Jan 28, 2013 4:20 pm

Hello to all,

I try to create a compound collission with Newton. If i use only NewtonCreateConvexHull it works as expected. But when mixing Spheres and NewtonCreateConvexHull or only use Spheres (using the Offset Matrix), the Object does not Collide as expected ( the Object seems to leave the world in a unexpected way, the object rotates where it should not .. )

After reading this post i tried to switch the position index in the "id" matrix. But my object seems not to behave right.

So i think maybe i am freeing the memory of the Offset matrix to early, or, ... i don't know.

Maybe my thoughts of generating the newtoncreateCompoundCollision are generaly wrong. So did i missed something important here ?

Corpsman

Code: Select all
Procedure TNewtonCompoundCollision.AddtoNewtonworld(Const Parent: Pointer;
  Const Newtonworld: Pnewtonworld; Mass_, Material: Integer; Origin: TVector3;
  callback: NewtonApplyForceAndTorque);
Var
  Collision: Array Of PNewtonCollision; // TMP Variablen für die Collider
  rescollider: PNewtonCollision; // Der Resultierende Collider
  i, j: integer;
  id: TMatrix4x4;
  Inertia: TVector3;
Begin
  If (high(Fpoints) = -1) And (high(FSPheres) = -1) And
    (high(fCylinders) = -1) And (high(fCones) = -1) Then exit;
  fmass := Mass_;
  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;
  id := IdentityMatrix4x4;
  // Hinzufügen der Spheres
  For i := 0 To high(FSPheres) Do Begin
    // Die Position der Sphere mit einrechnen
//     Todo : Klären wie das genau geht..
    If (FSPheres[i].Point) = v3(0, 0, 0) Then Begin
      Collision[j] := NewtonCreateSphere(newtonworld, FSPheres[i].Radius, FSPheres[i].Radius, FSPheres[i].Radius, 0, Nil);
    End
    Else Begin
      id := IdentityMatrix4x4;
      id[2, 0] := FSPheres[i].Point.x;
      id[2, 1] := FSPheres[i].Point.y;
      id[2, 2] := FSPheres[i].Point.z;
      //      id[0, 2] := FSPheres[i].Point.x;
      //      id[1, 2] := FSPheres[i].Point.y;
      //      id[2, 2] := FSPheres[i].Point.z;
      Collision[j] := NewtonCreateSphere(newtonworld, FSPheres[i].Radius, FSPheres[i].Radius, FSPheres[i].Radius, 0, @id[0, 0]);
    End;
    inc(j);
  End;
  // Todo : Hinzufügen der Cylinder
  // Todo : Hinzufügen der Cones
  // Erzeugen des Gesamtkolliders
  If High(Collision) = 0 Then Begin
    rescollider := Collision[0];
  End
  Else Begin
    rescollider := newtoncreateCompoundCollision(newtonworld, High(Collision) + 1, @Collision[0], 0);
  End;
  // Übernehmen dieses Pointers in die Newtonworld
  id := IdentityMatrix4x4;
  fNewtonBody := NewtonCreateBody(NewtonWorld, rescollider, @id[0]);

  If Mass_ = 0 Then
    NewtonBodySetMassMatrix(fNewtonBody, 0, 0, 0, 0)
  Else Begin
    Inertia := ZeroV3();
    // Keine Ahnung für was der Inertia Vektor ist, aber so wird er erzeugt.
    NewtonConvexCollisionCalculateInertialMatrix(rescollider, @Inertia, @Origin);
    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
    NewtonReleaseCollision(NewtonWorld, Collision[0]);
  End
  Else Begin
    NewtonReleaseCollision(NewtonWorld, rescollider);
    For i := 0 To high(Collision) Do Begin
      NewtonReleaseCollision(NewtonWorld, Collision[i]);
    End;
  End;
  Setlength(Collision, 0);

  // 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
    // Todo : Callback für die Schwerkraft
    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; 
Attachments
Test.png
Designer Screenshot of the compund Object.
Test.png (74.89 KiB) Viewed 3077 times
--
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: how to use spheres, convexhulls in a compound collision

Postby JoeJ » Mon Jan 28, 2013 6:01 pm

... too much delphi for me to understand quickly, but maybe you missed a change in how to setup compounds in 3.00 (now begin()... end() style).
If you are on older newton version, just ignore the following or update :)

This is code i use to setup a test compound from 3 spheres:
Note: I used different matrices for the sub bodies because of paranoia, but i don't think you need to keep them after calling NewtonCreateSphere().


Code: Select all
      Shape *cShapes[3]; // Shape == NewtonCollision
      sMat4 cTrans[3]; // matrices for 3 sub-bodies

      cTrans[0].Identity();
      *cTrans[0].Translation() = sVec3 (-2,0,0);
      cShapes[0] = CreateSphereShape (2.5, &cTrans[0]); // == NewtonCreateSphere (world, radius, some id, &cTrans[0]);
      
      cTrans[1].Identity();
      *cTrans[1].Translation() = sVec3 (2,0,0);
      cShapes[1] = CreateSphereShape (3.3, &cTrans[1]);
      
      cTrans[2].Identity();
      *cTrans[2].Translation() = sVec3 (0,2,0);
      cShapes[2] = CreateSphereShape (2.3, &cTrans[2]);
      
      NewtonCollision *compoundShape = NewtonCreateCompoundCollision (world, 1002);
      NewtonCompoundCollisionBeginAddRemove(compoundShape);
      
                NewtonCompoundCollisionAddSubCollision(compoundShape, cShapes[0]);
      ReleaseShape (cShapes[0]); // == NewtonDestroyCollision (cShapes[0])
      NewtonCompoundCollisionAddSubCollision(compoundShape, cShapes[1]);
      ReleaseShape (cShapes[1]);
      NewtonCompoundCollisionAddSubCollision(compoundShape, cShapes[2]);
      ReleaseShape (cShapes[2]);

      NewtonCompoundCollisionEndAddRemove(compoundShape);
      
      trans.Identity(); // matrix for compound
      *trans.Translation() = sVec3 (-10,60,-20);
      CreateRigidBody (100, trans, compoundShape); // sets inertia, callback etc.

      ReleaseShape (compoundShape);

User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: how to use spheres, convexhulls in a compound collision

Postby Corpsman » Tue Jan 29, 2013 7:44 am

sry i forgot to name the Version i use, its 2.34

I am Using Newton on Linux (KUbuntu 12.10 and Windows from Win98 to Win7).

Is there a special reason, to prever one of the versions ?

Well i finally found the Headers for 3.0 here viewtopic.php?f=9&t=7395. I will give it a try, if my problems solved with the 3.0 version, than this would be a good reason to migrate *g*..

Is there a documentation for the Routines : NewtonCompoundCollisionAddSubCollision, NewtonCompoundCollisionBeginAddRemove or the 3.0 Version at all ? And whats all about with this shapes ?

I was not able to find any Informations in the Wiki.

Corpsman
--
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: how to use spheres, convexhulls in a compound collision

Postby JoeJ » Tue Jan 29, 2013 10:16 am

3.x contains lots of bugfixes, optimisations, improvements that are not in 2.x.
There are some minor changes (like the new compound creation) you need to update in your code, but it's not much.
Unluckily documentation is out of date for those things, but it's selfexplaining looking at newton.h usually.

Shapes are NewtonCollision - i renamed stuff because i initially supported two physics engines (but listed those cases in the source comments).

NewtonCompoundCollisionBeginAddRemove(compoundShape) starts the process of adding sub-collisions.
NewtonCompoundCollisionAddSubCollision(compoundShape, newtonColl) adds a collision in local space of compound.
NewtonCompoundCollisionEndAddRemove(compoundShape) ends the process and now you can use compoundShape as collision to create a body with it.

However, compounds worked well on 2.x, so there is most probably something you do wrong.
Maybe you can make a simpler setup test code with just 2 bodies and local data to find what it is.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: how to use spheres, convexhulls in a compound collision

Postby Corpsman » Tue Jan 29, 2013 1:04 pm

Maybe you can make a simpler setup test code with just 2 bodies and local data to find what it is.


*g* the code i posted, is the one and only i am using, because it can hold every case i need..

In my Modeleditor everything is created in convex hulls, and brought intuitively into the datatypes you see in the code i posted.

=> i have tested to get version 3.0 to work

In the "coreLibrary_300/projects/" Directory there is no Linux directory, so i tried posix32 ( i am using a 32-Bit Kubuntu here ) typing "make" gives me the libnewton.so i need, copied to /usr/lib ( as the console told me todo )

But unfortunatunelly my fpc 2.6.0-6 compiler could not link the newton.pas to my project, telling me "/usr/bin/ld: cannot find -lNewtonf"

So i need to stay with ver. 2.34. Still any ideas ?

[Edit]
As i found out i have to rename libnewton.so to libnewtonf.so, then it works *g*. It seems that there were a single and double version.. So testing will begin.

[Edit2]
So some news. If i Run my application, first routine after the version check, ( version 3.03 )
is run the NewtonCreate (); routine, then i get an access violation in

dgTypes.cpp line 369 : memcpy (&vertexList[count * stride + 2], &vertexList[m + 2], (stride - 2) * sizeof (dgFloat64));

for me this i the end fpr Newton 3.0 ( as i am unfortunatunally not able to debug c )

I will swap back to 2.34 and try to find the error.
--
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: how to use spheres, convexhulls in a compound collision

Postby Corpsman » Tue Jan 29, 2013 3:27 pm

Jehaa,

I found the bug (caused by me).

Code: Select all
      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, FSPheres[i].Radius, FSPheres[i].Radius, 0, @id[0, 0]);


The code in Thread 1 used id[2,..] which uses the z coordinate of the Right, Up and View directions. If you and all that want to use my code switch it with the sniped i posted it works as expected.

Thanks for trying to help JoeJ
--
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


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests

cron