Lazarus + GLScene

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Lazarus + GLScene

Postby Kjow » Sat Jun 26, 2010 7:52 am

Hi all,

I'm trying to make a simple Newton 2.0 demo for Lazarus and GLScene, but I have some problems.
First of all, this demo compile and works right by command line (e.g. fpc SDLNewtonBasicDemo.pas ):

// =============================================================================
//
// SDLNewtonBasicDemo.pas
// Very basic demo that shows how to use Newton Game Dynamics.
//
// =============================================================================
// Note : Using Newton 2.xx
// =============================================================================
//
// 2010 by Sascha Willems (www.saschawillems.de)
// Newton Game Dynamics © 2003-2010 by Julio Jerez (www.newtondynamics.com)
//
// =============================================================================

So, I tried to copy some code from it to my "demo", but when compiling I have this error:

Hint: Start of reading config file C:\Develop\fpc\2.5.1\bin\i386-Win32\fpc.cfg
Hint: End of reading config file C:\Develop\fpc\2.5.1\bin\i386-Win32\fpc.cfg
Free Pascal Compiler version 2.5.1 [2010/06/26] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Win32 for i386
Compiling Engine3D.lpr
Compiling main.pas
main.pas(34,14) Hint: Type "TVector3f" redefinition
main.pas(38,14) Hint: Type "TMatrix4f" redefinition
main.pas(92,60) Hint: Parameter "TimeStep" not used
main.pas(92,78) Hint: Parameter "ThreadIndex" not used
main.pas(141,49) Error: Wrong number of parameters specified for call to "ForceAndTorqueCallback"
main.pas(92,11) Hint: Found declaration: ForceAndTorqueCallback(const PNewtonBody,Single,LongInt); CDecl;
main.pas(152) Fatal: There were 1 errors compiling module, stopping

then it point to this row:

NewtonBodySetForceAndTorqueCallBack(NewtonBody, ForceAndTorqueCallback);

Where am I in wrong?

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

Re: Lazarus + GLScene

Postby JernejL » Sat Jun 26, 2010 9:25 am

those types are named same in both newton.pas and glscene's vectors unit.

if you refer to them like var a = newtonunit.tglvector3f you'll no longer have such issues, but the likely cause is, that command line and ide build process use different order of including files in uses clause.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1588
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Lazarus + GLScene

Postby Kjow » Sat Jun 26, 2010 8:30 pm

I renamed types as here:

TVector3fCustom = record
X,Y,Z : Single;
end;

TMatrix4fCustom = array[0..3, 0..3] of Single;

but I think that the problem is here:

main.pas(141,49) Error: Wrong number of parameters specified for call to "ForceAndTorqueCallback"
main.pas(92,11) Hint: Found declaration: ForceAndTorqueCallback(const PNewtonBody,Single,LongInt); CDecl;

with:

procedure ForceAndTorqueCallback(const Body : PNewtonBody; TimeStep : Float; ThreadIndex : int); cdecl;
var
Mass : Single;
Inertia : TVector3fCustom;
Force : TVector3fCustom;
begin
NewtonBodyGetMassMatrix(Body, @Mass, @Inertia.x, @Inertia.y, @Inertia.z);
Force := V3(0, -9.8 * Mass, 0);
NewtonBodyAddForce(Body, @Force.x);
end;

constructor TNewtonBox.Create(pSize, pPosition : TVector3fCustom; pMass : Single);
var
Inertia : TVector3fCustom;
Collision : PNewtonCollision;
begin
...
...
// Finally set the callback in which the forces on this body will be applied
NewtonBodySetForceAndTorqueCallBack(NewtonBody, ForceAndTorqueCallback);
end;
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Stucuk » Sat Jun 26, 2010 10:28 pm

Code: Select all
NewtonBodySetForceAndTorqueCallback(NewtonBody, @ForceAndTorqueCallback);


