Jump to content
  • Advertisement
Sign in to follow this  
215648

Collision problem

This topic is 2137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

struct AABB 
{
// Initialize to an infinitely small bounding box.
AABB()
: minPt(INFINITY, INFINITY, INFINITY),
 maxPt(-INFINITY, -INFINITY, -INFINITY){}
 
    D3DXVECTOR3 center()const
{
return (minPt+maxPt)*0.5f;
}
 
D3DXVECTOR3 extent()const
{
return (maxPt-minPt)*0.5f;
}
 
float GetWidth()const
{
return maxPt.x - minPt.x;
}
 
float GetHeight()const
{
return maxPt.y - minPt.y;
}
 
float GetDepth()const
{
return maxPt.z - minPt.z;
}
 
bool Intersects(const AABB& other) const
{
if (minPt.x > other.maxPt.x || other.minPt.x > maxPt.x
|| minPt.y > other.maxPt.y || other.minPt.y > maxPt.y
|| minPt.z > other.maxPt.z || other.minPt.z > maxPt.z)
return false;
 
return true;
}
 
 
D3DXVECTOR3 minPt;
D3DXVECTOR3 maxPt;
 
//==============================
other code
//==============================

if (mTrees[i].box.Intersects(Box.box) == true)
   {
  RECT R = {5, 40, 0, 0};
      d3d->Font->DrawText(0, "Collision Detected", -1, &R, DT_NOCLIP, D3DCOLOR_XRGB(0,0,0));
   }
 

};

Share this post


Link to post
Share on other sites
Advertisement

Are you a machine ? You've collided with human beings in a public forum and banged square up against their strange habits of communicating in words.

 

Hello, what is your code and model number. We can exchange code in your native language. What will we make ? Physics, excellent. Do we understand one another ?

Share this post


Link to post
Share on other sites

It doesn't works, it always returns true ("collision detected" is always drawn) even when they've (objects) not collided.

EDIT: Now it works, only the problem is that it stops working when I scale model.

 

bool Intersects(const AABB& Object2, const D3DXMATRIX& Object1WorldMatrix, const D3DXMATRIX& Object2WorldMatrix)
{
 
const D3DXVECTOR3 min = minPt;
const D3DXVECTOR3 max = maxPt;
 
const D3DXVECTOR3 otherMin = Object2.minPt;
const D3DXVECTOR3 otherMax = Object2.maxPt;
 
 
        D3DXVECTOR3 mesh1_cornersInModelSpace[8];
        mesh1_cornersInModelSpace[0] = D3DXVECTOR3( min.x, min.y, min.z ); 
        mesh1_cornersInModelSpace[1] = D3DXVECTOR3( max.x, max.y, max.z );
        mesh1_cornersInModelSpace[2] = D3DXVECTOR3( min.x, min.y, min.z ); 
        mesh1_cornersInModelSpace[3] = D3DXVECTOR3( max.x, max.y, max.z ); 
        mesh1_cornersInModelSpace[4] = D3DXVECTOR3( min.x, min.y, min.z ); 
        mesh1_cornersInModelSpace[5] = D3DXVECTOR3( max.x, max.y, max.z ); 
        mesh1_cornersInModelSpace[6] = D3DXVECTOR3( min.x, min.y, min.z ); 
        mesh1_cornersInModelSpace[7] = D3DXVECTOR3( max.x, max.y, max.z );  
 
 
       D3DXVECTOR3 mesh2_cornersInModelSpace[8];
 
       mesh2_cornersInModelSpace[0] = D3DXVECTOR3( otherMin.x, otherMin.y, otherMin.z );
       mesh2_cornersInModelSpace[1] = D3DXVECTOR3( otherMax.x, otherMax.y, otherMax.z );
       mesh2_cornersInModelSpace[2] = D3DXVECTOR3( otherMin.x, otherMin.y, otherMin.z );
       mesh2_cornersInModelSpace[3] = D3DXVECTOR3( otherMax.x, otherMax.y, otherMax.z );
       mesh2_cornersInModelSpace[4] = D3DXVECTOR3( otherMin.x, otherMin.y, otherMin.z );
       mesh2_cornersInModelSpace[5] = D3DXVECTOR3( otherMax.x, otherMax.y, otherMax.z );
       mesh2_cornersInModelSpace[6] = D3DXVECTOR3( otherMin.x, otherMin.y, otherMin.z );
       mesh2_cornersInModelSpace[7] = D3DXVECTOR3( otherMax.x, otherMax.y, otherMax.z ); 
 
 
       D3DXVECTOR3 mesh1_cornersInWorldSpace[8];
       
  for(int i = 0; i < 8; i++)
     D3DXVec3TransformCoord(&mesh1_cornersInWorldSpace[i], &mesh1_cornersInModelSpace[i], &Object1WorldMatrix);
 
       D3DXVECTOR3 mesh2_cornersInWorldSpace[8];
       
  for(int i = 0; i < 8; i++)
     D3DXVec3TransformCoord(&mesh2_cornersInWorldSpace[i], &mesh2_cornersInModelSpace[i], &Object2WorldMatrix);
 
 
       D3DXVECTOR3 minBoundsWorldSpace = mesh1_cornersInWorldSpace[0];
       D3DXVECTOR3 maxBoundsWorldSpace = mesh1_cornersInWorldSpace[0];
        
  for (int i = 1; i < 8; i++)
       {
           minBoundsWorldSpace.x = min(minBoundsWorldSpace.x,mesh1_cornersInWorldSpace[i].x);
           minBoundsWorldSpace.y = min(minBoundsWorldSpace.y,mesh1_cornersInWorldSpace[i].y);
           minBoundsWorldSpace.z = min(minBoundsWorldSpace.z,mesh1_cornersInWorldSpace[i].z);
 
           maxBoundsWorldSpace.x = max(maxBoundsWorldSpace.x,mesh1_cornersInWorldSpace[i].x);
           maxBoundsWorldSpace.y = max(maxBoundsWorldSpace.y,mesh1_cornersInWorldSpace[i].y);
           maxBoundsWorldSpace.z = max(maxBoundsWorldSpace.z,mesh1_cornersInWorldSpace[i].z);
       }
 
       D3DXVECTOR3 minBoundsWorldSpace2=mesh2_cornersInWorldSpace[0];
       D3DXVECTOR3 maxBoundsWorldSpace2=mesh2_cornersInWorldSpace[0];
 
  for (int i = 1; i < 8; i++)
       {
          minBoundsWorldSpace2.x = min(minBoundsWorldSpace2.x,mesh2_cornersInWorldSpace[i].x);
          minBoundsWorldSpace2.y = min(minBoundsWorldSpace2.y,mesh2_cornersInWorldSpace[i].y);
          minBoundsWorldSpace2.z = min(minBoundsWorldSpace2.z,mesh2_cornersInWorldSpace[i].z);
 
          maxBoundsWorldSpace2.x = max(maxBoundsWorldSpace2.x,mesh2_cornersInWorldSpace[i].x);
          maxBoundsWorldSpace2.y = max(maxBoundsWorldSpace2.y,mesh2_cornersInWorldSpace[i].y);
          maxBoundsWorldSpace2.z = max(maxBoundsWorldSpace2.z,mesh2_cornersInWorldSpace[i].z);
       }
 
       if ((minBoundsWorldSpace.x < maxBoundsWorldSpace2.x) && (maxBoundsWorldSpace.x > minBoundsWorldSpace2.x) &&
             (minBoundsWorldSpace.y < maxBoundsWorldSpace2.y) && (maxBoundsWorldSpace.y > minBoundsWorldSpace2.y) &&
              (minBoundsWorldSpace.z < maxBoundsWorldSpace2.z) && (maxBoundsWorldSpace.z > minBoundsWorldSpace2.z))
 return true;
  else
     return false;
           
}
Edited by newtechnology

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!