Reposition Physics Body

Hi all,

I have a rectangular image object which has a physics body. I am rotating the object around a custom reference point by setting the object.xReference. Obviously I need to rotate the physics body with it which I know is a known issue in corona.

I’ve tried removing the body and adding a new one but the body seems to always rotate around the center point. I’ve applied a torque to the physics body to get it to rotate to the angle I need, but now need to re-position the physics body’s x and y positions without moving the image aswell.

Any ideas?

Michael [import]uid: 156990 topic_id: 32008 reply_id: 332008[/import]

What is it you are trying to do? Rotating an image around a given point is one thing, but rotating a physics object around a given point is another.

It’s more a matter of how you think about the problem, rather than the problem itself.

Rotating an image around a point is simply a matter of where the rotation centre is. In you case, it is the given reference point. I assume you have code which does this already.

Rotating an object around a given point is about making sure that that point is either the object’s centre of gravity or providing a counter weight of some sort. This is because the Box2D engine attempts to emulate real world objects, at least as far as 2D will let it.

So, if you are sure that you want your physics object to be the same size and shape as the image you are applying it to, you’ve got choices if you, lets say, want it to rotate around it’s top left corner…

You could simply create a pivot joint at that location and another, static, physics object. Then use a motor on the pivot joint to rotate the object. This is my preferred solution.

If however, you don’t want the object fixed in place, you can’t use a joint. In this case you will literally need to provide a counter weight object. The counter weight will need to be on the opposite side of the where you are rotating the image/object around. This will involve a bit of collision mask trickery, because I’m assuming you don’t want other objects in your game/app to bounce off the counter weight - it’ll be invisible, of course. You will then need to use a weld joint to attach it to your original image/object and then work out what the rotational force would be to make your pair of objects rotate. Apply that force at the correct position and they should do a nice ballet around each other.

The third option sits in between the first two, as far as complexity goes. You create a physics shape for your image and instead of shaping it around the image, you shape it off-centre, so that when you apply rotational force it simply rotates around the physics object’s centre, but the image itself is not visually centred there. This presents the problem that the image would rotate as you want, but other objects would go through the portions of it which do not intersect the physics shape.

This last option is most easily achieved by creating a display group, giving it a physics body with the shape you want and then adding an image to the group. The image would not sit at 0,0.

Ultimately, if what you are trying to achieve is literally the effect of, for example, an oddly shaped space ship adrift in space, with its centre of gravity near, say, the engines, and spinning gently but strangely, then you could just add your physics object as normal, to your current image, and then weld another physics object somewhere inside the object. The welded object would have to have a density value much higher than the first object. When it spins it would cause the natural centre of gravity to be off-centre from the middle of the object and give it a weird spin.

TL;DR: The easiest way is to use a pivot joint at the location you want to spin your object, but if you want the effect of an off-set centre of gravity within your object, weld another (much heavier) object off-centre inside your first object. [import]uid: 8271 topic_id: 32008 reply_id: 127612[/import]

Thanks for your reply Horacebury. I was hoping there would be an easier way to rotate the physics object around a custom reference point.

I guess a Weld Joint will have to do.

Thanks again,

Michael [import]uid: 156990 topic_id: 32008 reply_id: 127697[/import]

No, a weld joint will hold the object fixed in place. Use a pivot joint. That will let it rotate around the joint axis freely. [import]uid: 8271 topic_id: 32008 reply_id: 127703[/import]

What is it you are trying to do? Rotating an image around a given point is one thing, but rotating a physics object around a given point is another.

It’s more a matter of how you think about the problem, rather than the problem itself.

Rotating an image around a point is simply a matter of where the rotation centre is. In you case, it is the given reference point. I assume you have code which does this already.

Rotating an object around a given point is about making sure that that point is either the object’s centre of gravity or providing a counter weight of some sort. This is because the Box2D engine attempts to emulate real world objects, at least as far as 2D will let it.

So, if you are sure that you want your physics object to be the same size and shape as the image you are applying it to, you’ve got choices if you, lets say, want it to rotate around it’s top left corner…

You could simply create a pivot joint at that location and another, static, physics object. Then use a motor on the pivot joint to rotate the object. This is my preferred solution.

If however, you don’t want the object fixed in place, you can’t use a joint. In this case you will literally need to provide a counter weight object. The counter weight will need to be on the opposite side of the where you are rotating the image/object around. This will involve a bit of collision mask trickery, because I’m assuming you don’t want other objects in your game/app to bounce off the counter weight - it’ll be invisible, of course. You will then need to use a weld joint to attach it to your original image/object and then work out what the rotational force would be to make your pair of objects rotate. Apply that force at the correct position and they should do a nice ballet around each other.

The third option sits in between the first two, as far as complexity goes. You create a physics shape for your image and instead of shaping it around the image, you shape it off-centre, so that when you apply rotational force it simply rotates around the physics object’s centre, but the image itself is not visually centred there. This presents the problem that the image would rotate as you want, but other objects would go through the portions of it which do not intersect the physics shape.

This last option is most easily achieved by creating a display group, giving it a physics body with the shape you want and then adding an image to the group. The image would not sit at 0,0.

Ultimately, if what you are trying to achieve is literally the effect of, for example, an oddly shaped space ship adrift in space, with its centre of gravity near, say, the engines, and spinning gently but strangely, then you could just add your physics object as normal, to your current image, and then weld another physics object somewhere inside the object. The welded object would have to have a density value much higher than the first object. When it spins it would cause the natural centre of gravity to be off-centre from the middle of the object and give it a weird spin.

TL;DR: The easiest way is to use a pivot joint at the location you want to spin your object, but if you want the effect of an off-set centre of gravity within your object, weld another (much heavier) object off-centre inside your first object. [import]uid: 8271 topic_id: 32008 reply_id: 127612[/import]

Thanks for your reply Horacebury. I was hoping there would be an easier way to rotate the physics object around a custom reference point.

I guess a Weld Joint will have to do.

Thanks again,

Michael [import]uid: 156990 topic_id: 32008 reply_id: 127697[/import]

No, a weld joint will hold the object fixed in place. Use a pivot joint. That will let it rotate around the joint axis freely. [import]uid: 8271 topic_id: 32008 reply_id: 127703[/import]