Lazarus + GLScene

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Lazarus + GLScene

Postby Carli » Tue Jun 29, 2010 10:41 am

this is already answered

You need to generate a NewtonCollision from your mesh.
TreeCollision is static, anything else not. So use anything else but TreeCollision
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Lazarus + GLScene

Postby Julio Jerez » Tue Jun 29, 2010 11:18 am

But I do not know Pascal of GLScene, so I can not respond.

n teh piece of code you posted you can no use a static collision tree and make dynamics, thay are static by definition.

Unfortunatly Newton does not supports dynamics Triangle mesh (collision tree) collision

you can use compound collison made of convex hulls to approximate your mesh. and that is the closest to a dynamics irregular shape collision you can get using newton.
Julio Jerez
Moderator
Moderator
 
Posts: 12480
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Lazarus + GLScene

Postby Kjow » Tue Jun 29, 2010 2:32 pm

Thanks!

So, I need to retreive vertices from my mesh and pass to newtonworld by convex hull.

to make a dynamic cube I simply need to do:

Code: Select all
  collision   := NewtonCreateBox (NewtonWorld, GLCube1.CubeWidth, GLCube1.CubeHeight, GLCube1.CubeDepth, 0, nil);
  NewtonBody  := NewtonCreateBody(Newtonworld,collision);

  pMass := 100;

  Ixx := pMass * (GLCube1.CubeHeight * GLCube1.CubeHeight   GLCube1.CubeDepth * GLCube1.CubeDepth) / 12;
  Iyy := pMass * (GLCube1.CubeWidth * GLCube1.CubeWidth   GLCube1.CubeDepth * GLCube1.CubeDepth) / 12;
  Izz := pMass * (GLCube1.CubeWidth * GLCube1.CubeWidth   GLCube1.CubeHeight * GLCube1.CubeHeight) / 12;

  NewtonBodySetMassMatrix(NewtonBody, pMass, Ixx, Iyy, Izz);

  NewtonBodySetMatrix(NewtonBody,NewtonImport.PFloat(GLCube1.AbsoluteMatrixAsAddress));
  NewtonBodySetUserData(NewtonBody,GLCube1);
  GLCube1.TagObject := TObject(NewtonBody);

  NewtonBodySetForceAndTorqueCallBack(NewtonBody, @ForceAndTorqueCallBack);

PS This code works right and it falls down and interacts well with the static mesh above.

So, I think that instead of NewtonCreateBox I need to call NewtonCreateConvexHull or NewtonCreateConvexHullFromMesh. I understood that these two calls are similar, but they start from different things.

So, if I reuse this code, but with the ConvexHull instead of Box it should work, is it right?

PS NewtonCreateBody is needed for dynamic meshes?
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Julio Jerez » Tue Jun 29, 2010 2:49 pm

NewtonCreateConvexHull is what you need to use, you pass the array of points form you mesh.

NewtonCreateConvexHullFromMesh is just a wraper that call NewtonCreateConvexHull with the points that are part of a collision mesh.
you do not need to make a NewtonMesh just to make a convex hull, that will be a waste of time and memory.

also you should use the function to calculate the Inertial and the center of mass instead of the close formule you are using.
the function in newton will calculate the correct value for all cituations where the formula is specific to one type of shape.
Julio Jerez
Moderator
Moderator
 
Posts: 12480
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Lazarus + GLScene

Postby Kjow » Tue Jun 29, 2010 6:08 pm

Thank you very much, Julio Jerez!

I'll do some test these days :)
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Stucuk » Tue Jun 29, 2010 7:09 pm

Kjow wrote:I opened a thread called "Lazarus + GLScene" because in the GLScene community Newton is not so much used (there is ODE component, but I hope there will be soon a Newton component also), so I don't think that ask about GLScene usage WITH Newton is a so strange thing.


I didn't mean you shouldn't have opened this thread. I ment you to ask the GLScene community how to extract info from a GLFreeForm as its something thats a part of GLScene and not Newton.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Lazarus + GLScene

Postby Kjow » Wed Jun 30, 2010 4:07 am

Stucuk wrote:
Kjow wrote:I opened a thread called "Lazarus + GLScene" because in the GLScene community Newton is not so much used (there is ODE component, but I hope there will be soon a Newton component also), so I don't think that ask about GLScene usage WITH Newton is a so strange thing.


I didn't mean you shouldn't have opened this thread. I ment you to ask the GLScene community how to extract info from a GLFreeForm as its something thats a part of GLScene and not Newton.


Yes, I know. ;)
My hope was that there was someone that uses GLScene, seeing there are the headers for ObjectPascal and GLScene is a powerful tool for this platform :)

