Jump to content
  • Advertisement
Sign in to follow this  

getting angle of impact with my collision detection code.

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

Hi folks, I have this problem, I have a function which checks if a triangle intersects with another triangle:
bool CheckForIntersection(ScePspFVector3 v1, ScePspFVector3 v2, ScePspFVector3 v3, ScePspFVector3 u1, ScePspFVector3 u2, ScePspFVector3 u3, ScePspFVector3& dir1, ScePspFVector3& dir2) {

	ScePspFVector3 planePartEquation1, planePartEquation2, Normal1, Normal2, directionVec;
	// creating the plane equation.
	planePartEquation1 = Vector3D::Substract(v2, v1);
	planePartEquation2 = Vector3D::Substract(v3, v1);
	Normal1 = Vector3D::CrossProduct(planePartEquation1, planePartEquation2);
	float distance1 = -(Vector3D::DotProduct(Normal1, v1));

	dir2 = Normal1;

	//checking u's against the plane equation.
	float distanceU1 = Vector3D::DotProduct(Normal1, u1) + distance1;
	float distanceU2 = Vector3D::DotProduct(Normal1, u2) + distance1;
	float distanceU3 = Vector3D::DotProduct(Normal1, u3) + distance1;

	//check to see if they are on 1 side.
	float u12 = distanceU1 * distanceU2;
	float u13 = distanceU1 * distanceU3;
	if(u12>0.0f && u13 > 0.0f)
		return false;					// no intersection

	// creating the plane equation.
	planePartEquation1 = Vector3D::Substract(u2, u1);
	planePartEquation2 = Vector3D::Substract(u3, u1);
	Normal2 = Vector3D::CrossProduct(planePartEquation1, planePartEquation2);
	float distance2 = -(Vector3D::DotProduct(Normal2, u1));

	dir1 = Normal2;

	//checking u's against the plane equation.
	float distanceV1 = Vector3D::DotProduct(Normal2, v1) + distance2;
	float distanceV2 = Vector3D::DotProduct(Normal2, v2) + distance2;
	float distanceV3 = Vector3D::DotProduct(Normal2, v3) + distance2;

	//check to see if they are on 1 side.
	float v12 = distanceV1 * distanceV2;
	float v13 = distanceV1 * distanceV3;
	if(v12>0.0f && v13 > 0.0f)
		return false;					// no intersection

	// compute the direction of intersection.
	directionVec = Vector3D::CrossProduct(Normal1, Normal2);

	// compute the largest component of the vector.
	float max = fabs(directionVec.x);
	int index = 0;
	float bb = fabs(directionVec.y);
	float cc = fabs(directionVec.z);
	if(bb>max) { max = bb; index = 1; }
	if(cc>max) { max = cc; index = 2; }

	// simplified projection onto L

	float vp1 = 0.0f, vp2 = 0.0f, vp3 = 0.0f, up1 = 0.0f, up2 = 0.0f, up3 = 0.0f;

	if(index==1) {
		vp1 = v1.x; vp2 = v2.x; vp3 = v3.x;
		up1 = u1.x; up2 = u2.x; up3 = v3.x;
	else if(index==2) {
		vp1 = v1.y; vp2 = v2.y; vp3 = v3.y;
		up1 = u1.y; up2 = u2.y; up3 = v3.y;
	else if(index==3) {
		vp1 = v1.z; vp2 = v2.z; vp3 = v3.z;
		up1 = u1.z; up2 = u2.z; up3 = v3.z;
	float a, b, c, d, e, f, x1, x2, y1, y2;

	// compute interval for triangle 1 and 2
	if(!Vector3D::ComputeInterval(vp1, vp2, vp3, distanceV1, distanceV2, distanceV3, v12, v13, a, b, c, x1, x2)) return false;
	if(!Vector3D::ComputeInterval(up1, up2, up3, distanceU1, distanceU2, distanceU3, u12, u13, d, e, f, y1, y2)) return false;

	float xx, yy, xxyy, tmp;
	ScePspFVector2 isect1, isect2;

	xx = x1 * x2;
	yy = y1 * y2;
	xxyy = xx * yy;

	tmp = a * xxyy;


	tmp = d * xxyy;

	if(isect1.x > isect1.y){
		tmp = isect1.x;
		isect1.x = isect1.y;
		isect1.y = tmp;
	if(isect2.x > isect2.y){
		tmp = isect2.x;
		isect2.x = isect2.y;
		isect2.y = tmp;

	if(isect1.y<isect2.x || isect2.y<isect1.x) return false;

	return true;
I loop with two meshes between the collision triangles to check If there is a hit. Now the problem is I thought I should calculate the angle like this:
float angle = atan2((-normal.z * direction.x + normal.x * direction.z), (normal.x * direction.x + normal.z * direction.z));
but I get strange behaviour of this. I have this track with almost axis aligned walls. The angles however are always the same. Whatever triangle from my car hits whatever triangle from the wall. it does not matter whether I crash into the wall full frontal or just slightly. Can anyone tell me what I am doing wrong and how I can retrieve the correct angle ? (or is my collision detection system not correct ??) greets ghoti

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!