Page 1 of 1

[Solved] Problem with collision with mutliple objects.

Posted: Fri Aug 21, 2015 2:52 am
by Twinki61
I'm working on a basic top-down game and I've been trying to figure out for the longest time why when my character collides with just one wall, they are knocked back as they should be, however, when colliding with two or more walls (In my code I handle a wall as a 64x64 segment) the character glides right through. I've tried using my own collision system, and several different implementations of AABB, all with the same result. I'm fairly new to programming so I have no idea what the problem is nor how to come to a solution. :cry:

Code:

Code: Select all

function love.load()

	player = {
		x = 0,
		y = 0,
		rotation = 0,
		velocity = 0,
		speed = 200,
		maxVelocity = 1.0,
	}

	map = {
		{0,0,0,0,0,0},
		{0,0,0,1,0,1},
		{0,1,0,1,0,0},
		{0,1,1,1,0,0},
	}
end


function aabb(ax, ay, aw, ah, bx, by, bw, bh)
	local ax2,ay2,bx2,by2 = ax + aw, ay + ah, bx + bw, by + bh
	return ax < bx2 and ax2 > bx and ay < by2 and ay2 > by
end


function love.update(dt)
	if love.keyboard.isDown("w") then
		player.velocity = player.velocity + 1 * dt
	else
		player.velocity = (player.velocity / 1.03)
	end

	if love.keyboard.isDown("r") then
		player.x = 0
		player.y = 0
	end

	player.rotation = math.atan2(love.mouse.getY() - player.y, love.mouse.getX() - player.x)

	player.x = player.x + ((math.cos(player.rotation) * (player.speed * player.velocity)) * dt)
	player.y = player.y + ((math.sin(player.rotation) * (player.speed * player.velocity)) * dt)

	for i, v in ipairs(map) do
		for j, k in ipairs(v) do
			if k == 1 then
				if aabb(player.x, player.y, 64, 64, j*64, i*64, 64, 64) then
					player.velocity = -player.velocity
				end
			end
		end
	end

end

function love.draw()
	for i, v in ipairs(map) do
		for j, k in ipairs(v) do
			if k == 1 then
				love.graphics.setColor(0,255,64)
				love.graphics.rectangle("fill", j*64, i*64, 64, 64)
			end
		end
	end

	love.graphics.setColor(0,255,255)
	love.graphics.rectangle("fill", player.x, player.y, 64, 64)

	love.graphics.setColor(255,128,0)
	love.graphics.line(player.x+32, player.y+32, love.mouse.getX(), love.mouse.getY())

	love.graphics.setColor(0,0,0)
end
Use W to move and R to reset character, point with the mouse.

Re: Problem with collision with mutliple objects.

Posted: Fri Aug 21, 2015 9:50 am
by sefan
If you collide with 2 objects during the same frame you will trigger player.velocity = -player.velocity 2 times.
Try to add something like this:
Added player.collide to your code

Code: Select all

...
   player = {
      x = 0,
      y = 0,
      rotation = 0,
      velocity = 0,
      speed = 200,
      maxVelocity = 1.0,
      collide = false
   }
...
   for i, v in ipairs(map) do
      for j, k in ipairs(v) do
         if k == 1 and player.collide == false then
            if aabb(player.x, player.y, 64, 64, j*64, i*64, 64, 64) then
               player.velocity = -player.velocity
               player.collide = true
            end
         end
      end
   end
player.collide = false
...

Re: Problem with collision with mutliple objects.

Posted: Fri Aug 21, 2015 11:54 am
by Twinki61
Haha, wow, thank you. I never would have guessed a debounce would solve the problem. It works perfectly! :awesome: