Skip to content

Idea: Set custom Center of Mass #6

@cloakedch

Description

@cloakedch

First of all, I wanted to thank you for publishing your great work! Thanks for providing it here on github.

Now, I have a scene in which I would like simulate the flight of a glider (without thrust). However, a lot of gliders have longer fuselages than i.e. the cessna provided in the example. Now when adding colliders for the fuselage and the tail it distorts the real center of mass. In my situation, the center of mass is behind the wings, so the glider tilts backwards during stall, which is not realistic.

I had the idea that one could provide a custom (local) center of mass vector to override the calculated CoM world vector. So in my situation I did something like this in AirplanePhysics.cs:

public bool useCustomCoM;
public Vector3 customCoM;

...

Vector3 com;
if (useCustomCoM)
{
   com = rb.transform.TransformVector(customCoM);
}
else
{
   com = rb.worldCenterOfMass;
}

// use com instead of rb.worldCenterOfMass

(This is just an example of how I tried to do it. Implementation would need to be tailored to the specific use).

However, I failed to update the gizmo drawing, since the custom CoM would also be used in AircraftGizmos.cs. Maybe someone has an idea how to solve that?

In addition, the input variable customCoM would probably be only needed when useCustomCoM is true, so one would need to create a custom unity editor script. This, however, overwrites the other fields.

Off Topic: I also wanted to ask: Is a downward elevator / tail force supported? It seems like the cessna example uses a positive lift for the tail). However, I learned that stable aircrafts use a negative lift (in the opposite direction of the wing lift), so that in case of a stall the aircraft tilts forward.

Edit:
I forgot that one could simply set rb.centerOfMass, so in effect, something like this should do the trick:

public bool useCustomCoM;
public Vector3 customCoM;

...

private void Awake()
 {
     rb = GetComponent<Rigidbody>();
     if (useCustomCoM)
     {
         rb.centerOfMass = customCoM;
     }
 }

...

#if UNITY_EDITOR
    // For gizmos drawing.
    public void CalculateCenterOfLift(out Vector3 center, out Vector3 force, Vector3 displayAirVelocity, float displayAirDensity)
    {
        Vector3 com;
        BiVector3 forceAndTorque;
        if (aerodynamicSurfaces == null)
        {
            center = Vector3.zero;
            force = Vector3.zero;
            return;
        }

        if (rb == null)
        {
            rb = GetComponent<Rigidbody>();
            if (useCustomCoM)
            {
                rb.centerOfMass = customCoM;
            }
            com = rb.worldCenterOfMass;
            forceAndTorque = CalculateAerodynamicForces(-displayAirVelocity, Vector3.zero, Vector3.zero, displayAirDensity, com);
        }
        else
        {
            if (useCustomCoM)
            {
                rb.centerOfMass = customCoM;
            }
            com = rb.worldCenterOfMass;
            forceAndTorque = currentForceAndTorque;
        }

        force = forceAndTorque.p;
        center = com + Vector3.Cross(forceAndTorque.p, forceAndTorque.q) / forceAndTorque.p.sqrMagnitude;
    }
#endif

(Sorry, not really optimized).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions