Logic Bouncing Ball

I have a ball that bounces off the walls. This bounce is simple, I just do it (snippet)

if ( x - moveSpeed < 0 ) // Ball hit left wall xVel *= -1; 

However, I also have a rectangle that the player is moving. The bounce on it almost works like a rebound on the walls.

But I realized that when the ball received the same movement as the image, it is impossible to make it rise again. Therefore, I need some kind of calculation regarding the movement of the rectangles in order to influence the outgoing corner of the ball. When moving, the rectangle always received a constant speed of movement. This figure shows a rectangle moving to the left, and the ball enters it during its movement, which leads to a 90-degree angle. (Which should not always be 90).

Sorry for my shitty pictures, I hope they make sense. My math is rusty, so I really might need a push in the right direction.

+6
source share
4 answers

No need for any fantastic math. My understanding of these types of games is that the angle that the ball moves away from the oar is determined by where it bounces on the blades. If it bounces in the middle, then the current angle is saved. When he bounces closer to the edge of the oar, the angle is adjusted towards that side of the paddle. Think of a spatula as a rounded surface.

+6
source

Here is a tutorial on some kind of physics (this is what you need to know), and you need to learn about vectors. The tutorial does not exactly match what you are looking for (reflection of the bounce and angles), but it is a BIG start to start, because you need to know all this to complete the project. Game physics 101

If you want to make it easy, here is the C ++ code that accurately describes how to do what you are looking for.

Edit


First you need to check out the second link, its tutorial on what you need to know. But if you want to do more than just throw the ball, turn on other moving objects or something like that, check out the first link.

+9
source

There is a simulation route for real physics (as opposed to @Treebranche's answer, which I think these games really work) can become very complicated. You can consider friction, rotation, contact duration, etc. Here are some links that discuss this.

https://physics.stackexchange.com/questions/11686/finding-angular-velocity-and-regular-velocity-when-bouncing-off-a-surface-with-f

https://physics.stackexchange.com/questions/1142/is-there-a-2d-generalization-of-the-coefficient-of-restitution/

+4
source

This code demonstrates how to bounce the ball backward or in the other direction by changing the X or Y ball using ball.headingX=-ball.headingX and ball.headingY=-ball.headingY .

Putting theory into practice:

 /* update ball x heading */ ball.x+=ball.headingX; /* update ball y heading */ ball.y+=ball.headingY; /* if ball at most right of screen then reverse ball x heading */ if( (ball.x>PONG_SCREEN_RIGHT) ) { ball.headingX=-ball.headingX; } /* check if ball location at top or bottom of screen,if true reverse ball y heading */ if( (ball.y<PONG_SCREEN_TOP) || (ball.y>PONG_SCREEN_BOTTOM-2) ) { ball.headingY=-ball.headingY; } /* check if ball lands on pad, if true bounce back */ if ( (ball.y>= PlayersPad.LEFT) && (ball.y<= PlayersPad.RIGHT) && (ball.x==PlayersPad.x)) { ball.headingX=-ball.headingX; playersScore+=10; } /* let computer track ball movement */ if (ball.x>PONG_SCREEN_RIGHT-18) computersPad.y=ball.y; /* check if ball misses pad, if true display you missed */ if (ball.x<PONG_SCREEN_LEFT) { displayYouMissed(); ball.x=ball_Default_X; ball.y=ball_Default_Y; } 
+1
source

Source: https://habr.com/ru/post/905002/


All Articles