Page 1 of 1

[SOLVED :D]STI Collision Map Help

Posted: Sat Apr 26, 2014 5:47 pm
by lalorobot
First I am quite new to all this and my brain is half dry already so this might be a stupid question.
What I want to know is how to check which tiles are in the collision map and then make a rectangle shape (hardoncollider) at its position
Like this
Red: Bounding Box
Green: Tiles
At first:(Bad art explanation! lol)
Image

After Checking which tiles are collidable:
Image

level.lua

Code: Select all

level = {}

function level.load()
	-- Grab window size
	windowWith = love.graphics.getWidth()
	windowHeight = love.graphics.getHeight()
	
	-- Load a map exported to Lua from Tiled
	map = sti.new("maps/forest1")
	Col = map:getCollisionMap("Collidable")
	print(Col)
	print(Col.properties)
	print('\n')
	print("Map.width = " .. map.width)
	print('\n')
	print("Map.orientation = " .. map.orientation)
	print('\n')
	print("Map.tileheight = " .. map.tileheight)
	print('\n')
	print("Map.height = " .. map.height)
	print('\n')
	print("Map.tilewidth = " .. map.tilewidth)
end

function level.update(dt)
	map:update(dt)
end

function level.draw()
	local tx = -0
	local ty = -7
	local w = windowWith
	local h = windowHeight
	
    -- Move the top-left-most x/y position by tx/ty, respectively
    love.graphics.translate(tx, ty)
    
    -- Draw only the tiles on screen
    map:setDrawRange(tx, ty, w, h)
	map:draw()
	--map:drawCollisionMap(Collision)
end	
Thanks in advance :awesome:

EDIT: Added .love I dont know if it will help but whatever :P

Re: [SOLVED :D]STI Collision Map Help

Posted: Mon Apr 28, 2014 9:30 pm
by lalorobot
After looking at STI's code I ended up doing this:

In level.load()

Code: Select all

	bounding = {}
	local tw = map.tilewidth
	local th = map.tileheight
	for y=1, map.height do
		for x=1, map.width do
			local tx, ty
			if map.orientation == "orthogonal" then
				tx = (x - 1) * tw
				ty = (y - 1) * th
			end
			if Col.data[y][x] == 1 then
				TER = Collider:addRectangle(tx, ty, tw, th)
				table.insert(bounding, TER)
			end
		end
	end
Right now I have only tested it on one map but it works so i'm happy :awesome:

Re: [SOLVED :D]STI Collision Map Help

Posted: Tue Apr 29, 2014 2:55 pm
by Karai17
STI doesn't have native collision detection, so yeah you need to roll your own. I'm glad you got it working. :)