Very new to Lua. Collision stuff i guess.

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
DukeOfDew
Prole
Posts: 1
Joined: Wed Apr 25, 2012 10:46 pm

Very new to Lua. Collision stuff i guess.

Post by DukeOfDew »

Ok so this is really the last thing i need for my game to have a basic shape. How do i make it so that my character will not pass an object that i draw?

Im using the CheckCollision function to find if there is a collision, only problem is, i dont know what to do with the result.

Not sure what the procedure here is yet but im just going to copy and paste the little code that i have;

function love.load()
require("entities.lua")
ents.Startup()
love.graphics.setBackgroundColor( 255, 255, 255 )
imageHouse = love.graphics.newImage("textures/house.png")
imagePlayer = love.graphics.newImage("textures/player.png")
x = 50
y = 50
speed = 100
end

function love.draw()
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.draw( imageHouse, 0, 0, 0, 1, 1, 0, 0 )

love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.rectangle( "fill", 100, 75, 20, 20)

love.graphics.draw(imagePlayer, x, y)
end
end

function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
elseif love.keyboard.isDown("left") then
x = x - (speed * dt)
end

if love.keyboard.isDown("down") then
y = y + (speed * dt)
elseif love.keyboard.isDown("up") then
y = y - (speed * dt)
end
end

function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2) --This function checks to see if two objects are in a collision
if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
return true
end

What im asking is, how can i make it so that 'imagePlayer' can not move through this;

love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.rectangle( "fill", 100, 75, 20, 20)

Any help guys?
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Very new to Lua. Collision stuff i guess.

Post by Puzzlem00n »

First of all, I suggest changing your check collision function to the more standard version:

Code: Select all

-- Collision detection function.
-- Checks if a and b overlap.
-- w and h mean width and height.
function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)

  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
Alright, let's explain how that function works. I once received this example in one of the questions I asked: http://silentmatt.com/rectangle-intersection/
You see how it works? The statements are all true at some point, but they are only all true at the same time when the rectangles collide. Then, the function returns true. Now, how do we call this function? We need to put this somewhere:

Code: Select all

CheckCollision(x of first object, y of first object, width of first object, height of first object, x of second object, y of second object, width of second object, height of second object)
We know the x's, y's, width and height of your rectangle at all times, so we can turn that into this:

Code: Select all

CheckCollision(100, 75, 20, 20, x of second object, y of second object, width of second object, height of second object)
As well as this, we can also find the width and height of the image through handy love functions:

Code: Select all

CheckCollision(100, 75, 20, 20, x, y, imagePlayer:getWidth(), imagePlayer:getHeight)
On second thought, it may be more efficient to do this in love.load().

Code: Select all

function love.load()
require("entities.lua")
ents.Startup()
love.graphics.setBackgroundColor( 255, 255, 255 )
imageHouse = love.graphics.newImage("textures/house.png")
imagePlayer = love.graphics.newImage("textures/player.png")

--New stuff here
w = imagePlayer:getWidth()
h = imagePlayer:getHeight()
----

x = 50
y = 50
speed = 100
end

Code: Select all

CheckCollision(100, 75, 20, 20, x, y, w, h)
This is what we need to call this in love.update to check if it occurs.

Code: Select all

if CheckCollision(100, 75, 20, 20, x, y, w, h) then
--do collision
end
So, here's our changes in full:

Code: Select all

function love.load()
	require("entities.lua")
	ents.Startup()
	love.graphics.setBackgroundColor( 255, 255, 255 )
	imageHouse = love.graphics.newImage("textures/house.png")
	imagePlayer = love.graphics.newImage("textures/player.png")
	w = imagePlayer:getWidth()
	h = imagePlayer:getHeight()
	x = 50
	y = 50
	speed = 100
end

function love.draw()
	love.graphics.setColor( 255, 255, 255, 255 )
	love.graphics.draw( imageHouse, 0, 0, 0, 1, 1, 0, 0 )

	love.graphics.setColor( 255, 255, 255, 255 )
	love.graphics.rectangle( "fill", 100, 75, 20, 20)

	love.graphics.draw(imagePlayer, x, y)
end

function love.update(dt)
	if love.keyboard.isDown("right") then
		x = x + (speed * dt)
	elseif love.keyboard.isDown("left") then
		x = x - (speed * dt)
	end

	if love.keyboard.isDown("down") then
		y = y + (speed * dt)
	elseif love.keyboard.isDown("up") then
		y = y - (speed * dt)
	end

	if CheckCollision(100, 75, 20, 20, x, y, w, h) then
		--do collision
	end
end

function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)
  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
Of course, now you need to run what you want to do when they collide in that --do collision area, in this case, form a wall. This post is getting long though, so I think I'll let someone else tell you how to do that. :)
I LÖVE, therefore I am.
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

Re: Very new to Lua. Collision stuff i guess.

Post by sanjiv »

I'm also a newb, and basically, I only moved the player if the new position wasn't in collision with something. Here's what I did, though I am finding some bugs with this solution.

1. I created a table of 'structure' objects, like walls or platforms.
2. For every object in that table, I checked for collision against the player (in love.update(dt))
3. If there was no collision, I moved the player normally, as you already do.
4. If there WAS a collision, I made sure movement was within allowable limits--But how did I determine allowable limits?

On collision with a structure object, I checked where the center of the 'player' object was to the 'structure' object. This way, I knew if a collision was to the right, to the left, above, or below. If the collision was below the player, for example, as in you hit the ground, then I knew to add a 'ground' limit to player movement.

I.e. if hitGroundCollision then y=math.min( yGround , y+ySpeed*dt)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest