- Home /
How to rotate a ship based on gravity around a planet?
Hey everyone, here's a youtube link to my problem. I'm trying to get my ship to rotate with the gravity pull so that it can go into a more orbit-like motion. If I need to supply more info please let me know. Here are a couple of the scripts on my ship with a couple of my ideas commented out.
using UnityEngine;
using System.Collections;
public class PlayerControllerForce : MonoBehaviour {
public float speed;
public float maxSpeed;
//public static float angleFacing;
//public Vector3[] positions = new Vector3[2];
//private Vector3 lastPosition;
private Rigidbody playerRB;
//private int frameNumber = 0;
//private positions : LinkedList.<Vector3> = new LinkedList.<Vector3>();
// Use this for initialization
void Start () {
playerRB = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void FixedUpdate () {
//forward movement and limiting top speed
playerRB.AddForce (transform.forward * speed);
if (playerRB.velocity.magnitude > maxSpeed) {
playerRB.velocity = playerRB.velocity.normalized * maxSpeed;//returning a vector with the value of 1 and multiplying by maxSpeed
}
//Vector3 currentPosition = playerRB.transform.position;
//if (frameNumber % 2 == 0) {
// lastPosition = playerRB.transform.position;
//}
//for (int i = 0; i <= 1; i++) {
// positions[i] = playerRB.transform.position;
//}
//Debug.Log ("lp is " + lastPosition);
//Debug.Log ("p is " + playerRB.transform.position);
//frameNumber ++;
//angleFacing = Mathf.Atan2 ((playerRB.transform.position.z - lastPosition.z), playerRB.transform.position.x - lastPosition.x);
//if (angleFacing != 0) {
// playerRB.rotation = Quaternion.Euler (0.0f, angleFacing + 90, 0.0f);
//}
//Debug.Log ("angle is " + angleFacing);
}
}
AND this one for all of my user input and rotation.
using UnityEngine;
using System.Collections;
public static class ExtensionMethods {
public static float Remap (this float value, float from1, float to1, float from2, float to2) {
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
}
public class Rotate_Script : MonoBehaviour {
public static float direction = 90.0f;
public float countdown = 3.0f;
public GameObject scaleGUI; //rotation cube GUI
private Rigidbody playerRB;
private float scaleRot = 1.0f; //value holder for scale ratio
private bool empty = false;
// Use this for initialization
void Start () {
playerRB = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void FixedUpdate () {
if (scaleRot > 0 && empty == false){
//Angle adjustments
if (Input.GetKey ("up") && countdown > 0.0f) {
direction -= 2.0f;
countdown -= Time.deltaTime;
scaleRot = countdown.Remap (0.0f, 3.0f, 0.0f, 1.0f);
scaleGUI.transform.localScale = new Vector3 (1.0f, scaleRot, 1.0f);
} else if (Input.GetKey ("down") && countdown > 0.0f) {
direction += 2.0f;
countdown -= Time.deltaTime;
scaleRot = countdown.Remap (0.0f, 3.0f, 0.0f, 1.0f);
scaleGUI.transform.localScale = new Vector3 (1.0f, scaleRot, 1.0f);
}
} else {
empty = true;
scaleRot += 0.1f * Time.deltaTime;
scaleGUI.transform.localScale = new Vector3 (1.0f, scaleRot, 1.0f);
}
//GUI Code maybe move this later
if (scaleRot >= 1.0f) {
countdown = 3.0f;
empty = false;
}
playerRB.rotation = Quaternion.Euler (0.0f, direction, 0.0f);
//Debug.Log (oldPosition.x);
//Debug.Log (playerRB.position.x);
}
}
Answer by AlwaysSunny · May 12, 2015 at 10:15 PM
This is not a physically sound way to cap a velocity (following your video). The "best" way involves a creating a "soft" cap that limits forces that would result in exceeding a soft threshold. A hard threshold can create weirdness, but may serve as a (potentially problematic) insurance policy against crazy big forces.
Generally any time you write over (as distinct from "change", which is better but still affects physics purity) a body's velocity, you should not expect it to behave accurately in all conditions or play well with others except in very special cases. You can use the Dot Product of (velocity DOT requested_acceleration) to measure whether adding a force will exceed a soft threshold.
Regarding getting an orbit thing going, you should be aware that creating a stable orbit involves some rather complex thought. From what I'm seeing about the game and your request, I think your best bet is to fudge it. As opposed to using the gravitational formula and finding the proper angle of entry, etc, etc. That's literally rocket science.
Given a body at rest, there are two helpful formulas I know. One is the famous gravitational formula of classical physics, and the other is the force to apply to achieve a stable orbit. I do not know how to do this when the body in question is a vehicle capable of moving under its own power; that introduces a whole new set of problems with which I'm not familiar.
Think game science! How can you turn what you know into what you need in the simplest terms? You do not appear to need a true gravitational simulation to achieve orbit or "orbit like" behavior. A nearby "heavy" object might affect a lighter body like your ship by looking at whether a clockwise or counter-clockwise path would produce movement around the heavy body, and applying some force, scaled by relative distance, to achieve that kind of motion. It's definitely an "arcade style" physics approach. This is just a thought, though. There is surely a nice tidy way to achieve this. You've picked an deceptively more difficult task than you realize.