Improvements in Collision Resolution System

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
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Improvements in 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 56 times
"I don't know."
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Improvements in Collision Resolution System

Post by ivan »

Hello there.
To be honest, this code needs a lot of work if you plan to build a game on top of it.
The bad news is that collision detection can get very complicated depending on what you are trying to do.
So building a robust and salable system is hard.
Checking if two shapes overlap is not enough if you want any kind of collision resolution.
So you need to find the "separation vector" which is basically the collision normal multiplied by the penetration depth.
You might find my tutorial helpful:
https://2dengine.com/?p=collisions
I also highly recommend Box2D or love.physics.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Improvements in Collision Resolution System

Post by Tjakka5 »

One neccessary improvment would be to implement a spatial hash. It will make it so you're game will perform well, even with hundreds of thousands of colliders.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Improvements in Collision Resolution System

Post by ivan »

Partitioning is good, but not always necessary.
Let's say you game has dynamic objects (enemies) and static objects (environment).
Dynamic objects have to be checked against all other dynamic objects so:
nchecks = ndynamic * ((ndynamic - 1)/2)
For 100 dynamic objects that's 4950 checks.
Static objects do not move and cannot collide with each other.
Therefore, 20 dynamic plus 80 static objects requires only 1790 checks.
In short, if you group/filter your objects correctly you can reduce the number of collision checks considerably.
If you divide your dynamic objects into "bullets" and "enemies" you can ignore bullet vs bullet collisions which is even faster.
Partitioning is good if you want to have a lot of moving objects that can potentially collide with each other.
Post Reply

Who is online

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