- Home /
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).
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
To be fair, orbiting satellites will act like picture 1 without outside interference... If you want to fake picture 2, just make it look at the planet, but I don't think you want that.
Basically, picture 1 is more realistic anyway, so it's not your physics model which is at fault, it's your expectations!
I must disagree: http://www.youtube.com/watch?v=54$$anonymous$$SV2B399o&feature=player_detailpage#t=104s
Think about emulating 2 points, like both ends of this satellite. Rotation is natural.
EDIT: I found wiki article about that: http://en.wikipedia.org/wiki/Synchronous_rotation
It may not be natural, but most satellites need to face the earth. Having a communications satellite point out into space wouldn't do much good except to talk to aliens.
If an object enters the gravity field of a planet naturally and without any previous rotation, it may stabilize in a sync orbit - but simulating this by script probably will not give the same result. Physx just emulates real world physics, and many natural phenomena simply are ignored.
Answer by aldonaletto · Nov 06, 2011 at 08:34 PM
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.
Answer by Peter G · Nov 06, 2011 at 08:42 PM
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:
http://imageshack.us/photo/my-images/718/satalliteexplanation.png/
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.
Answer by gundream · Nov 06, 2011 at 06:46 PM
Description
The torque applied to the rigidbody every frame.
// Rotates the object around the world y-axis
constantForce.torque = Vector3.up * 2;