|
-
Nov 10th, 2002, 12:29 PM
#1
Thread Starter
Frenzied Member
paddle/breakout game
Hi!
I'm programming a Paddle/Breakout game in C++ and I have just gotten to the ball movement...
When the ball hits the paddle,wall or a block how should the new angle be calculated??
-
Nov 10th, 2002, 12:34 PM
#2
Good Ol' Platypus
Usually if it hits the left or right wall, we just inverse the xspeed (xspeed = -xspeed), and if it hits the paddle or the top wall, we inverse the yspeed (yspeed = -yspeed). If you're using angles, you simply add 90 degrees to it each time it hits a wall.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 10th, 2002, 01:18 PM
#3
Thread Starter
Frenzied Member
I was using this code, so I just wanted to check...do you know why it wont run....I suspect that its the if statement that is wrong...
PHP Code:
if(ball.x > (SCREEN_WIDTH - ball.width) )
{
xangle = -xangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
if(ball.x <0)
{
xangle = xangle;
yangle = yangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
if (ball.y < 0)
{
xangle = xangle;
yangle = yangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
if(ball.x > SCREEN_HEIGHT)
{
xangle = -xangle;
yangle = -yangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
-
Nov 10th, 2002, 01:49 PM
#4
Good Ol' Platypus
I've fixed your code:
PHP Code:
if(ball.x > (SCREEN_WIDTH - ball.width) || ball.x < 0)
{
ball.xv = -ball.xv;
Move_BOB(&ball);
}
if(ball.y > (SCREEN_HEIGHT - ball.height) || ball.y < 0)
{
ball.yv = -ball.yv;
Move_BOB(&ball);
}
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 10th, 2002, 02:25 PM
#5
Thread Starter
Frenzied Member
I figured that out while reading your reply 
thanks!
-
Nov 10th, 2002, 03:10 PM
#6
Thread Starter
Frenzied Member
When I start the game the ball goes in the same direction even if I hit the ball with the corner of the paddle.....how would I make it go steeper and broader depending on how I hit the ball.....i have been trying butt off to do it but it won't work...any suggestions??
-
Nov 10th, 2002, 03:28 PM
#7
Thread Starter
Frenzied Member
-
Nov 10th, 2002, 03:36 PM
#8
Good Ol' Platypus
Glad you solved it. What did you end up doing?
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 10th, 2002, 03:53 PM
#9
Thread Starter
Frenzied Member
I did the following, but it still doesn't work perfectly...when the ball hits the paddle on the far right the ball is smashed down instead of going up....
PHP Code:
//the ball has gone outside the screen
if(ball.x <= 0 | ball.x >= SCREEN_WIDTH - ball.width ){
xangle = -xangle;
}
// the ball has gone outside the screen
if(ball.y <= 0 |ball.y == SCREEN_HEIGHT -ball.height+ paddle.height ){
yangle = -yangle;
}
if(ball.y > SCREEN_HEIGHT){
yangle = -yangle;
}
// the ball has hit the paddle
if(Collision_BOBS(&ball,&paddle)){
yangle=-yangle; // change direction
// This collision detection determine where the ball hit the paddle and
// calculate a new direction
if(ball.x >= paddle.x && paddle.x <=19){
if(xangle >0){
xangle = 2;
}
else if(xangle <0){
xangle = -2;
}
}
if(ball.x > paddle.x +19 && paddle.x <=38){
if(xangle > 0){
xangle = 1;
}
else if(xangle <0){
xangle = -1;
}
}
if(ball.x >= paddle.x +38 && paddle.x <=57){
if(xangle > 0){
xangle = 0.5;
}
else if(xangle < 0){
xangle = -0.5;
}
}
if(ball.x > paddle.x +57 && paddle.x <=76){
if(xangle < 0){
xangle = -0.5;
}
else if(xangle > 0){
xangle = 0.5;
}
}
if(ball.x > paddle.x +76 && paddle.x <=95){
if(xangle <0){
xangle =-1;
}
else if(xangle >0){
xangle = 1;
}
}
if(ball.x > paddle.x +95 && paddle.x <=115){
yangle = -yangle;
if(xangle <0){
xangle = -2;
}
else if(xangle >0){
xangle = 2;
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|