void FixedUpdate()
    {
        if (toReset)
        {
            /*
             * height = 550;
             * init_angle_roll = 5;
             * init_angle_pitch = 90;
             * init_xoffset = 15;
             * init_zoffset = 90;
             * init_zspeed = 1;
             * angle_tvc = 7;
             * thrust_engine = 5000;
             * thrust_rcs = 0.2f;
             * collision_speed = 5;
             */

            rb.position = transform.parent.transform.TransformPoint(new Vector3(Random.Range(-init_xoffset, init_xoffset), height, Random.Range(-init_zoffset, init_zoffset)));
            rb.velocity = new Vector3(0, 0, init_zspeed);

            rb.rotation        = Quaternion.Euler(Random.Range(-init_angle_pitch, init_angle_pitch), 0, Random.Range(-init_angle_roll, init_angle_roll)); //Attention pour le angle pitch on voudra par la suite juste 90 mais d'un cote
            rb.angularVelocity = Vector3.zero;

            isEngineOn      = false;
            rollTVC         = 0;
            pitchTVC        = 0;
            isRollPosRCSOn  = false;
            isRollNegRCSOn  = false;
            isPitchPosRCSOn = false;
            isPitchNegRCSOn = false;

            toReset = false;
            return;
        }

        if (rb.position.y < 1.821 && Mathf.Abs(Vector3.Dot(transform.up, Vector3.right)) < 0.1 && Mathf.Abs(Vector3.Dot(transform.up, Vector3.forward)) < 0.1)
        {
            isRollPosRCSOn  = false;
            isRollNegRCSOn  = false;
            isPitchPosRCSOn = false;
            isPitchNegRCSOn = false;
        }

        if (isEngineOn)
        {
            Vector3 thrust = new Vector3(0, 2 * thrust_engine * Time.deltaTime, 0);

            Quaternion rotation        = Quaternion.Euler(pitchTVC, 0, rollTVC);
            Vector3    vectored_thrust = rotation * thrust;

            Vector3 worldForce = transform.TransformVector(vectored_thrust);
            Vector3 worldPoint = transform.TransformPoint(new Vector3(0, -1, 0));

            rb.AddForceAtPosition(worldForce, worldPoint, ForceMode.Force);
            pd.DrawEngineRays(worldForce, worldPoint, thrust_engine / 1000);
        }

        if (isRollPosRCSOn)
        {
            Vector3 thrust_left = new Vector3(thrust_rcs, 0, 0);

            Vector3 worldForce_left_1 = transform.TransformVector(thrust_left);
            Vector3 worldForce_left_2 = transform.TransformVector(thrust_left);

            Vector3 worldPoint_left_1 = transform.TransformPoint(new Vector3(0, 41, 4));
            Vector3 worldPoint_left_2 = transform.TransformPoint(new Vector3(0, 41, -4));

            rb.AddForceAtPosition(worldForce_left_1, worldPoint_left_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_left_2, worldPoint_left_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_left_1, worldPoint_left_1, 1);
            pd.DrawEngineRays(worldForce_left_2, worldPoint_left_2, 1);
        }

        if (isRollNegRCSOn)
        {
            Vector3 thrust_right = new Vector3(-thrust_rcs, 0, 0);

            Vector3 worldForce_right_1 = transform.TransformVector(thrust_right);
            Vector3 worldForce_right_2 = transform.TransformVector(thrust_right);

            Vector3 worldPoint_right_1 = transform.TransformPoint(new Vector3(0, 41, 4));
            Vector3 worldPoint_right_2 = transform.TransformPoint(new Vector3(0, 41, -4));

            rb.AddForceAtPosition(worldForce_right_1, worldPoint_right_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_right_2, worldPoint_right_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_right_1, worldPoint_right_1, 1);
            pd.DrawEngineRays(worldForce_right_2, worldPoint_right_2, 1);
        }

        if (isPitchPosRCSOn)
        {
            Vector3 thrust_left = new Vector3(0, 0, thrust_rcs);

            Vector3 worldForce_left_1 = transform.TransformVector(thrust_left);
            Vector3 worldForce_left_2 = transform.TransformVector(thrust_left);

            Vector3 worldPoint_left_1 = transform.TransformPoint(new Vector3(4, 41, 0));
            Vector3 worldPoint_left_2 = transform.TransformPoint(new Vector3(-4, 41, 0));

            rb.AddForceAtPosition(worldForce_left_1, worldPoint_left_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_left_2, worldPoint_left_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_left_1, worldPoint_left_1, 1);
            pd.DrawEngineRays(worldForce_left_2, worldPoint_left_2, 1);
        }

        if (isPitchNegRCSOn)
        {
            Vector3 thrust_right = new Vector3(0, 0, -thrust_rcs);

            Vector3 worldForce_right_1 = transform.TransformVector(thrust_right);
            Vector3 worldForce_right_2 = transform.TransformVector(thrust_right);

            Vector3 worldPoint_right_1 = transform.TransformPoint(new Vector3(4, 41, 0));
            Vector3 worldPoint_right_2 = transform.TransformPoint(new Vector3(-4, 41, 0));

            rb.AddForceAtPosition(worldForce_right_1, worldPoint_right_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_right_2, worldPoint_right_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_right_1, worldPoint_right_1, 1);
            pd.DrawEngineRays(worldForce_right_2, worldPoint_right_2, 1);
        }

        if (rb.position.y > height + 50 || rb.position.y < -1 || Mathf.Abs(transform.parent.transform.InverseTransformPoint(rb.position).x) > 30 || Mathf.Abs(transform.parent.transform.InverseTransformPoint(rb.position).z) > 100)
        {
            colorizePlane.Colorize(Color.red);
            ac.EndEpisode(0);
        }

        if (rb.IsSleeping())
        {
            if (Mathf.Abs(Vector3.Dot(transform.up, Vector3.right)) < 0.1 && Mathf.Abs(Vector3.Dot(transform.up, Vector3.forward)) < 0.1 && Vector3.Dot(transform.up, Vector3.up) > 0.9)
            {
                colorizePlane.Colorize(Color.green);
                ac.EndEpisode(1);
            }
            else
            {
                colorizePlane.Colorize(Color.red);
                ac.EndEpisode(0);
            }
        }

        //pas ouf
        //float alt = rb.position.y;
        //Debug.Log((int) alt + "U |" + "predicted using vel" + (int) (alt - rb.velocity.y * Time.deltaTime) + "U |" + rb.velocity.magnitude + "U/s");

        //super
        //float alt = 10*rb.position.y;
        //Debug.Log((int)alt + "m |" + "predicted using vel" + (int)(alt - rb.velocity.y * Time.deltaTime) + "m |" + rb.velocity.magnitude + "m/s");
    }
示例#2
0
    void FixedUpdate()
    {
        if (ac.episodeFinished)
        {
            return;
        }

        if (toReset)
        {
            rb.position = transform.parent.transform.TransformPoint(new Vector3(Random.Range(-init_xoffset, init_xoffset), height, Random.Range(-init_zoffset, -init_zoffset)));
            rb.velocity = new Vector3(0, 0, init_zspeed);

            rb.rotation        = Quaternion.Euler(Random.Range(85, init_angle_pitch), 0, Random.Range(-init_angle_roll, init_angle_roll));
            rb.angularVelocity = Vector3.zero;

            isEngineOn      = false;
            rollTVC         = 0;
            pitchTVC        = 0;
            isRollPosRCSOn  = false;
            isRollNegRCSOn  = false;
            isPitchPosRCSOn = false;
            isPitchNegRCSOn = false;

            anc.ResetAnimation();

            toReset = false;
            return;
        }

        if (rb.position.y < 1.821 && Mathf.Abs(Vector3.Dot(transform.up, Vector3.right)) < 0.1 && Mathf.Abs(Vector3.Dot(transform.up, Vector3.forward)) < 0.1)
        {
            isRollPosRCSOn  = false;
            isRollNegRCSOn  = false;
            isPitchPosRCSOn = false;
            isPitchNegRCSOn = false;
        }

        if (isEngineOn)
        {
            Vector3 thrust = new Vector3(0, thrust_engine * Time.deltaTime * 2, 0);

            Quaternion rotation        = Quaternion.Euler(pitchTVC, 0, rollTVC);
            Vector3    vectored_thrust = rotation * thrust;

            Vector3 worldForce = transform.TransformVector(vectored_thrust);
            Vector3 worldPoint = transform.TransformPoint(new Vector3(0, -1, 0));

            rb.AddForceAtPosition(worldForce, worldPoint, ForceMode.Force);
            pd.DrawEngineRays(worldForce, worldPoint, thrust_engine / 1000);
        }

        if (isRollPosRCSOn)
        {
            Vector3 thrust_left = new Vector3(thrust_rcs, 0, 0);

            Vector3 worldForce_left_1 = transform.TransformVector(thrust_left);
            Vector3 worldForce_left_2 = transform.TransformVector(thrust_left);

            Vector3 worldPoint_left_1 = transform.TransformPoint(new Vector3(0, 41, 4));
            Vector3 worldPoint_left_2 = transform.TransformPoint(new Vector3(0, 41, -4));

            rb.AddForceAtPosition(worldForce_left_1, worldPoint_left_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_left_2, worldPoint_left_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_left_1, worldPoint_left_1, 1);
            pd.DrawEngineRays(worldForce_left_2, worldPoint_left_2, 1);
        }

        if (isRollNegRCSOn)
        {
            Vector3 thrust_right = new Vector3(-thrust_rcs, 0, 0);

            Vector3 worldForce_right_1 = transform.TransformVector(thrust_right);
            Vector3 worldForce_right_2 = transform.TransformVector(thrust_right);

            Vector3 worldPoint_right_1 = transform.TransformPoint(new Vector3(0, 41, 4));
            Vector3 worldPoint_right_2 = transform.TransformPoint(new Vector3(0, 41, -4));

            rb.AddForceAtPosition(worldForce_right_1, worldPoint_right_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_right_2, worldPoint_right_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_right_1, worldPoint_right_1, 1);
            pd.DrawEngineRays(worldForce_right_2, worldPoint_right_2, 1);
        }

        if (isPitchPosRCSOn)
        {
            Vector3 thrust_left = new Vector3(0, 0, thrust_rcs);

            Vector3 worldForce_left_1 = transform.TransformVector(thrust_left);
            Vector3 worldForce_left_2 = transform.TransformVector(thrust_left);

            Vector3 worldPoint_left_1 = transform.TransformPoint(new Vector3(4, 41, 0));
            Vector3 worldPoint_left_2 = transform.TransformPoint(new Vector3(-4, 41, 0));

            rb.AddForceAtPosition(worldForce_left_1, worldPoint_left_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_left_2, worldPoint_left_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_left_1, worldPoint_left_1, 1);
            pd.DrawEngineRays(worldForce_left_2, worldPoint_left_2, 1);
        }

        if (isPitchNegRCSOn)
        {
            Vector3 thrust_right = new Vector3(0, 0, -thrust_rcs);

            Vector3 worldForce_right_1 = transform.TransformVector(thrust_right);
            Vector3 worldForce_right_2 = transform.TransformVector(thrust_right);

            Vector3 worldPoint_right_1 = transform.TransformPoint(new Vector3(4, 41, 0));
            Vector3 worldPoint_right_2 = transform.TransformPoint(new Vector3(-4, 41, 0));

            rb.AddForceAtPosition(worldForce_right_1, worldPoint_right_1, ForceMode.Force);
            rb.AddForceAtPosition(worldForce_right_2, worldPoint_right_2, ForceMode.Force);

            pd.DrawEngineRays(worldForce_right_1, worldPoint_right_1, 1);
            pd.DrawEngineRays(worldForce_right_2, worldPoint_right_2, 1);
        }

        if (rb.position.y > height + 50 || rb.position.y < -1 || Mathf.Abs(transform.parent.transform.InverseTransformPoint(rb.position).x) > 30 || Mathf.Abs(transform.parent.transform.InverseTransformPoint(rb.position).z) > 100)
        {
            cp.Colorize(Color.red);
            ac.EndEpisode(0);
        }

        if (rb.IsSleeping())
        {
            if (Mathf.Abs(Vector3.Dot(transform.up, Vector3.right)) < 0.1 && Mathf.Abs(Vector3.Dot(transform.up, Vector3.forward)) < 0.1 && Vector3.Dot(transform.up, Vector3.up) > 0.9)
            {
                cp.Colorize(Color.green);
                ac.EndEpisode(1);
            }
            else
            {
                cp.Colorize(Color.red);
                ac.EndEpisode(0);
            }
        }

        //Animations
        if (rb.position.y < 20 && rb.velocity.magnitude > 0.1)
        {
            anc.PlayAnimation();
        }

        rpc.AnimateParticle(isRollPosRCSOn, isRollNegRCSOn, isPitchPosRCSOn, isPitchNegRCSOn);
        epc.AnimateParticle(isEngineOn);
        tc.SetTelemetryText(10 * rb.position.y - 7, rb.velocity.magnitude);
        lspc.UpdateLandingSmoke(10 * rb.position.y - 7, isEngineOn);
        //erac.isEngineOn = isEngineOn;
        erac.isEngineOn = false;
        tvcac_engine_1.UpdateTVCAnimation(rollTVC, pitchTVC);
        tvcac_engine_2.UpdateTVCAnimation(rollTVC, pitchTVC);
        tvcac_engine_3.UpdateTVCAnimation(rollTVC, pitchTVC);
    }