No Collision Detected

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

No Collision Detected

Postby lesPaul456 » Thu Dec 31, 2009 9:30 pm

Hello,

I just installed the Newton Physics SDK, and I'm trying to integrate it with Dark GDK.

I created a two collision objects and bodies. I am updating the gravity of one of them. The gravity works fine, but there is no collision. The moving object passes right through the static one. Does Newton handle collision itself, like I thought? Or, do I need to respond to the collision myself?

Here's my code:

Code: Select all
#include "DarkGDK.h"
#include "Newton.h"

#include "dMatrix.h"

#define BOXID 1

// Callbacks
void ApplyForceAndTorqueCallback(const NewtonBody* body, const float timeStep, int threadIndex);
void SetTransformCallback(const NewtonBody* body, const float* matrix, int threadIndex);

// the main entry point for the application is this function
void DarkGDK ( void )
{
   // turn on sync rate and set maximum rate to 60 fps
   dbSyncOn   ( );
   dbSyncRate ( 60 );

   NewtonWorld* pWorld = NewtonCreate();

   dbMakeObjectBox(BOXID, 5, 5, 5);

   NewtonCollision* pCollision = NewtonCreateBox(pWorld, 5, 5, 5, BOXID, NULL);
   NewtonBody* pBody = NewtonCreateBody(pWorld, pCollision);

   NewtonReleaseCollision(pWorld, pCollision);

   NewtonBodySetMassMatrix(pBody, 1.0f, 5.0f, 1.0f, 5.0f);
   NewtonBodySetUserData(pBody, (void*)BOXID);
   
   NewtonBodySetForceAndTorqueCallback(pBody, ApplyForceAndTorqueCallback);
   NewtonBodySetTransformCallback(pBody, SetTransformCallback);

   dMatrix matrix;
   matrix.m_posit.m_x = 0.0f;
   matrix.m_posit.m_y = 10.0f;
   matrix.m_posit.m_z = 0.0f;
   NewtonBodySetMatrix(pBody, &matrix[0][0]);

   dbMakeObjectBox(10, 50, 5, 50);

   pCollision = NewtonCreateBox(pWorld, 50, 5, 50, 10, NULL);
   NewtonBody* pGroundBody = NewtonCreateBody(pWorld, pCollision);

   NewtonReleaseCollision(pWorld, pCollision);

   // our main loop
   while ( LoopGDK ( ) )
   {
      // update the screen
      dbSync ( );

      NewtonUpdate(pWorld, 0.01f);
   }

   NewtonDestroyAllBodies(pWorld);
   NewtonDestroy(pWorld);

   // return back to windows
   return;
}

void ApplyForceAndTorqueCallback(const NewtonBody* body, const float timeStep, int threadIndex)
{
   float force[3];
   float torque[3];

   force[0] = 0.0f; // Force on the x-axis.
   force[1] = -10.0f; // Gravity on the y-axis.
   force[2] = 0.0f; // Force on the z-axis.

   torque[0] = 0.0f;
   torque[1] = 0.0f;
   torque[2] = 0.0f;

   NewtonBodyAddForce(body, force);
   NewtonBodyAddTorque(body, torque);
}

void SetTransformCallback(const NewtonBody* body, const float* matrix, int threadIndex)
{
   dMatrix& mat = *((dMatrix*)matrix);

   int objID = (int)NewtonBodyGetUserData(body);

   dbPositionObject(objID, mat.m_posit.m_x, mat.m_posit.m_y, mat.m_posit.m_z);
   dbRotateObject(objID, mat.GetXYZ_EulerAngles().m_x, mat.GetXYZ_EulerAngles().m_y, mat.GetXYZ_EulerAngles().m_z);
}
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Stucuk » Thu Dec 31, 2009 9:37 pm

I assume its because you havn't setup any mass matrix, etc on the ground body.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: No Collision Detected

Postby lesPaul456 » Thu Dec 31, 2009 10:09 pm

Thanks for the reply.

I tried giving the ground a mass, but that didn't work. The moving object still passes through the ground. Just to be sure, Newton handles the collision itself right? All I have to do is setup the collision primitives and bodies, and then call NewtonUpdate, right?

Thanks!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Julio Jerez » Thu Dec 31, 2009 11:16 pm

my sugestion is that you implement debug display so that you can see where the physics shapes are in teh world.
I am guessing the physics shapes are either different size or in different possition than what the visual objects indicate.

some graphics libraries use matrices that need to be transposed, other uses angles in different order than pitch, yaw, roll.
all those problems can be solved but the first thing you need to do is implement debug display using the debug functions of newton to render the collision shape and see if the physics is right.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: No Collision Detected

Postby lesPaul456 » Thu Dec 31, 2009 11:45 pm

Thanks. Could you explain how to use the debug display?

Thanks again!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Julio Jerez » Fri Jan 01, 2010 8:52 am

look at the demos ther are some examples funtion that can help you.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: No Collision Detected

Postby lesPaul456 » Fri Jan 01, 2010 10:17 am

Thanks Julio, but I'm feeling kind of dumb right now...

I looked through the demos, and I saw the DebugDisplay files...but I'm not sure how to add them to my project. When I tried, I got an error asking for the "gluw", "glut" and "glui" header files. I added them to my project's additional includes, but then I got an error asking for the "freeglut.lib" file. I couldn't find that, and now I'm a little confused....

Just looking through the DebugDisplay.cpp file, I think I can see how it works. If I can't get this to work, I'll try to make my own using DirectX (I'm not really a C++ programmer though).

Thanks!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Julio Jerez » Fri Jan 01, 2010 11:00 am

basically after you call an newton updtae you can itrate over all bodies in teh scene and do this

Code: Select all
static void DebugShowGeometryCollision (void* userData, int vertexCount, const dFloat* faceVertec, int id)
{
   int i;
   i = vertexCount - 1;
   dVector p0 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
   for (i = 0; i < vertexCount; i ++) {
      dVector p1 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
//      glVertex3f (p0.m_x, p0.m_y, p0.m_z);
//      glVertex3f (p1.m_x, p1.m_y, p1.m_z);
          // here submit a line for point p0 to p1 using your graphics engine functions.


                                         // next out line for this polygon
      p0 = p1;
   }
}


static void DebugShowBodyCollision (const NewtonBody* body, void* userData)
{
//   float matrix[16];
      NewtonBodyGetMatrix(body, &matrix[0][0]);
      NewtonCollisionForEachPolygonDo (NewtonBodyGetCollision(body), matrix, DebugShowGeometryCollision, NULL);
}



void DebugRenderCollision (const NewtonWorld* world)
{
//   glDisable (GL_LIGHTING);
//   glDisable(GL_TEXTURE_2D);

   dVector aabbP0 (-1000.0f, -500.0f, -1000.0f);
   dVector aabbP1 (1000.0f, 500.0f, 1000.0f);

//   glColor3f(1.0f, 1.0f, 1.0f);
//   glBegin(GL_LINES);
 
                   // add the initilization code to render triangles
   NewtonWorldForEachBodyInAABBDo (world, &aabbP0[0], &aabbP1[0], DebugShowBodyCollision, NULL);
//   glEnd();

}


NewtonUpdate (world, timestep);
DebugRenderCollision (world);
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: No Collision Detected

Postby lesPaul456 » Fri Jan 01, 2010 11:48 am

Sorry about all this...

When I added those methods, I saw nothing different. I tried pulling the camera back, but there was nothing to be seen.

I'm guessing this is because DarkGDK uses DirectX for rendering. Unfortunately, DarkGDK has no functions for drawing 3D lines, and I do not know how to do this in DirectX. I'll keep messing around with it though.

Thank you for your time. If you have any ideas on how I can fix this, let me know. :D
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Julio Jerez » Fri Jan 01, 2010 11:52 am

It has nothingg to do with d3d or directx, it can be done with either one.
if you can draw tringle then you can render then as solid triangles

Code: Select all
static void DebugShowGeometryCollision (void* userData, int vertexCount, const dFloat* faceVertec, int id)
{
   int i;
   i = vertexCount - 1;
   dVector p0 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
   dVector p1 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
   for (i = 2; i < vertexCount - 2; i ++) {
      dVector p2 (faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);

      // here submit a line for point p0, p1, p2 using your graphics engine functions.
      ReanderDarkGDK (p0, p1, p2) ;

     // next out line for this polygon
      p1 = p2;
   }
}
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: No Collision Detected

Postby lesPaul456 » Fri Jan 01, 2010 12:04 pm

Thanks Julio.

I'm not sure how to render 3d lines with DarkGDK. I'm posting a question about on their forums. I'll post back once I get this working.

Thanks again for all your help!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Julio Jerez » Fri Jan 01, 2010 12:25 pm

but you do not really need to render lines you can render teh debug as triangles, teh funtion i just posted does that.

I am sure the engine must have a way to render triangles.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: No Collision Detected

Postby lesPaul456 » Fri Jan 01, 2010 1:01 pm

There's the dbMakeObjectTriangle command. I tried it, but I still didn't see anything.

However, I added a few lines to allow me to control the camera. Then, I watched the object fall. It passed through the box, fell for a while, and then just stopped. So, I guess the collision is being detected and, just as you stated, the graphical representation is not positioned correctly.

So, maybe I'm just missing something, like a scale factor between Newton and DarkGDK.

Thanks!
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Re: No Collision Detected

Postby Julio Jerez » Fri Jan 01, 2010 3:01 pm

dbMakeObjectTriangle woudl bio be a good idea because it will brabably hav eot submited to some king of renderin pile line and it will ne a matrix.

there go to be a immidiate mode to render promitive if DGSDK, teh idea of using the Debug physic renderin is tah teh gemetry is in global space so you do no need any object matrux to render it,
threfore it show any discreapancy between teh graphics and teh physics.
I suggest you find out how to render in immidiate mode is DGsdk, tehr got to be a way, maybe creation the objects and destriung each time.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: No Collision Detected

Postby lesPaul456 » Sat Jan 02, 2010 12:09 am

Okay, I got the debug display working.

In the center of the screen I can see the body I created for the ground. The falling object is no where to be seen.
User avatar
lesPaul456
 
Posts: 17
Joined: Thu Dec 31, 2009 9:24 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 635 guests