public static void Magnitude(double x1, double y1, double x2, double y2, double expectedResult)
        {
            double xComponent = x2 - x1;
            double yComponent = y2 - y1;

            Assert.AreEqual(expectedResult, VectorLibrary.Magnitude(xComponent, yComponent), Tolerance);
        }
Пример #2
0
 /// <summary>
 /// Crosses the product.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>Vector3D.</returns>
 public Vector3D CrossProduct(Vector3D point)
 {
     double[] matrix = VectorLibrary.CrossProduct(
         Xcomponent, Ycomponent, Zcomponent,
         point.Xcomponent, point.Ycomponent, point.Zcomponent);
     return(new Vector3D(matrix[0], matrix[1], matrix[2]));
 }
Пример #3
0
    void boundaryCollision()
    {
        // Border Line points
        ballIndex      = 0;
        LineStartPoint = new Vector3(borders[ballIndex].position.x - ballRadius, 0, borders[ballIndex].position.z);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x - ballRadius, 0, borders[ballIndex].position.z - (borders[ballIndex].position.z * 2));


        // Vertical X axis positive
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            // Reflect Velocity
            velocity        = VectorLibrary.getVectorReflection(velocity, true);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.x + ballRadius, borders[ballIndex].position.x));

            // Transposes ball back if it goes outside the boundary
            position.x -= overlapDistance;
        }

        ballIndex      = 1;
        LineStartPoint = new Vector3(borders[ballIndex].position.x + ballRadius, 0, borders[ballIndex].position.z);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x + ballRadius, 0, borders[ballIndex].position.z - (borders[ballIndex].position.z * 2));

        // Vertical X axis negative
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            velocity        = VectorLibrary.getVectorReflection(velocity, true);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.x - ballRadius, borders[ballIndex].position.x));
            position.x     += overlapDistance;
        }


        ballIndex      = 2;
        LineStartPoint = new Vector3(borders[ballIndex].position.x, 0, borders[ballIndex].position.z - ballRadius);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x - (borders[ballIndex].position.x * 2), 0, borders[ballIndex].position.z - ballRadius);

        // Horizontal Z axis positive
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            velocity        = VectorLibrary.getVectorReflection(velocity, false);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.z + ballRadius, borders[ballIndex].position.z));
            position.z     -= overlapDistance;
        }

        ballIndex      = 3;
        LineStartPoint = new Vector3(borders[ballIndex].position.x, 0, borders[ballIndex].position.z + ballRadius);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x - (borders[ballIndex].position.x * 2), 0, borders[ballIndex].position.z + ballRadius);

        // Horizontal Z axis negative
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            velocity        = VectorLibrary.getVectorReflection(velocity, false);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.z - ballRadius, borders[3].position.z));
            position.z     += overlapDistance;
        }
    }
 public static void CrossProduct_3D(
     double x1, double y1, double z1,
     double x2, double y2, double z2,
     double xExpected, double yExpected, double zExpected)
 {
     double[] result = VectorLibrary.CrossProduct3D(x1, y1, z1, x2, y2, z2);
     Assert.AreEqual(xExpected, result[0], Tolerance);
     Assert.AreEqual(yExpected, result[1], Tolerance);
     Assert.AreEqual(zExpected, result[2], Tolerance);
 }
Пример #5
0
 // Check if ball is near waypoint
 bool isTargetPositionReached(Vector3 vec, Vector3 vec2)
 {
     if (VectorLibrary.getMagnitude(VectorLibrary.subVectors(vec, vec2)) <= centerRadius)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #6
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);
            }
        }
    }
Пример #7
0
    void ballCollision()
    {
        // Balls can stick to each other as I haven't implemented to reset position by the overlap amount
        foreach (cueBall ball in balls)
        {
            // Skip if ball is itself
            if (ball == this)
            {
                continue;
            }

            if (VectorLibrary.circleCollision(position, ball.position, ballRadius))
            {
                Vector3[] velocities = VectorLibrary.getCollidedVelocityBall(velocity, position, ball.velocity, ball.position, mass, ball.mass);
                // Assign new final velocities after collision
                this.velocity = velocities[0];
                ball.velocity = velocities[1];
            }
        }
    }
Пример #8
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");
    }
Пример #9
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;
    }
Пример #10
0
 /// <summary>
 ///  True: Segments are parallel, on the same line, oriented in the opposite direction.
 /// </summary>
 /// <param name="line1"></param>
 /// <param name="line2"></param>
 /// <param name="tolerance">Tolerance by which a double is considered to be zero or equal.</param>
 /// <returns></returns>
 public static bool IsCollinearOppositeDirection(LineSegment line1, LineSegment line2, double tolerance = ZeroTolerance)
 {
     return(VectorLibrary.IsCollinearOppositeDirection(line1.ToVector(), line2.ToVector(), tolerance));
 }
 public static void CrossProduct(double x1, double y1, double x2, double y2, double expectedResult)
 {
     Assert.AreEqual(expectedResult, VectorLibrary.CrossProduct(x1, y1, x2, y2), Tolerance);
 }
Пример #12
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);
        }
    }
Пример #13
0
 /// <summary>
 /// Dots the product.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>System.Double.</returns>
 public double DotProduct(CartesianCoordinate3D point)
 {
     return(VectorLibrary.DotProduct(X, Y, Z, point.X, point.Y, point.Z));
 }
Пример #14
0
 /// <summary>
 /// Crosses the product.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>Point3D.</returns>
 public CartesianCoordinate3D CrossProduct(CartesianCoordinate3D point)
 {
     double[] matrix = VectorLibrary.CrossProduct(X, Y, Z, point.X, point.Y, point.Z);
     return(new CartesianCoordinate3D(matrix[0], matrix[1], matrix[2], Tolerance));
 }
 public static void Magnitude_3D_Throws_ArgumentException_for_Zero_Magnitude()
 {
     Assert.Throws <ArgumentException>(() => VectorLibrary.Magnitude3D(0, 0, 0));
 }
 public static void DotProduct_3D(
     double x1, double y1, double z1,
     double x2, double y2, double z2, double expectedResult)
 {
     Assert.AreEqual(expectedResult, VectorLibrary.DotProduct3D(x1, y1, z1, x2, y2, z2), Tolerance);
 }
Пример #17
0
 /// <summary>
 /// True: The concave side of the vector is inside the shape.
 /// This is determined by the direction of the vector.
 /// </summary>
 /// <param name="line1"></param>
 /// <param name="line2"></param>
 /// <param name="tolerance">Tolerance by which a double is considered to be zero or equal.</param>
 /// <returns></returns>
 public static bool ConcaveInside(LineSegment line1, LineSegment line2, double tolerance = ZeroTolerance)
 {
     return(VectorLibrary.IsConcaveInside(line1.ToVector(), line2.ToVector(), tolerance));
 }
Пример #18
0
 /// <summary>
 /// Dots the product.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>System.Double.</returns>
 public double DotProduct(Vector3D point)
 {
     return(VectorLibrary.DotProduct(
                Xcomponent, Ycomponent, Zcomponent,
                point.Xcomponent, point.Ycomponent, point.Zcomponent));
 }
Пример #19
0
 /// <summary>
 /// Vectors form a 90 degree angle.
 /// </summary>
 /// <param name="line1"></param>
 /// <param name="line2"></param>
 /// <param name="tolerance">Tolerance by which a double is considered to be zero or equal.</param>
 /// <returns></returns>
 public static bool IsOrthogonal(LineSegment line1, LineSegment line2, double tolerance = ZeroTolerance)
 {
     return(VectorLibrary.IsOrthogonal(line1.ToVector(), line2.ToVector(), tolerance));
 }
Пример #20
0
 /// <summary>
 /// Returns the cross product/determinant of the coordinates.
 /// x1*y2 - x2*y1
 /// </summary>
 /// <param name="coordinate">The coordinate.</param>
 /// <returns>System.Double.</returns>
 public double CrossProduct(CartesianCoordinate coordinate)
 {
     return(VectorLibrary.CrossProduct(X, Y, coordinate.X, coordinate.Y));
 }