#### Archived

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

# making an object stop moving-collision detection

This topic is 6014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
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);
//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 on other sites
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 on other sites
right now I have....
if ((x-10.0f, 2.0f),( x-10.0, 0.0f) == (mapx,-1.0f),(mapx,13.0f) )
{ x-=.4;
}
else
{
x+=.2f;
}

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002137
×