Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Bouncing ball on 2D pane  (Read 3944 times)

0 Members and 2 Guests are viewing this topic.

dd

  • Newbie
  • *
  • Posts: 10
    • View Profile
Bouncing ball on 2D pane
« on: August 10, 2024, 05:04:55 pm »
I'm creating a simple pool game in SFML and C++. As of current, you can launch the cue ball, however it doesn't bounce, and simply stops whenever it hits the wall, which is intentional, as of current.
I want it to bounce similar to that of when you're actually playing pool. I've thought on how to do this, and I can't think of a way to implement it the way I want. I've looked at other sites, however most are trying to implement a falling ball, or have some other strange system they want implemented aswell.
So how would I do this?

dd

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Bouncing ball on 2D pane
« Reply #1 on: August 10, 2024, 07:28:08 pm »
Figured I can achieve an effect like what I want by doing `Ball.setRotation(Ball.getRotation()*-1)`

Hapax

  • Hero Member
  • *****
  • Posts: 3387
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Bouncing ball on 2D pane
« Reply #2 on: August 11, 2024, 04:33:35 pm »
Trigonometry and studies on energy conservation would help here for sure.

Just finding a way to use trigonometry to create a "reflection vector" would help for bouncing/rebounding balls.

However, since it's likely that your table boundaries are perfectly vertical and horizontal (assuming a 2D downwards projection) you could "reflect" by inverting the direction's x or y, depending on which side it hits.

Of course, when balls collide with other balls, you will definitely need some trigonometry.



e.g. for a ball that collides with a wall on the right-hand side of the screen (positive x value):
first, I presume you have a velocity vector (the amount added to the ball's position/the ball is moved).
once it collides with the (right-hand) side, negative the x value of the velocity vector:
velocityVector.x = -velocityVector.x;
For example, if the currently velocity vector is (14, 14) (a South-East movement of 10), negating its x value would mean it becomes (-14,14) (a South-West movement of 10).

Something to be aware of is that when a collision happens and the velocity is negated, the collision should not happen again while the ball is still in its position. So, you can move the ball an extra movement after a collision to push it away from the collision area. Or, you could just move the x part to the maximum it is allowed to be before collision.

Doing it more accurately would mean that you would need to calculate the distance it has intersected the collision boundary, move the ball back to the exact point it would have collided, then - after flipping the direction of the velocity vector - move the ball forward by the distance it had intersected.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*