[Solved] Problem with colisions
Posted: Mon Jun 22, 2015 4:20 am
Hi, i'm learning Love (and LUA xD) and i'm doing a scenario 2D and trying to put colisions, sorry for my bad english xD
for example, i have a table on lua, the i will put all the sprites that i want to have colisions, i have two boxes, in the part that i see if i'm pressing the "->" and "<-" buttons i have a while that see if the position of the pj its in middle of someone other sprite, and if there is in there, then that i can't move, but, with the first box, if i collide in the left side i can't go left, but if i collide in the right side i can go right, and now, if i collide in the second box on the left side i can go left, but. if i collide on the right side i can't go right, and i don't now why, i want that if i collide on any side, i can go to the another side, but if you don't understand i left my code here, hoping you understand my problem xD
for example, i have a table on lua, the i will put all the sprites that i want to have colisions, i have two boxes, in the part that i see if i'm pressing the "->" and "<-" buttons i have a while that see if the position of the pj its in middle of someone other sprite, and if there is in there, then that i can't move, but, with the first box, if i collide in the left side i can't go left, but if i collide in the right side i can go right, and now, if i collide in the second box on the left side i can go left, but. if i collide on the right side i can't go right, and i don't now why, i want that if i collide on any side, i can go to the another side, but if you don't understand i left my code here, hoping you understand my problem xD
Code: Select all
camera = {}
camera.x = 0
camera.y = 0
camera.scaleX = 1
camera.scaleY = 1
camera.rotation = 0
function camera:set()
love.graphics.push()
love.graphics.rotate(-self.rotation)
love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
love.graphics.translate(-self.x, -self.y)
end
function camera:unset()
love.graphics.pop()
end
function camera:move(dx, dy)
self.x = self.x + (dx or 0)
self.y = self.y + (dy or 0)
end
function camera:rotate(dr)
self.rotation = self.rotation + dr
end
function camera:scale(sx, sy)
sx = sx or 1
self.scaleX = self.scaleX * sx
self.scaleY = self.scaleY * (sy or sx)
end
function camera:setPosition(x, y)
self.x = x or self.x
self.y = y or self.y
end
function camera:setScale(sx, sy)
self.scaleX = sx or self.scaleX
self.scaleY = sy or self.scaleY
end
function camera:move(dx, dy)
self.x = self.x + (dx or 0)
self.y = self.y + (dy or 0)
end
function love.load()
sprites = {}
for i=1, 2 do
sprites[i] = {}
for j=1, 5 do
sprites[i][j] = 0
end
end
sprites[1][1] = love.graphics.newImage("Cubo.png")
sprites[1][2] = 50
sprites[1][3] = sprites[1][2] + 44
sprites[1][4] = 600 - 46
sprites[1][5] = sprites[1][4] + 46
sprites[2][1] = love.graphics.newImage("Cubo.png")
sprites[2][2] = 300
sprites[2][3] = sprites[2][2] + 44
sprites[2][4] = 600 - 46
sprites[2][5] = sprites[2][4] + 46
sprite = love.graphics.newImage("sprite.png")
arbol = love.graphics.newImage("Arbol.png")
r, g, b, a = love.graphics.getColor( )
spritex = 384
spritey = 540
love.graphics.setBackgroundColor(104,197,255)
end
function love.update(dt)
banderita = false
if love.keyboard.isDown("right") then
banderita = true
i = 1
while sprites[i] do
if spritex+32<sprites[i][2] or spritex>sprites[i][3] then
if banderita then
banderita = true
else
banderita = false
end
else
if spritey+60<sprites[i][4] or spritey>sprites[i][5] then
if banderita then
banderita = true
else
banderita = false
end
else
banderita = false
end
end
i = i + 1
end
end
if banderita then
spritex = spritex + 2
camera:move(2,0)
banderita = false
else
spritex = spritex -2
camera:move(-2,0)
end
if love.keyboard.isDown("left") then
banderita = true
i = 1
while sprites[i] do
if spritex+32<sprites[i][2] or spritex>sprites[i][3] then
if banderita then
banderita = true
else
banderita = false
end
else
if spritey+60<sprites[i][4] or spritey>sprites[i][5] then
if banderita then
banderita = true
else
banderita = false
end
else
banderita = false
end
end
i = i + 1
end
end
if banderita then
spritex = spritex - 2
camera:move(-2,0)
else
spritex = spritex + 2
camera:move(2,0)
end
if love.keyboard.isDown("up") then
i = 1
spritey = spritey - 2
end
if love.keyboard.isDown("down") then
i = 1
spritey = spritey + 2
end
end
function love.draw()
camera:set()
love.graphics.setColor(0,0,0)
love.graphics.print('Hello World!', 700, 300)
love.graphics.setColor(r,g,b)
love.graphics.push()
love.graphics.translate(sprites[1][2], sprites[1][4])
love.graphics.draw(sprites[1][1], 0, 0)
love.graphics.pop()
love.graphics.push()
love.graphics.translate(sprites[2][2], sprites[2][4])
love.graphics.draw(sprites[2][1], 0, 0)
love.graphics.pop()
love.graphics.push()
love.graphics.translate(40, 450)
love.graphics.draw(arbol, 0, 0)
love.graphics.pop()
love.graphics.push()
love.graphics.translate(540, 450)
love.graphics.draw(arbol, 0, 0)
love.graphics.pop()
love.graphics.push()
love.graphics.translate(spritex, spritey)
love.graphics.draw(sprite, 0, 0)
love.graphics.pop()
love.graphics.draw(sprite, 800, 260)
camera:unset()
end