turning physics body on and off

Ok,

I have an object that I wish to turn on and off every 5 seconds or so.

Now i can easily do this using timers and logic.

The issue is that it has a static physics body that I need to turn on and off also.

I basically just make the object isVisible = false and then remove the physics body , I then after 5 seconds add  the physics body again and turn on visibility again.

Is this the correct way? seems to be the only thing I can think of but it seems very wasteful.

Thank you

Instead of removing and then re-adding the body, you can set it’s isActive property to false, which is more efficient.  (You’ll still have to set the isVisible property to false too, as you’re already doing.)

  • Andrew

Andrew, yes of course, good answer, thank you

You could also set the physics body to isSensor=true, so that you can still detect collision without the body interacting with your object.

After being an idiot for a few hours I have to say please do not neglect just checking the API what your working with.

Example :

body.isActive = true

NOPE

body.isBodyActive = true

YES

wasted hours for not paying attention.

:slight_smile:

Whoops, sorry for not having written it correctly in the first place!

  • Andrew

Andrew, no worries, more fool me for not reading Api when you pointed me in the right direction :slight_smile:

Instead of removing and then re-adding the body, you can set it’s isActive property to false, which is more efficient.  (You’ll still have to set the isVisible property to false too, as you’re already doing.)

  • Andrew

Andrew, yes of course, good answer, thank you

You could also set the physics body to isSensor=true, so that you can still detect collision without the body interacting with your object.

After being an idiot for a few hours I have to say please do not neglect just checking the API what your working with.

Example :

body.isActive = true

NOPE

body.isBodyActive = true

YES

wasted hours for not paying attention.

:slight_smile:

Whoops, sorry for not having written it correctly in the first place!

  • Andrew

Andrew, no worries, more fool me for not reading Api when you pointed me in the right direction :slight_smile: