Page 2 of 2

Re: love.physics is the most frustrating thing ever.

Posted: Tue May 17, 2011 10:25 am
by vrld
Tesselode wrote:3. Use an if statement: if (shape1 = your first shape and shape2 = your second shape) OR if (shape1 = your second shape and shape2 = your first shape)
4. Figure out which bodies the shapes apply to so you can make them react properly.
At least those two points can be handled more gracefully:

You can attach data to a shape using Shape:setData. This data can be any Lua value; especially it can be a table. Attach a table with the body and some id field:

Code: Select all

theShape:setData {
    body = theBody,
    id = theID, -- this again can be any lua value
}
Now create a dispatcher table, which will hold functions for every pair of shapes that you want to handle collisions:

Code: Select all

dispatcher = {}
dispatcher[id1] = {}
dispatcher[id1][id2] = function(a, b) --[[ do stuff --]] end
dispatcher[id1][id3] = function(a, b) --[[ do stuff --]] end

dispatcher[id2] = {}
dispatcher[id2][id4] = function(a, b, contact) --[[ do stuff --]] end
The collision callback(s) will use the dispatcher table(s) to handle collisions:

Code: Select all

function persist(a,b,contact)
    if dispatcher[a.id] and dispatcher[a.id][b.id] then
        dispatcher[a.id][b.id](a,b,contact)
    elseif dispatcher[b.id] and dispatcher[b.id][a.id] then
        dispatcher[b.id][a.id](b,a,contact)
    end
end
You can modify this approach to support things like collisions with groups (e.g. lava-pits, trampolines, ...) by using a group id which will be used for dispatching.

Re: love.physics is the most frustrating thing ever.

Posted: Tue May 17, 2011 7:58 pm
by Tesselode
ivan wrote: I assume your platforms are not moving so you can just create static shapes (whose density/mass is 0).
I did mention rotating platforms.

I see what you guys mean about the shapes being separate from the bodies, but it would be nice if the shapes could be automatically calculated from a polygon or something. I wish all this physics stuff required less code.

Also, I've figured out how Box2D works; I used it to make Tilt, but then a bunch of people experienced strange glitches and I didn't know why. And it's hard to tell what's causing the glitches because of how much code I had to use. So I started recoding it, but it's a lot of code and it's quite tedious. Part of that, though, is having lots of different screens and transitions. Maybe I'll try using gamestates.

Edit: This topic is a little pointless; it was really just me venting about how much code it takes to write anything in Box2D. I think I will go back to working on Tilt, maybe make a couple of alpha releases to test basic functionality, and then start the coding with good organization in mind. I imagine that's how most people make their games anyway.

Re: love.physics is the most frustrating thing ever.

Posted: Tue May 17, 2011 8:26 pm
by slime
This little utility might be useful to people, although it's not free and it doesn't have a specific LÖVE exporter (yet?)

Re: love.physics is the most frustrating thing ever.

Posted: Thu May 19, 2011 2:19 am
by Xgoff
i found it annoying that there is apparently no way of using just the collision detection without having to drag the physics into it one way or another... which would generally mean that certain things you would try to do would have side effects like making objects ignore collision detection completely

oh and b2Assert

Re: love.physics is the most frustrating thing ever.

Posted: Thu May 19, 2011 2:41 am
by slime
If you're talking about collision detection vs collision response, you can have any shape be a Sensor (Shape:setSensor).