// Use this for initialization
    public override void NetworkStart()
    {
        base.NetworkStart();
        if (!isOwner)
        {
            return;
        }
        utils      = Utilities.Instance;
        HOME_TIMER = "MovingColliderHomeReset_" + gameObject.GetInstanceID().ToString();
        utils.CreateTimer(HOME_TIMER, HomeResetTime);

        velocity          = Vector3.zero;
        target_velocity   = Vector3.zero;
        moving            = false;
        at_home           = true;
        distanceThreshold = 0.1f;
        current_target    = null;
        // Save the original target as the home
        home = nextTargetObject;

        // Set IOStates
        d_Moving.initialize(moving);
        d_AtHome.initialize(at_home);

        if (nextTargetObject != null)
        {
            StartCoroutine(MoveToNextTarget());
        }
    }
    // Use this for initialization
    public override void NetworkStart()
    {
        base.NetworkStart();
        if (!IsOwner)
        {
            return;
        }

        velocity          = Vector3.zero;
        target_velocity   = Vector3.zero;
        moving            = false;
        at_home           = true;
        distanceThreshold = 0.1f;
        current_target    = null;
        // Save the original target as the home
        home = nextTargetObject;

        // Set IOStates
        d_Moving.initialize(moving);
        d_AtHome.initialize(at_home);

        if (nextTargetObject != null)
        {
            StartCoroutine(MoveToNextTarget());
        }
    }
Example #3
0
    private bool TickGyros(MotionState state, MotionTarget target)
    {
        Quaternion targetOrientation = Quaternion.Inverse(target.Rotation.Value);
        Quaternion orientation       = state.Orientation;
        double     delta             = Math.Abs(orientation.X - targetOrientation.X)
                                       + Math.Abs(orientation.Y - targetOrientation.Y)
                                       + Math.Abs(orientation.Z - targetOrientation.Z)
                                       + Math.Abs(orientation.W - targetOrientation.W);

        SetGyrosOverride(true);
        bool hold = delta < angularPrecision && state.AngularVelocityLocalYPR.LengthSquared() < angularPrecision;

        SetHoldGyros(hold);
        if (hold)
        {
            return(true);
        }

        foreach (IMyGyro gyro in gyros)
        {
            Vector3D rotationAngles   = calculateGyroRotation(gyro, targetOrientation);
            Vector3D rotationSpeedRad = rotationAngles * angularCorrectionFactor;

            Matrix gyroOrientation;
            gyro.Orientation.GetMatrix(out gyroOrientation);
            Vector3D relativeAngularVelocity = Vector3D.TransformNormal(state.AngularVelocityLocalYPR, gyroOrientation);
            Vector3D rotationSpeedDampened   = rotationSpeedRad - relativeAngularVelocity * rotationDampening;

            gyro.SetValueFloat("Yaw", (float)rotationSpeedDampened.Y);
            gyro.SetValueFloat("Pitch", -(float)rotationSpeedDampened.X);
            gyro.SetValueFloat("Roll", (float)rotationSpeedDampened.Z);
        }

        return(false);
    }
Example #4
0
    private bool T1(MotionState s, MotionTarget t)
    {
        var w = MatrixD.Invert(SC.WorldMatrix); var v = s.VelocityLocal; var p = Vector3D.Transform(s.Position, w); var g = s.GravityLocal; var u = t.Speed; var x = Vector3D.Transform(t.Position ?? (s.Position + (u + v) * D * .66), w); var a = u - v; var b = x - p; if (a.LengthSquared() < velocityPrecision && b.LengthSquared() < positionPrecision)
        {
            CT(); SD(true); return(true);
        }
        var c = GA(a) + g; var d = a / c; var e = p + v * d + c * d * d / 2; var f = x - e; var h = GA(f); var i = h + g; var j = (f - v * D) / D2 - g; var o = Vector3D.Max(-Vector3D.One, Vector3D.Min(Vector3D.One, .5 * j / h)) * 100; if (f.X < 0)

        {
            o.X = -o.X;
        }
        if (f.Y < 0)
        {
            o.Y = -o.Y;
        }
        if (f.Z < 0)
        {
            o.Z = -o.Z;
        }
        if (v.LengthSquared() >= MS * MS && (o * v).Min() >= -.01)
        {
            CT(); return(false);
        }
        SD(d.Max() < D); TO(o); return(false);
    }
    private IEnumerator MoveToNextTarget()
    {
        // Basic lock for coroutines. Do not allow other coroutines to run when moving
        if (moving || !isOwner)
        {
            yield break;
        }
        moving = true;

        if (nextTargetObject == null)
        {
            // Wait for nextTargetObject to bet set
            yield return(new WaitUntil(() => nextTargetObject != null));
        }
        if (nextTargetObject != home)
        {
            at_home = false;
        }
        MotionTarget nextTarget = nextTargetObject.GetComponent <MotionTarget>();

        // Reset to home if we fail to find a next target
        if (nextTarget == null)
        {
            Debug.Log("Target was not a MotionTarget");
            nextTargetObject = home;
            yield return(new WaitForSeconds(1f));

            moving = false;
            StartCoroutine(MoveToNextTarget());
            yield break;
        }

        current_target = nextTarget;
        while ((current_target.transform.position - transform.position).magnitude > distanceThreshold)
        {
            //target_velocity = (current_target.transform.position - transform.position).normalized * current_target.speed;
            // Synchronize with fixed update
            yield return(new WaitForFixedUpdate());
        }

        // Put the coroutine to sleep while we wait
        target_velocity = Vector3.zero;
        if (current_target.waitTime > 0f)
        {
            yield return(new WaitForSeconds(current_target.waitTime));
        }
        nextTargetObject = current_target.nextTargetObject;

        moving = false;
        if (current_target.gameObject == home)
        {
            at_home = true;
        }

        if (Automatic)
        {
            StartCoroutine(MoveToNextTarget());
        }
    }
Example #6
0
 // Use this for initialization
 void Start()
 {
     velocity          = Vector3.zero;
     target_velocity   = Vector3.zero;
     distanceThreshold = 0.1f;
     current_target    = null;
     // Save the original target as the home
     home = nextTargetObject;
     StartCoroutine(MoveToNextTarget());
 }
Example #7
0
    private bool TickThrusters(MotionState state, MotionTarget target)
    {
        Vector3D currentVelocity = state.VelocityLocal;
        Vector3D currentPosition = Vector3D.Transform(state.Position, state.WorldMatrixInverse);
        Vector3D currentGravity  = state.GravityLocal;

        Vector3D targetSpeed    = target.Speed;
        Vector3D targetPosition = Vector3D.Transform(target.Position ?? (state.Position + (targetSpeed + currentVelocity) * DELTA * .66), state.WorldMatrixInverse);

        Vector3D speedDifference    = targetSpeed - currentVelocity;
        Vector3D positionDifference = targetPosition - currentPosition;

        if (speedDifference.LengthSquared() < velocityPrecision && positionDifference.LengthSquared() < positionPrecision)
        {
            ClearThrustersOverride();
            SetDampeners(true);
            return(true);
        }


        Vector3D deceleration       = GetAccelerations(speedDifference) + currentGravity;
        Vector3D decelerationTimes  = speedDifference / deceleration;
        Vector3D positionAtFullStop = currentPosition + currentVelocity * decelerationTimes + deceleration * decelerationTimes * decelerationTimes / 2;

        Vector3D maxAccelerationDelta = targetPosition - positionAtFullStop;
        Vector3D shipAcceleration     = GetAccelerations(maxAccelerationDelta);
        Vector3D acceleration         = shipAcceleration + currentGravity;
        Vector3D accelerationForTick  = (maxAccelerationDelta - currentVelocity * DELTA) / DELTA_SQ_2 - currentGravity;
        Vector3D overrides            = Vector3D.Max(-Vector3D.One, Vector3D.Min(Vector3D.One, 0.5 * accelerationForTick / shipAcceleration)) * 100;

        if (maxAccelerationDelta.X < 0)
        {
            overrides.X = -overrides.X;
        }
        if (maxAccelerationDelta.Y < 0)
        {
            overrides.Y = -overrides.Y;
        }
        if (maxAccelerationDelta.Z < 0)
        {
            overrides.Z = -overrides.Z;
        }

        if (currentVelocity.LengthSquared() >= maxSpeed * maxSpeed && (overrides * currentVelocity).Min() >= -0.01)
        {
            ClearThrustersOverride();
            return(false);
        }

        SetDampeners(decelerationTimes.Max() < DELTA);
        SetThrustersOverride(overrides);
        return(false);
    }
Example #8
0
    private bool G1(MotionState s, MotionTarget t)
    {
        var a = Quaternion.Inverse(t.Rotation.Value); var b = s.Orientation; var d = Math.Abs(b.X - a.X) + Math.Abs(b.Y - a.Y) + Math.Abs(b.Z - a.Z) + Math.Abs(b.W - a.W); SG(true); bool h = d < angularPrecision && s.AngularVelocityLocalYPR.LengthSquared() < angularPrecision; HG(h); if (h)

        {
            return(true);
        }
        foreach (var g in G)
        {
            var c = CG(g, a); var w = MatrixD.Invert(g.WorldMatrix); var x = Vector3D.TransformNormal(s.AngularVelocityWorldYPR, w); var y = c * angularCorrectionFactor;
            var z = y - s.AngularVelocityLocalYPR * rotationDampening; g.SetValueFloat("Yaw", -(float)z.Y); g.SetValueFloat("Pitch", (float)z.X); g.SetValueFloat("Roll", -(float)z.Z);
        }
        return(false);
    }
 public void GoHome()
 {
     if (!isOwner)
     {
         return;
     }
     if (!moving)
     {
         nextTargetObject = home;
         StartCoroutine(MoveToNextTarget());
     }
     else
     {
         MotionTarget home_target = home.GetComponent <MotionTarget>();
         if (home_target != null)
         {
             current_target = home_target;
         }
     }
 }
Example #10
0
    private IEnumerator MoveToNextTarget()
    {
        while (true)
        {
            if (nextTargetObject == null)
            {
                // Wait for nextTargetObject to bet set
                yield return(new WaitUntil(() => nextTargetObject != null));
            }
            MotionTarget nextTarget = nextTargetObject.GetComponent <MotionTarget>();
            // Reset to home if we fail to find a next target
            if (nextTarget == null)
            {
                Debug.Log("Target was not a MotionTarget");
                nextTargetObject = home;
                yield return(new WaitForSeconds(1f));

                continue;
            }

            current_target = nextTarget;
            while ((current_target.transform.position - transform.position).magnitude > distanceThreshold)
            {
                //target_velocity = (current_target.transform.position - transform.position).normalized * current_target.speed;
                // Synchronize with fixed update
                yield return(new WaitForFixedUpdate());
            }

            // Put the coroutine to sleep while we wait
            target_velocity = Vector3.zero;
            if (current_target.waitTime != 0f)
            {
                yield return(new WaitForSeconds(current_target.waitTime));
            }
            nextTargetObject = current_target.nextTargetObject;
        }
    }