Oh, you meant destroying Bodies! You can call somebody:destroy(), and remove all references to it. Hope this helps.dbltnk wrote:There's a tutorial game that does what I want to do, destruction.love: I need to remove a entity from the game. Now I just need to find out how this works. =D
Framework/code for entities?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Framework/code for entities?
Help us help you: attach a .love.
Re: Framework/code for entities?
You were right, collision detection via Box2D is really tedious and not really what I'm looking for.
Is there an equivalent for the Actionscript 3.0 function hitTestObject() in LÖVE/LUA? The only thing I need to know is if two images collide. A simple true/false is all I'm looking for. =D
Is there an equivalent for the Actionscript 3.0 function hitTestObject() in LÖVE/LUA? The only thing I need to know is if two images collide. A simple true/false is all I'm looking for. =D
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Framework/code for entities?
It depends. If you want to compare two rectangles, it's pretty simple:dbltnk wrote:You were right, collision detection via Box2D is really tedious and not really what I'm looking for.
Is there an equivalent for the Actionscript 3.0 function hitTestObject() in LÖVE/LUA? The only thing I need to know is if two images collide. A simple true/false is all I'm looking for. =D
Code: Select all
function collide(obj1, obj2) --obj1 and obj2 are assumed to be tables with these fields: x, y, w, h
if (obj1.x > obj2.x and obj1.x < obj2.x + obj2.w) or (obj2.x > obj1.x and obj2.x < obj1.x + obj1.w) then
if (obj1.y > obj2.y and obj1.y < obj2.y + obj2.h) or (obj2.y > obj1.y and obj2.y < obj1.y + obj1.h) then
return true
end
end
end
For two circles, it's even easier:
Code: Select all
function collideCircle(obj1, obj2) --tables with fields x, y, r
return math.sqrt((obj1.x-obj2.x)^2 + (obj1.y-obj2.y)^2) < obj1.r + obj2.r
end
Help us help you: attach a .love.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Framework/code for entities?
In your first collision function you forget to return false if it is not the case, even though it returns nil now, I think it's better coding style to at least return something. (and note that nil ~= false)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Framework/code for entities?
Again: laziness.bartbes wrote:In your first collision function you forget to return false if it is not the case, even though it returns nil now, I think it's better coding style to at least return something. (and note that nil ~= false)
Help us help you: attach a .love.
Re: Framework/code for entities?
Thanks for the input guys. I've written that circle collision detection yesterday and wanted to to implement boxes today. Then it's time for combining those and I'm all set-up. Maybe I'll post the code here so that not everyone has to re-invent the wheel for something supposedly simple as collision detection.
Re: Framework/code for entities?
Functions for collisions between two rectangles or two circles are done. Now I'm trying to find a way to combine those into a function that does good collision detection between a circle and a box. This is pretty hard as my math skills are way to rusty. *g*
Anyways, here are my current collision functions:
And the not-yet-working one with false positives for rectangle and circle:
Anyways, here are my current collision functions:
Code: Select all
function collide_circle(x1,y1,r1,x2,y2,r2)
local dx = x1-x2
local dy = y1-y2
if dx^2+dy^2 < (r1+r2)^2 then return true else return false end
end
Code: Select all
function collide_box(x3,y3,w3,h3,x4,y4,w4,h4)
if ((x4 > (x3 + w3/2 + w4/2 )) or (x3 > (x4 + w4/2 + w3/2 )) or (y4 > (y3 + h3/2 + h4/2)) or (y3 > (y4 + h4/2 + h3/2)) ) then return false else return true end
end
Code: Select all
function collide_circle_box(x1,y1,r1,x3,y4,w3,h3)
if ((x1 > (x3 + w3/2 + r1 )) or (x3 > (x1 + r1 + w3/2 )) or (y1 > (y3 + h3/2 + r1)) or (y3 > (y1 + r1 + h3/2)) ) then return false else return true end
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Framework/code for entities?
I'm not sure, but I think you have to include a circle vs point collision function, because a rectangle has four points (corners). If you've checked that, I think you only need to check whether the midpoint of the circle is inside the rectangle (ie x1 < x_circle < x2 and y1 < y_circle < y2).
Help us help you: attach a .love.
Re: Framework/code for entities?
I'm done with the first version of my collision "framework". So far it's not very useful as it only checks for collisions between hard-coded objects. My next step will be the creation of a object table with whom I'll be able to spawn circle and rectangle objects. The collision framework will then use the appropriate function to check for collision, depending on the objects being two circles, two rectangles or a circle and a rectangle.
What do you think of the collision code so far? Where can it be improved?
What do you think of the collision code so far? Where can it be improved?
Code: Select all
function load()
-- Load images.
images = {
circle1 = love.graphics.newImage("/Assets/circle1.png"),
circle2 = love.graphics.newImage("/Assets/circle2.png"),
box3 = love.graphics.newImage("/Assets/box3.png"),
box4 = love.graphics.newImage("/Assets/box4.png"),
}
--set up the font and some texts that will be used later
font = love.graphics.newFont(love.default_font, 10)
love.graphics.setFont(font)
debug_circle = "empty"
debug_box = "empty"
debug_circle_box = "empty"
helptext = "Press 1-4 to randomly move the objects. R = Reset. ESC = Exit."
--set up the variables for the two circles
x1 = 120
y1 = 150
r1 = 78
x2 = 300
y2 = 150
r2 = 57
--set up the variables for the two rectangles
x3 = 120
y3 = 295
w3 = 100
h3 = 100
x4 = 290
y4 = 295
w4 = 140
h4 = 70
end
function update(dt)
--every few milli-seconds, this runs the functions that check for collisions
collide_circle(x1,y1,r1,x2,y2,r2)
collide_box(x3,y3,w3,h3,x4,y4,w4,h4)
collide_circle_box(x1,y1,r1,x3,y4,w3,h3)
--x1 = love.mouse.getX() --uncomment this is you want to stick the blue rectangle to your mouse
--y1 = love.mouse.getY() --uncomment this is you want to stick the blue rectangle to your mouse
end
function draw()
-- here the four sprites are drawn
love.graphics.draw(images.circle1,x1,y1)
love.graphics.draw(images.circle2,x2,y2)
love.graphics.draw(images.box3,x3,y3)
love.graphics.draw(images.box4,x4,y4)
--text output telling you which collisions happened
if collide_circle(x1,y1,r1,x2,y2,r2) == true
then debug_circle = "The circles collide."
else debug_circle = "The circles don't collide."
end
if collide_box(x3,y3,w3,h3,x4,y4,w4,h4) == true
then debug_box = "The boxes collide."
else debug_box = "The boxes don't collide."
end
if collide_circle_box(x1,y1,r1,x3,y4,w3,h3) == true
then debug_circle_box = "Circle1 and box3 collide."
else debug_circle_box = "Circle1 and box3 don't collide."
end
love.graphics.draw(debug_circle,10,20)
love.graphics.draw(debug_box,10,35)
love.graphics.draw(debug_circle_box,10,50)
love.graphics.draw(helptext, 10, 390)
end
function collide_circle(x1,y1,r1,x2,y2,r2)
-- this function checks for a collision between the two circles
local dx = x1-x2
local dy = y1-y2
if dx^2+dy^2 < (r1+r2)^2
then return true
else return false
end
end
function collide_box(x3,y3,w3,h3,x4,y4,w4,h4)
-- this function checks for a collision between the two rectangles
if ((x4 > (x3 + w3/2 + w4/2 ))
or (x3 > (x4 + w4/2 + w3/2 ))
or (y4 > (y3 + h3/2 + h4/2))
or (y3 > (y4 + h4/2 + h3/2)) )
then return false
else return true
end
end
function collide_circle_box(x1,y1,r1,x3,y4,w3,h3)
-- this function checks for a collision between the first circle and the cirst rectangle
local dx = math.abs(x1 - x3);
local dy = math.abs(y1 - y3);
if dx > (w3/2 + r1)
then return false
end
if dy > (h3/2 + r1)
then return false
end
if (dx <= (w3/2))
then return true
end
if (dy <= (h3/2))
then return true
end
local dc = (dx - w3/2)^2 + (dy - h3/2)^2
if dc <= r1^2
then return true
else
return false
end
end
function keypressed(key)
--exit the game when ESC is pressed
if key == love.key_escape then love.system.exit() end
--restart the game when R is presses
if key == love.key_r then love.system.restart() end
--move each sprite to a random location when the appropriate key (1-4) is pressed
if key == love.key_1 then x1 = math.random(50,350) y1 = math.random(50,350) end
if key == love.key_2 then x2 = math.random(50,350) y2 = math.random(50,350) end
if key == love.key_3 then x3 = math.random(50,350) y3 = math.random(50,350) end
if key == love.key_4 then x4 = math.random(50,350) y4 = math.random(50,350) end
--move the first rectangle when an arrow key is pressed
if key == love.key_down then y3 = y3+20 end
if key == love.key_up then y3 = y3-20 end
if key == love.key_left then x3 = x3-20 end
if key == love.key_right then x3 = x3+20 end
end
- Attachments
-
- Collider.love
- (8.37 KiB) Downloaded 102 times
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests