Improving Collision Resolution System

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Improving Collision Resolution System

Post by BruceTheGoose »

I'm trying to develop a simple collision system and its okay but I was wondering if someone could suggest improvements. One issue is the fact that I cannot slide against a wall when going diagonally.

Here is the Collision code

Code: Select all

Player = {}
Player.x = 400
Player.y = 300

Player.newX = 0
Player.newY = 0

Player.moveX = false
Player.moveY = false

Player.velX = 0
Player.velY = 0

function Player.update(dt)
  
  if(love.keyboard.isDown("w")) then
    Player.velY = -200
  end
  if(love.keyboard.isDown("a")) then
    Player.velX = -200
  end
  if(love.keyboard.isDown("s")) then
    Player.velY = 200
  end
  if(love.keyboard.isDown("d")) then
    Player.velX = 200
  end
  
  Player.newX = Player.x + Player.velX * dt
  Player.newY = Player.y + Player.velY * dt
  
  
  Player.moveX = true
  Player.moveY = true
  
  for _,v in ipairs(Tile) do
    if(checkCollisionXY(Player.newX,Player.newY,25,25,v.x,v.y,50,50)) then
      if(checkCollisionY(Player.newY,25,v.y,50)) then
        Player.moveY = false
      end
      if(checkCollisionX(Player.newX,25,v.x,50)) then
        Player.moveX = false
      end
      
      if(not Player.moveX or not Player.moveY) then
        break
      end
    end    
  end
  
  if(Player.moveY) then
    Player.y = Player.newY
    Player.newY = 0
  end
  
  if(Player.moveX) then
    Player.x = Player.newX
    Player.newX = 0
  end
  
  
  Player.velX = 0
  Player.velY = 0
  
end


function checkCollisionXY(x1,y1,w1,h1,x2,y2,w2,h2)
  return(x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 < y2 + h2)
end

function checkCollisionX(x1,w1,x2,w2)
  
  return(x1 + w1 > x2 and x1 < x2 + w2 )
  
end

function checkCollisionY(y1,h1,y2,h2)
  
  return(y1 + h1 > y2 and y1 < y2 + h2)
  
end


function Player.draw()
  love.graphics.setColor(100,200,50,150)
  love.graphics.rectangle("fill",Player.x,Player.y,25,25)
  love.graphics.setColor(255,255,255,255)
end
Attachments
Collision.love
(1.33 KiB) Downloaded 86 times
"I don't know."
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: Improving Collision Resolution System

Post by Nelvin »

Depending on the final speed of your moving objects (defined by the velocity and dt) objects may tunnel through each other without a collision.

The core issue is, you're object may move so fast, that the last check happens before a colliding tile and the next one when it's in a position fully behind a tile. You may think that it's not going to happen because you're object's don't move that fast, but if you use timebased movement (as you do, at the moment), a simple 0.5 second delay caused by the OS, harddisk access or whatever not related to your game at all and the player in your current implementation will easily move through a single tile wall.

Check google for lots of articles which will explain the issue much further than I would be able in a short reply (search for collision tunneling) and here's one of the possible solutions https://gafferongames.com/post/fix_your_timestep/
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 63 guests