You need @ to turn it into a pointer. The way your calling it is actualy telling the compiler that you want to Run ForceAndTorqueCallback and give its result as a parameter to NewtonBodySetForceAndTorqueCallback. As a pointer the compiler doesn't try to run ForceAndTorqueCallback.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Lazarus + GLScene

Postby Kjow » Mon Jun 28, 2010 3:46 am

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

Re: Lazarus + GLScene

Postby Kjow » Mon Jun 28, 2010 6:45 pm

Hi again,

I got a cube (GLCube) that falls over a mesh (GLFreeForm), but the FreeForm is static!

Here the code I use, that loads into Newton the mesh:

Code: Select all
GLFreeForm.LoadFromFile(ExtractFilePath(Application.ExeName)+'mesh.obj');

  xT:= 1;
  yT:= 1;
  zT:= 1;
  massT:=0.001;

  IxxT := massT * (1/12) * (yT*yT+zT*zT);
  IyyT := massT * (1/12) * (xT*xT+zT*zT);
  IzzT := massT * (1/12) * (xT*xT+yT*yT);

  static:= NewtonCreateTreeCollision(Newtonworld, 0);
  NewtonTreeCollisionBeginBuild(static);

  test:= TAffineVectorList.create;
  test:= GLFreeForm.MeshObjects.Items[0].ExtractTriangles;

  for i:= 0 to test.Count div 3 do
  begin
    v[0]:= test[i*3 + 0];
    v[1]:= test[i*3 + 1];
    v[2]:= test[i*3 + 2];
    NewtonTreeCollisionAddFace(static, 3, @v[0], 12, 1);
  end;

  NewtonTreeCollisionEndBuild(static, 1); // with optimization

  NewtonBodyStaticMesh := NewtonCreateBody(Newtonworld, static);

  NewtonBodySetMassMatrix(NewtonBodyStaticMesh, massT, IxxT, IyyT, IzzT);
  Newtonbodysetmatrix(NewtonBodyStaticMesh,NewtonImport.PFloat(GLFreeForm.AbsoluteMatrixAsAddress));

  NewtonbodysetUserdata(NewtonBodyStaticMesh,GLFreeForm);
  GLFreeForm.TagObject := TObject(NewtonBodyStaticMesh);

  NewtonBodySetForceAndTorqueCallBack(NewtonBodyStaticMesh, @ForceAndTorqueCallBack);

  test.Free;     


I load this on Form.Create, and everything works right. The GLCube seems to react well/realistic with the mesh.

Now, how can I load a second mesh (e.g. GLFreeForm2), but as dynamic object? I mean that the second mesh should fall, respond to gravity and bounce over first mesh.

Is the code I use right? Can it be optimized?

I'm sorry, but I'm starting now with Newton...
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Stucuk » Tue Jun 29, 2010 12:40 am

Collision Trees are only static objects.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Lazarus + GLScene

Postby Kjow » Tue Jun 29, 2010 3:22 am

