Page 1 of 1
need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 8:42 am
by titangate
the things i've done with physics:
create bodies/shapes
set category/masks
use persist contact callbacks
destroy bodies(bodies, not shapes) when missile dies(2 seconds)
you can hold left key to shoot around, wsad to move, press right key for temporary boost. after play around a bit, the game would eventually crash and gives me the following pop-up error message.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 10:38 am
by bartbes
This happens when you destroy a body/shape when it is still colliding.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 11:21 am
by ivan
titangate wrote:destroy bodies(bodies, not shapes)
When you destroy a body all of its associated shapes are destroyed as well.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 2:38 pm
by titangate
thanks for your help, but i'd like to ask for more help
so i'm currently thinking, when trying to delete, mark the body/shape as 'needed to delete', when receive remove collision callback on those shapes and there're no more shapes colliding with them, push them into a table and delete them afterwards. sounds quite troublesome, is there any replacement or this is an OK approach?
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 3:06 pm
by ivan
titangate wrote:thanks for your help, but i'd like to ask for more help
so i'm currently thinking, when trying to delete, mark the body/shape as 'needed to delete', when receive remove collision callback on those shapes and there're no more shapes colliding with them, push them into a table and delete them afterwards. sounds quite troublesome, is there any replacement or this is an OK approach?
Finite state machines. When a body needs to be deleted you change its state to 'destroyed'. After processing the collisions and updating the b2world, you update the states of your bodies. Naturally, the 'destroyed' state will delete its owner body.
Behavior trees are another approach that can also be used for AI and animation.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 3:16 pm
by bartbes
Right, you probably want to mark it for deletion and set it to not collide with anything, then you can remove it the next frame.
See
Shape:setMask.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 4:46 pm
by titangate
emm. sorry to bother, but i implemented as i described but the crash continued on.
i'm pasting the related code:
Code: Select all
function Map:destroyBody(obj)
self.destroys[obj] = true
end
function Map:update(dt)
for k,v in pairs(self.destroys) do
self.destroys[k] = true
end
collides = {}
self.world:update(dt)
handlepersist()
for k,v in pairs(self.destroys) do
if v then
k.shape:destroy()
k.body:destroy()
self.destroys[k] = nil
end
end
for unit,v in pairs(self.units) do
if unit.update then unit:update(dt) end
end
end
function handlepersist()
for k,v in ipairs(collides) do
local a,b,c=unpack(v)
if a and map.destroys[a] then map.destroys[a] = false end
if b and map.destroys[b] then map.destroys[b] = false end
if map.units[a] and map.units[b] then
if a.persist then
a:persist(b,c)
end
if b.persist then
b:persist(a,c)
end
end
end
end
-- Collision callback that got registered
function persist(a,b,coll)
table.insert(collides,{a,b,coll})
end
and when i'm trying to delete the bodies, i did as follows:
self.shape:setMask(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
map:destroyBody(self)
map:removeUnit(self)
thanks for your patience and time. please help me to find what's wrong with these steps :/ i'm getting kind of frustrated..
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 5:01 pm
by titangate
ok.. i found this way that potentially solved my problem
k.body:setPosition(-200000,-200000)
just toss the body far away. so far i've encountered no crashes, although i know this method is super awkward.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 5:10 pm
by titangate
bartbes wrote:Right, you probably want to mark it for deletion and set it to not collide with anything, then you can remove it the next frame.
See
Shape:setMask.
THANKS GUYS! i missed this. this should do the same as toss it away, and i tried, everything is functioning now.
I can keep on working now!
i've almost given up lol.
Re: need help:randomly crashes caused by physics
Posted: Sun Jul 03, 2011 7:47 pm
by tentus
titangate wrote:ok.. i found this way that potentially solved my problem
k.body:setPosition(-200000,-200000)
just toss the body far away. so far i've encountered no crashes, although i know this method is super awkward.
Just to put this out there, this is a bad solution because it will put everything at the same place. Only change one of the x-y coords if you're going to do this.