How to create circular movement / motion for a GameObject with Arcade Physics?

How can I create a circular movement with physics using velocity for a GameObject?

I want a rotating platform that can be interacted with the player, I also want to instantiate this platform passing a radius and speed to it to make the rotation bigger/smaller - faster/slower.

I watched some YouTube tutorials and read some stuff online about circular motion and I was able to KINDA of do it, check the gif below (or here: https://imgur.com/a/kAeV1BK):

The code for that is:

const increment = Math.PI / 36;
let degree = 0;
platform.update = (time, delta) => {
    // increment the degree
    degree += increment;

    // calculate velocityY and velocityX
    const velocityX = Math.cos(degree);
    const velocityY = Math.sin(degree);

    // set the body velocity
    platform.body.setVelocity(
        velocityX * 120,
        velocityY * 120
    );
};

The thing is, I have no control over the speed and size of the circle, that’s because I have almost no clue of what I did :sweat_smile:

Does anyone knows how to properly do it?

Thank you!

1 Like

Nice, thanks for sharing this. Any way to make it without rotating the ship? I want the platform to move in a circular path, but not rotate. Like the ones in Super Mario World

Ok I managed to do it by using a dummy object with rotation, but then setting the velocityFromRotation into another object. Like:

this.physics.velocityFromRotation(
    PhaserMath.DegToRad(dummyRotation.body.rotation),
    300,
    platform.body.velocity
);

But is this the best way to do it?

Also, how do I keep the same speed, but change the “rotation radius”?

You could tween a rotation variable or property and then do velocityFromRotation() from that.

Or you may want to use a path follower:

1 Like

Yeeees! This is EXACTLY what I wanted. Thanks :raised_hands: