Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Box2D - deleting the World and Bodies

Author
Message
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 6th Nov 2010 15:09
When it comes to deleting a game level, what is the process in Box 2D?

Can I just b2DeleteWorld and it will delete everything in it?

Do I have to b2DeleteBody on everything one by one? If so, does this delete the fixtures too or must they also be manually deleted?

If everything has to be manually deleted is there a method for retrieving all of the bodies and fixtures in a world? Or do I have to retain a record of all the bodies/fixtures I have created?

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Nov 2010 21:56
One thing I have found is that joints must be deleted before bodies or you get a crash.

It would be nice to know what the official method should be.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 9th Nov 2010 22:58
Quote: "Can I just b2DeleteWorld and it will delete everything in it?"


This is what should happen.

Quote: "Do I have to b2DeleteBody on everything one by one? If so, does this delete the fixtures too or must they also be manually deleted?"


Deleting a body should delete all fixtures attached to it. If you have set a destruction callback function (b2SetDestructionListener) it should automatically be called on any fixtures implicitly deleted, so that you can do any cleanup which you need to. The same should be true of joints when one of the attached bodies is deleted.

Quote: "If everything has to be manually deleted is there a method for retrieving all of the bodies and fixtures in a world? Or do I have to retain a record of all the bodies/fixtures I have created?"


To loop through all bodies in a world, you can use 'b2GetWorldFirstBody', followed by repeated calls to 'b2GetNextBody' until it returns zero. For fixtures, the equivalents are 'b2GetBodyFirstFixture' and 'b2GetNextFixture'.

Quote: "One thing I have found is that joints must be deleted before bodies or you get a crash."


This is a bug. I'll look into it ASAP. It should implicitly delete the fixtures and notify the destruction listener as I described above.

[b]
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 9th Nov 2010 23:31 Edited at: 9th Nov 2010 23:32
Thanks Diggsey, answer one makes it all nice and easy.

Just to clarify on the last one:

Quote: "One thing I have found is that joints must be deleted before bodies or you get a crash."


It is most likely trying to delete the joint after the body in my code that caused the crash, NOT deleting a body while a joint is attached to it. I didn't test the actual problem, I simply swapped the order.

Here's how it's all looking now, and the good news is I can play multiple games and destroy the world each time



Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 10th Nov 2010 04:50
nice balls

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 10th Nov 2010 11:07
+1

Quote: "It is most likely trying to delete the joint after the body in my code that caused the crash, NOT deleting a body while a joint is attached to it."


It shouldn't crash at all though. If you are doing something wrong, Box2D should show an error message (ie. "Joint does not exist", or something similar).

[b]
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 10th Nov 2010 13:05
@BatVink - what are your plans for releasing SmartBalls?

It looks great!
Even sharing some of the source code that we could learn from might be really cool too, for those of us that bought Box2D but feel a little overwhelmed, well at least I do.
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 10th Nov 2010 15:12
smartBalls is a working title, that logo is just a test of overlaying the streaks on a textured background.

This is one of my fundraising games, I use them at local school christmas fairs. The plan is then to consider AppUp.

I'll try and extract the Box2D code, it is a very simple implementation which I created by reading the pinball code. As its my first attempt it has been created through trial and error. It has some useful stuff though, like detecting collisions and getting their location.

BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 10th Nov 2010 16:36
I would love to hear more about your fundraising games, our schools here in the US need fundraising also, maybe I could write a game just for that.

Email me if you get a moment and are willing to tell me more about how to use my game in a fundraiser.

And thanks for your willingness to share some Box2D code!
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Nov 2010 12:11
I've posted my collision code here for starters, removing all the game specific stuff so you can see how I've iterated through the collisions for anything important. Added more comments but feel free to ask questions.

It's important to know that when everything was made, the body Ids were stored in globals (array for balls and gScenery.xxx for other stuff). I didn't store the fixtures so you have to work back to get them for the collisions(I have no performance issues so I haven't finetuned this).
arrBall(n).hit is the time of last collision. I use this to filter out multiple reactions to the same collision. I found that one collision of 2 balls could trigger 2 or 3 collisions at 60 FPS.



Login to post a reply

Server time is: 2026-07-21 18:59:02
Your offset time is: 2026-07-21 18:59:02