I need some help with a player reacting to collision detection.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Uppo9
Prole
Posts: 3
Joined: Fri Jul 30, 2021 3:28 am

I need some help with a player reacting to collision detection.

Post by Uppo9 »

NewGameTest.love
(194.13 KiB) Downloaded 139 times
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.
Uppo9
Prole
Posts: 3
Joined: Fri Jul 30, 2021 3:28 am

Re: I need some help with a player reacting to collision detection.

Post 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.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: I need some help with a player reacting to collision detection.

Post 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
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: I need some help with a player reacting to collision detection.

Post by GVovkiv »

If you wish, i can make demo about what i mean, but later
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: I need some help with a player reacting to collision detection.

Post 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
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: I need some help with a player reacting to collision detection.

Post 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.
Uppo9
Prole
Posts: 3
Joined: Fri Jul 30, 2021 3:28 am

Re: I need some help with a player reacting to collision detection.

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests