Bump change value on contact

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
sefan
Prole
Posts: 23
Joined: Fri Jul 31, 2015 7:03 am
Contact:

Bump change value on contact

Post by sefan »

Hi,

I currently using Bump as my collision library and need some help.
I'm trying to make so if the player stand on a button in will activate and when the player leaves the button deactivates it.
Is there any smart way to do this with Bump?
Or should i use Love Physics for this? that have beginContact and endContact.
Sefan.se GitHub.com Itch.io
Looking for help on graphics/music/sound effects. PM me if interested.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Bump change value on contact

Post by MrFariator »

It is quite simple to do this, although implementation may depend on your approach. For starters, assuming you are making a 2D platformer, one way to check if player is standing on a button is to check if player's collision box is just above the button's collision box, ie. something like this:

Image

Where that tiny green line is what player object (blue) needs to be touching for the button (red) to be considered pressed. The line is essentially a box that's at least one pixel tall, located above the button object, and is as wide as the button (or any other specified width).

For instance, if each button checks if player is standing on the button individually, it'd be something like (inside the button object's code):

Code: Select all

-- Is button pressed?
local pressed = false;

-- Create filter for the button object
local filter = function ( other )
  if other.isPlayer then
    return true 
  end
end

. . .

-- During update, check button's hitbox if player is on it.
-- Parameters are listed as potential examples, the important
-- part is to use the filter defined earlier!
local cols, len = world:queryRect ( buttonX, buttonY-1, buttonWidth, 1, filter )
pressed = (len>0) -- set to false or true, len will be larger than 0 only if an object with the field isPlayer is touching the button
However, because this way you'd need to loop through every button object, it is not very efficient way to do this. As such, it'd be more efficient to check for button presses as you move the player object. Following this, you could look at the example movePlayer function provided on the github page:

Code: Select all

function movePlayer(player, dt)
  local goalX, goalY = player.vx * dt, player.vy * dt
  local actualX, actualY, cols, len = world:move(player, goalX, goalY, playerFilter)
  player.x, player.y = actualX, actualY
  for i=1,len do
    local other = cols[i].other
    if other.isCoin then
      takeCoin(other)
    elseif other.isExit then
      changeLevel()
    elseif other.isSpring then
      highJump()
    end
  end
end
From this function if you pay attention to the for loop, you can see that you could add a case for other.isButton, which would then call a function or piece of code that actually activates the button that was touched. This will depend on how you store objects into the bump world.

However, because there's no equivalent for endContact in Bump (at least, to my knowledge), you'll have to use some other method to check when player stops touching the button. One way to do this could be that player object stores the buttons it is currently touching in a table, and whenever player stops touching a given button (ie. it doesn't appear in the cols table returned by world:move() in movePlayer()) it removes that button from the table, and also deactivates it.

Alternatively after a button has been activated, it itself will check when player has stopped touching it, perhaps using the "individual" approach mentioned earlier. This would have the benefit of removing a bunch of button control code from the player's codebase, and only activated buttons would query the Bump world for collisions.

There's a plenty of ways to approach this, and the best one may depend on how you wish to architecture your code. I myself like to store objects as tables into the Bump world with fields like ID (a string) and various properties (like isTile, isDamaging, isEnemy, etc). Then if necessary, I use the observer pattern to call that object's designated function for the situation (for example in this case, activate() on a button object when a collision is detected).
User avatar
sefan
Prole
Posts: 23
Joined: Fri Jul 31, 2015 7:03 am
Contact:

Re: Bump change value on contact

Post by sefan »

Thanks MrFariator.
Got an idea how to solve this now :)
Sefan.se GitHub.com Itch.io
Looking for help on graphics/music/sound effects. PM me if interested.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 51 guests