Is it possible to change dynamically an object shape?

Hi everybody!:slight_smile:

I’m here with another noob question :rolleyes:

I’ve been searching in the documentation and on the internet but I didn’t find any answer.

My problem is I want to build an object that changes his scale values dynamically during the game, swinging from a smaller to a bigger dimension and the other way around.

The problem is, can I change the radius property of the physical body dinamycally? Or do I have to remove and re-build the physical body any frame, like someone suggested in another post? (this sounds like a bad solution to me)

Example:

local object = display.newImageRect(group, "image.png", 100, 100); physics.addBody(object, {radius = 100, ecc...}); -- transition.to(object, {xScale = 1.8, yScale = 1.8, transition = easing.continuousLoop, time = ...}); --the radius property is still 100... how do I change it?

From Corona documentation:

.

You can scale the display object up or down, but the physics engine will still consider the object as it was before the applied scale values. Essentially, it will visually reflect the applied scale values, but the collision bounds of the body will remain the same as the original body definition.

ldurniat

Yeah, I’ve read this, but my question was if there’s some function to change the shape of a physics object without removing and re-adding the physic body.

From your answer I guess there’s not.

Nope.  You cannot change the shape of a physics body.

But you can move bodies and you can create polygonal bodies so you can invent some workarounds.

In example you can create sphere from many poligons using sine/cos functions (for each «tile» use x=SphereCenterX+sin(Angle)*radius, y=SphereCenterY+cos(Angle)*radius, repeat for each «tile» incrementing angle with some step)

After creation you can control rotation of this whole assembly and postion. 

When you need to rescale shape you can just add few polygons (which were behind the screen, in the storeroom) and increase the radius property of this assembled shape.

Otherwords you CAN’T change shape of ONE rigid body, but you don’t have to stop on this. You CAN bring to life your idea. All you need is to learn API, possibilities and, of course, your imagination. It’s just a bit more complicated than simple.

This is a popular question on forum, if I will have some time, I will do few example/s to show approach.

The idea itself is as old as the world and has been used on consoles where restrictions were Spartan

You’re right, this is a possible solution.

I’m actualy not sure about what you mean by adding polygons that where behind the screen, in the storeroom. I thought that once you add one or more shapes to an object, they can’t be moved, but I actually need to read better the physics API.

Thanks for the answer anyway.

with “real box2d” what you’d do is create multiple fixtures for your body, one for each radius you need, then set all-but-one of them to be sensors = easy-peasy.  unfortunately corona doesn’t provide access to the fixtures once created in order to individually set them as sensors (after creation, isSensor applies to all fixtures of the body).  you could kinda-sorta do something equivalent with multiple bodies, with weld joints to hold them together, and similarly setting just one as non-sensor, but it’ll likely have stability issues that the multi-fixture solution wouldn’t have.

Why are you thinking that I said you need to weld them somehow together using ONLY engine api?

You CAN control coordinates and rotation of rigid body even with applied gravity or forces. So you can USE this!

And do NOT think you can move bodies only using transitions. As many geeks said physics engine calculations will counteract with transition calculations, so you need to use your own controllers instead of transitions, this is important.

Remember, this is not the best and usual way, use in non-standart situations. (can read one at the end of this post)

For example, at rubbish dump level you need to create a boss monster. Best example will be a big transformer assembled from pieces of garbage. each piece will be a single independent rigid body with gravity scale set to ZERO (!) so you will be able to APPLY FORCES, but easily KEEP THEM ASSEMBLED TOGETGHER, all forces while counteracting will balance each other so the final result will be equivalent to summary mass like body is whole, or THE SECOND WAY you can use STATIC bodies, while they can react on collisions and other irritants and you can move/rotate them

So if you need to write your own controller, lets check, maybe we have some advantages:

  1. On each serious shot/impact you can tear out and throw away a piece of monster «body» or you can add another one, or your boss will be able to pickup and cling a piece to it’s body

  2. You can use skeletal animation - one piece of garbage will be a hand, another wil be a mace/club and so on

  3. You can bind dependant chains/ropes to each independent piece to represent hair/stripes/tentacles/fallen out internal organs/cannons and so on. (also you can bind particle emitters!)

  4. You CAN/MUST apply gravity and forces to bound attachments from above p 3.

  5. You can easily simulate gravity, jumps and complicated trajectories using simple sine/cosine trigonometric functions, in example sine( from zero to 180 degree) to simulate jump

  6. Each piece can react on force independent from others while you keep it near, such air-swimming effect swaying in the wind.

But the most important thing: you have an outline of your «big» body. On the borders you have first set of rigid line bodies (invisible) closely each near other (very closely, as closely as better collision precision you need). Under this first set you have another one set of lines (or somwhere behind the screen, in some place, doesn’t matter, you can put them to right place anytime you need). When you need to scale up your «big» body, you just pushing apart each from another lines from first set to give place for second set. Doing so you have good collision contour any time and with any size.

combined.png

You can easily control this array of lines by translating them all or rotating relatively to the center of big body. 

Of course this is very simple exlpanation. In each situation you need to invent your own way, but I think you understood basic idea.

I’ll give you another example: you are shooting bullets in a boss and this boss grows in size with each bullet hit and shrinks back on shooting delay. And you need to make an animation where this boss is scattered in parts and restored again on some condition. And you need this boss to react on all physics. How will you realize this using ONLY standart physic approach? 

Now I have a lot of work, I’m very tired, maybe I have badly expressed, if I will have more time I will do an example later.

From Corona documentation:

.

You can scale the display object up or down, but the physics engine will still consider the object as it was before the applied scale values. Essentially, it will visually reflect the applied scale values, but the collision bounds of the body will remain the same as the original body definition.

ldurniat

Yeah, I’ve read this, but my question was if there’s some function to change the shape of a physics object without removing and re-adding the physic body.

From your answer I guess there’s not.

Nope.  You cannot change the shape of a physics body.

But you can move bodies and you can create polygonal bodies so you can invent some workarounds.

In example you can create sphere from many poligons using sine/cos functions (for each «tile» use x=SphereCenterX+sin(Angle)*radius, y=SphereCenterY+cos(Angle)*radius, repeat for each «tile» incrementing angle with some step)

After creation you can control rotation of this whole assembly and postion. 

When you need to rescale shape you can just add few polygons (which were behind the screen, in the storeroom) and increase the radius property of this assembled shape.

Otherwords you CAN’T change shape of ONE rigid body, but you don’t have to stop on this. You CAN bring to life your idea. All you need is to learn API, possibilities and, of course, your imagination. It’s just a bit more complicated than simple.

This is a popular question on forum, if I will have some time, I will do few example/s to show approach.

The idea itself is as old as the world and has been used on consoles where restrictions were Spartan

You’re right, this is a possible solution.

I’m actualy not sure about what you mean by adding polygons that where behind the screen, in the storeroom. I thought that once you add one or more shapes to an object, they can’t be moved, but I actually need to read better the physics API.

Thanks for the answer anyway.

with “real box2d” what you’d do is create multiple fixtures for your body, one for each radius you need, then set all-but-one of them to be sensors = easy-peasy.  unfortunately corona doesn’t provide access to the fixtures once created in order to individually set them as sensors (after creation, isSensor applies to all fixtures of the body).  you could kinda-sorta do something equivalent with multiple bodies, with weld joints to hold them together, and similarly setting just one as non-sensor, but it’ll likely have stability issues that the multi-fixture solution wouldn’t have.

Why are you thinking that I said you need to weld them somehow together using ONLY engine api?

You CAN control coordinates and rotation of rigid body even with applied gravity or forces. So you can USE this!

And do NOT think you can move bodies only using transitions. As many geeks said physics engine calculations will counteract with transition calculations, so you need to use your own controllers instead of transitions, this is important.

Remember, this is not the best and usual way, use in non-standart situations. (can read one at the end of this post)

For example, at rubbish dump level you need to create a boss monster. Best example will be a big transformer assembled from pieces of garbage. each piece will be a single independent rigid body with gravity scale set to ZERO (!) so you will be able to APPLY FORCES, but easily KEEP THEM ASSEMBLED TOGETGHER, all forces while counteracting will balance each other so the final result will be equivalent to summary mass like body is whole, or THE SECOND WAY you can use STATIC bodies, while they can react on collisions and other irritants and you can move/rotate them

So if you need to write your own controller, lets check, maybe we have some advantages:

  1. On each serious shot/impact you can tear out and throw away a piece of monster «body» or you can add another one, or your boss will be able to pickup and cling a piece to it’s body

  2. You can use skeletal animation - one piece of garbage will be a hand, another wil be a mace/club and so on

  3. You can bind dependant chains/ropes to each independent piece to represent hair/stripes/tentacles/fallen out internal organs/cannons and so on. (also you can bind particle emitters!)

  4. You CAN/MUST apply gravity and forces to bound attachments from above p 3.

  5. You can easily simulate gravity, jumps and complicated trajectories using simple sine/cosine trigonometric functions, in example sine( from zero to 180 degree) to simulate jump

  6. Each piece can react on force independent from others while you keep it near, such air-swimming effect swaying in the wind.

But the most important thing: you have an outline of your «big» body. On the borders you have first set of rigid line bodies (invisible) closely each near other (very closely, as closely as better collision precision you need). Under this first set you have another one set of lines (or somwhere behind the screen, in some place, doesn’t matter, you can put them to right place anytime you need). When you need to scale up your «big» body, you just pushing apart each from another lines from first set to give place for second set. Doing so you have good collision contour any time and with any size.

combined.png

You can easily control this array of lines by translating them all or rotating relatively to the center of big body. 

Of course this is very simple exlpanation. In each situation you need to invent your own way, but I think you understood basic idea.

I’ll give you another example: you are shooting bullets in a boss and this boss grows in size with each bullet hit and shrinks back on shooting delay. And you need to make an animation where this boss is scattered in parts and restored again on some condition. And you need this boss to react on all physics. How will you realize this using ONLY standart physic approach? 

Now I have a lot of work, I’m very tired, maybe I have badly expressed, if I will have more time I will do an example later.