"Collisions" without physics?

Hello,

I’m putting together a simple game that will needs to have the basic logic that if my ‘player’ is in state 1 and collides with an ‘enemy’ then the player is destroyed/dies. If the player is in state 2, and collides with an enemy, that enemy dies/is destroyed.

I’m trying to do this by myself and all the docs point to physics which I’m not currently using. My game can be imagined as being like asteroids but the movements of my asteroids is programmed to move in a new random direction every 5 seconds - I’m not using any of the physics features and don’t feel the need to.

Must I use physics for any kind of collision detection or overlapping of pixels? Is there a simpler way of doing it without having to handle all the variables associated with physics?

[import]uid: 121833 topic_id: 34676 reply_id: 334676[/import]

You can certainly do it yourself – basically, you compare the location (including width/height) of your first object, with the location of your second object. If they overlap, you have a collision.

I was kind of in the same boat as you – I was creating a puzzle-type game that didn’t use physics but I needed to know when two objects were in the same spot. What I ended up doing was using the physics engine just to look for collisions, period. I set the gravity to 0,0 and only have the objects that can collide as physics bodies. I don’t use any of the other features of the physics engine.

It turned out really well. And even though the “programmer” in me thinks that my code isn’t as efficient as it could be (because I’m including the complete Box2D physics engine), my goal wasn’t to write the most efficient code – it was to write a game. And if I can use the capabilities of the framework to help me do that, shouldn’t I? :slight_smile:

It might be worth looking into for your case. Just use the collision part, and forget about the rest of it.

Jay [import]uid: 9440 topic_id: 34676 reply_id: 137805[/import]

Hi @ItsSinclair,
If you want to pursue non-physics collisions, you might find this tutorial useful:
http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

Best of luck!
Brent [import]uid: 200026 topic_id: 34676 reply_id: 137834[/import]

Thanks guys, this is great.

I’ll start looking into it [import]uid: 121833 topic_id: 34676 reply_id: 138139[/import]

You can certainly do it yourself – basically, you compare the location (including width/height) of your first object, with the location of your second object. If they overlap, you have a collision.

I was kind of in the same boat as you – I was creating a puzzle-type game that didn’t use physics but I needed to know when two objects were in the same spot. What I ended up doing was using the physics engine just to look for collisions, period. I set the gravity to 0,0 and only have the objects that can collide as physics bodies. I don’t use any of the other features of the physics engine.

It turned out really well. And even though the “programmer” in me thinks that my code isn’t as efficient as it could be (because I’m including the complete Box2D physics engine), my goal wasn’t to write the most efficient code – it was to write a game. And if I can use the capabilities of the framework to help me do that, shouldn’t I? :slight_smile:

It might be worth looking into for your case. Just use the collision part, and forget about the rest of it.

Jay [import]uid: 9440 topic_id: 34676 reply_id: 137805[/import]

Hi @ItsSinclair,
If you want to pursue non-physics collisions, you might find this tutorial useful:
http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

Best of luck!
Brent [import]uid: 200026 topic_id: 34676 reply_id: 137834[/import]

Thanks guys, this is great.

I’ll start looking into it [import]uid: 121833 topic_id: 34676 reply_id: 138139[/import]

@Sinclair, I’m sure you’ve sorted this issue, but I wanted to include a couple of different non-physics based collision implementations I found in my travels. I forget the exact links I found them from, so I wanted to post them here for posterity. The first is:

[lua]
local function collision(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
if (obj1.x > (obj2.x + obj2.width)) then return false
elseif ((obj1.x + obj1.width) < obj2.x) then return false
elseif (obj1.y > (obj2.y + obj2.height)) then return false
elseif ((obj1.y + obj1.height) < obj2.y) then return false
else return true
end
end
[/lua]

and the next is:

[lua]
local function hitTestObjects(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
return obj1.contentBounds.xMin < obj2.contentBounds.xMax
and obj1.contentBounds.xMax > obj2.contentBounds.xMin
and obj1.contentBounds.yMin < obj2.contentBounds.yMax
and obj1.contentBounds.yMax > obj2.contentBounds.yMin
end
[/lua]

Obviously these only are useful for rectangles and not circles. [import]uid: 135394 topic_id: 34676 reply_id: 142104[/import]

@Sinclair, I’m sure you’ve sorted this issue, but I wanted to include a couple of different non-physics based collision implementations I found in my travels. I forget the exact links I found them from, so I wanted to post them here for posterity. The first is:

[lua]
local function collision(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
if (obj1.x > (obj2.x + obj2.width)) then return false
elseif ((obj1.x + obj1.width) < obj2.x) then return false
elseif (obj1.y > (obj2.y + obj2.height)) then return false
elseif ((obj1.y + obj1.height) < obj2.y) then return false
else return true
end
end
[/lua]

and the next is:

[lua]
local function hitTestObjects(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
return obj1.contentBounds.xMin < obj2.contentBounds.xMax
and obj1.contentBounds.xMax > obj2.contentBounds.xMin
and obj1.contentBounds.yMin < obj2.contentBounds.yMax
and obj1.contentBounds.yMax > obj2.contentBounds.yMin
end
[/lua]

Obviously these only are useful for rectangles and not circles. [import]uid: 135394 topic_id: 34676 reply_id: 142104[/import]