Attaching physics body to a changing image

I have a simple problem, I am not sure how to resolve and need help.

I have an animal moving in the screen, and it need to have a physics body. The image of the animal changes - it has states of moving, eating, etc - so I cannot attach the physics to its image/animation. I was wondering what is the best way to handle this?

Hi @arashbi,

This can be a little tricky, but it’s solvable. One of these methods may work for you:

  1. [SIMPLE] When the animal frame changes, remove the physics body and add a new one, based on the new frame (shape). This method is easier, however if you’re doing a very fast animation sequence, it may reduce performance to continually remove and add new bodies.

  2. [COMPLEX] Build every possible shape as a separate fixture into one physics body, then selectively “ignore” all of the shapes that are not active by using pre-collision and the PhysicsContact property. This would likely be faster and better for performance, but it’s more intensive on the setup side, and also it requires special handling of collisions.

Take care,

Brent

I thought of the first one. I got worried about the garbage it might produce, beside the performance. Isn’t every physics body produced ?

I will try the second approach.

BTW, is it possible to create a pseudo  object and attach a physics body to it?

Hi @arashbi,

An object can only have one physics body attached at one time (or a multi-element physics body). So, you’d have to directly remove the previous one before adding a new one.

Here’s a tutorial on working with multi-element bodies and the physics contact property:

http://coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Best regards,

Brent

Hi @arashbi,

This can be a little tricky, but it’s solvable. One of these methods may work for you:

  1. [SIMPLE] When the animal frame changes, remove the physics body and add a new one, based on the new frame (shape). This method is easier, however if you’re doing a very fast animation sequence, it may reduce performance to continually remove and add new bodies.

  2. [COMPLEX] Build every possible shape as a separate fixture into one physics body, then selectively “ignore” all of the shapes that are not active by using pre-collision and the PhysicsContact property. This would likely be faster and better for performance, but it’s more intensive on the setup side, and also it requires special handling of collisions.

Take care,

Brent

I thought of the first one. I got worried about the garbage it might produce, beside the performance. Isn’t every physics body produced ?

I will try the second approach.

BTW, is it possible to create a pseudo  object and attach a physics body to it?

Hi @arashbi,

An object can only have one physics body attached at one time (or a multi-element physics body). So, you’d have to directly remove the previous one before adding a new one.

Here’s a tutorial on working with multi-element bodies and the physics contact property:

http://coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Best regards,

Brent