computer pong paddle

Started by
20 comments, last by phil67rpg 10 years, 4 months ago

You gotta check for collision with the upper and lower walls. So let's say the upper wall is at y=upperY and the lower wall is at y = lowerY.

You got some radius of the ball, so you would check for collisions kinda like this:


//ball.position.y is the y coord of your ball
if(ball.position.y+ball.radius > upperY )
{
    //Collision at (ball.position.x, upperY)
    //calculate the bounce-off - we want to "reflect the ball from the wall
    //To calculate reflection of a vector we need the normal of the wall
    // However since the wall is parallel with the Ox basis vector, the normal of the wall
    //is perpendicular to it, so for the upper wall we'll have upperNormal(0,-1)
    //where ball.direction is the directional vector of the ball:
    kFi = dot(ball.direction,upperNormal);
    ball.direction.x = ball.direction.x - 2*kFi*upperNormal.x;
    ball.direction.y = ball.direction.y - 2*kFi*upperNormal.y
}

//ball.position.y is the y coord of your ball
if(ball.position.y-ball.radius < lowerY )
{
    //Collision at (ball.position.x, lowerY)
    //calculate the bounce-off - we want to "reflect the ball from the wall
    //To calculate reflection of a vector we need the normal of the wall
    //However since the wall is parallel with the Ox basis vector, the normal of the wall
    //is perpendicular to it, so for the lower wall we'll have lowerNormal(0,1)
    //where ball.direction is the directional vector of the ball:
    kFi = dot(ball.direction,lowerNormal);
    ball.direction.x = ball.direction.x - 2*kFi*lowerNormal.x;
    ball.direction.y = ball.direction.y - 2*kFi*lowerNormal.y
}

//And you use the new ball direction vector to move your ball once again

It's better if your ball.direction vector is normlized - then you can do this for movement:


//Each frame(if you got a game clock it's better)
ball.position = ball.position + ball.direction*ball.speed*deltaTime;
//Where you can calculate deltaTime each frame like so(not really a great way though):
deltaTime = fps*100/6;
//fps is frames per second

P.S. If something is not clear enough, tell me what it is and I'll elaborate more on it smile.png

Advertisement

thanks lightxbulb could you help me with the paddle movement only.

well I can get the paddle to move up or down the screen but I cant get it to move up and down and up and down again. here is the code that moves the paddle up the screen.

RECT rect;
rect.left=0;
rect.top=vel_y+664;
rect.right=35;
rect.bottom=vel_y+764;
 
 
 
if (vel_y <= 0 && vel_y >= -659) 
{
vel_y-=5;
}

You can have more than 1 if in the same block of code.

You can, for example, have 1 if statement that controls moving up, and 1 if statement that controls moving down.

Hello to all my stalkers.


You can, for example, have 1 if statement that controls moving up, and 1 if statement that controls moving down.

ok I have implemented 2 if statements but it only moves the paddle up and stays at the top of the screen.

 
 
if (vel_y <= 0 && vel_y >= -659) 
{
vel_y-=5;
}
 
 
if (vel_y >= 0 && vel_y <= 659)
{
vel_y+=5;
}

If the first if is triggered, vel_y is never above 0, and it gets decremented. This means vel_y never becomes higher than 0. This means if the first if happens, it will never swap to using the second if.

The same logic applies for the second if.

You need to re-think your if logic and try again.

I don't want to outright tell you how to solve this, because this stuff you need to be able to figure out on your own.

As a tip, try drawing the top and bottom borders on a piece of paper, and label the y coordinates. Then, using the paper, figure out when you want the paddle to move up and down.

Hint: You might have to change something to keep track of which direction the paddle is currently moving. I would suggest storing the paddle's speed and using that.

Hello to all my stalkers.


As a tip, try drawing the top and bottom borders on a piece of paper, and label the y coordinates. Then, using the paper, figure out when you want the paddle to move up and down.

I have taken your advice and will work on this problem.

well I can get the paddle to move up or down the screen but I cant get it to move up and down and up and down again. here is the code that moves the paddle up the screen.


RECT rect;
rect.left=0;
rect.top=vel_y+664;
rect.right=35;
rect.bottom=vel_y+764;
 
 
 
if (vel_y <= 0 && vel_y >= -659) 
{
vel_y-=5;
}

I'll just give you the way I'd do it, and not fix your code, as CoreLactose mentioned it's more useful if you figure it out yourself(though I believe that sometimes a person needs to have seen how it works numerous times to make it work).

Here's what I would do:

1)I will give my paddle some constant speed for movement (we don't need acceleration just yet I think :) )

2)I will check when the player pressed up/down and resolve these cases

3)I will check for collisions with the walls

Incoming pseudo-code:


//At init:
int paddleSpeed = 1;
int paddleY = screenHeight/2;//considering that is the coords for the center of your paddle
int paddleSizeY = 10;
int upperWallY = 10;
int lowerWallY = screenHeight-10;

//In the main loop:
//where keyPressed is a function that checks if that key was pressed
//And the other statement checks if a collision would occur
If((keyPressed(KEY_UP) == true) And ((paddleY-paddleSizeY/2-paddleSpeed*deltaTime)>upperWallY ))
{
   paddleY = paddleY-paddleSpeed*deltaTime;
}

If((keyPressed(KEY_DOWN) == true) And ((paddleY+paddleSizeY/2+paddleSpeed*deltaTime)<lowerWallY ))
{
   paddleY = paddleY+paddleSpeed*deltaTime;
}

I believe the question refers to making the AI paddle move up and down, based on the initial post.

While I would do this based on the ball's current position or something, the question seems to be related to just moving the paddle up to the ball, and then back down again.

Hello to all my stalkers.

Lolz seems like I went full retard tongue.png I thought he was talking about his paddle.

Here's a revised version then:

1)You calculate where the ball will collide with the wall behind the AI paddle:


k = (wallBehindAIPaddleX - ballX)/ ballVelocityX; //care not to have ballVelocityX == 0
pointOfCollisionAtY = ballY + ballVelocityY*k;

2)If the y component of that point is < upperWallY or >lowerWallY the AI paddle should aim for either upperWallY+paddleSizeY/2 or lowerWallY-paddleSizeY/2, otherwise it should aim just for that point ( basically if the ball is assumed to go beyond the upper or lower wall, the paddle should just wait at the upper or lower walls, because it can't go beyond them).

Basically something like this:


//Check this once each time the ball bounces off something or the game starts
If(pointOfCollisionAtY-paddleSizeY/2<upperWallY)
{
   pointOfCollisionAtY = upperWallY + paddleSizeY/2;
}

If(pointOfCollisionAtY+paddleSizeY/2>upperWallY)
{
   pointOfCollisionAtY = upperWallY - paddleSizeY/2;
}

//In the main loop:
//If the distance between the paddle and the point where it wants to be can be
//taken in one go - go there
If(abs(paddleY - pointOfCollisionAtY)<=(paddleSpeed*deltaTime))
{
    paddleY = pointOfCollisionAtY;
}
//If it can't, do it by steps each frame until you can get to where you want
else
{
    if( paddleY > pointOfCollisionAtY) paddleY = paddleY - paddleSpeed*deltaTime;
    if( paddleY < pointOfCollisionAtY) paddleY = paddleY + paddleSpeed*deltaTime;
}

This topic is closed to new replies.

Advertisement