Natural rotation of orbiting object

Hi

I’m working on a scene, where satellite is orbiting Earth.

Because engine is not emulating point gravity I write my own. It looks like this:
At start I set velocity of rigidbody satellite component.
Next, in fixed update function, I’m calculating gravity vector, doing some math on it, and applying as a force to the rigidbody.

Looks good but it is not rotating (case 1).

alt text

It should look like case 2. It can’t be animation, because satellite must dynamically react to future collisions.

Of course I cannot use rotation in transform component. Another problem is, that angular velocity is not constant (elliptic orbit).

Do you have any ideas how this can be done ? Or maybe I’m not on a right track with my gravity emulation ?

Thanks,
Kuba

I agree to @syclamoth: I don’t think rotation is natural - the satellite just conserves the original angular velocity it had when entered the orbit, unless some asymmetry in its mass distribution make it orient one face to the planet after some time.

You can apply the gravity to the point you want to face the planet, what would simulate a gravity center: use AddForceAtPosition to apply the force at some point. The position is specified in world coordinates, thus you must use TransformPoint to convert the point:

// define the point relative to the satellite center
// that should face the Earth:
var gravityCenter: Vector3 = Vector3(2, 0, 0);

function FixedUpdate(){
    // calculate the gravity force 
    // as before and apply it at the point:
    var point = transform.TransformPoint(gravityCenter);
    rigidbody.AddForceAtPosition(gForce, point);
}

NOTE: Set rigidbody.angularDrag to a higher value (1.5 or above) to kill initial oscillations quickly.

Description

The torque applied to the rigidbody every frame.

// Rotates the object around the world y-axis

constantForce.torque = Vector3.up * 2;

Forewarning: This isn’t perfectly physically accurate.

I would use a configurable joint. GASP. Yes, configurable joints are quite scary given how many variables there are, but most of the time you won’t need more than a few of them. Attach a configurable joint and leave all the variables in their initial states. The code will change the variables that you need.

Now, time for a picture and some simple trig:

That explains the math I’m doing, now here’s the code for it:

using UnityEngine;
using System.Collections;

public class Satalite : MonoBehaviour {
	
	private ConfigurableJoint joint;
	
	public float rotationalTorque = 1;
    //How strong is the rotational force.
	public Transform orbitingBody;

	// Use this for initialization
	void Start () {
		joint = GetComponent<ConfigurableJoint>();
		var rotationDriver = joint.angularYZDrive;
        //tell Unity which rotation mode to use.
		rotationDriver.mode = JointDriveMode.Position;
        //We want to reach a specific rotation, not a velocity.
		rotationDriver.positionSpring = rotationalTorque;
		joint.angularYZDrive = rotationDriver;
        //reassign the joint.
	
	}
	
	// Update is called once per frame
	void Update () {
		var relativePos = transform.position - orbitingBody.position;
		relativePos = relativePos.normalized;
		
		var theta = Mathf.Acos(relativePos.x) * Mathf.Rad2Deg;
		if(relativePos.y < 0)
			theta = 360 - theta;
		
		var rotation = Quaternion.Euler(0 , 0 , 180 - theta);
            //The axis you use will depend on how your object is oriented. You might need to play around with this a little.
		joint.targetRotation = rotation;
		
	}
}

This causes the object to face towards the center of the planet. If it gets hit, it will sway back and forth trying to realign itself. I have no idea what unit the torque (spring force) is closest to so its tricky figuring out how much to apply.

The only issue I have found with using configurable joints for rotation is that they don’t like to have their initial rotation be anything but zeroes. You could fix that by adding in the initial rotation to the final rotation, but I didn’t here for simplicity’s sake.