Skip to main content
gamedev.net
🔒 Locked

Objects Not Moving Together

Started by Ructions Jan 16, 2004 at 3:41 PM 2 replies 550+ views
Original Post
Ructions
Ructions
i am making am arkanoid game and at the start i want my ball and paddle to move together across the screen together. so i set the x coordinates to equal eachother. ball.x = paddle.x; here is the code that draws them glLoadIdentity(); glTranslatef(0.0f,-6.5f,-20.0f); glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex3f(PaddleWidth + paddle.x , 0.0f + paddle.y, 2.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.0f + paddle.x , 0.0f + paddle.y, 2.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.0f + paddle.x , PaddleHeight + paddle.y, 2.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(PaddleWidth + paddle.x ,PaddleHeight + paddle.y,2.0f); glEnd(); // Done Drawing The paddle glBindTexture(GL_TEXTURE_2D, texture[1]); glTranslatef(0.0,-6.5f,-20.0f); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.7f + ball.x , 0.7f + ball.y , 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f + ball.x , 0.7f + ball.y, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f + ball.x , -0.7f + ball.y, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.7f + ball.x , -0.7f + ball.y, 0.0f); glEnd(); // Done Drawing The ball but them i move them across the screen the ball never stays in the same place as the paddle it stays behind it. i have printed the x ordinates to the screen and they are the same so i dont know thats wrong with it.. any help. newbie to opengl.
Tree Penguin
Tree Penguin
Remove the 2nd call to glTranslatef, it makes everything move another (0.0,-6.5f,-20.0f) units so the ball will always be (0.0,-6.5f,-20.0f) units away from the paddle.

[edited by - Tree Penguin on January 16, 2004 6:04:19 PM]
Ructions
Ructions
thanks thats working better now.

another question i am testing for collsion of the ball off the paddle and am using this code.

if ((ball.x<=paddle.x+PaddleWidth)&&(ball.x>=paddle.xPaddleWidth)&&
(ball.y<=paddle.y+PaddleHeight)&&(ball.y>=paddle.y-PaddleHeight))

i am then reflecting the ball using

// reflect ball
ball.dy=-ball.dy;

// push ball out of paddle since it made contact
ball.y+=ball.dy;

but it does collsion even when its not hitting the paddle any suggestions
Tree Penguin
Tree Penguin
You haven''t told the program to revert the x speed, so the ball probably bounce like the paddle was perpendicular to the one actually there, so add the line:
ball.dx=-ball.dx;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.