How can I make a ball bounce when it collisions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How can I make a ball bounce when it collisions
I am making a pong game and I was wondering how could I do for the ball to move randomly in any direction and make it bounce when it touches the paddle
Re: How can I make a ball bounce when it collisions
Do you use love.physics for movement? If so, you will need to look into setting callbacks:
https://love2d.org/wiki/World:setCallbacks
e.g. beginContact function to change its direction by using applyAngularImpulse / applyForce:
https://love2d.org/wiki/Body:applyAngularImpulse
(probably applying opposite direction to current angle / inverted angle)
https://love2d.org/wiki/World:setCallbacks
e.g. beginContact function to change its direction by using applyAngularImpulse / applyForce:
https://love2d.org/wiki/Body:applyAngularImpulse
(probably applying opposite direction to current angle / inverted angle)
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: How can I make a ball bounce when it collisions
Normally just invert one of axis velocities:
Code: Select all
vx = -vx
-- or
vy = -vy
Re: How can I make a ball bounce when it collisions
For a random direction you can do (only for diagonal moves):
Or for an entirely random direction you could also do something like this
If you opt for the first solution then for the bounce the Darkfrei solution will be the right one (as well as the best for a pong I presume), but if you want a more precise physics by opting for the second solution you can apply the reflection of the velocity vector but this is more technical if you are a beginner or not comfortable with math.
I still gave you a quick example of this method, the ball will not go in the same direction depending on where it hits the racket, that is to say that if it hits all to the right of the racket the ball will go all the way on the right, if it hits the center it will go straight, etc...
Edit: Sorry for the typo, it should be 'math.random < 0.5' instead of 'math.random < 0' because 'math.random' without a parameter returns floating-point numbers between 0 and 1.
Code: Select all
vx = math.random() < 0 and -1 or 1
vy = math.random() < 0 and -1 or 1
Code: Select all
local a = math.random() * (math.pi*2)
local vx = math.cos(a)
local vy = math.sin(a)
I still gave you a quick example of this method, the ball will not go in the same direction depending on where it hits the racket, that is to say that if it hits all to the right of the racket the ball will go all the way on the right, if it hits the center it will go straight, etc...
Edit: Sorry for the typo, it should be 'math.random < 0.5' instead of 'math.random < 0' because 'math.random' without a parameter returns floating-point numbers between 0 and 1.
- Attachments
-
- Ball-reflection.love
- (1.03 KiB) Downloaded 87 times
Last edited by Bigfoot71 on Wed May 10, 2023 1:36 pm, edited 1 time in total.
Re: How can I make a ball bounce when it collisions
Generally, in a Pong game the collisions will be against axis-aligned surfaces, so darkfrei's method works fine as a crude approximation. For a more precise response, Bump's "Bounce" collision resolution works better, especially at higher ball speeds.
But yeah, it's interesting to make it behave as if the racket was curved, to give the player some degree of control over the direction of the shot.
But yeah, it's interesting to make it behave as if the racket was curved, to give the player some degree of control over the direction of the shot.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests