Jump to content
  • Advertisement
Sign in to follow this  
Winegums

Rotating and bouncing off of planes

This topic is 3825 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, I was sure I'd made this thread earlier but maybe not. I'm trying to make a plane in OpenGL and I want to collide things with it. I think the collision maths is correct, but the rendering of the plane could be wrong.
bool SpherePlaneCollision(const SpherePhysicsClass* sphere, 
						  const PlanePhysicsClass* plane)
{
	//get distance of plane from origin
	float d = fabs(plane->mPos.x) + fabs(plane->mPos.y) + fabs(plane->mPos.z);
	
	float distToPlane = Dot(sphere->mPos, plane->mNormal) + d;

	if(fabs(distToPlane) < sphere->mRadius)
	{	
		return true;
	}


	return false;
}

Like I said, I think the problem is in how i draw the plane. I store the plane as a point, a normal, and extents horzontally/vertically. I think this should be sufficient data to represent the plane? void PlanePhysicsClass::Render() { float xRot = RADIAN_TO_DEGREE * Dot(this->mNormal, Vector3(1.0f,0.0f,0.0f)); float yRot = RADIAN_TO_DEGREE * Dot(this->mNormal, Vector3(0.0f,1.0f,0.0f)); float zRot = RADIAN_TO_DEGREE * Dot(this->mNormal, Vector3(0.0f,0.0f,1.0f)); Vector3 p1, p2, p3, p4; p1.x = this->mPos.x - this->mExtents.x; p1.z = this->mPos.z - this->mExtents.y; p1.y = 0.0f; p2.x = this->mPos.x - this->mExtents.x; p2.z = this->mPos.z + this->mExtents.y; p2.y = 0.0f; p3.x = this->mPos.x + this->mExtents.x; p3.z = this->mPos.z + this->mExtents.y; p3.y = 0.0f; p4.x = this->mPos.x + this->mExtents.x; p4.z = this->mPos.z - this->mExtents.y; p4.y = 0.0f; glPushMatrix(); glBindTexture(GL_TEXTURE_2D, NULL); glRotatef(xRot, 1.0f,0.0f,0.0f); glBegin(GL_QUADS); // Start Drawing Quads glTexCoord2f(1.0f, 0.0f); glVertex3f(p1.x, p1.y, p1.z); // Point 1 (Back) glTexCoord2f(1.0f, 1.0f); glVertex3f(p2.x, p2.y, p2.z); // Point 2 (Back) glTexCoord2f(0.0f, 1.0f); glVertex3f(p3.x, p3.y, p3.z); // Point 3 (Back) glTexCoord2f(0.0f, 0.0f); glVertex3f(p4.x, p4.y, p4.z); // Point 4 (Back) glEnd(); glPopMatrix(); } I get the feeling that the rotations are wrong (infact I'm almost certain, since for a normal of (1,0,0) i get a rotation of ~57 degrees about the x-axis).

Share this post


Link to post
Share on other sites
Advertisement
Quote:
float xRot = RADIAN_TO_DEGREE * Dot(this->mNormal, Vector3(1.0f,0.0f,0.0f));

The dot product of two units vectors is the cosine of the angle between them.

You're getting xRot = 57 degrees with mNormal = ( 1,0,0 ) because the dot product of the two vectors = 1. 1 radian = 57 degrees.

Try:

float xRot = RADIAN_TO_DEGREE*acos(Dot());

Share this post


Link to post
Share on other sites
Hi, I tried what you posted but it gave me strange results. However doing 90 - RADIAN_TO... seemed to work for the y-axis, but not for the x and z.

A normal of say (0.3f,1.0f,0.0f) visually rotates the plane about the x axis, but bounces the object off as though the plane was rotated about the z-axis.

A normal of (0.0f,1.0f,0.3f) visually rotates the object about the x axis, and bounces it off in what appears to be the correct manner.



float xRot = 90.0f - RADIAN_TO_DEGREE * acos(Dot(this->mNormal, Vector3(1.0f,0.0f,0.0f)));
float yRot = 90.0f - RADIAN_TO_DEGREE * acos(Dot(this->mNormal, Vector3(0.0f,1.0f,0.0f)));
float zRot = 90.0f - RADIAN_TO_DEGREE * acos(Dot(this->mNormal, Vector3(0.0f,0.0f,1.0f)));

Vector3 p1, p2, p3, p4;

p1.x = this->mPos.x - this->mExtents.x;
p1.z = this->mPos.z - this->mExtents.y;
p1.y = 0.0f;

p2.x = this->mPos.x - this->mExtents.x;
p2.z = this->mPos.z + this->mExtents.y;
p2.y = 0.0f;

p3.x = this->mPos.x + this->mExtents.x;
p3.z = this->mPos.z + this->mExtents.y;
p3.y = 0.0f;

p4.x = this->mPos.x + this->mExtents.x;
p4.z = this->mPos.z - this->mExtents.y;
p4.y = 0.0f;

glPushMatrix();
glBindTexture(GL_TEXTURE_2D, NULL);
glTranslatef(this->mPos.x, this->mPos.y, this->mPos.z);
glRotatef(xRot, 1.0f,0.0f,0.0f);
glRotatef(yRot, 0.0f,1.0f,0.0f);
glRotatef(zRot, 0.0f,0.0f,1.0f);
glBegin(GL_QUADS); // Start Drawing Quads
glTexCoord2f(1.0f, 0.0f); glVertex3f(p1.x, p1.y, p1.z); // Point 1 (Back)
glTexCoord2f(1.0f, 1.0f); glVertex3f(p2.x, p2.y, p2.z); // Point 2 (Back)
glTexCoord2f(0.0f, 1.0f); glVertex3f(p3.x, p3.y, p3.z); // Point 3 (Back)
glTexCoord2f(0.0f, 0.0f); glVertex3f(p4.x, p4.y, p4.z); // Point 4 (Back)
glEnd();
glPopMatrix();

Share this post


Link to post
Share on other sites
First: I apologize as I'm afraid I'm very short on time tonight and can't give you all the help you've asked for with regard to rendering the plane and your collision equations. I'm thinking it's better for me to give you a couple comments than not reply at all.

So:

You've thrown in a couple complications after your original post.

From what I can determine from your code, you're trying to orient your plane according to it's normal vector. Correct?

If so:

1. Your setup of p1, p2, p3 and p4 determines where the extent coordinates are for the "untilted" plane with respect to rotation about the normal. If you want the plane to be rotated about normal, that will have to be a separate parameter for your plane. In your posted code, by setting all y values of those coordinates to 0, you've already determined the rotation about the y-axis (which is fine).

2. If mNormal is the normal vector for the plane and you want to use dot products to determine angles, mNormal has to be normalized, i.e., has to be of unit length. The dot product of 2 vectors A and B = |A|*|B|*cos(angle between them). |A| and |B| mean the length of the vectors. If one of the vectors is not unit length, then the dot product is not the cos() alone.

3. I'm not sure I can make this clear in a short time, but the angles for rotating about the x and z axis to orient the plane are not the dot product of the normal with those axes. I'm running short on time here so I can't explain fully. I'll try to get back tomorrow.

4. The distance of the plane from the origin is the square root of the squares of the coordinates sqrt(dx*dx+dy*dy+dz*dz).

I haven't looked into your distToPlane forumula.

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
4. The distance of the plane from the origin is the square root of the squares of the coordinates sqrt(dx*dx+dy*dy+dz*dz).


I do not believe this is correct. Consider a normalized plane normal, your distance will always return one.

I believe the easiest way of finding Distance from a point to a plane is to dot that point with the normalized plane normal, then add D. Therefore, to check distance to the origin all you need to do is look at the absolute value of your D variable to get the distance to the origin (since the dot product with the origin will always result in zero). Note: this only works if your plane normal is normalized in your plane equation. Otherwise look at this website.

I have not taken a close look at anything else posted but I will do so when I will attempt to do so when I get home.

Good luck!

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!