Tile Collision Problem

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
XCaptain
Prole
Posts: 23
Joined: Tue Apr 09, 2013 11:18 am

Tile Collision Problem

Post by XCaptain »

I've been trying to make a character that can freely move around a tile map (non-gridlocked). I can get the collision detection to work, but if the player isn't moving one tile then there's a gap between the player and the tile.
yr9OGQw.jpg
yr9OGQw.jpg (3.86 KiB) Viewed 143 times
This error only happens when going up or left. Here's the code I'm using to handle player movement:

Code: Select all

local player = {}

function player:load()
	self.x = 0
	self.y = 0
	self.h = 32
	self.w = 32
	self.mode = 'fill'
	self.speed = 64
	self.o = -1
	self.map = {}
end

function player:setPos(x,y)
	self.x = x
	self.y = y
end

function player:getPos()
	return self.x, self.y
end

function player:setMap(map,current)
	self.map = map[current]
end

function player:draw()
	love.graphics.setColor(0,186,186)
	love.graphics.rectangle(self.mode, (self.x + self.o) * 32, (self.y + self.o)* 32, self.h, self.w)
end

function player:update(dt)
	if love.keyboard.isDown('d') and self.map[math.floor(self.x) + 1][math.floor(self.y)] ~= "wall" then
		self.x = self.x + 0.5
	end
	if love.keyboard.isDown('a') and self.map[math.floor(self.x) - 1][math.floor(self.y)] ~= "wall" then
		self.x = self.x - 0.5
	end
	if love.keyboard.isDown('w') and self.map[math.floor(self.x)][math.floor(self.y) - 1] ~= "wall" then
		self.y = self.y - 0.5
	end
	if love.keyboard.isDown('s') and self.map[math.floor(self.x)][math.floor(self.y) + 1] ~= "wall" then
		self.y = self.y + 0.5
	end
end

function player:nextTile()
	
end

return player
Is there a way to avoid this?
Attachments
Tilemap.love
(3.23 KiB) Downloaded 156 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Tile Collision Problem

Post by Robin »

How about:

Code: Select all

   if love.keyboard.isDown('a') and self.map[math.floor(self.x - 0.5)][math.floor(self.y)] ~= "wall" then
(and the same change for going up)

By the way, you're not using dt. That's generally not what you want.
Help us help you: attach a .love.
User avatar
XCaptain
Prole
Posts: 23
Joined: Tue Apr 09, 2013 11:18 am

Re: Tile Collision Problem

Post by XCaptain »

Robin wrote:How about:

Code: Select all

   if love.keyboard.isDown('a') and self.map[math.floor(self.x - 0.5)][math.floor(self.y)] ~= "wall" then
(and the same change for going up)

By the way, you're not using dt. That's generally not what you want.
Thanks for the help! :awesome:
Although I still don't understand why on up and left were effected?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Tile Collision Problem

Post by Robin »

Because the (x, y) coordinates refer to the upper-left corner of things. That means there is a slightly different formula for the bottom and the right, and the one you already had worked.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest