Exemple #1
0
    /// <summary>
    /// Check speed, and if below a certain amount for too long, the car is marked to be stuck and should stop wasting simulation time.
    /// </summary>
    private void CheckIfStillRunning()
    {
        // Get speed
        var speed = BaseOfCar.GetComponent <Rigidbody2D>().velocity.magnitude;

        // Update max speed (for fitness)
        if (speed > MaxSpeedReached)
        {
            MaxSpeedReached = speed;
        }

        // If below threshhold count amount of checks below threshhold
        if (speed < MinSpeed)
        {
            AmountOfTimesUnderSpeed++;

            // If amount of times below threshold exceeds specified amount, stop running.
            if (AmountOfTimesUnderSpeed >= MaxAmountOfTimesUnderSpeed)
            {
                EndSimulation();
            }
        }
        else
        {
            AmountOfTimesUnderSpeed = 0;
        }
    }
Exemple #2
0
    /// <summary>
    /// Update physical car (transform, properties, scale of game objects) from variables.
    /// </summary>
    public void UpdateFromVariables()
    {
        // Apply values from dictionary
        LeftWheelRadius    = GenesToValues[GeneType.LeftWheelRadius];
        RightWheelRadius   = GenesToValues[GeneType.RightWheelRadius];
        LeftWheelPosition  = new Vector2(GenesToValues[GeneType.LeftWheelPosX], GenesToValues[GeneType.LeftWheelPosY]);
        RightWheelPosition = new Vector2(GenesToValues[GeneType.RightWheelPosX], GenesToValues[GeneType.RightWheelPosY]);
        Speed          = GenesToValues[GeneType.Speed];
        BaseDimensions = new Vector2(GenesToValues[GeneType.BaseDimensionsX], GenesToValues[GeneType.BaseDimensionsY]);

        // Reset transform
        BaseOfCar.transform.localPosition = Vector3.zero;
        BaseOfCar.transform.localRotation = Quaternion.Euler(Vector3.zero);

        // Apply values to base.
        //BaseDimensions.x *= 2f; // TO HELP FIX THE CAR JUST BEING WHEELS
        BaseOfCar.transform.localScale = new Vector3(BaseDimensions.x, BaseDimensions.y, 1f);

        Vector2 sizeOfBase = new Vector2(BaseOfCar.GetComponent <BoxCollider2D>().size.x *BaseOfCar.transform.localScale.x,
                                         BaseOfCar.GetComponent <BoxCollider2D>().size.y *BaseOfCar.transform.localScale.y);

        // Apply values to left wheel
        LeftWheel.transform.localScale = new Vector3(LeftWheelRadius, LeftWheelRadius, 1f);

        var leftWheelPos = new Vector2(
            Mathf.Clamp01(LeftWheelPosition.x) - 0.5f,
            Mathf.Clamp01(LeftWheelPosition.y) - 0.5f);

        leftWheelPos.x *= (sizeOfBase.x);
        leftWheelPos.y *= (sizeOfBase.y);


        LeftWheel.transform.localPosition = new Vector3(leftWheelPos.x, leftWheelPos.y, 1f);

        // Apply values to right wheel
        RightWheel.transform.localScale = new Vector3(RightWheelRadius, RightWheelRadius, 1f);

        var rightWheelPos = new Vector2(
            Mathf.Clamp01(RightWheelPosition.x) - 0.5f,
            Mathf.Clamp01(RightWheelPosition.y) - 0.5f);

        rightWheelPos.x *= (sizeOfBase.x);
        rightWheelPos.y *= (sizeOfBase.y);

        RightWheel.transform.localPosition = new Vector3(rightWheelPos.x, rightWheelPos.y, 1f);

        // Apply speed values to left and right wheels.
        var newMotor = new JointMotor2D {
            maxMotorTorque = 1000000f, motorSpeed = Speed
        };

        LeftWheel.GetComponent <HingeJoint2D>().useMotor = true;
        LeftWheel.GetComponent <HingeJoint2D>().motor    = newMotor;

        RightWheel.GetComponent <HingeJoint2D>().useMotor = true;
        RightWheel.GetComponent <HingeJoint2D>().motor    = newMotor;
    }
Exemple #3
0
    /// <summary>
    /// Finish simulating, save relevant values and freeze rigid bodies.
    /// </summary>
    public void EndSimulation()
    {
        if (!HasRanSimulation)
        {
            HasRanSimulation = true;

            // Freeze all ridid bodies.
            foreach (var childRigidBodies in GetComponentsInChildren <Rigidbody2D>())
            {
                childRigidBodies.constraints = RigidbodyConstraints2D.FreezeAll;
            }

            // Set end position.
            EndPosition = BaseOfCar.GetComponent <Rigidbody2D>().position;
        }
    }