Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Collision between particles (Planes) and Box

This topic is 2196 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

@DonTzzy: This is 2D OOB/Circle collision detection

 

I'm looking for 3D AABB/Sphere collision detection sample.

Share this post


Link to post
Share on other sites
Advertisement

Currently, I would go for detecting collision between spheres (particles) and boxes (buildings)

 

Any idea how to detect collision between AABB and Sphere?

Edited by Medo3337

Share this post


Link to post
Share on other sites

blink.png

 

I have been trying to do it myself, I made AABB vs AABB, Sphere vs Sphere, but couldn't get AABB vs Sphere to work correctly.

 

So I'm looking for code sample.

 

@imoogiBG: I have multiple rain drops per particles, so it need soft particles or the particles plane would appear intersecting the terrain/ground.

Edited by Medo3337

Share this post


Link to post
Share on other sites

http://stackoverflow.com/questions/4578967/cube-sphere-intersection-test

 

also if you want to figure it out yourself and write yourself a better solution use:

Canonical (im not really shure that is the right word in english) equation of a plane : Ax + By+ Cz + D = 0, and point projection on a plane.

 

Take a look at some linear algebra and analytic geometry articles you will certenly need them in the near future.

Share this post


Link to post
Share on other sites

@imoogiBG: I got this solution when I used Google, unfortunately it doesn't work

 

Here is what I'm doing:

bool doesCubeIntersectSphere(D3DXVECTOR3 C1, D3DXVECTOR3 C2, D3DXVECTOR3 S, float R) 
{     
    float dist_squared = R * R;     

    /* assume C1 and C2 are element-wise sorted, if not, do that now */     

if (S.x < C1.x) dist_squared -= squared(S.x - C1.x);     
    else if (S.x > C2.x) dist_squared -= squared(S.x - C2.x);     

    if (S.y < C1.y) dist_squared -= squared(S.y - C1.y);     
    else if (S.y > C2.y) dist_squared -= squared(S.y - C2.y);     

    if (S.z < C1.z) dist_squared -= squared(S.z - C1.z);     
    else if (S.z > C2.z) dist_squared -= squared(S.z - C2.z);     

    return dist_squared > 0;
}

bool TestBoxToSphereCollision(AABB box, D3DXMATRIX boxWorldMatrix, Sphere sphere, D3DXVECTOR3 spherePosition)
{
// We have min and max values, use these to get the 8 corners of the bounding box
D3DXVECTOR3 cornersInModelSpace[8];
cornersInModelSpace[0] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z ); // xyz
cornersInModelSpace[1] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // Xyz
cornersInModelSpace[2] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z ); // xYz
cornersInModelSpace[3] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // XYz
cornersInModelSpace[4] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z ); // xyZ
cornersInModelSpace[5] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // XyZ
cornersInModelSpace[6] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z ); // xYZ
cornersInModelSpace[7] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // XYZ 

// Now we transform each corner by the world matrix
D3DXVECTOR3 cornersInWorldSpace[8];
for( int i = 0; i < 8; i++ )
D3DXVec3TransformCoord( &cornersInWorldSpace[i], &cornersInModelSpace[i], &boxWorldMatrix );

D3DXVECTOR3 minBoundsWorldSpace = cornersInWorldSpace[0];
D3DXVECTOR3 maxBoundsWorldSpace = cornersInWorldSpace[0];
for (int i=1;i<8;i++)
{
minBoundsWorldSpace.x = min(minBoundsWorldSpace.x, cornersInWorldSpace[i].x);
minBoundsWorldSpace.y = min(minBoundsWorldSpace.y, cornersInWorldSpace[i].y);
minBoundsWorldSpace.z = min(minBoundsWorldSpace.z, cornersInWorldSpace[i].z);


maxBoundsWorldSpace.x = max(maxBoundsWorldSpace.x, cornersInModelSpace[i].x);
maxBoundsWorldSpace.y = max(maxBoundsWorldSpace.y, cornersInModelSpace[i].y);
maxBoundsWorldSpace.z = max(maxBoundsWorldSpace.z, cornersInModelSpace[i].z);
}

return doesCubeIntersectSphere(minBoundsWorldSpace, maxBoundsWorldSpace, spherePosition, sphere.radius);
}

Let me know what's wrong in the code above.

Share this post


Link to post
Share on other sites

your algorithm is wrong here, those lines are exactly the same:

 

cornersInModelSpace[5] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // XyZ

cornersInModelSpace[7] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // XYZ 

cornersInModelSpace[3] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // XYz

cornersInModelSpace[1] = D3DXVECTOR3( box.Max.x, box.Max.y, box.Max.z ); // Xyz

 

and also :

 

cornersInModelSpace[0] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z );

cornersInModelSpace[2] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z );

cornersInModelSpace[4] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z );

cornersInModelSpace[6] = D3DXVECTOR3( box.Min.x, box.Min.y, box.Min.z );

 

 

Im not going to teach you how to code, but i really think that I must say those things to you: I see that you have great passion but your C++ is not quite good.

 

See D3DXVECTOR3, D3DXMATRIX, AABB ... those structures are really big(in bytes) and in

bool TestBoxToSphereCollision(AABB box, D3DXMATRIX boxWorldMatrix, Sphere sphere, D3DXVECTOR3 spherePosition)

 

you're passign them by value. The same syntax is absolutely OK with C#/Java/ect languages, but in C++ this is a different thing. Use references and/or pointers. Try something like this:

 

 

 

bool TestBoxToSphereCollision(AABB& box, D3DXMATRIX& boxWorldMatrix, Sphere& sphere, D3DXVECTOR3& spherePosition)

 

 

 

also you're missign CONSTs A-LOT. const are something like the greatest hit for the compiler. If you use const your code will be faster I promise! Also const methods/functions will help you with debugging and fixing mistakes in code semantics. so your fuction declaration will probably look something like this:

 

bool TestBoxToSphereCollision(const AABB& box, const D3DXMATRIX& boxWorldMatrix, const Sphere& sphere, const D3DXVECTOR3& spherePosition)

 

 

 

 

Excuse me if you're already familiar with those things.

Edited by imoogiBG

Share this post


Link to post
Share on other sites

@imoogiBG: I just copied and pasted the code from a very old project that I made when I started in C++.

 

I was trying to get it work then I can rewrite the code, trust me this is not the final code for usage purposes. I figured out I had a problem with calculating the bounding box min and max.

 

I got it to work! the only problem now is that I don't see rain in the area around the building, while the rain should appear around it.

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!