OK, but how to modify this code to add a dynamic object? (I mean a GLFreeForm, I already added a "dynamic" GLCube, but it isn't a "mesh")

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

Re: Lazarus + GLScene

Postby Carli » Tue Jun 29, 2010 4:04 am

You can use a convex hull to get a dynamic object.
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Lazarus + GLScene

Postby Kjow » Tue Jun 29, 2010 4:29 am

Do you mean NewtonCreateConvexHullFromMesh or NewtonCreateConvexHull?

However, how to use it?

I don't know how to fill each parameter, e.g.:

dyn:= NewtonCreateConvexHull(Newtonworld, ???, ???, ???, 0.002, 2, NewtonImport.PFloat(GLFreeForm2.AbsoluteMatrixAsAddress));

or

dyn:= NewtonCreateConvexHullFromMesh(newtonWorld, ???, 0.002, 2);
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Re: Lazarus + GLScene

Postby Carli » Tue Jun 29, 2010 5:17 am

1.???=number of points (=3*triangles)
2.???=pointer to first point (array[0..count-1][0..2] of single)
3.???=stride (=sizeof(array[0..2] of single))
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Lazarus + GLScene

Postby Kjow » Tue Jun 29, 2010 5:23 am

And how can I extract these info from a GLFreeForm?

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

Re: Lazarus + GLScene

Postby Stucuk » Tue Jun 29, 2010 5:39 am

Carli wrote:1.???=number of points (=3*triangles)

Not everyone will be using triangles. Its just a cloud of points. So you can't say its 3*triangles as i could pass 6 points which would create a valid convex hull but would not create any triangle/polygon.

Carli wrote:3.???=stride (=sizeof(array[0..2] of single))

Thats only correct if he is only passing the actual vertex's. If his vertex structure has additional information then that would be wrong.

Kjow wrote:And how can I extract these info from a GLFreeForm?

That would be best asked on the GLScene forum as most here are not going to be using it.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Lazarus + GLScene

Postby Carli » Tue Jun 29, 2010 6:09 am

Stucuk wrote:Not everyone will be using triangles.

...
Thats only correct if he is only passing the actual vertex's.


but i saw that he is not very familar with 3d etc. (espeacially when he asks GLScene questions in a Newton-Forum) so i told him only those things he would need to get that one example running.

For a more general usage, i would have said to him "click on NewtonCreateConvexHull and read the wiki"
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Lazarus + GLScene

Postby Kjow » Tue Jun 29, 2010 7:55 am

Thank you both.
I asked about GLScene here, because the topic is "Lazarus + GLScene", so I wished for help from people that could help me on this platform.

The wiki is not very friendly, especially for ObjectPascal users, and there isn't a tutorial that starts from 0% and arrives to 100% of knowledge. Nowhere I found an example or tutorial that tell me that I need some dll calls instead of others... all I know is from tests and sparse Bits and some help...

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.

However, if there isn't anyone that know GLScene, I'll limit myself to ask here things strictly relevant to Newton. :)

So, to add a dynamic object, do I need the NewtonCreateConvexHull call? (and not NewtonCreateConvexHullFromMesh)

Can anyone adapt this code to make this object dynamic?

Code: Select all
GLFreeForm.LoadFromFile(ExtractFilePath(Application.ExeName)+'mesh.obj');

  xT:= 1;
  yT:= 1;
  zT:= 1;
  massT:=0.001;

  IxxT := massT * (1/12) * (yT*yT+zT*zT);
  IyyT := massT * (1/12) * (xT*xT+zT*zT);
  IzzT := massT * (1/12) * (xT*xT+yT*yT);

  static:= NewtonCreateTreeCollision(Newtonworld, 0);
  NewtonTreeCollisionBeginBuild(static);

  test:= TAffineVectorList.create;
  test:= GLFreeForm.MeshObjects.Items[0].ExtractTriangles;

  for i:= 0 to test.Count div 3 do
  begin
    v[0]:= test[i*3 + 0];
    v[1]:= test[i*3 + 1];
    v[2]:= test[i*3 + 2];
    NewtonTreeCollisionAddFace(static, 3, @v[0], 12, 1);
  end;

  NewtonTreeCollisionEndBuild(static, 1); // with optimization

  NewtonBodyStaticMesh := NewtonCreateBody(Newtonworld, static);

  NewtonBodySetMassMatrix(NewtonBodyStaticMesh, massT, IxxT, IyyT, IzzT);
  Newtonbodysetmatrix(NewtonBodyStaticMesh,NewtonImport.PFloat(GLFreeForm.AbsoluteMatrixAsAddress));

  NewtonbodysetUserdata(NewtonBodyStaticMesh,GLFreeForm);
  GLFreeForm.TagObject := TObject(NewtonBodyStaticMesh);

  NewtonBodySetForceAndTorqueCallBack(NewtonBodyStaticMesh, @ForceAndTorqueCallBack);

  test.Free;     


Thank you very much for patience.
Kjow
 
Posts: 56
Joined: Thu Nov 13, 2008 11:33 am

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests