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; } }
// 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"); }