Adjusting hitboxes using 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
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

Adjusting hitboxes using bounce

Post by ward614 »

Hello!

I am currently working on a game using bounce for collision detection and resolution. I am hoping to make the hitbox for the player a bit smaller than the actual sprite, however it appears that the hitbox is being reset when I call world:move. How can I keep the adjusted hitbox I created in world:add? Adjusting values in my movement code makes the player move rather than affecting the hitbox.

Thank you!

Code: Select all

world:add(player,player.x+1,player.y+1,player.width-1,player.height-1)

Code: Select all

local goalX = player.x + (player.speed*player.xdir*dt)
local goalY = player.y + (player.speed*player.ydir*dt)
  
local actualX, actualY, cols, len = world:move(player, goalX, goalY, playerFilter)
  player.x, player.y = actualX, actualY
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Adjusting hitboxes using bounce

Post by MrFariator »

Can you provide more code? These snippets shouldn't affect the size of the collision box present in the bump world, after it has been added. Are you using any other bump functions on your player object, like world:update?

Also keep in mind, that if you want your collision box to be one pixel smaller than the sprite on all sides, you might want to deduct the width and height by twice the amount of the x and y offsets:

Code: Select all

local player = { x = 0, y = 0, width = 32, height = 32 } -- visual representation

-- we want the collision box to be 30x30, centered, differing from the visual representation
-- in order to do this, offset the hitbox by 1 on x and y, and then deduct 2 from both width and height
world:add(player,player.x+1,player.y+1,player.width-2,player.height-2) -- collision representation
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

Re: Adjusting hitboxes using bounce

Post by ward614 »

Thank you for responding! As far as I can tell, there are no other calls to bump affecting the player other than these two, and if I comment out the world:move statement the hitbox matches what is in the world:add statement. I have included both of the .lua files containing these statements below (i have been making new files for each "object" to help me organize)

Code: Select all

require "wall"
require "Bad"
require "win"

function generateLevel1()
  
  world = bump.newWorld(32)
  createMine(250,250)
  createPlayer(100,100)
  createWin(200,200)
  createRoom()
  
  for i,v in ipairs(listofwalls) do
    world:add(v,v.x,v.y,v.width,v.height)
  end
  
  for i,v in ipairs(listofbad) do
    world:add(v,v.x,v.y,v.width,v.height)
  end
  
  for i,v in ipairs(listofsuperbad) do
    world:add(v,v.x,v.y,v.width,v.height)
  end
  
  for i,v in ipairs(listofbomb) do
    world:add(v,v.x,v.y,v.width,v.height)
  end
  
  for i,v in ipairs(listofmine) do
    world:add(v,v.x,v.y,v.width,v.height)
  end
  
  world:add(player,player.x+1,player.y+1,player.width-2,player.height-2)
  
  world:add(win,win.x,win.y,win.width,win.height)
  
end

Code: Select all

player = {}
require "main"

function createPlayer(x, y)
  
  player.x = x
	player.y = y
	player.width = 17
	player.height = 32
	player.xdir = 0
	player.ydir = 0
	player.friction = 0
	player.speed = 150
  player.isPlayer = true
  player.img = love.graphics.newImage("Graphics/player.png")
  
end
function player.update(dt)
  
  player.move(dt)
  
end

function player.move(dt)
  
  local playerFilter = function(player, other)
    if other.isBad then return 'cross'
    elseif other.isWall then return 'touch'
    elseif other.isWin then return 'cross'
    elseif other.isSuperBad then return 'cross'
    elseif other.isBomb then return 'cross'
    elseif other.isMine then return 'cross'
    end
  end
  
  local goalX = player.x + (player.speed*player.xdir*dt)
  local goalY = player.y + (player.speed*player.ydir*dt)
  
  local actualX, actualY, cols, len = world:move(player, goalX, goalY, playerFilter)
  
  player.x, player.y = actualX, actualY
  
  
  for i = 1, len do
    local col = cols[i]
    local other = col.other
    
      if other.isBad or other.isSuperBad or other.isBomb or other.isMine then
        --Kills player if they hit these
        playerKill()
      
      elseif other.isWin then
      --Begins process for going to next level
        world:remove(other)
        table.removekey(win,other)
        nextLevel(dt)
      end
    
  end
  
end

function player.draw()
  
  love.graphics.draw(player.img,player.x,player.y)
  
end
function love.keypressed(key, scancode, isrepeat)
  --Exits game on pressing excape
   if key == "escape" then
      love.event.quit()
   end
   
   if key == "0" then nextLevel(dt) end
   if key == "1" then
     currentLevel = currentLevel -2
     nextLevel(dt) 
     end
   
   if isDead == false then
     
    if key == "left" or key == "a"  then
      player.ydir = 0
      player.xdir = -1
    end
   
    if key == "right" or key == "d"  then
      player.ydir = 0
      player.xdir = 1
    end
   
    if key == "up" or key == "w"  then
      player.xdir = 0
      player.ydir = -1
    end
   
    if key == "down" or key == "s"  then
      player.xdir = 0
      player.ydir = 1
    end
    
  end
   
end
function playerKill()
  world:remove(player)
  start = os.time()
  player.img = love.graphics.newImage("Graphics/splode.png")
  isDead = true
  player.speed = 0
end

function player.reset()
    for i=0, 10 do rawset(player, i, nil) end
end
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Adjusting hitboxes using bounce

Post by MrFariator »

Can you provide a .love of your project? I believe I would need to see your project in action to properly diagnose what the problem is, because I don't see anything wrong with the bump code.
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

Re: Adjusting hitboxes using bounce

Post by ward614 »

Here you go! It is currently set with player.x and player.y showing +1 and the width and height at -2 each. You can see that the right and bottom side of the sprite are adjusted by the change to width and height, but the player.x and player.y changes have no effect even if you crank them up to +50.
Maze Runner.love
(20.09 KiB) Downloaded 25 times
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Adjusting hitboxes using bounce

Post by MrFariator »

Ah, right. That's simply because you add the character to the world with this:

Code: Select all

world:add(player,player.x+1,player.y+1,player.width-2,player.height-2)
But then you move the character with this:

Code: Select all

local goalX = player.x + (player.speed*player.xdir*dt)
local goalY = player.y + (player.speed*player.ydir*dt)
local actualX, actualY, cols, len = world:move(player, goalX, goalY, playerFilter)
player.x, player.y = actualX , actualY
You add the player object to the bump world with an offset, but you don't update the object with an offset; you assume its top-left position to equal to player.xy.

As such, you wind up having the player's X and Y positions in the bump world always match the value in the player table. If you want to account for the offset, you will need to keep doing the following:

Code: Select all

local goalX = player.x + (player.speed*player.xdir*dt) + 1 -- add the offset to the goal positions
local goalY = player.y + (player.speed*player.ydir*dt) + 1
local actualX, actualY, cols, len = world:move(player, goalX, goalY, playerFilter)
player.x, player.y = actualX - 1, actualY - 1 -- deduct the offset from the actual positions
So in short, you need to keep in mind how you want to sync up the representation of your character. There can be two approaches:
1) Your player table is a 1:1 representation of the bump world representation (player.xy equaling where the collision box's top-left is in the bump world), and then you apply offsets to how you render your character.
2) Your player table is more of a representation of the rendering positions of your objects (player.xy equaling where the visual top-left might be), and then you apply any desired offsets to the bump world objects when updating them.

Your usage of world:move was assuming approach 1, when you were trying to do 2, essentially.
ward614
Prole
Posts: 9
Joined: Sat Feb 22, 2020 7:09 pm

Re: Adjusting hitboxes using bounce

Post by ward614 »

Thank you so much for showing me this! I knew something was wrong here, but never tried to adjust both the goal values and the actualX/Y values at the same time, so I didn't see the effect! I really appreciate your help!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], pgimeno and 58 guests