Archived

This topic is now archived and is closed to further replies.

Aznbro

making an object stop moving-collision detection

Recommended Posts

Aznbro    122
making an object stop moving-collision detection if I have a cube moving through space... glTranslatef(-5.0f,y,-100.0f); glBegin(GL_QUADS); //the cube glColor3f(1.0, 0.0, 0.0); glNormal3f( 0.0f, 0.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( -15.0f, 2.0f,1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( -17.0f, 2.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( -17.0f, 0.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( -15.0f, 0.0f, 1.0f); glEnd(); glTranslatef(5.0f,-y,100.0f); ...how can I have it stop moving at the x axis(53)???

Share this post


Link to post
Share on other sites
Fruny    1658
First thing, look up glPushMatrix and glPopMatrix, that will save you some grief on the translate(y)/translate(-y) thingy.

Second, you are moving along the y axis, what is the issue with the x axis ?

Third assuming you wanted to stop at y==53, you already have the variable to check. The actual program''s logic is up to you.

Share this post


Link to post
Share on other sites
Aznbro    122
Well, the cube moves along the y axis, because the user can only move it up and down. secondly, I have a background map that moves along the x axis towards the left of the screen...I don''t know how to make a difference between the 2 to say that if one edge of the cube hits this certain line segment on the y axis, then it stops.

Also, currently my cube just goes straight through the obstacles.

1.How do I diffrientiate between the background with all the obstacles and the one cube you control?

2.How do I make the cube stop when it hit these obstacles?

3.Can you cout in openGL?

This game is suppose to be like the game falldown...

Share this post


Link to post
Share on other sites
mkaltner    122
It's pretty easy.
What you want is a global variable to keep track of the cubes position. You already have one but lets say its:

  
float m_fCubePositionX = 0.0f;


and initialize it to 0.
Next, when moving the cube, you'd do something like this:

  
if (m_fCubePositionX < 53.0f)
{
m_fCubePositionX += 1.0f;
}

// render cube using m_fCubePositionX as it's x axis here



This would render the cube each frame, updating the x position until it reached 53.

- Mike

EDIT: Typo....

Edited by - mkaltner on January 29, 2002 12:31:15 PM

Share this post


Link to post
Share on other sites
Aznbro    122
Thanks Mike...I''m new to openGl and C++ kind of, but can anyone please tell me how you would I guess basically label the entire cube...for example, you labeled it m_fCubePositionX. Also, in this statement :
if (m_fCubePositionX < 53.0f)
{
m_fCubePositionX += 1.0f;}
...how would it know know that 53.0f is suppose to mean an x axis(what I want... x=53 would have a vertical line moving up and down)

Because I want to make it kind of look like a 2d cube crashing into a wall. 53, would only be one point of that line segment, so how are you suppose to right that out???

Share this post


Link to post
Share on other sites
Aznbro    122
This is my wall....

glColor3f(1.0, 1.0, 1.0);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-11.0f, 13.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-13.0f, 13.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-13.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-11.0f, -1.0f, 1.0f);

This is my cube....

glTranslatef(-5.0f,y,-100.0f);
glBegin(GL_QUADS);
//the cube
glColor3f(1.0, 0.0, 0.0);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -15.0f, 2.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( -17.0f, 2.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -17.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -15.0f, 0.0f, 1.0f); glEnd();
glTranslatef(5.0f,-y,100.0f);

Share this post


Link to post
Share on other sites
Aznbro    122
note: my square is controlled by the user, you can move it up and down. The square also is constantly moving forward very slowly.(that''s all)
My wall is straight up and moving left at a constant speed. I want to make it so that you can dodge the block(wall) by moving up and down. And make it so that if the square hit the wall, the wall drags it to the left side of the screen until the use moves the square down or up out of the way of the walls path.

Share this post


Link to post
Share on other sites