I agree with letting Box2D handle as much of the movement as possible.
If love.physics is only used for collision detection then you waste its biggest advantage and it can be cumbersome.
The advantage is imo that you automatically get realistic collision reactions in complex situations.
Sometimes that can be unwanted, like in your game:
If an enemy is moving (walking) then being hit by a bullet would stop them? Maybe set the mass of the bullet very low.Otherwise trajectory is affected by enemies the bullet has already hit. Enemy collider velocity is set to 0,0 after being hit by bullet to prevent any movement.
Or use setSensor() https://love2d.org/wiki/Fixture:setSensor
That means Box2D will not do any collision reaction but collisions will still trigger callback functions.
One thing I noticed: When moving objects with setPosition(x,y) and they collide with another object then the second object will be pushed, as expected. But if the first object stops moving then the second object instantly also stops moving.
When setting the object's speed and then letting Box2D handle the movement it will result in realistic transfer of impulse on impact.
Box2D also takes care of the problems with very fast objects "tunneling" through each other.
Story time: Why I like Box2D.
In my Asteroids-clone I used Box2D and at the beginning it was not really needed:
Bullets were circles, asteroids were circles, Space ships were circles. If they collided it would always result in deletion of at least one element. (Except when two asteroids bounced into each other, which was neat but would not warrant using a whole physics system.)
Eventually I added multiplayer and now you could shot bullets at other players and their ship would get pushed around by the impulse. That was fun. I added asteroids that require multiple bullet hits to destroy and the bullets would bounce of nicely.
And polygon-walls. And debris stuff that floats around. And satellites that can be pulled by attaching a rope.
Adding all that stuff was relatively simple, instead of fiddling with math I just looked up things in the wiki.
Generally I find adding lots of debug print() and drawing very useful. Just some global boolean variable showDebug that gets toggled by keypress.Also, it feels like too much, but how do I check the Box2D internals to monitor things like this issue?
I often add something to display an object's angular speed or mass or their index numbers.
Wiki has this nice function to draw all bodies: https://love2d.org/wiki/Tutorial:PhysicsDrawing
That saved me lots of headache because sometimes my graphics were drawn in the wrong place or some body/collider was not deleted correctly and was floating around invisible, causing collisions with invisible stuff.
I am not sure why the code on that page is incomplete, the part to draw contacts is missing. I swear I saw a complete version somewhere. It should be like:
Code: Select all
--draw contacts
lg.setColor(255,0,0,255)
local contacts = world:getContactList()
for i=1,#contacts do
local x1,y1,x2,y2 = contacts[i]:getPositions()
if (x1) then
lg.circle ("fill",x1,y1,6,4)
end
if (x2) then
lg.circle ("fill",x2,y2,6,4)
end
end