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:
The ball code/physics starts on line 136 in game:update(), I'd be so grateful if anyone could give me a helping hand!
bump.lua bounce
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: bump.lua bounce
There is a problem here:
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:
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.
Code: Select all
ball.x, ball.y, cols, len = world:move(ball, ball.x, ball.y, bounce) -- Bounce filter?
Try defining a function called bounce:
Code: Select all
local function bounce(item, other)
return "bounce"
end
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: bump.lua bounce
https://github.com/kikito/bump.luapgimeno wrote:There is a problem here:You haven't defined any global or local called bounce, so you're basically passing nil there to the function.Code: Select all
ball.x, ball.y, cols, len = world:move(ball, ball.x, ball.y, bounce) -- Bounce filter?
You probably wanted "bounce", a string.
Me and my stuff True 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.
Re: bump.lua bounce
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
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
Re: bump.lua bounce
I don't see in the docs anything that suggests that a string is accepted in place of a function.zorg wrote:https://github.com/kikito/bump.lua
You probably wanted "bounce", a string.
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".
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: bump.lua bounce
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.pgimeno wrote:I don't see in the docs anything that suggests that a string is accepted in place of a function.zorg wrote:https://github.com/kikito/bump.lua
You probably wanted "bounce", a string.
Me and my stuff True 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.
Re: bump.lua bounce
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...
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 171 times
Re: bump.lua bounce
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.
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.
Re: bump.lua bounce
I ended up doing
Which works, somehow.
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
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 6 guests