Question about breakout - ball and paddle collison

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DOMINOSRULZ
Prole
Posts: 16
Joined: Mon Jul 27, 2020 3:18 am
Contact:

Question about breakout - ball and paddle collison

Post by DOMINOSRULZ »

I am in the process of updating a recent breakout clone I made, doing homebrew ports of it for the Nintendo Switch and Nintendo 3DS. There are some things I want to fix, and one of those things is the ball to paddle collision, which as of now, is done always in a 45 degree angles. I've looked for numerous solutions for it, with many saying some aspect of trig and vectors is involved. I've tried doing this to half successful results, however, I think I may have found another simple solution for this in an older thread in the Allegro fourms from 2003:
Posted on 05/05/2003 6:46 PM View Profile

First of all, most breakout clones do not keep a constant ball speed. When the ball leaves at a a sharp angle it travels much more quickly than when it bounces more or less vertically. Indeed horizontal speed is usually completely decoupled from vertical. Indeed the common solution is this:

vy = -vy;
vx += (ballx_centre - batx_centre) * arbitrary_constant;

If you really want to keep a common speed then obviously follow any of the other pieces of advice posted before this.
I tested this solution, and it seems to give me exact results. From what is shown here, it seems to take the distance between ball center and paddle center, multiply by a constant, and then just add the results to current x velocity. I wanted to ask if this is a good solution to implement, especially since there is a lot of emphasis on using trigonometry and vectors to get the ball to reflect at different angles, but it seems as though this solution provides basically the same result, and even uses a bit of math. Any thoughts?
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Question about breakout - ball and paddle collison

Post by togFox »

Suck and see. :)

You haven't explained what alternate trig/vector solutions you've considered but that sample code above seems to be fine. The ball coming off the ends of the bat will have a larger horizontal velocity. The arbitrary_constant should be set at 1 and then tweaked up or down to magnify the effect after play testing.
Last edited by togFox on Sat May 04, 2024 11:39 pm, edited 1 time in total.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Question about breakout - ball and paddle collison

Post by pgimeno »

Many people use trig where none is needed. This problem does not need to involve angles at all. The solution in the post you've quoted is simple and elegant.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Question about breakout - ball and paddle collison

Post by RNavega »

If that results in a fun behavior then that's golden.
I'd also try an alternative where the current bat X speed is added to the ball X speed. This would let the player drag the bat when the ball is about to hit, to make the ball speed up or speed down horizontally, like you can kinda do in air hockey.
So upon contact, adding the bat X speed (maybe times some constant) to the ball X speed.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 11 guests