[Solved] Group of rigidbodies insist on moving sideways when applying force

Hello everyone!

I have 3 gameobjects with rigidbody properties. Those rigidbodies are:

  • Engine_structure
  • Intake (that is
    connected to engine_structure)
  • Thruster (that is also connected to
    engine_structure)

The engine_structure sits in the middle where thruster is on the back and intake in the front of the group and all three are connected with fixed joints.

When i apply RelativeTorque to Engine_structure everything works as intented. But when i apply AddForce on the back of the thruster relative to the engine_structure the group of rigidbodies goes sideways and rolls all together.

My first thought was to have a look at the center of mass of all rigidbodies so i wrote a script on the editor to check where is the center of mass:

#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

[CanEditMultipleObjects]
[CustomEditor(typeof(Rigidbody))]
public class rigidbodyeditor : Editor
{
    public bool enable_rigidbody_com_calculation = true;

    void OnSceneGUI()
    {
        if (GUI.Button(new Rect(0,0,60,20), "Center of mass")) 
        {
            if (enable_rigidbody_com_calculation == true)
                enable_rigidbody_com_calculation = false;
            if (enable_rigidbody_com_calculation == false)
                enable_rigidbody_com_calculation = true;
        }
        if (enable_rigidbody_com_calculation == true)
        {
            Rigidbody rb = target as Rigidbody;
            Handles.color = Color.red;
            //Handles.SphereCap(1, rb.transform.TransformPoint(rb.centerOfMass), rb.rotation, 1f);

            List<Rigidbody> rigids = new List<Rigidbody>();

            foreach (Transform l_rb in rb.gameObject.GetComponentsInChildren(typeof(Transform)))
            {
                if (l_rb.GetComponent<Rigidbody>() != null)
                {
                    rigids.Add(l_rb.rigidbody);
                }
            }

            Rigidbody[] rigid_array = rigids.ToArray();
            Vector3 CoM = Vector3.zero;
            float x = 0;
            float y = 0;
            float z = 0;

            foreach (Rigidbody rigid in rigid_array)
            {
                x += rigid.worldCenterOfMass.x;
                y += rigid.worldCenterOfMass.y;
                z += rigid.worldCenterOfMass.z;
            }

            CoM = new Vector3(x / rigid_array.Length, y / rigid_array.Length, z / rigid_array.Length);

            Handles.SphereCap(2, rb.transform.TransformPoint(CoM), rb.rotation, 2f);
        }
    }
    public override void OnInspectorGUI()
    {
        GUI.skin = EditorGUIUtility.GetBuiltinSkin(UnityEditor.EditorSkin.Inspector);
        DrawDefaultInspector();
    }
}

#endif

That code is intended to calculate and display on the scene window the centerofmass of all rigidbodies that are children of the selected gameobject with rigidbody attached. It does not work very well becuase there are some miscalculations but i have removed all children of the gameobjects and used joints to weld them. So the code works fine.

So when i finally wrote the code, killed all bugs and runned it i saw that the center of mass was not in the center of the objects. So i went at my original flight script and manipulated the center of mass of all the 3 rigidbodies to their center (Vector3.zero). I used Vector3.zero because reading the documentation [here][1] i took care of this note " Note: centerOfMass is relative to the transform’s position and rotation, but will not reflect the transform’s scale! " and assumed that vector3.zero would be relative to transform’s position of the gameobject.

here is the initialization function of the manipulation of the center of mass of all 3:

    public void initializeCenterOfMass()
    {
        for (int i = 0; i < Enum.GetNames(typeof(Thrusters)).Length; i++)
        {
            Thrusters*.Thruster.rigidbody.centerOfMass = Vector3.zero;*

}
for (int i = 0; i < Enum.GetNames(typeof(Intakes)).Length; i++)
{
Intakes*.Intake.rigidbody.centerOfMass = Vector3.zero;*
}
ship.rigidbody.centerOfMass = Vector3.zero;
}
By adding initializeCenterOfMass() to the Start function i tried to play the game, and not only it did not fix the problem, it also made it worse! Now all 3 are shaking with the thruster doing most of the work!
So, i disabled the function temporarily and focused on finding out what is messing with AddRelativeForce. I tried to play with drag and angular drag. I tried setting everything to 0. I also tried to do the opposite - set everything to a high enough value. I tried to play with angular drag of all 3 but nothing from what i tried worked.
Moving on, i said to myself "if it is a center of mass problem, gravity should be able to tell me through the force of attraction if the object would turn. So i went to physics manager and setted a value on gravity to see but once i tested it, the attraction was flawless. Like everything was balanced out. In case you want to see the other settings on physics manager here they are:
![alt text][2]
After all those fail attempts i tried playing with the objects separately. If i apply to thruster relative force it will again go sideways but if i apply global force it goes straight. I was somewhat happy about it and i tried to attach through fixed joint the intake it would still go straight line. Unfortunatelly that was not the case if i attached to that fixed joint the engine_structure. Besides, global force is not what i wanted.
With my final card on the game not working, I squeezed the whole Google to find if anyone had the same problem with me but it turns out that i am the first one.
So i am ending up cancelling the unwanted force of the object and i would like to ask you how would i do that. I will first create a function that that analyzes and gives feedback to the force. I am on the designing face of that function but my first thoughts on this is that the equation should include defenetly the user input of the mouse and keyboard so that there will be a reference of user’s command to add torque/rotate the object. That imaginary equation should also include the direction of both the engine.
With all that said, i’d be glad if someone can actually help me with finding a way to cancel the unwanted force. I would also like to say that even if i seem to be confident that this is the solution to my problem, i feel that it is the worst and most desperate. If you have other ideas for me to test let me know.
Thanks.
_[1]: http://docs.unity3d.com/ScriptReference/Rigidbody-centerOfMass.html*_
_
[2]: http://i.imgur.com/DEQp7Cp.png*_

Well after some frustrating attempts to create that imaginary algorithms that would in theory calculate the unwanted forces and remove them by applying negative force to all 3 i ACCIDENTALLY pressed the “z” when i had selected the thruster and i saw that the pivot point (the center of the engine) was not in the center! I immediatly stopped working on the frustrating algorithm and made a quick search on the internet when i found this video here.

So i went ahead, created empty gameobjects for all 3, put them inside the gameobjects and moved them to the center just like the tutorial on the video. After that, everything worked as intented. I am leaving this here in case other people have same issue they can have a look.