Platformer game using Tiled , colission detection layer fail

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
Oprogrammer
Prole
Posts: 9
Joined: Sat Mar 01, 2014 10:37 pm

Platformer game using Tiled , colission detection layer fail

Post by Oprogrammer »

Hey guys , i am using Tiled to create my tile map , i have two layers , Solid and Death , before when i had integrated Solid layer only my character would not slip through the tiles , but ever since i have "tried" to integrate the death layer my character just slips into the tiles,

This is the code i have for the collision detection part.

Code: Select all

function player:isColliding(map, x, y) -- character collides with tile layer solid only from tmx map. 
		local layer = map.tl["Solid"]
		local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
		local tile = layer.tileData(tileX, tileY)
		local layer2 = map.tl["Death"]
		local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
		local tile = layer2.tileData(tileX, tileY)
		return not(tile == nil)	
	end
	
and here is my player:collide (event)

Code: Select all

 function player:collide(event)
		if event == "floor" then
			self.y_vel = 0
			self.standing = true
		end
		if event == "ceiling" then
			self.y_vel = 0
		end
		
		if event == "death" then
			self.y_vel = 0
			player.x = 256
		end
	end
I assume there is something wrong in the player:collide function but i don't know what.

PS: i am using advance tiled loader, and looked at their range of in depth tutorials but could not even get the game to compile after the changes.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Platformer game using Tiled , colission detection layer

Post by Azhukar »

Try this:

Code: Select all

function player:isColliding(map, x, y) -- character collides with tile layer solid only from tmx map. 
	local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
	local layer = map.tl["Solid"]
	local tile = layer.tileData(tileX, tileY)
	local layer2 = map.tl["Death"]
	local tile2 = layer2.tileData(tileX, tileY)
	return (tile~=nil or tile2~=nil)
end
Oprogrammer
Prole
Posts: 9
Joined: Sat Mar 01, 2014 10:37 pm

Re: Platformer game using Tiled , colission detection layer

Post by Oprogrammer »

Hello ,

Thank you for the quick reply !! , i have put your code it and i am glad to say that my character does not slip into the tiles :).

I wanted to ask, as the tile in the "Death" layer has a physical presence , how would i make a function that would go if player is colliding with tile layer "Death" then ....

Edit: I have added this to my player: update(dt) function

Code: Select all

function player:update(dt) -- update player position 
		local halfX = self.w / 2
		local halfY = self.h / 2
		
		self.y_vel = self.y_vel + (world.gravity * dt)
		
		self.x_vel = math.clamp(self.x_vel, -self.speed, self.speed)
		self.y_vel = math.clamp(self.y_vel, -self.flySpeed, self.flySpeed)
		
		local nextY = self.y + (self.y_vel*dt)
		if self.y_vel < 0 then
			if not (self:isColliding(map, self.x - halfX, nextY - halfY))
				and not (self:isColliding(map, self.x + halfX - 1, nextY - halfY)) then
				self.y = nextY
				self.standing = false
			else
				self.y = nextY + map.tileHeight - ((nextY - halfY) % map.tileHeight)
				self:collide("ceiling")
			end
		end
		if self.y_vel > 0 then
			if not (self:isColliding(map, self.x-halfX, nextY + halfY))
				and not(self:isColliding(map, self.x + halfX - 1, nextY + halfY)) then
					self.y = nextY
					self.standing = false
			else
				self.y = nextY - ((nextY + halfY) % map.tileHeight)
				self:collide("floor")
			end
			else self.y = nextY - ((nextY + halfY) % map.tileHeight)
				self:collide("death")
		end
		
		local nextX = self.x + (self.x_vel * dt)
		if self.x_vel > 0 then
			if not(self:isColliding(map, nextX + halfX, self.y - halfY))
				and not(self:isColliding(map, nextX + halfX, self.y + halfY - 1)) then
				self.x = nextX
			else
				self.x = nextX - ((nextX + halfX) % map.tileWidth) 
			end
		elseif self.x_vel < 0 then
			if not(self:isColliding(map, nextX - halfX, self.y - halfY))
				and not(self:isColliding(map, nextX - halfX, self.y + halfY - 1)) then
				self.x = nextX
			else
				self.x = nextX + map.tileWidth - ((nextX - halfX) % map.tileWidth)
			end
		end
		
		self.state = self:getState()
	end
The issue i have is it registers a "floor" as a "death" which makes my character re spawn to the start ( which it should in an event of a death) I know it is linked to the "if not (self:isColliding) " as i have put an else statement in saying if event is not floor then event = death .
Last edited by Oprogrammer on Wed Mar 05, 2014 12:04 am, edited 1 time in total.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Platformer game using Tiled , colission detection layer

Post by Azhukar »

Oprogrammer wrote:The issue i have is it registers a "floor" even as a "death"
You will have to return a value in player:isColliding that will indicate whether the collision was caused by one layer or the other. Then you can get this value in your update and react accordingly.
Oprogrammer
Prole
Posts: 9
Joined: Sat Mar 01, 2014 10:37 pm

Re: Platformer game using Tiled , colission detection layer

Post by Oprogrammer »

Would i have to put an If else statement in the player:isColliding , that would say something like

Code: Select all

If tile then
     a=1
end

if tile2 then
a =2 
end

and then in the player:update(dt) i just put a statement that goes if a =1 then ......... else .........

PS: sorry i am new to lua and don't understand all the concepts.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Platformer game using Tiled , colission detection layer

Post by Azhukar »

Something similar.

Read about function return values here http://www.lua.org/pil/5.1.html
Post Reply

Who is online

Users browsing this forum: ausboss20001 and 24 guests