NewtonCollisionCalculateAABB bug?

Report any bugs here and we'll post fixes

Moderators: Sascha Willems, Thomas

NewtonCollisionCalculateAABB bug?

Postby VeT » Sun Nov 15, 2009 7:47 pm

Code: Select all
NewtonCollision* treecollision;
void newton_addstaticcollisiongeometry()
{   
   D3DXVECTOR4 minBox;
   D3DXVECTOR4 maxBox;
   D3DXMATRIX mat;
   
   treecollision = NewtonCreateTreeCollision(nworld,0);   
   NewtonTreeCollisionBeginBuild(treecollision);
   
   newton_treecollisionaddblocks(treecollision);

   NewtonTreeCollisionEndBuild(treecollision, 0);

   if (newton_level) { NewtonDestroyBody(nworld, newton_level); }
   newton_level = NewtonCreateBody(nworld, treecollision);
   
   NewtonBodyGetMatrix(newton_level, &mat);   
   NewtonCollisionCalculateAABB (treecollision, &mat, &minBox, &maxBox);

   // add some extra padding
   minBox.x -=  50;
   minBox.y -=  50;
   minBox.z -=  50;
   
   maxBox.x +=  50;
   maxBox.y +=  50;
   maxBox.z +=  50;
   
   // set the new world size
   NewtonSetWorldSize (nworld, &minBox, &maxBox);

   NewtonReleaseCollision(nworld, treecollision);

   draw_red_rect3d(minBox.x, minBox.y, minBox.z, maxBox.x, maxBox.y, maxBox.z);
}


But i get this
http://pic.ipicture.ru/uploads/091116/T3Uog6z7YX.jpg
Yellow lines is collision, that was added and work
Red lines is minBox and maxBox, calculated by NewtonCollisionCalculateAABB()
So, looking like red cube may be larger than all level collition, highlighted with yellow.
Whats wrong?
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: NewtonCollisionCalculateAABB bug?

Postby Julio Jerez » Sun Nov 15, 2009 8:27 pm

the aabb in Netwon is not always a tight AABB wraped around the body shape there some padding that vairies with the different collision shapes.

you should not be using NewtonCollisionCalculateAABB() for game play
the aabb is newton is an optimal aabb for physics collision it is not a tight AABB around the shape.
if you want to calculate a tight aabb you need to use the function GetSuportVertex,
here it is a function to do that

Code: Select all
// Utility function to calculate the Bounding Box of a collision shape
void CalculateLocalAABB (const NewtonCollision* shape, dVector& boxMin, dVector& boxMax)
{
   // for exact axis find minimum and maximum limits
   for (int i = 0; i < 3; i ++) {
      dVector point;
      dVector dir (0.0f, 0.0f, 0.0f, 0.0f);

      // find the most extreme point along this axis and this is maximum Box size on that direction
      dir[i] = 1.0f;
      NewtonCollisionSupportVertex (shape, &dir[0], &point[0]);
      boxMax[i] = point % dir;

      // find the most extreme point along the opposite axis and this is maximum Box size on that direction
      dir[i] = -1.0f;
      NewtonCollisionSupportVertex (shape, &dir[0], &point[0]);
      boxMin[i] = -(point % dir);
   }
}


by setting matrix to identity you get the local aabb of a body,
if you want a world aligned AABB, you take the body matrix and pass the inverse of the body matrix to the function
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB bug?

Postby JernejL » Sun Nov 15, 2009 9:02 pm

NewtonCollisionSupportVertex sounds like really useful function, but has zero documentation, even forum search returns only 6 posts and none explains what it really does.

Can you please write a short description for it so i can document it on the wiki?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: NewtonCollisionCalculateAABB bug?

Postby Julio Jerez » Sun Nov 15, 2009 9:43 pm

ok I will try to make a skect so teh it is more clear,
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB bug?

Postby VeT » Sun Nov 15, 2009 9:56 pm

dir[i] = 1.0f;
dir = matrix.RotateVector (dir);
NewtonCollisionSupportVertex (collision, &dir[0], &p1[0]);

i think you mean NewtonCollisionSupportVertex (collision, &dir[i], &p1[i]); ?
Thanks for describing, i'll try this.
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: NewtonCollisionCalculateAABB bug?

Postby Julio Jerez » Sun Nov 15, 2009 10:10 pm

Ok here is how it works
The function NewtonCollisionSupportVertex returns the point that maps to the maximum projection of all points of the collision shape alone direction vector dir.
The pseudo code will look like this.
Code: Select all
NewtonCollisionSupportVertex (shape, dir, returnPoint)
{
   returnPoint = point[0]
   float maxValue = -1.0e15
   for each point in shape
   {
      if (dotProduct (point[i], dir) > max) {
         maxValue = dotProduct (point[i], dir)
         returnPoint = point[i]
      }
   }
}


In the following diagram the function will return point 2 because the maximum projection of each of the
point of the shape along dir happen at point[2]
Image

as you can see if you have an efficient implemention of NewtonCollisionSupportVertex you can calculate the bounding box along any direction, not just in global space.
For global space you do not need matrix
this function which you can find in the SDK demos in file PhyscUtis.cpp, calculates the AABB for any NewtonBody

