need help:randomly crashes caused by physics

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
titangate
Prole
Posts: 11
Joined: Tue Apr 26, 2011 4:24 am

need help:randomly crashes caused by physics

Post 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.
Attachments
game.love
(1.32 MiB) Downloaded 164 times
Screen shot 2011-07-03 at 1.36.48 AM.png
Screen shot 2011-07-03 at 1.36.48 AM.png (92.88 KiB) Viewed 4956 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: need help:randomly crashes caused by physics

Post by bartbes »

This happens when you destroy a body/shape when it is still colliding.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: need help:randomly crashes caused by physics

Post by ivan »

titangate wrote:destroy bodies(bodies, not shapes)
When you destroy a body all of its associated shapes are destroyed as well.
User avatar
titangate
Prole
Posts: 11
Joined: Tue Apr 26, 2011 4:24 am

Re: need help:randomly crashes caused by physics

Post by titangate »

thanks for your help, but i'd like to ask for more help :P
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?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: need help:randomly crashes caused by physics

Post by ivan »

titangate wrote:thanks for your help, but i'd like to ask for more help :P
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.
Last edited by ivan on Sun Jul 03, 2011 3:17 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: need help:randomly crashes caused by physics

Post 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.
User avatar
titangate
Prole
Posts: 11
Joined: Tue Apr 26, 2011 4:24 am

Re: need help:randomly crashes caused by physics

Post 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..
Last edited by bartbes on Sun Jul 03, 2011 5:39 pm, edited 1 time in total.
Reason: Code tag
User avatar
titangate
Prole
Posts: 11
Joined: Tue Apr 26, 2011 4:24 am

Re: need help:randomly crashes caused by physics

Post 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.
User avatar
titangate
Prole
Posts: 11
Joined: Tue Apr 26, 2011 4:24 am

Re: need help:randomly crashes caused by physics

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: need help:randomly crashes caused by physics

Post 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.
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests