[Solved] Problem with colisions

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
naitomea351
Prole
Posts: 2
Joined: Mon Jun 22, 2015 4:09 am

[Solved] Problem with colisions

Post by naitomea351 »

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

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
Last edited by naitomea351 on Wed Jun 24, 2015 4:46 am, edited 1 time in total.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Problem with colisions

Post by HugoBDesigner »

Okay, it was a bit hard to understand what you were saying (I suppose your first language is Spanish? Can't tell).
To make it easier for me (and anyone else trying to help), I'd like you to answer a few questions:

1) Is the code that you WROTE for collision not working, or did you NOT write a code for collision?
2) The movement system is based on tiles (moving tile by tile) or is it dynamic (free moving, like in most games)?

I hope you can answer this so we can help you better :ultraglee:
@HugoBDesigner - Twitter
HugoBDesigner - Blog
naitomea351
Prole
Posts: 2
Joined: Mon Jun 22, 2015 4:09 am

Re: Problem with colisions

Post by naitomea351 »

HugoBDesigner wrote:Okay, it was a bit hard to understand what you were saying (I suppose your first language is Spanish? Can't tell).
To make it easier for me (and anyone else trying to help), I'd like you to answer a few questions:

1) Is the code that you WROTE for collision not working, or did you NOT write a code for collision?
2) The movement system is based on tiles (moving tile by tile) or is it dynamic (free moving, like in most games)?

I hope you can answer this so we can help you better :ultraglee:
yep, i speak spanish xD

well, first yesterday i solved my problem, i'll put the code after and before but, anyway i will answer your questions xD

1.- the code i put wasn't working, if i collide with something i was can't go back:

Image

2.- i don't understand the difference between based on tiles or dynamic xD

Here's the code:

before:

Code: Select all

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
after:

Code: Select all

function love.update(dt)
  --if arg[#arg] == "-debug" then require ("mobdebug") .start() end
  
  if love.keyboard.isDown("right") and love.keyboard.isDown("left") then
    banderita = banderita
    trabadoiz = trabadoiz
    trabadoder = trabadoder
  else
    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
          banderita = banderita
        else
          if spritey+60<sprites[i][4] or spritey>sprites[i][5] then
            banderita = banderita
          else
            banderita = false
            trabadoder = true
            
            if trabadoiz then
              spritex = spritex + 5
              camera:move(5,0)
              trabadoiz = false
              trabadoder = false
            end
          end
        end
        i = i + 1
      end
    end
     
    if banderita then
      spritex = spritex + 5
      camera:move(5,0)
      banderita = false
    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
          banderita = banderita
        else
          if spritey+60<sprites[i][4] or spritey>sprites[i][5] then
            banderita = banderita
          else
            banderita = false
            trabadoiz = true
            
            if trabadoder then
              spritex = spritex - 5
              camera:move(-5,0)
              trabadoder = false
              trabadoiz = false
            end
          end
        end
        i = i + 1
      end
    end
    
    if banderita then
      spritex = spritex - 5
      camera:move(-5,0)
      banderita = false
    end
  end
	 
	if love.keyboard.isDown(" ") then
		i = 1
		spritey = spritey - 2
	end
	 
	if love.keyboard.isDown("down") then
		i = 1
		spritey = spritey + 2
	end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 7 guests