Reputation: 19
Suppose I have a sphere and a plane drawn in XY and I can move the ball. I want to know if it hits the plane. My thought is:
-Get the sphere position (center)
-Compare the (sphere position (Z coordinate) + radius) with the coordinate Z = 0
if true, means that de sphere hit the plane.
But how get the sphere position? I can use the transformation matrix? Like:
GLfloat matrix[4][4];
glGetFloatv(GL_MODELVIEW_MATRIX, &matrix[0][0]);
The code to draw the sphere is:
glPushMatrix();
glTranslatef(1.0, altura_braco, 0.0);
glScalef(1.0, 1.0, 1.0);
glColor3f(0.0f, 1.0f, 1.0f);
glutSolidSphere(0.2, 100.0, 100.0);
glPopMatrix();
Upvotes: 0
Views: 1056
Reputation: 64283
I think you misunderstood what the opengl is for. It's purpose is only to render things.
Having said that, it doesn't support collision detection. That you have to implement your self, or use a game engine.
if true, means that de sphere hit the plane. But how get the sphere position?
You have both sphere and plane equations, and use them. If you detect intersection, then the collide. This answer explains how to detect whether an object intersect the sphere.
The equation for a plane is :
a*x + b*y + c*z = d
and the equation for the sphere is :
(x-x0)^2 + (y-y0)^2 + (z-z0)^2 = r^2
You can check whether they intersect by solving this set of equations.
Upvotes: 4