How do you rotate a shape?

Hi,

If you are building a top down view scroller, and the player is a shape that is rectangular and so has narrow front and long sides, how do you handle rotating the physic shape to match the shape of the sprite? When the physics.setDrawMode(“hybrid”) is enabled, the physic shape stays in the original shape direction, and yet the sprite is rotating as directed via the .rotation property ??

Eventually I see cases where the collision shape won’t be accurate when the sprite has rotated visually and yet the physic shape has retained the original angle it was defined with.

How are folks handling this situation? Google doesn’t turn up much at all??

G

Hi @gslender,

How are you rotating the object, by a transition or updating its “.rotation” property? Or, are you animating a sprite where just the frames rotate while the object stays at rotation of 0?

In the first case, the physics body will (should) rotate along with the shape. But for a sprite, where the frames rotate but the object itself stays at rotation of 0, you’d have to sync up the animation frames somehow (and it could get complex). If you can use the first option, that is ideal… and if you are, but the object shape in “hybrid” doesn’t seem to be rotating, then there’s probably something off in your code, which I’d need to inspect to see what’s going on.

Best regards,

Brent

Right you are…!!

I had a re-look at my code and was adding the shape after I had rotated it the first time, and so it was always out by 90 deg. I see now that I must add the body to the physics engine and then set the initial rotation of the sprite.

Thanks for replying.

G

Hi @gslender,

How are you rotating the object, by a transition or updating its “.rotation” property? Or, are you animating a sprite where just the frames rotate while the object stays at rotation of 0?

In the first case, the physics body will (should) rotate along with the shape. But for a sprite, where the frames rotate but the object itself stays at rotation of 0, you’d have to sync up the animation frames somehow (and it could get complex). If you can use the first option, that is ideal… and if you are, but the object shape in “hybrid” doesn’t seem to be rotating, then there’s probably something off in your code, which I’d need to inspect to see what’s going on.

Best regards,

Brent

Right you are…!!

I had a re-look at my code and was adding the shape after I had rotated it the first time, and so it was always out by 90 deg. I see now that I must add the body to the physics engine and then set the initial rotation of the sprite.

Thanks for replying.

G