Page 1 of 1

Problem in Collision Detection

Posted: Mon Feb 03, 2014 9:22 am
by Tongshenrong

Code: Select all

function love.load()
  --player
  player = {}
  player.x = 50
  player.y = 50
  player.radius = 20
  player.speed = 150
  -- enemy
  enemy = {}
  enemy.x = love.graphics.getWidth()/2
  enemy.y = love.graphics.getHeight()/2
  enemy.radius = 20
end
 
function love.update(dt)
  -- collision veriables
  player_radius = player.radius
  enemy_radius = enemy.radius
  x_sub = math.abs(player.x - enemy.x)
  y_sub = math.abs(player.y - enemy.y)
  distance = math.sqrt(math.abs(x_sub * x_sub) + math.abs(y_sub * y_sub))
  -- movement of the player
  if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
    player.y = player.y - player.speed * dt
  end
  if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
    player.y = player.y + player.speed * dt
  end
  if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
    player.x = player.x - player.speed * dt
  end
  if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
    player.x = player.x + player.speed * dt
  end
  -- collision check
  status = "No collision"
  if distance <= player_radius + enemy_radius then
    status = "Collision detected!"
    -- stop the player!
  end
end
 
function love.draw()
  -- set status color to white
  love.graphics.setColor(255, 255, 255)
  -- draw the status
  love.graphics.print("Status: " .. status)
  -- set player's color to red
  love.graphics.setColor(255, 0, 0)
  -- draw the player
  love.graphics.circle("fill", player.x, player.y, player.radius)
  -- set enemy's color to green
  love.graphics.setColor(0, 255, 0)
  -- draw the enemy
  love.graphics.circle("fill", enemy.x, enemy.y, enemy.radius)
end
please look at line 39, i want to do what i wrote there... When collision happens the circle must stop :(

Re: Problem in Collision Detection

Posted: Mon Feb 03, 2014 10:23 am
by Azhukar
Add this to that line.

Code: Select all

local dx = (enemy.x-player.x) --vector from enemy to player
local dy = (enemy.y-player.y)
local len = math.sqrt(dx*dx+dy*dy) --vector length
local nx = dx/len --normalized vector coordinates
local ny = dy/len
local desiredDistance = player_radius+enemy_radius
player.x = enemy.x - nx*desiredDistance --move player along vector from enemy to player
player.y = enemy.y - ny*desiredDistance

Re: Problem in Collision Detection

Posted: Mon Feb 03, 2014 11:38 am
by Tongshenrong
Thanks for the code,
But my real problem is i can't solve the response of the collision in my mind. I can detect the collision but i don't know how to write that(u wrote)...
Whats the logic of this?
(sry for my bad english)

Re: Problem in Collision Detection

Posted: Mon Feb 03, 2014 12:07 pm
by Azhukar
Tongshenrong wrote:Thanks for the code,
But my real problem is i can't solve the response of the collision in my mind. I can detect the collision but i don't know how to write that(u wrote)...
Whats the logic of this?
(sry for my bad english)
Vectors.

This is what happens:
1. your player moves into enemy
2. their circles intersect = collision is detected
3. vector from enemy center to player center is created
4. player center is moved along this vector until he no longer intersects the enemy