Thank you anyway for the rest of help, it is really usefull.
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Kjow » Thu Jul 01, 2010 4:34 am

Hi again :)

I think that I found the way to insert the right parameters, but my dynamic object doesn't fall down.

Code: Select all
  GLFreeForm.LoadFromFile(ExtractFilePath(Application.ExeName)+'dyn.obj');
  GLFreeForm.Roll(-45);
  GLFreeForm.Position.Y:=30;

  xD:= 5;
  yD:= 5;
  zD:= 5;
  massD:=10;

  IxxD := massD * (1/12) * (yD*yD+zD*zD);
  IyyD := massD * (1/12) * (xD*xD+zD*zD);
  IzzD := massD * (1/12) * (xD*xD+yD*yD);

  dyn:= NewtonCreateConvexHull(Newtonworld, GLFreeForm.MeshObjects.Items[0].Vertices.Count, @(GLFreeForm.MeshObjects.Items[0].Vertices.List), sizeof(GLFreeForm.MeshObjects.Items[0].Vertices)*3, 0.002, 2, NewtonImport.PFloat(GLFreeForm.AbsoluteMatrixAsAddress));

  NewtonBodyDyn:= NewtonCreateBody(Newtonworld, dyn);

  NewtonBodySetMassMatrix(NewtonBodyDyn, massD, IxxD, IyyD, IzzD);

  NewtonBodySetMatrix(NewtonBodyDyn,NewtonImport.PFloat(GLFreeForm.AbsoluteMatrixAsAddress));
  NewtonBodySetUserData(NewtonBodyDyn,GLFreeForm);
  GLFreeForm.TagObject:= TObject(NewtonBodyDyn);

  NewtonBodySetForceAndTorqueCallBack(NewtonBodyDyn, @ForceAndTorqueCallBack);


This code compiles, but my "dyn" object remain at its position and it is passed through by the "dynamic cube". What am I missing?

I started from the working code of the Cube:

Code: Select all
  collision   := NewtonCreateBox (NewtonWorld, GLCube1.CubeWidth, GLCube1.CubeHeight, GLCube1.CubeDepth, 0, nil);
  NewtonBody  := NewtonCreateBody(Newtonworld,collision);

  pMass := 100;

  Ixx := pMass * (GLCube1.CubeHeight * GLCube1.CubeHeight + GLCube1.CubeDepth * GLCube1.CubeDepth) / 12;
  Iyy := pMass * (GLCube1.CubeWidth * GLCube1.CubeWidth + GLCube1.CubeDepth * GLCube1.CubeDepth) / 12;
  Izz := pMass * (GLCube1.CubeWidth * GLCube1.CubeWidth + GLCube1.CubeHeight * GLCube1.CubeHeight) / 12;

  NewtonBodySetMassMatrix(NewtonBody, pMass, Ixx, Iyy, Izz);

  NewtonBodySetMatrix(NewtonBody,NewtonImport.PFloat(GLCube1.AbsoluteMatrixAsAddress));
  NewtonBodySetUserData(NewtonBody,GLCube1);
  GLCube1.TagObject := TObject(NewtonBody);

  NewtonBodySetForceAndTorqueCallBack(NewtonBody, @ForceAndTorqueCallBack);



Thank you! :)
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Kjow » Thu Jul 01, 2010 5:02 am

ehm... I was missing to update Graphic Object on time progress :)

Code: Select all
NewtonBodySetTransformCallback(NewtonBodyDyn, @UpdateGraphicObject);


So, now it falls down, but it doesn't hit with the TreeCollision (the same TreeCollision that interact well with the falling cube).
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Julio Jerez » Thu Jul 01, 2010 8:50 am

you need to implement debug display so tha yo ucna see wher etyeh collsion shape are and hwo they look like.
Julio Jerez
Moderator
Moderator
 
Posts: 12480
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Lazarus + GLScene

Postby Kjow » Thu Jul 01, 2010 4:16 pm

Do you mean Iterators functions?

There are some examples?
I can't find nothing...

For those use ObjectPascal: can you help me? (no GLScene, just NewtonImport + OP)
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Julio Jerez » Thu Jul 01, 2010 4:45 pm

These are the integrators function:

Code: Select all
NewtonMaterial* NewtonWorldGetFirstMaterial (const NewtonWorld* world);
NewtonMaterial* NewtonWorldGetNextMaterial (const NewtonWorld* world, const NewtonMaterial* material);
NewtonBody* NewtonWorldGetFirstBody (const NewtonWorld* world);
NewtonBody* NewtonWorldGetNextBody (const NewtonWorld* world, const NewtonBody* curBody);
NewtonJoint* NewtonBodyGetFirstJoint (const NewtonBody* body);
NewtonJoint* NewtonBodyGetNextJoint (const NewtonBody* body, const NewtonJoint* joint);
NewtonJoint* NewtonBodyGetFirstContactJoint (const NewtonBody* body);
NewtonJoint* NewtonBodyGetNextContactJoint (const NewtonBody* body, const NewtonJoint* contactJoint);

