bump.lua - push passiv object

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
ertt
Prole
Posts: 10
Joined: Mon Mar 20, 2017 11:02 pm

bump.lua - push passiv object

Post by ertt »

Hey lovers,

im working on a little platform-like game with traps and items that can push the player. A goal is, that the player could not resist againt the pushers or walls. I am working with bump for the collision detection.
In the attached demo i have a player-object and a wall who pushes the player from left to right. this works basically.
BUT: sometimes the player is pushed 12px/dt, sometimes 1px/dt and i have no idea what the problem is.

I guess that the problem comes from the update order. So i want to push the player-object, but before i can do that i collide with it and so i dont move forward. Something like that.
Mayby i have a understandig-problem how to work correct with bump.

Has anyone a idea how to handle this?

Thanks!

ertt
Attachments
passiv.love
(6.7 KiB) Downloaded 119 times
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: bump.lua - push passiv object

Post by MrFariator »

If we can assume that the pusher is going to move a specified amount of units multiplied by dt, and the pusher comes along the player object, we can do the following:
- check the location at which the collision begins to occur
- get the difference between the pusher's final location and where the collision occurred
- Update player's position by this difference (both in the object and bump world)

I feel that in your original code you were over complicating this by doing divisions and multiplications on the derived dx and dy values (based on the pusher's movement, not the difference in collision location and pusher's intended final location). Presumably the float calculations just got tiny bit wrong at times, making the pusher move the player very erratically with the default filter (since you weren't passing any).

I took the opportunity to edit your code, and here are the main parts:

Code: Select all

function collidewithnormal(player, dx, dy)
  -- simply update player's location based on the push amount
  local x,y = world:getRect(player)
  x, y = x + dx, y + dy
  world:update ( player, x, y )
  player.x, player.y = x, y
end

Code: Select all

--inside updatepush()
-- use a filter that returns "cross" so we don't stop moving when touching player. Pusher wants to move all the way!
local newx, newy, cols, len = world:check(push, futurex, futurey, function(item, other) return 'cross' end)

local dx,dy
for i = 1, len do
    local col = cols[i]
    local other = col.other
    if other.isPlayer then
        -- get the difference between touch and final position
        dx, dy = (newx - col.touch.x),
                 (newy - col.touch.y)
        collidewithnormal(other, dx, dy)
    end
end
Attached is the .love with all the changes. I did a really quick pass on this so there may be mistakes (and also made player move automatically towards left), and in particular may fail with very fluctuating framerates, but I think this should give you the general idea. My code assumes that player does not end up inside a wall based on a pusher's movement; that case would require additional code.
Attachments
passiv.love
(7.71 KiB) Downloaded 138 times
User avatar
ertt
Prole
Posts: 10
Joined: Mon Mar 20, 2017 11:02 pm

Re: bump.lua - push passiv object [SOLVED]

Post by ertt »

Hey.
This absolutly works and is a good start for future development of this project. Thanks.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 45 guests