bump.lua bounce

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
User avatar
milk
Prole
Posts: 39
Joined: Sun Jul 17, 2016 7:20 pm

bump.lua bounce

Post by milk »

Hey!

I've been trying to make a small pong game using kikito's bump.lua bounce filter but I can't seem to get it to work, I've got collision working, but the ball just sticks on the paddle :c

Essentially what I'm trying to do is on collision with the paddle (player or ai) the ball inverts its velocity (at least that's what I'm assuming I should do), I wrote a function to calculate the angle of collision to the paddle, so I can use that to reflect the ball at the opposite angle of its approach (see getAngle() function). Maybe this picture helps too:
2016-10-26-032718_1366x768_scrot.png
2016-10-26-032718_1366x768_scrot.png (12.17 KiB) Viewed 6860 times
The ball code/physics starts on line 136 in game:update(), I'd be so grateful if anyone could give me a helping hand!
Attachments
pong.love
(1.22 MiB) Downloaded 177 times
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: bump.lua bounce

Post by pgimeno »

There is a problem here:

Code: Select all

ball.x, ball.y, cols, len = world:move(ball, ball.x, ball.y, bounce) -- Bounce filter?
You haven't defined any global or local called bounce, so you're basically passing nil there to the function.

Try defining a function called bounce:

Code: Select all

local function bounce(item, other)
  return "bounce"
end
The other problem is that the bounce collision resolution only helps for 1 frame. During the rest of frames, it's you who has to update the variables. In this case, you don't update the ball's xvel and yvel on collision, so the ball keeps going left and bounces again (and again and again). Until the above problem was fixed, collision resolution was the default, which is "slide", which basically sticks the ball to the paddle. That's what you were seeing.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: bump.lua bounce

Post by zorg »

pgimeno wrote:There is a problem here:

Code: Select all

ball.x, ball.y, cols, len = world:move(ball, ball.x, ball.y, bounce) -- Bounce filter?
You haven't defined any global or local called bounce, so you're basically passing nil there to the function.
https://github.com/kikito/bump.lua
You probably wanted "bounce", a string.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
sherpal
Prole
Posts: 17
Joined: Wed Sep 14, 2016 9:29 am
Location: Belgium

Re: bump.lua bounce

Post by sherpal »

Just a small remark on your formula to get the angle: you don't really need it.

If v = (v_x, v_y) is the speed vector of your ball at the collision, and n = (n_x, n_y) is the normal vector (thus normalized to have norm 1) of the collision, you can get the velocity u = (u_x, u_y) after the collision by using the formula
u = v - 2 * (v|n) * n,
where (v|n) is the scalar product of v and u defined as (v|n) = v_x * n_x + v_y * n_y.

You can check it because u is the only vector satisfying
- u + v = lambda * n for some real number lambda (basically that means that u+v is parallel to n),
- (u|n) > 0, and
- |(v|n)| = (u|n).

I can do a drawing if it's not clear :)
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: bump.lua bounce

Post by pgimeno »

zorg wrote:https://github.com/kikito/bump.lua
You probably wanted "bounce", a string.
I don't see in the docs anything that suggests that a string is accepted in place of a function.
bump.lua docs wrote: local actualX, actualY, cols, len = world:move(item, goalX, goalY, <filter>)
[...]
  • filter is an optional function. If provided, it must have this signature: local type = filter(item, other). By default, filter always returns "slide".
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: bump.lua bounce

Post by zorg »

pgimeno wrote:
zorg wrote:https://github.com/kikito/bump.lua
You probably wanted "bounce", a string.
I don't see in the docs anything that suggests that a string is accepted in place of a function.
Now that i look at it a bit more, you're probably right :v Also, in that case, your solution might be the one that could probably work. :o
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
milk
Prole
Posts: 39
Joined: Sun Jul 17, 2016 7:20 pm

Re: bump.lua bounce

Post by milk »

So I'm back, I added some basic AI and got bouncing working off the paddles; only problem now is I can't figure out how to stop the ball from well, being very buggy when hitting the ceiling/floor, you'll see what I mean if you open it. The bounce code starts on line 141.

I'm honestly not sure if I'm making use of the bounce filter properly though :/ It seems to work somewhat though. It's almost as if the ball is reflecting back onto the wall, and then back onto it, ad infinitum, so I'm not sure what to do now...
Attachments
pong.love
w/up=up, d/down=down, `=toggle console
(1.22 MiB) Downloaded 167 times
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: bump.lua bounce

Post by pgimeno »

It seems to me that you're only toggling the horizontal component of the velocity and not the vertical one. You can use the collision normal to determine the bounce direction.

It seems to me that bump.lua has a provision for bouncing, but I don't know enough of it as to tell you what and how it's used. The docs refer to https://github.com/kikito/bump.lua/blob ... renade.lua and https://github.com/kikito/bump.lua/blob ... debris.lua as examples on how to handle bouncing.

On an unrelated note, your ball moves at the same speed horizontally while the vertical speed changes, which means that the magnitude of the velocity is different depending on whether your ball is moving diagonally or horizontally. One would expect the ball speed to be constant.
User avatar
milk
Prole
Posts: 39
Joined: Sun Jul 17, 2016 7:20 pm

Re: bump.lua bounce

Post by milk »

I ended up doing

Code: Select all

    for i,v in ipairs (cols) do
        if cols[i].normal.y == -1 then -- Hit bottom
            ball.yvel = -ball.yvel
            ball.direction = ball.direction * -1
        elseif cols[i].normal.y == 1 then -- Hit top
            ball.yvel = -ball.yvel
            ball.direction = ball.direction * -1
        end
        if cols[i].normal.x ~= 0 then -- hitting a paddle, I think.
            if ball.direction == -1 then
                ball.yvel = ball.yvel - pA * angleAmp
            elseif ball.direction == 1 then
                ball.yvel = ball.yvel - aA * angleAmp
            end
        end
    end
Which works, somehow.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 4 guests