'How can I rotate a vector (centripetal force)?

I'm trying to implement centripetal force in a programming language.

I saw some videos teaching the theory. But I dont know how to apply that in a programming language.

If I understand I have to apply centripetal force ac = v²/r to the velocity vector. But I dont know exactly how to proceed.

I have two game objects, one depicting Earth, other depicting Moon. What I wanted is to translate the moon around earth and using a button to "cut/cancel" the centripetal force in order to the moon get out to the earth's orbit.

I have no clue how to start that.

All I know is to rotate like this:

velocity.x = Mathf.Cos(Time.time) * earth_moon_radius;
velocity.z = Mathf.Sin(Time.time) * earth_moon_radius;
moon.transform.position = velocity;

But how to apply centripetal force as described above?



Solution 1:[1]

If you just want to have the moon rotating around earth and some trigger to release the moon, it's easier to use rotation around a center instead of forces. Given the following GameObject hierarchy:

Center (MoonRotator attached)
-- Moon
-- Earth

public class MoonRotator : MonoBehaviour
{
    public bool cancelCentripetalForce = false;
    public Vector3 angularVelocity = new Vector3 (0f, 0f, 100f);

    public GameObject moon;

    void Update () {
        if (cancelCentripetalForce) {
            Vector3 radius = moon.transform.position - transform.position;
            Vector3 angularVelocityRadians = Mathf.Deg2Rad * angularVelocity;
            moon.rigidbody.velocity = Vector3.Cross (angularVelocityRadians, radius);
            moon.transform.parent = null;
            Destroy (this);
        } else {
            Vector3 rot = transform.rotation.eulerAngles + angularVelocity * Time.deltaTime;
            transform.rotation = Quaternion.Euler (rot);
        }
    }
}

If cancelCentripetalForce is set true Moon stops travelling around earth but proceeds with its current tangential velocity. This is given as:
v = ? × r

Earth has localPosition (0, 0, 0) and Moon is in this example located in the x-y plane rotating around the z axis.

Solution 2:[2]

If you want to cancel the force, add an opposing force vector that is based on your object's linear velocity and current direction.

So I have an object pointing straight along the Z axis. The object's 'forward' vector is 0, 0, 1. Do 1 - Math.abs(forward.x), same for y and z to get 1, 1, 0 when you're pointing forward along the Z axis. You want the direction you're pointing in to be 0'd so that the inertia from that direction is not damped in any way. Now you can apply a cancellation force in any direction that you are NOT pointed in.

This means that if your object is moving in any direction in world space that it's not facing in you can easily apply a cancellation force that is multiplied by the object's linear velocity to get circular motion that uses forces instead of directly setting velocity.

Another way you can do it is solve for V by manually setting it, then find radius using V=RW. You should know V and R or W. Now you can find the force necessary to keep the orbit stable around a point, rather than adding inertia from every frame.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kay
Solution 2 Luddens Desir