Search Unity

[SOLVED] Space Ship Movement

Discussion in 'Scripting' started by BradMick, Oct 12, 2015.

  1. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Howdy all!

    Back again, this time I'm experimenting with some Physics as they apply to a Space Ship. So far, I've gotten the bulk of what I wanted to work, well...Working. I have one small hiccup though:

    First, the Space Ship object instantiates four Thruster Objects. I then do all the math to figure out the amount of torque to be applied to the rigid body center of mass. The idea is that the four (up to a maximum of 6) thrusters each balance each other out, since they're radially symetric (the idea here is that as a thruster is destroyed, maneuvering becomes more and more difficult due to differential thrust). Right now every single thruster seems to be acting through the same point which is the top most thruster of the four. It definitely rotates the ship, but with all four thrusters it should be pushed forward as all the thrusters act on the center of mass of the ship.

    So the question is: What did I do wrong? Do I need to treat each thruster individually? Or is the current setup sufficient to make things work with some tweaking?

    Here's my code, and also attached is the project.

    Any and all help is greatly appreciated!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Rigidbody))]
    5.  
    6. public class SpaceShip : MonoBehaviour
    7. {
    8.     public Rigidbody spaceShipRB;
    9.  
    10.     const int MAX_COMBAT_THRUSTERS = 6;
    11.     public int numberOfCombatThrusters = 4;
    12.  
    13.     private Thruster[] combatThruster = new Thruster[MAX_COMBAT_THRUSTERS];
    14.     public Transform[] combatThrusterPosition = new Transform[MAX_COMBAT_THRUSTERS];
    15.  
    16.     public Transform spaceShipCOM;
    17.     private float[] angle = new float[MAX_COMBAT_THRUSTERS];
    18.     private Vector3[] thrusterToCOMDistance = new Vector3[MAX_COMBAT_THRUSTERS];
    19.  
    20.     private float[] spaceShipTorque = new float[MAX_COMBAT_THRUSTERS];
    21.  
    22.     //Calculates the Angle from the Thruster to the COM of the Ship
    23.     public void CalculateThrusterToCOMAngle(int i)
    24.     {
    25.         Vector3 targetDirection = spaceShipCOM.position - combatThrusterPosition[i].position;
    26.         Vector3 forward = combatThrusterPosition[0].forward;
    27.  
    28.         angle[i] = Mathf.Deg2Rad * Vector3.Angle(targetDirection, forward);
    29.     }
    30.     //Returns the angle between the Thruster and the COM of the Ship
    31.     public float GetThrusterToCOMAngle(int i)
    32.     {
    33.         return angle[i];
    34.     }
    35.  
    36.  
    37.     //Calculates the Distance from the Thruster to the Ship COM
    38.     public void CalculateThrusterToCOMDistance(int i)
    39.     {
    40.         thrusterToCOMDistance[i] = combatThrusterPosition[i].position - spaceShipCOM.position;
    41.     }
    42.     //Returns the distance between the Thruster and the COM of the Ship
    43.     public float GetThrusterToCOMDistance(int i)
    44.     {
    45.         return thrusterToCOMDistance[i].magnitude;
    46.     }
    47.    
    48.  
    49.     //Calculate the Torque applied to the Spaceship COM
    50.     public void CalculateTorque(int i)
    51.     {
    52.         spaceShipTorque[i] = thrusterToCOMDistance[i].magnitude * combatThruster[i].GetThrust() * Mathf.Sin(GetThrusterToCOMAngle(i));
    53.     }
    54.     //Returns the Torque applied to the Spaceship COM
    55.     public float GetTorque(int i)
    56.     {
    57.         return spaceShipTorque[i];
    58.     }
    59.     //Applies the Calculated Torque values of the Thrusters to the RigidBody
    60.     public void ApplyTorque(int i)
    61.     {
    62.         spaceShipRB.centerOfMass = spaceShipCOM.position;
    63.         spaceShipRB.AddTorque(spaceShipCOM.transform.right * spaceShipTorque[i] * Time.deltaTime);
    64.     }
    65.    
    66.     void Awake()
    67.     {
    68.         spaceShipRB = GetComponent<Rigidbody>();
    69.  
    70.         for (int i = 0; i < numberOfCombatThrusters; i++)
    71.         {
    72.             combatThruster[i] = (Thruster)Instantiate(Resources.Load("Thruster", typeof(Thruster)), combatThrusterPosition[i].position, Quaternion.identity);
    73.             CalculateThrusterToCOMAngle(i);
    74.             CalculateThrusterToCOMDistance(i);
    75.             CalculateTorque(i);
    76.         }
    77.     }
    78.  
    79.     void Start()
    80.     {
    81.  
    82.     }
    83.  
    84.     void FixedUpdate()
    85.     {
    86.         for (int i = 0; i < numberOfCombatThrusters; i++)
    87.         {
    88.             ApplyTorque(i);
    89.             combatThruster[i].transform.position = combatThrusterPosition[i].position;
    90.             combatThruster[i].transform.rotation = combatThrusterPosition[i].rotation;
    91.         }
    92.     }
    93.  
    94. }
    95.  
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'll just leave this here...
     
  3. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Hahah...With as much as I've read the API you'd think I would have caught that. Oh well. I'll plug that in and see what happens.
     
  4. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Hmm...Well, I got the ApplyForceAtPosition working...but there's no torque effect when there's only 1 thruster on the ship. If I have 1 thruster I should get a rotation about the COM in one axis, 2 thrusters is no rotation, 3 would give rotations in 2 axes and 4 would result in no rotation again. So...sort of there with ApplyForceAtPosition
     
  5. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Solved it with this:

    spaceShipRB.AddForceAtPosition(new Vector3(0,0,spaceShipTorque), combatThrusterPosition.position);

    Thanks for the assist!

    V/R

    Brad
     
  6. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Spoke to soon...So, the behavior works...sort of. The ship starts rotating about the center of mass, and then goes flying off in a crazy direction. Here's an updated package for review. I'm sure it's something small that i'm missing...any help is greatly appreciated!

    V/R

    Brad
     

    Attached Files:

  7. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    And solved it. I stripped everything down to the bare essentials. Experimented with some simple Vectors only and voila. Worked like a champ. Going strictly off the API and reading/understanding saved the day here. Thanks for the point in the right direction, this one is solved!
     
    StarManta likes this.