Fixing Collision
Posted: Sun Jun 22, 2014 7:49 pm
Im working on a little concept here and wanted to get the collision working. I feel like I have hacked and slashed my way through to get a very rudimentary collision system working and I was wondering if anyone can provide some guidance on how to improve it. Or if its just completely awful, maybe provide a better way to do it? Currently I have a map being loaded in from Tiled using STI. Im using the map:getCollisionMap() to check my players movement against a grid, but I want my player to be able to move anywhere and not confined to a grid. I thought if I could get a "ghost" player that is on the grid to follow my player close enough, I could just use the grid players collision and apply it to the free form player. This kind of worked but Im not happy with the fact that I can sometimes move through blocks, or get stuck on others.
Thanks in advance
Blake
EDIT
If you don't want to download it to try and find something here is the main.lua
Thanks in advance
Blake
EDIT
If you don't want to download it to try and find something here is the main.lua
Code: Select all
local sti = require "STI"
function love.load()
map = sti.new("Map")
collision = map:getCollisionMap("Collision")
player = {
x = 32,
y = 32,
gridX = 3,
gridY = 3,
width = 16,
height = 16
}
canMoveRight = true
canMoveLeft = true
canMoveup = true
canMoveDown = true
end
function love.update(dt)
map:update(dt)
if love.keyboard.isDown("right") then
if canMoveRight then
player.x = player.x + 2
end
if collision.data[player.gridY][player.gridX + 1] ~= 1 then
if player.x % 8 == 0 and player.x > player.gridX * 16 - 16 then
player.gridX = player.gridX + 1
end
end
if collision.data[player.gridY][player.gridX + 1] == 1 and player.x % 16 == 0 then
canMoveRight = false
else
canMoveRight = true
end
end
if love.keyboard.isDown("left") then
if canMoveLeft then
player.x = player.x - 2
end
if collision.data[player.gridY][player.gridX - 1] ~= 1 then
if player.x % 8 == 0 and player.x < player.gridX * 16 - 16 then
player.gridX = player.gridX - 1
end
end
if collision.data[player.gridY][player.gridX - 1] == 1 and player.x % 16 == 0 then
canMoveLeft = false
else
canMoveLeft = true
end
end
if love.keyboard.isDown("up") then
if canMoveUp then
player.y = player.y - 2
end
if collision.data[player.gridY - 1][player.gridX] ~= 1 then
if player.y % 8 == 0 and player.y < player.gridY * 16 - 16 then
player.gridY = player.gridY - 1
end
end
if collision.data[player.gridY - 1][player.gridX] == 1 and player.y % 16 == 0 then
canMoveUp = false
else
canMoveUp = true
end
end
if love.keyboard.isDown("down") then
if canMoveDown then
player.y = player.y + 2
end
if collision.data[player.gridY + 1][player.gridX] ~= 1 then
if player.y % 8 == 0 and player.y > player.gridY * 16 - 16 then
player.gridY = player.gridY + 1
end
end
if collision.data[player.gridY + 1][player.gridX] == 1 and player.y % 16 == 0 then
canMoveDown = false
else
canMoveDown = true
end
end
end
function love.draw()
map:draw()
love.graphics.rectangle( "fill", (player.gridX * 16 - 16), player.gridY * 16 - 16, player.width, player.height )
love.graphics.setColor(0,0,0)
love.graphics.rectangle( "fill", player.x, player.y, player.width, player.height)
love.graphics.setColor(255,255,255)
love.graphics.print(player.x % 16, 300, 300, 0, 2)
love.graphics.print(player.y % 16, 300, 350, 0, 2)
end
function love.resize(w, h)
map:resize(w, h)
end