Code: Select all
static void CalcylateAABB (const NewtonBody* body, dVector& minP, dVector& maxP)
{
   NewtonCollision *collision;

   // show an exact AABB
   collision = NewtonBodyGetCollision (body) ;

   dMatrix matrix;
   NewtonBodyGetMatrix (body, &matrix[0][0]);

   for (int i = 0; i < 3; i ++) {
      dVector support;
      dVector dir (0.0f, 0.0f, 0.0f, 0.0f);
      dir[i] = 1.0f;

      dVector localDir (matrix.UnrotateVector (dir));
      NewtonCollisionSupportVertex (collision, &localDir[0], &support[0]);
      support = matrix.TransformVector (support);
      maxP[i] = support[i]; 

      localDir = localDir.Scale (-1.0f);
      NewtonCollisionSupportVertex (collision, &localDir[0], &support[0]);
      support = matrix.TransformVector (support);
      minP[i] = support[i]; 
   }
}


The funtion NewtonCollisionSupportVertex is the workhorse of the Newton collision engine, it is call 10's of tounsand of time per secund by the collision system,
the pseudo code makes it looks ineficient, but optimized versions of that functions are complicate because the
trick is to make it such that if find the best point by testing the least number points possible of the shape.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB bug?

Postby VeT » Mon Nov 16, 2009 8:16 am

mmm... Julio, can you move void CalcylateAABB (const NewtonBody* body, dVector& minP, dVector& maxP) into DLL, please?
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: NewtonCollisionCalculateAABB bug?

Postby JernejL » Mon Nov 16, 2009 12:23 pm

VeT wrote:mmm... Julio, can you move void CalcylateAABB (const NewtonBody* body, dVector& minP, dVector& maxP) into DLL, please?


It only consists of a few NewtonCollisionSupportVertex calls.. that's easily incroporatable into your app..
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: NewtonCollisionCalculateAABB bug?

Postby VeT » Mon Nov 16, 2009 1:29 pm

Yes, but my engine dont works good with matrixes, so, on my opinion, it better to calculate matrixes in DLL.
Anyway, its usefull function for other users, i think.
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: NewtonCollisionCalculateAABB bug?

Postby JernejL » Mon Nov 16, 2009 2:35 pm

Ok, i added all this new information to the wiki, thanks.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: NewtonCollisionCalculateAABB bug?

Postby Julio Jerez » Mon Nov 16, 2009 3:01 pm

Vet you must have matrix operations in you engine.
how do you do matrix rotations?

That function is no really to complicated. take a secudn look you will see it is no tha complext.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB bug?

Postby Marc » Fri Nov 27, 2009 6:02 am

Is this still supposed to work?

When I try to convert a mesh to a newtoncollision, and calculate the aabb manually in front, I get:
The first 3 values are min, the last 3 max of the aabb.

RAW BB: -3.126976 -10.969679 -3.616533 3.500324 0.019121 3.568864

After having created the newton collision with different versions of newton.dll and using the supportvector code, I get:

2.11: NTC BB: -8.640879 -10.555354 -0.240925 1.854205 0.019121 3.549396
2.10: NTC BB: -10.968834 -9.440879 -3.500425 3.035548 3.495232 3.260570
2.8: NTC BB: -10.968834 -8.772891 -8.642138 3.415213 3.503505 3.384041

here is the code doing it, basically identical to to code from above:
Code: Select all
      float bb[6];
      for (int i = 0; i < 3; i ++)
      {
         dVector point;
         dVector dir (0.0f, 0.0f, 0.0f, 0.0f);

         // find the most extreme point along this axis and this is maximum Box size on that direction
         dir[i] = 1.0f;
         NewtonCollisionSupportVertex (col, &dir[0], &point[0]);
         bb[3 + i] = point % dir;

         // find the most extreme point along the opposite axis and this is maximum Box size on that direction
         dir[i] = -1.0f;
         NewtonCollisionSupportVertex (col, &dir[0], &point[0]);
         bb[i] = -(point % dir);
      }
      _tprintf(_T("NTC BB:   %f %f %f   %f %f %f\n"), bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]);
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: NewtonCollisionCalculateAABB bug?

Postby Julio Jerez » Fri Nov 27, 2009 10:14 am

yes it is supposed to work, it is what the engine use for collision.

are the results incorrect?

one way you can verify teh result in buy doing a brute force caluate AABB ison get debug display functions.
sicne they will report the faces one at a time, it is eassy to get the AABB.
the you can compare the result with the optimized verstion using SopporteVrtex.

The importance of using SupportVertex is that it is much more efficient than using brute force.
I believe they all work since those are the functions the engine uses for everything related to collision.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB bug?

Postby Marc » Fri Nov 27, 2009 1:37 pm

The collisions work correctly, what only makes me wonder is the result of this method to calculate the aabb. As far as I understand, the resulting bb should be tight. How can it be, that for the same mesh, I get so different results from different versions of newton.dll? Even if polygons get removed in the optimizing process for collision smoothness reasons or some other reasons, can this explain the differences between the versions?

I'll try and use the debug functions to check the mesh that gets created by newton.
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: NewtonCollisionCalculateAABB bug?

Postby Julio Jerez » Fri Nov 27, 2009 3:57 pm

not optimization does not move polygons, it just remove edges but does not change vertices.
as af as I knwo the AABB sopule teh same in all cases, but I hav enever make a comparizon form version to version.
There may be a Bug.

wher you using teh same funtion to calculate teh AABB in the previuos version?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Next

Return to Bugs and Fixes

Who is online

Users browsing this forum: No registered users and 3 guests