NewtonCollisionCalculateAABB incorrect? (new)

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

NewtonCollisionCalculateAABB incorrect? (new)

Postby agi_shi » Sat Apr 03, 2010 4:53 pm

EDIT: OK, now I believe I have finally found this odd little bug :mrgreen:

First of all, demo code that reproduces the bug (should compile as-is, it's console-based):
Code: Select all
#include <cstddef>
#include <iostream>
#include <Newton.h>

int main()
{
   std::cout.setf(std::ios::floatfield, std::ios::fixed);
   std::cout.precision(2);

   NewtonWorld *world = NewtonCreate();

   float boxCloud[8][3] = // 1x1x1 diameter box
   {
      { -0.5f, 0.5f, 0.5f }, // top left front
      { -0.5f, 0.5f, -0.5f }, // top left back
      { 0.5f, 0.5f, 0.5f }, // top right front
      { 0.5f, 0.5f, -0.5f }, // top right back
      { -0.5f, -0.5f, 0.5f }, // bottom left front
      { -0.5f, -0.5f, -0.5f }, // bottom left back
      { 0.5f, -0.5f, 0.5f }, // bottom right front
      { 0.5f, -0.5f, -0.5f } // bottom right back
   };
   NewtonCollision *boxCol = NewtonCreateConvexHull(world, 8, &boxCloud[0][0], sizeof(float) * 3, 0.01f, 0, NULL);
   NewtonCollision *boxMod = NewtonCreateConvexHullModifier(world, boxCol, 0);

   float scale2[] =
   {
      2.0f, 0.0f, 0.0f, 0.0f,
      0.0f, 2.0f, 0.0f, 0.0f,
      0.0f, 0.0f, 2.0f, 0.0f,
      0.0f, 0.0f, 0.0f, 1.0f
   };

   // CASE 1: IF WE SET THE SCALE HERE, THE CALCULATED AABB IS CORRECT
   //NewtonConvexHullModifierSetMatrix(boxMod, scale2);

   // create compound collision out of the convex hull modifier
   NewtonCollision *boxCompound = NewtonCreateCompoundCollision(world, 1, &boxMod, 0);

   // CASE 2: IF WE INSTEAD SET THE SCALE HERE, THE CALCULATED AABB IS INCORRECT (as if no scale set)
   NewtonConvexHullModifierSetMatrix(boxMod, scale2);

   float identity[] =
   {
      1.0f, 0.0f, 0.0f, 0.0f,
      0.0f, 1.0f, 0.0f, 0.0f,
      0.0f, 0.0f, 1.0f, 0.0f,
      0.0f, 0.0f, 0.0f, 1.0f
   };
   float aabb[2][3];
   NewtonCollisionCalculateAABB(boxCompound, identity, &aabb[0][0], &aabb[1][0]);

   // output should be about (-1, -1, -1) for min and (1, 1, 1) for max, +/- newton's padding factor
   std::cout << "aabb min : (" << aabb[0][0] << ", " << aabb[0][1] << ", " << aabb[0][2] << ")" << std::endl;
   std::cout << "aabb max : (" << aabb[1][0] << ", " << aabb[1][1] << ", " << aabb[1][2] << ")" << std::endl;

   NewtonDestroy(world);
}


Please take a look at the two comments labeled "CASE 1:" and "CASE 2:"

The bug is as follows:
- create a convex hull (I do not know if this bug also applies to primitives)
- create a convex hull modifier out of the convex hull
- create a compound collision out of the convex hull modifier
- scale the compound collision by scaling it's contained convex hull modifier (obviously this is more useful when there are several pieces instead of just one)
- now, the AABB will be incorrect, it will not take into account the scale

HOWEVER! If we set the scale of the convex hull modifier BEFORE we add it to the compound collision, then the AABB is correct - unless we rescale the modifier again, then it would just report the old AABB.

Only the AABB seems to be incorrect - actual collisions, as well as ray casting, work fine either way.

Thanks for your time :)
Last edited by agi_shi on Mon May 03, 2010 7:25 pm, edited 1 time in total.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: NewtonCollisionCalculateAABB incorrect for convex modifier?

Postby Julio Jerez » Sat Apr 03, 2010 8:56 pm

are you sure? was it ever right in any previus version?
I have not mess with that code in a long time.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB incorrect for convex modifier?

Postby agi_shi » Sat Apr 03, 2010 9:07 pm

Julio Jerez wrote:are you sure? was it ever right in any previus version?
I have not mess with that code in a long time.

Basically what I'm doing is calculating the bounding box of a physics object and then testing it against the camera's frustum to see if I should render it or not. The problem is that even if I scale the object (by setting the convex hull modifier's matrix), I get the "original" bounding box, so the object disappears even if it should still be in view.

I'm not 100% sure if it worked right in "beta 18". I do remember, however, that I did have some issues where scaling an object wouldn't actually scale it until it interacted with another object. Meaning if I scaled an object and then did a ray-cast, the ray cast would behave as if the object wasn't scaled - until another object collided with this object, and then everything worked. Maybe this bounding box issue has the same root.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: NewtonCollisionCalculateAABB incorrect for convex modifier?

Postby Julio Jerez » Sun Apr 04, 2010 8:56 am

Ray cast malfuntion too? all right I will check it out and see what it is.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB incorrect for convex modifier?

Postby agi_shi » Sun Apr 04, 2010 12:18 pm

Julio Jerez wrote:Ray cast malfuntion too? all right I will check it out and see what it is.

Hm, yeah, I'm not 100% sure what's going wrong. I made a little test, and everything seems to work fine:
-snip-
All of the output is as expected. Therefore, perhaps it is a bug somewhere in my own code :| I'll dig deeper and tell you if I find anything :)
Last edited by agi_shi on Tue May 04, 2010 7:00 am, edited 1 time in total.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby agi_shi » Mon May 03, 2010 7:25 pm

Found the bug, see original post for details!
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby Julio Jerez » Mon May 03, 2010 8:42 pm

so AABB on scaled collisions are wrong?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby agi_shi » Tue May 04, 2010 6:58 am

Julio Jerez wrote:so AABB on scaled collisions are wrong?

Only when the collision is part of a compound collision, and it is then scaled. The compound collision's AABB won't change to fit the scaled part.

The AABB of the convex hull modifier itself is correct. Just the AABB of the compound collision is wrong.

The demo code in my original post demonstrates that.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby Julio Jerez » Tue May 04, 2010 8:38 am

Ok Agi I see the problem. However I do not think fixing is worth doing it, because it will slow down the run time performance.
Do not worry there is a solution that is even better than using NewtonCollisionCalculateAABB.

It is not worth fixing it, because NewtonCollisionCalculateAABB is meant to be a quick test to find out in what cell of a multi resultion grid the AABB belong.
The AABB is cached and it needs to be extremally fast, because it is called many, many time for proximity test in the broadphase collision. If fact this is the code that does that
Code: Select all
void dgCollisionCompound::CalcAABB (const dgMatrix &matrix, dgVector& p0, dgVector& p1) const
{
   dgVector origin (matrix.TransformVector(m_root->m_origin));
   dgVector size (m_root->m_size.m_x * dgAbsf(matrix[0][0]) + m_root->m_size.m_y * dgAbsf(matrix[1][0]) + m_root->m_size.m_z * dgAbsf(matrix[2][0]) + DG_MAX_COLLISION_PADDING, 
               m_root->m_size.m_x * dgAbsf(matrix[0][1]) + m_root->m_size.m_y * dgAbsf(matrix[1][1]) + m_root->m_size.m_z * dgAbsf(matrix[2][1]) + DG_MAX_COLLISION_PADDING, 
                 m_root->m_size.m_x * dgAbsf(matrix[0][2]) + m_root->m_size.m_y * dgAbsf(matrix[1][2]) + m_root->m_size.m_z * dgAbsf(matrix[2][2]) + DG_MAX_COLLISION_PADDING,
               dgFloat32 (0.0f));

   p0 = origin - size;
   p1 = origin + size;
}

As you can see the Box you get is inaccurate and the only thing it guarantee is that the shape is inside of the AABB.


Here is the solution for and accurate AABB on any shape, that you can find in the FAQ forum.

viewtopic.php?f=15&t=5163&p=36834#p36834

I copied that AABB function in front of your demo and I pasted this at the end of the main function
Code: Select all
   dMatrix matrix (GetIdentityMatrix());
   dVector p0;
   dVector p1;
   CalculteLocalAABB (matrix, boxCompound, p0, p1);

   std::cout << "aabb min : (" << p0[0] << ", " << p0[1] << ", " << p0[2] << ")" << std::endl;
   std::cout << "aabb max : (" << p1[0] << ", " << p1[1] << ", " << p1[2] << ")" << std::endl;

and it gives these result for all cases.

    aabb min : (-1.00, -1.00, -1.00)
    aabb max : (1.00, 1.00, 1.00)
as you can see you get the correct AABB without padding.
I hope this is a better solution.

The benefit of using the AABB in function in the FAQ is that it will result to a very fast calculation for single shapes, and it will do a hierarchical AABB calculation for compound shape or terrains.
NewtonCollisionSupportVertex is one of the most efficient functions in the engine because it is the work horse of the collision system.
It is used to determine collision points and ray hit on all shapes, with high precision. So it is not slow compared to the quality of the result you will get.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby agi_shi » Tue May 04, 2010 12:45 pm

Okay that makes sense, thanks for looking into it :mrgreen:
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby JernejL » Tue May 04, 2010 6:07 pm

I added the new info to the wiki, hopefully readers there will realise that calculateAABB may not be what they need, and use supportvertex instead.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: NewtonCollisionCalculateAABB incorrect? (new)

Postby Julio Jerez » Tue May 04, 2010 6:43 pm

Delfi make sure you add the right function,
viewtopic.php?f=15&t=5163&p=36834#p36834
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron