0
votes

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();

1
How are you drawing the sphere each frame if you do not know its position? or are you asking about the position after the "rebound"? - Preet Kukreti
I just saw your other question. It seems like you have copy pasted a working example, and are tinkering with it? I think you need to read up about the relationship between local coord space, modelview/view matrix (+ relative nesting with push/pop), and perspective/projection matrix before asking any more questions. - Preet Kukreti
I draw a sphere in the edge of a "arm" that I can move, and the sphere moves together (rotate and translate). I did the hierarchical structure of matrix to complete the moves (works perfect). But I want to restrict the ball (edge of the arm) to cross the plane. - João Paulo Moro Loureiro
@genpfault are you a grammatical reviewer? Could you help? No. So don't came to delete my polite "Thanks.!" at the end of my question. - João Paulo Moro Loureiro
You should do the matrix calculations yourself, and then you can study them to see the ball's location. You can do the same matrix calculations or just position it directly when you later want to draw it. See glprogramming.com/red/appendixf.html and glprogramming.com/red/chapter03.html . - Patashu

1 Answers

4
votes

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.