for can for example go over the list of all bodies in the world by doing this
Code: Select all
for (NewtonBody* body = NewtonWorldGetFirstBody (world); body; body = NewtonWorldGetNextBody (world, body)
{
   ...

}
Julio Jerez
Moderator
Moderator
 
Posts: 12480
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Lazarus + GLScene

Postby Stucuk » Thu Jul 01, 2010 5:58 pm

See Here for Sascha Willems's Newton Demos. They should contain debug display code.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Lazarus + GLScene

Postby Kjow » Fri Jul 02, 2010 3:29 am

Thank you both, I'll try to debug :)
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Kjow » Fri Jul 02, 2010 5:48 pm

I'm trying to port the debug code of Sascha Willems' SDLNewtonJointDemo.pas

But I have an error that I don't understand. This is the code:

Code: Select all
// =============================================================================
//  Debug_ShowGeometryCollision
// =============================================================================
//  Callback that shows collision geometry of a body
// =============================================================================
procedure Debug_ShowGeometryCollision(const Body : PNewtonBody; VertexCount : Integer; const FaceArray : PFloat; FaceId : int); cdecl;
var
 i     : Integer;
 v0,v1 : array[0..2] of Single;
 vA    : array of Single;
begin
  if VertexCount = 0 then
    exit;
  SetLength(vA, VertexCount*3);
  Move(FaceArray^, vA[0], VertexCount*3*SizeOf(Single));
  v0[0] := vA[(VertexCount-1)*3];
  v0[1] := vA[(VertexCount-1)*3+1];
  v0[2] := vA[(VertexCount-1)*3+2];
  for i := 0 to VertexCount-1 do
    begin
      v1[0] := vA[i*3];
      v1[1] := vA[i*3+1];
      v1[2] := vA[i*3+2];
      Form1.GLLines1.AddNode(v0[0], v0[1], v0[2]);
      Form1.GLLines1.AddNode(v1[0], v1[1], v1[2]);
      v0 := v1;
    end;
end;

// =============================================================================
//  Debug_ShowBodyCollision
// =============================================================================
//  Show collision geometry for all bodies in the current scene
// =============================================================================
procedure Debug_ShowBodyCollision(const Body : PNewtonBody); cdecl;
var
 TmpM : TMatrix4f;
begin
NewtonBodyGetMatrix(Body, @TmpM[0,0]);
NewtonCollisionForEachPolygonDo(NewtonBodyGetCollision(Body), @TmpM[0,0], @Debug_ShowGeometryCollision, nil);
end;

// =============================================================================
//  Debug_ShowCollision
// =============================================================================
//  Show all collision geometries.
// =============================================================================
procedure Debug_ShowCollision;
begin
 NewtonWorldForEachBodyInAABBDo(Form1.NewtonWorld, @(Form1.WorldSizeMin[0]), @(Form1.WorldSizeMax), @Debug_ShowBodyCollision, nil);
end;


And these are the errors:

main.pas(136,103) Error: Incompatible type for arg no. 3: Got "<address of procedure(const PNewtonBody,LongInt,const PFloat,LongInt);CDecl>", expected "<procedure variable type of procedure(Pointer,LongInt,const PFloat,LongInt);CDecl>"
NewtonImport.pas(864,11) Hint: Found declaration: NewtonCollisionForEachPolygonDo(const PNewtonCollision,const PFloat,NewtonCollisionIterator,Pointer); CDecl;
main.pas(146,125) Error: Incompatible type for arg no. 4: Got "<address of procedure(const PNewtonBody);CDecl>", expected "PNewtonBodyIterator"
NewtonImport.pas(596,11) Hint: Found declaration: NewtonWorldForEachBodyInAABBDo(const PNewtonWorld,const PFloat,const PFloat,PNewtonBodyIterator,Pointer); CDecl;
main.pas(426) Fatal: There were 2 errors compiling module, stopping


So the error is in the:

NewtonWorldForEachBodyInAABBDo(Form1.NewtonWorld, @(Form1.WorldSizeMin[0]), @(Form1.WorldSizeMax), @Debug_ShowBodyCollision, nil);

Particulary here: @Debug_ShowBodyCollision

It seems (at my eyes) similar to @ForceAndTorqueCallback (link) that I use already without problems.

Do you know where is the problem?

Thank you!
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron