NewtonCollisionSupportVertex

From Newton Wiki
Jump to: navigation, search

NewtonCollisionSupportVertex

void NewtonCollisionSupportVertex (const NewtonCollision* collision, const dFloat* dir, dFloat* vertex);

Usage

NewtonCollisionSupportVertex returns a point, that maps to the maximum projection of all the points of the collision shape along the specified direction.

Parameters

  • const NewtonCollision* collision - newtoncollision object
  • const dFloat* dir - the direction along which newton should calculate maximum length

Return

  • dFloat* vertex - returns a vertex, which is at maximum length point in local space of the specified newton collision.

Description

This function is very efficiently implemented in newton, and is used to calculate the collision object maximum extents along any direction. It can be used to calculate precise AABB and OOBB bounding boxes, or determine size of any object against another object for manual collision detection & response.

In the following diagram the function will return the coordinates of point #2, because it is the farthest of all points in this shape along arrow direction.

SupportVertex.jpg

ExampleS

This function, which you can find in the SDK demos in file PhyscUtis.cpp, calculates the AABB for any NewtonBody. It differs from NewtonCollisionCalculateAABB in that it returns a precise and tight object AABB while NewtonCollisionCalculateAABB returns a collision-optimal AABB.

static void CalculateAABB (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]; 
   }
}

This example calculates a local AABB for a newton collision, it is not transformed to global space, this will simply return the size of the collision body shape around it's centre.

void CalculteLocalAABB (const dMatrix& matrix, NewtonCollision *collision, dVector& p0, dVector& p1)
{
   for (int i = 0; i < 3; i ++) {
      dVector dir (0.0f, 0.0f, 0.0f, 0.0f);

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

      dir = dir.Scale (-1.0f);
      NewtonCollisionSupportVertex (collision, &dir[0], &p0[0]);
   }
}

Remarks

Added in Newton 2.0

See also

NewtonCollisionCalculateAABB