예제 #1
0
    void Update()
    {
        // Apply Gravity
        if (this.transform.position.y > 0)
        {
            velocity = VectorLibrary.addVectors(velocity, VectorLibrary.getScalarMultiple(gravity, Time.deltaTime));
        }

        if (deccelarate)
        {
            // Get direction
            if (!(Mathf.Abs(velocity.x) < 0.1f && Mathf.Abs(velocity.z) < 0.1f))
            {
                direction = VectorLibrary.getUnitDirection(velocity, VectorLibrary.zeroVector());
            }
            // Apply deccelaration
            deccelaration = VectorLibrary.getScalarMultiple(-direction, frictionFactor);

            // Calculate new Velocity: v = v_old + a * deltaT
            velocity = VectorLibrary.addVectors(velocity, VectorLibrary.getScalarMultiple(-deccelaration, Time.deltaTime));
        }
        // p = p_old + vel * deltaT
        Vector3 velTimesdeltaT = VectorLibrary.getScalarMultiple(velocity, Time.deltaTime);

        this.transform.position = VectorLibrary.addVectors(this.transform.position, velTimesdeltaT);

        // When ground is hit
        if (this.transform.position.y < 0)
        {
            if (velocity.y < 0)
            {
                // H = e*h -1 So it goes up again
                velocity.y = velocity.y * e * -1;
            }

            if (velocity.y < 0.1f)
            {
                velocity    = new Vector3(velocity.x, 0, 0);
                deccelarate = true;
            }
            if (Mathf.Abs(velocity.x) < 0.1f && Mathf.Abs(velocity.z) < 0.1f)
            {
                velocity = new Vector3(0.0f, 0.0f, 0.00f);
            }
        }
    }
예제 #2
0
    // Start is called before the first frame update
    void Start()
    {
        Vector3 a = new Vector3(1, 3, 2);
        Vector3 b = new Vector3(4, 7, 1);

        Vector3 point = new Vector3(2.66f, 0f, 2.66f);
        Vector3 start = new Vector3(2f, 0f, 2f);
        Vector3 end   = new Vector3(12f, 0f, 12f);

        float scalarNum = 2;

        float   radius = 5f;
        float   theta  = 2.214298f;
        Vector3 n      = new Vector3(-3, 0f, 4);


        Vector3 b1 = new Vector3(1, 10, 1);
        Vector3 b2 = new Vector3(1, 20, 1);


        Debug.Log("Vector 1: " + a + "  Vector 2: " + b + "  Scalar Number: " + scalarNum);
        Debug.Log("Angle between 2 3D Vectors - Output: " + VectorLibrary.angleOfVectors(a, b) + "\nExpected Output: 0.47 radians");
        Debug.Log("3D Vector addition - Output: " + VectorLibrary.addVectors(a, b) + "\nExpected Output: (5, 10, 3)");
        Debug.Log("3D Vector subtraction - Output: " + VectorLibrary.subVectors(a, b) + "\nExpected Output: (-3, -4, 1)");
        Debug.Log("3D Dot Product Vector - Output: " + VectorLibrary.dotProduct(a, b) + "\nExpected Output: 27");
        Debug.Log("Unit vector of a 3D Vector - Output: " + VectorLibrary.getUnitVector(a) + "\nExpected Output: (0.3, 0.8 , 0.5)");

        Debug.Log("Vector reflection (axis aligned X) Output: " + VectorLibrary.getVectorReflection(point, true) + "\nExpected Output: (-2.66f,0f,2.66f) ");
        Debug.Log("Vector reflection (axis aligned Z) Output: " + VectorLibrary.getVectorReflection(point, false) + "\nExpected Output: (2.66f,0f,-2.66f) ");

        Debug.Log("Polar to Cartesian - Output:  " + VectorLibrary.convertToCartesian(radius, theta) + "\nExpected Output: -3, 4");
        Debug.Log("Cartesian to Polar - Output:  " + VectorLibrary.convertToPolar(n).x + " " + VectorLibrary.convertToPolar(n).z + "\nExpected Output: 5 2.214 ");

        Debug.Log("Unit Direction Vector - Output: " + VectorLibrary.getUnitDirection(a, b) + "\nExpected Output: (0.6, 0.8, -0.2)");
        Debug.Log("Magnitude of a 3D Vector - Output: " + VectorLibrary.getMagnitude(a) + "\nExpected Output: 3.7416");
        Debug.Log("Scalar Multiple of a 3D Vector - Output: " + VectorLibrary.getScalarMultiple(a, scalarNum) + "\nExpected Output: 2,6,4");
        Debug.Log("Vectors nearly equal with radius - Output: " + VectorLibrary.circleCollision(b1, b2, radius) + "\nExpected Output: False");
        Debug.Log("3D zero Vector - Output: " + VectorLibrary.getScalarMultiple(a, scalarNum) + "\nExpected Output: (2,6,4)");
        Debug.Log("A point is on a Line - Output: " + VectorLibrary.isPointOnLine(point, start, end) + "\nExpected Output: True");
    }
예제 #3
0
    void FixedUpdate()
    {
        direction = VectorLibrary.getUnitDirection(velocity, VectorLibrary.zeroVector());

        // Apply deccelaration
        deccelaration = VectorLibrary.getScalarMultiple(-direction, frictionFactor);

        // Calculate new Velocity: v = v_old + a * deltaT
        velocity = VectorLibrary.addVectors(velocity, VectorLibrary.getScalarMultiple(-deccelaration, Time.deltaTime));

        // Calculate new Position: p = p_old + v * deltaT
        position = VectorLibrary.addVectors(this.transform.position, VectorLibrary.getScalarMultiple(velocity, Time.deltaTime));

        // Bounce Back the Cue Ball from boundaries
        boundaryCollision();

        // Ball Collisions
        ballCollision();

        // Assign new position to the current object's position
        this.transform.position = position;
    }
예제 #4
0
    void Update()
    {
        if (isTargetPositionReached(this.transform.position, waypoints[waypointID].position) && !isRotating)
        {
            isRotating = true;
        }

        // Get new CenterPoint for each update
        centerPoint = waypoints[waypointID].position;


        if (isRotating)
        {
            centerPoint = waypoints[waypointID].position;
            // Transpose the ball to world cooridnate center
            pos = VectorLibrary.subVectors(this.transform.position, centerPoint);

            //float yof = this.transform.position.y;

            // Convert to Polar Coordinates
            Vector3 polarcoord = VectorLibrary.convertToPolar(pos);
            float   radius     = polarcoord.x;
            float   theta      = polarcoord.z;

            // Creating a angle for 1 turn
            angle += speed * angularSpeed * Time.deltaTime;

            // Adding the same speed to obejcts angle
            theta += speed * angularSpeed * Time.deltaTime;

            // Check if 1 Turn has been reached
            if (angle >= 360f * Mathf.Deg2Rad)
            {
                isRotating = false;
                waypointID++;
                // If last waypoint reached reset to first
                if (waypointID >= waypoints.Length)
                {
                    waypointID = 0;
                }
                angle = 0;
            }
            //Converting back to cartesian Coordinates and also resetting y position
            Vector3 cartesiancoord = VectorLibrary.convertToCartesian(centerRadius, theta); // Using a Fixed radius, so distance from ball to waypoint is always the same

            // Tranposing it back to its original position
            pos = VectorLibrary.addVectors(cartesiancoord, centerPoint);

            //pos.y = yof;
            // Assigning p_old = p_new
            this.transform.position = pos;
        }

        else
        {
            // v = d * s (d is direction and s speed)
            Vector3 direction = VectorLibrary.getUnitDirection(this.transform.position, waypoints[waypointID].position);
            Vector3 velocity  = VectorLibrary.getScalarMultiple(direction, speed);

            // p = p_old + vel * deltaT
            Vector3 velTimesdeltaT = VectorLibrary.getScalarMultiple(velocity, Time.deltaTime);
            this.transform.position = VectorLibrary.addVectors(this.transform.position, velTimesdeltaT);
        }
    }