Changing physics body properties

Hello everyone!

I was wondering if anyone has a solution for changing body properties such as density, friction and bounce once the body has been created. I found that adding/removing the body makes it stop for a second if it is moving.

I found this function:

https://developer.coronalabs.com/code/body-modification

However, the body that I am trying to change is in motion, and I want it to seem like nothing changed visually (except for bounce, etc.)

Does anyone know how to do that?

The code I posted should cause the body to preserve its velocity, but if that’s not happening can you post a sample of your code to demonstrate the problem? Essentially, if you want to completely change the body properties, you’ll need to remember the values of the body and reapply them - this can include the angular and linear velocities.

No the code does exactly what it describes: preserves velocity, coordinates, etc.

However, when the change occurs there is short moment of lag where the body stops for a few frames and then continues.

Looking at it again, it even seems like the object jumps back a frame.

Either way what I am trying to say is that the change doesn’t appear smoothly.

Hi @deleurapps,

For this level of smoothness, you’ll need to use the “event.contact” method in a pre-collision handler. This allows you to “override” the resulting bounce and friction response between two bodies with values that are more (or less) than they’d normally be. You can even tell it to ignore the collision entirely (like for a “pass-through platform” kind of scenario).

This tutorial should get you started:

http://coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

Sincerely,

Brent Sorrentino

Hey Brent!

Thanks for that! so basically I wouldn’t be changing the body’s properties, but just overriding them during collisions?

Also, I don’t see a density parameter in that function.

Yes, that’s the case. I realize it might not do everything you need… for example, you won’t be able to dynamically change a body’s density in “mid-air” without recreating the body (however, you could adjust the gravity scale of that object which might accomplish a similar result).

But for things like friction and bounce, you can change them for collisions, which effectively does what you need, since there isn’t a true “air friction” principle in Box2D, nor can an object “bounce off air”. :slight_smile:

Brent

great, thanks for that. I’ll see what I can accomplish with this.

ok, so I played around with it and I found that the preCollision thing works great, but I can’t adjust the density. Adjusting the gravityScale doesn’t quite do the job because its a little unpredictable, and it still doesn’t increase the mass of the object

I’ll try to look at the reason why the body “lags” when you remove/add the body, and see if I can modify the function I posted earlier to make it work better. I am guessing that it skips a frame of movement or something, so maybe I can manually adjust the coordinates to mimic the movement.

So I decided to just add a small “transition” during the switch which hides the lag

The code I posted should cause the body to preserve its velocity, but if that’s not happening can you post a sample of your code to demonstrate the problem? Essentially, if you want to completely change the body properties, you’ll need to remember the values of the body and reapply them - this can include the angular and linear velocities.

No the code does exactly what it describes: preserves velocity, coordinates, etc.

However, when the change occurs there is short moment of lag where the body stops for a few frames and then continues.

Looking at it again, it even seems like the object jumps back a frame.

Either way what I am trying to say is that the change doesn’t appear smoothly.

Hi @deleurapps,

For this level of smoothness, you’ll need to use the “event.contact” method in a pre-collision handler. This allows you to “override” the resulting bounce and friction response between two bodies with values that are more (or less) than they’d normally be. You can even tell it to ignore the collision entirely (like for a “pass-through platform” kind of scenario).

This tutorial should get you started:

http://coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

Sincerely,

Brent Sorrentino

Hey Brent!

Thanks for that! so basically I wouldn’t be changing the body’s properties, but just overriding them during collisions?

Also, I don’t see a density parameter in that function.

Yes, that’s the case. I realize it might not do everything you need… for example, you won’t be able to dynamically change a body’s density in “mid-air” without recreating the body (however, you could adjust the gravity scale of that object which might accomplish a similar result).

But for things like friction and bounce, you can change them for collisions, which effectively does what you need, since there isn’t a true “air friction” principle in Box2D, nor can an object “bounce off air”. :slight_smile:

Brent

great, thanks for that. I’ll see what I can accomplish with this.

ok, so I played around with it and I found that the preCollision thing works great, but I can’t adjust the density. Adjusting the gravityScale doesn’t quite do the job because its a little unpredictable, and it still doesn’t increase the mass of the object

I’ll try to look at the reason why the body “lags” when you remove/add the body, and see if I can modify the function I posted earlier to make it work better. I am guessing that it skips a frame of movement or something, so maybe I can manually adjust the coordinates to mimic the movement.

So I decided to just add a small “transition” during the switch which hides the lag