🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

collision between cube and pyramid with in the window

Started by
1 comment, last by GameDev.net 17 years, 6 months ago
hi everybody, i had created a cube and a pyramid in a single windowwhich can bemoved and rotated by using mouse...... now my task is to make this pyramid and cube to colloid each other when they are dragged and hitted over each other......... if anyone can help me on this pls post ur reply...... thanks in advance...... urs,,, deepu........
Advertisement
You could always do a triangle to triangle collision test.
Assuming you know abit about vectors and how to subtract them and what a dot product and cross product actually does I have some example code for you to look at.
Now I know it's not the most beatiful piece since I'm at work, but it should give you pretty much the idea of how you could do the intersection test.
Collision detection is a pretty big field and this is more or less a fleshed out example of a tri intersection function. It can be quite expensive so it might be a good idea to do a bound sphere radius intersection test of your objects before doing a more detailed examination.
Searching google for "collision" or "collision detection", "bsp collision", "sphere sphere collision" will yield tons and tons of links to good papers on the actual math involved and how to set up a good collision function.
I belive GameDev has quite a few articles on the subject also if you check out there resource collection.


// Triangle
struct tri
{
vec3 v0; // vertex 0
vec3 v1; // vertex 1
vec3 v2; // vertex 2
};

// Return min of the three values
float min3(float _val0, float _val1, float _val2)
{
return min(min(_val0, _val1), _val2);
}

// Return max of the three values
float max3(float _val0, float _val1, float _val2)
{
return max(max(_val0, _val1), _val2);
}

// Projects a triangle onto a vector and returns min, max result
void Project(tri _t, vec3 _V, float& _min, float& _max)
{
float dot0 = _v.Dot(_t.v0);
float dot1 = _v.Dot(_t.v1);
float dot2 = _v.Dot(_t.v2);
_min = min3(dot0, dot1, dot2);
_max = max3(dot0, dot1, dot2);
}

bool TestTri2Tri(tri _t0, tri _t1)
{
float dMin0, dMax0;
float dMin1, dMax1;

// Calculate tri0's edge vectors
vec3 e0[3] = {
_t0.v1.Subtract(_t0.v0),
_t0.v2.Subtract(_t0.v1),
_t0.v0.Subtract(_t0.v2),
};

// Calculate tri1's edge vectors
vec3 e1[3] = {
_t1.v1.Subtract(_t1.v0),
_t1.v2.Subtract(_t1.v1),
_t1.v0.Subtract(_t1.v2),
};

// Find normal vector for triangle 0 and 1
vec3 n0 = e0[0].CrossProduct(e0[1]);
vec3 n1 = e1[0].CrossProduct(e1[1]);

// Normalize
n0.Normalize();
n1.Normalize();

// Project triangle 1 onto normal of triangle 0 and test for separation
float dn0t0v0 = n0.Dot(_t0.v0);
Project(_t1, n0, dMin1, dMax1);
if (dn0t0v0 dmax)
return false;

// Check if triangles are parallel or not
vec3 n0xn1 = n0.CrossProduct(n1);
n0xn1.Normalize();
if (n0xn1.Dot(n0xn1) >= 0.0f)
{
// Triangles are NOT parallel

// Project triangle 0 onto normal vector of triangle 1 and
// test for separation
float dn1t1v0 = n1.Dot(_t1.v0);
Project(_t0, n1, dMin0, dMax0);
if (dn1t1v0 dMax)
return false;

// directions e0xe1[j]
for (int i = 0; i
// Trianglestruct tri{	vec3 v0;	// vertex 0	vec3 v1;	// vertex 1	vec3 v2;	// vertex 2};// Return min of the three valuesfloat min3(float _val0, float _val1, float _val2){	return min(min(_val0, _val1), _val2);}// Return max of the three valuesfloat max3(float _val0, float _val1, float _val2){	return max(max(_val0, _val1), _val2);}// Projects a triangle onto a vector and returns min, max resultvoid Project(tri _t, vec3 _V, float& _min, float& _max){	float dot0 = _v.Dot(_t.v0);	float dot1 = _v.Dot(_t.v1);	float dot2 = _v.Dot(_t.v2);	_min = min3(dot0, dot1, dot2);	_max = max3(dot0, dot1, dot2);}bool TestTri2Tri(tri _t0, tri _t1){	float dMin0, dMax0;	float dMin1, dMax1;	// Calculate tri0's edge vectors	vec3 e0[3] = {		_t0.v1.Subtract(_t0.v0),		_t0.v2.Subtract(_t0.v1),		_t0.v0.Subtract(_t0.v2),	};		// Calculate tri1's edge vectors	vec3 e1[3] = {		_t1.v1.Subtract(_t1.v0),		_t1.v2.Subtract(_t1.v1),		_t1.v0.Subtract(_t1.v2),	};		// Find normal vector for triangle 0 and 1	vec3 n0 = e0[0].CrossProduct(e0[1]);	vec3 n1 = e1[0].CrossProduct(e1[1]);		// Normalize	n0.Normalize();	n1.Normalize();		// Project triangle 1 onto normal of triangle 0 and test for separation	float dn0t0v0 = n0.Dot(_t0.v0);	Project(_t1, n0, dMin1, dMax1);	if (dn0t0v0 < dmin || dn0t0v0 > dmax)		return false;			// Check if triangles are parallel or not	vec3 n0xn1 = n0.CrossProduct(n1);	n0xn1.Normalize();	if (n0xn1.Dot(n0xn1) >= 0.0f)	{		// Triangles are NOT parallel				// Project triangle 0 onto normal vector of triangle 1 and		// test for separation		float dn1t1v0 = n1.Dot(_t1.v0);		Project(_t0, n1, dMin0, dMax0);		if (dn1t1v0 < dMin || dn1t1v0 > dMax)			return false;					// directions e0xe1[j]		for (int i = 0; i < 3; i++)		{			for (int j = 0; j < 3; j++)			{				vec3 dir = e0.CrossProduct(e1[j]);				dir.Normalize();				Project(_t0, dir, dMin0, dMax0);				Project(_t1, dir, dMin1, dMax1);				if (dMax0 < dMin1 || dMax1 < dMin0)					return false;			}		}	}	else	{		// Triangles are parallel		// directions n0xe0		for (int i = 0; i < 3; i++)		{			vec3 dir = n0.CrossProduct(e0);			dir.Normalize();			Project(_t0, dir, dMin0, dMax0);			Project(_t1, dor, dMin1, dMax1);			if (dMax0 < dMin1 || dMax1 < dMin0)				return false;		}				// directions n1xe1		for (int i = 0; i < 3; i++)		{			vec3 dir = n1.CrossProduct(e1);			Project(_t0, dir, dMin0, dMax0);			Project(_t1, dir, dMin1, dMax1);			if (dMax0 < dMin1 || dMax1 < dMin0)				return false;		}	}		// triangles are colliding	return true;}

This topic is closed to new replies.

Advertisement