Page 1 of 1
I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 4:31 am
by Uppo9
I'm new to game programming and love2d. So I am having a difficult time having the player react to collision detection and was wondering if anyone would care to give me advice. I have tried multiple things for reacting to collision detection including: blocking the players input, and reverting the player back to their previous position. With both of those things I had some problems. The problem with blocking the players input, is that while blocking one direction it blocked another direction. for example, if you were going right to the square and you were a little over half the square in the players y position it would block the down position and would block the right position also, which is correct. if you were a little less than the square it would block the up position and the right position. the problem with the other method is that if you moved forward into the right side and let go it would push you back 5. It would do the same with the other sides too but in the opposite positions of the side.
Re: I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 3:32 pm
by GVovkiv
For collisions, you can try AABB
(Just google for that or read directly here
https://developer.mozilla.org/en-US/doc ... _detection)
Re: I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 4:30 pm
by Uppo9
i already use that in my game though. the problem is that I am having problems having the player react to the collision detection. I am trying to make it so the player cant pass through objects in the game.
Re: I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 5:18 pm
by GVovkiv
Uppo9 wrote: ↑Fri Jul 30, 2021 4:30 pm
i already use that in my game though. the problem is that I am having problems having the player react to the collision detection. I am trying to make it so the player cant pass through objects in the game.
Then you can try implement pixel-perfect movement in game
For example:
(pseudo code)
Code: Select all
local direction
local speed = 10
local x, y = 0, 0
love.update = function(dt)
if love.keyboard.isDown("a") then
direction = 1 -- left
elseif love.keyboard.isDown("d") then
direction = 2 -- right
end
--here the trickest part, you should take in account dt and somehowe implement in by youself
--in that example, i don't use it, so remember about that
for i = 0, speed do
if direction == 1 then
x = x - 1 -- left
elseif direction == 2 then
x = x + 1 -- right
end
if checkCollision(x, y) then --here you check with aabb if player overlap some collision
-- if it everlap something, then we should push our "player" object out
if direction == 1 then
x = x + 1 -- push object right
elseif direction == 2 then
x = x - 1 -- push object left
end
break -- stop our movement loop
end
end
Re: I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 5:19 pm
by GVovkiv
If you wish, i can make demo about what i mean, but later
Re: I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 6:40 pm
by ReFreezed
Here's an example of very basic collisions using AABBs:
Code: Select all
local player = {x=200, y=200, width=20, height=20} -- x/y is the top left corner.
local box = {x=230, y=230, width=40, height=40}
local TAU = 2*math.pi
local function areAabbsOverlapping(a, b)
return a.x < b.x+b.width
and a.y < b.y+b.height
and a.x+a.width > b.x
and a.y+a.height > b.y
end
function love.update(dt)
-- Move player.
local dx = 0
local dy = 0
if love.keyboard.isDown("left") then dx = dx - 100*dt end
if love.keyboard.isDown("right") then dx = dx + 100*dt end
if love.keyboard.isDown("up") then dy = dy - 100*dt end
if love.keyboard.isDown("down") then dy = dy + 100*dt end
local lastX = player.x
local lastY = player.y
player.x = lastX + dx
player.y = lastY + dy
-- Handle collision.
if areAabbsOverlapping(player, box) then
local playerLastCenterX = lastX + player.width/2
local playerLastCenterY = lastY + player.height/2
local boxCenterX = box.x + box.width/2
local boxCenterY = box.y + box.height/2
-- Get the angle from the center of the box to the center of the
-- player where they were the last frame.
local angle = math.atan2(playerLastCenterY-boxCenterY, playerLastCenterX-boxCenterX)
-- Use the angle to determine what direction the player should get
-- "pushed out" towards.
if math.abs(angle) < 1/8*TAU then player.x = box.x + box.width -- Right side of box.
elseif math.abs(angle) > 3/8*TAU then player.x = box.x - player.width -- Left side of box.
elseif angle > 0 then player.y = box.y + box.height -- Bottom side of box.
else player.y = box.y - player.height -- Top side of box.
end
end
end
function love.draw()
love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
love.graphics.rectangle("fill", box.x, box.y, box.width, box.height)
end
Re: I need some help with a player reacting to collision detection.
Posted: Fri Jul 30, 2021 10:08 pm
by pgimeno
Collisions are not an easy topic, especially swept collisions like you seem to want. I'd suggest you to take a look at
bump.lua.
Also @ReFreezed, I'm allergic to the use of unnecessary trigonometry. Atchoo! In this case, slopes will give you a more direct answer. A slope of 1 means a 45 degree angle. You can easily check if the slope of a line is <1 or >1 by checking if dy<dx or dy>dx.
Re: I need some help with a player reacting to collision detection.
Posted: Sat Jul 31, 2021 12:57 am
by Uppo9
GVovkiv wrote: ↑Fri Jul 30, 2021 5:18 pm
Uppo9 wrote: ↑Fri Jul 30, 2021 4:30 pm
i already use that in my game though. the problem is that I am having problems having the player react to the collision detection. I am trying to make it so the player cant pass through objects in the game.
Then you can try implement pixel-perfect movement in game
For example:
(pseudo code)
Code: Select all
local direction
local speed = 10
local x, y = 0, 0
love.update = function(dt)
if love.keyboard.isDown("a") then
direction = 1 -- left
elseif love.keyboard.isDown("d") then
direction = 2 -- right
end
--here the trickest part, you should take in account dt and somehowe implement in by youself
--in that example, i don't use it, so remember about that
for i = 0, speed do
if direction == 1 then
x = x - 1 -- left
elseif direction == 2 then
x = x + 1 -- right
end
if checkCollision(x, y) then --here you check with aabb if player overlap some collision
-- if it everlap something, then we should push our "player" object out
if direction == 1 then
x = x + 1 -- push object right
elseif direction == 2 then
x = x - 1 -- push object left
end
break -- stop our movement loop
end
end
Thanks, for the solution, it really helped! the problem was caused by me checking the collision detection first before updating the players position. which caused the player to be pushed 5 from the square. I solved it by reversing the order.