Several questions on Box2d

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
thekingdf
Prole
Posts: 9
Joined: Thu Apr 26, 2012 6:31 pm

Several questions on Box2d

Post by thekingdf »

1. How do I make it so all bodies are automatically sleeping unless something collides with it?
This way I dont have to manage all of them. Currently the bodies will only switch to sleep mode after they stop moving (which means it doesnt work on static bodies).
Or is this redundant because even sleeping objects still perform collision checks?
and does this mean I have to manage all the bodies by using chunks to determine which ones to set active?

2. Will using this function "setActive(false)" instead of "setAwake(false)" make my game slightly faster?

3. What do the callbacks "preSolve()" and "postSolve()" mean?

4. Is there anything wrong with using just 1 body to store all the boundaries in my game?
So basically I have a lot of fixtures and edgeshapes but only 1 body at (0,0)
Will this not make my game slower when the body is activated due to all the unnecessary collision checks?
Or is the standard way to use a body for each boundary?

Im just trying to use box2d as efficient as possible ill take any tips please
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

Re: Several questions on Box2d

Post by juno »

Good question...I was wondering how box2d handles sleeping objects.
I imagine even sleeping bodies must be checked, otherwise how would the program know.
Maybe only the bounding box is checked? and if a collision is detected, a more in depth SAT is performed perhaps, and the body is woken!

In my own experience I never worried about this effecting the processing speed (dealing with ~60 bodies)
wat ya mean she's in another castle!?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Several questions on Box2d

Post by ivan »

thekingdf wrote:1. How do I make it so all bodies are automatically sleeping unless something collides with it?
World:setAllowSleeping
This way I dont have to manage all of them. Currently the bodies will only switch to sleep mode after they stop moving (which means it doesnt work on static bodies).
Or is this redundant because even sleeping objects still perform collision checks?
Sleeping bodies don't update their position/velocities, but collision checks are still performed on them. In fact, in the case of a collision they are awaken automatically. This saves just a tiny bit of calculation.
thekingdf wrote:2. Will using this function "setActive(false)" instead of "setAwake(false)" make my game slightly faster?
Probably not by a visible amount.
thekingdf wrote:3. What do the callbacks "preSolve()" and "postSolve()" mean?
These are relatively new callbacks in Box2D, called before and after the collision between 2 shapes is 'resolved'. Resolving a collision means moving the bodies apart and assigning new velocities to the bodies that have collided.
thekingdf wrote:4. Is there anything wrong with using just 1 body to store all the boundaries in my game?
This is totally fine as long as all the shapes in this body are STATIC. In fact there is (or used to be, depending which version of Box2D you have) a default "ground" body that you can use for this purpose as well as for attaching revolute joints.
thekingdf wrote:Im just trying to use box2d as efficient as possible ill take any tips please
Probably a good idea to keep the number of shapes you have to a minimum.
Also, avoid frequent creation/destruction of bodies and shapes.
thekingdf
Prole
Posts: 9
Joined: Thu Apr 26, 2012 6:31 pm

Re: Several questions on Box2d

Post by thekingdf »

World:setAllowSleeping
doesnt work for static bodies
i can set them to sleep manually initially but they will wake up permanently after a collision
This is totally fine as long as all the shapes in this body are STATIC. In fact there is (or used to be, depending which version of Box2D you have) a default "ground" body that you can use for this purpose as well as for attaching revolute joints.
i dont think you understood my goal
im trying to use chunks to cut my game up and i want to active only the bodies that are inside the chunks im currently using, thats why i said this probably wouldnt work with just 1 body because it would activate every shape on the map all the time

Anyways, is it even worth it to cut up the game into chunks? or should i be constantly checking collision against everything on the map and let box2d do the sleep thing?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Several questions on Box2d

Post by ivan »

thekingdf wrote:doesnt work for static bodies
i can set them to sleep manually initially but they will wake up permanently after a collision
Dynamic bodies go to sleep when their velocity becomes smaller than a certain threshold.
The point of this is to avoid extra calculations for bodies that are moving at tiny, non-perceivable velocities.
Static bodies don't have velocities* so putting them to sleep won't have much effect I think.
* - technically they do have velocities but since static shapes can't move - putting them to sleep doesn't reduce the volume of calculations/collision checks
thekingdf wrote:i dont think you understood my goal
im trying to use chunks to cut my game up and i want to active only the bodies that are inside the chunks im currently using, thats why i said this probably wouldnt work with just 1 body because it would activate every shape on the map all the time
It depends. Having a lot static shapes will probably be ok in terms of performance. But I should mention that Box2D will become a slideshow as your number of non-sleeping dynamic shapes gets large. So this approach might not be practical. Also there's the 'max proxies' constant which prevents you from creating too many shapes at the same time.
thekingdf wrote:Anyways, is it even worth it to cut up the game into chunks?
It will probably be easier to create chunks of bodies on the fly.
or should i be constantly checking collision against everything on the map and let box2d do the sleep thing?
How are you checking for collisions yourself from Lua?
thekingdf
Prole
Posts: 9
Joined: Thu Apr 26, 2012 6:31 pm

Re: Several questions on Box2d

Post by thekingdf »

I dont know man.

Im reading the api and it basically says bodies that arent active do not perform collision checks.
This means that i dont need to create the bodies on the fly i can just keep them inactive
Post Reply

Who is online

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