/// <summary> /// Calculates teh distance between a ball and a line /// </summary> /// <param name="ballPos"></param> /// <param name="lineStart"></param> /// <param name="lineEnd"></param> /// <returns></returns> public static float CalculateBallDist(Vec2 ballPos, Vec2 lineStart, Vec2 lineEnd) { Vec2 a = ballPos - lineStart; Vec2 b = lineEnd - lineStart; Vec2 bNorm = b.Normalized(); float pLength = a.Dot(bNorm); Vec2 pVec = pLength * bNorm; Vec2 distVec = a - pVec; return(distVec.Length); }
protected override void RenderSelf(GXPEngine.Core.GLContext glContext) { Vec2 endPoint = startPoint + vector * scaleFactor; Gizmos.RenderLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, color, lineWidth, true); Vec2 smallVec = vector.Normalized() * -10; // constant length 10, opposite direction of vector Vec2 left = new Vec2(-smallVec.y, smallVec.x) + smallVec + endPoint; Vec2 right = new Vec2(smallVec.y, -smallVec.x) + smallVec + endPoint; Gizmos.RenderLine(endPoint.x, endPoint.y, left.x, left.y, color, lineWidth, true); Gizmos.RenderLine(endPoint.x, endPoint.y, right.x, right.y, color, lineWidth, true); }
public TestVectorFunction() { //------------------------------------------------------------------------- // Addition //------------------------------------------------------------------------- Vec2 testVectorAddition1 = new Vec2(2, 3); Vec2 testVectorAddition2 = new Vec2(3, 4); Console.WriteLine("Addition ok ?:" + (testVectorAddition1.x + testVectorAddition2.x == 5 && testVectorAddition1.y + testVectorAddition2.y == 7)); //------------------------------------------------------------------------- // Subtraction //------------------------------------------------------------------------- Vec2 testVectorSubtraction1 = new Vec2(2, 3); Vec2 testVectorSubtraction2 = new Vec2(3, 4); Console.WriteLine("Subtraction ok ?:" + (testVectorSubtraction1.x - testVectorSubtraction2.x == -1 && testVectorSubtraction1.y - testVectorSubtraction2.y == -1)); //------------------------------------------------------------------------- // Multiplication Right //------------------------------------------------------------------------- Vec2 testMultiplicationRight = new Vec2(2, 3); Vec2 resultMultiplicationRight = testMultiplicationRight * 3; Console.WriteLine("Scalar multiplication right ok ?:" + (resultMultiplicationRight.x == 6 && resultMultiplicationRight.y == 9 && testMultiplicationRight.x == 2 && testMultiplicationRight.y == 3)); //------------------------------------------------------------------------- // Multiplication Left //------------------------------------------------------------------------- Vec2 testMultiplicationLeft = new Vec2(2, 3); Vec2 resultMultiplicationLeft = testMultiplicationRight * 3; Console.WriteLine("Scalar multiplication right ok ?:" + (resultMultiplicationLeft.x == 6 && resultMultiplicationLeft.y == 9 && testMultiplicationLeft.x == 2 && testMultiplicationLeft.y == 3)); //------------------------------------------------------------------------- // Magnitude //------------------------------------------------------------------------- Vec2 testVectorMagnitude = new Vec2(3, 4); Console.WriteLine("Magnitude ok ?:" + (testVectorMagnitude.Magnitude() == 5f)); //------------------------------------------------------------------------- // Normalize Current Vector //------------------------------------------------------------------------- Vec2 testVectorNormalizeCurrent = new Vec2(3, 4); testVectorNormalizeCurrent.Normalize(); Console.WriteLine("Normalized Curent Vector ok ?:" + (testVectorNormalizeCurrent.x == 0.6f && testVectorNormalizeCurrent.y == 0.8f)); //------------------------------------------------------------------------- // Normalize New Vector //------------------------------------------------------------------------- Vec2 testVectorNormalizeOther = new Vec2(3, 4); Vec2 normalizedVector = testVectorNormalizeOther.Normalized(); Console.WriteLine("Normalized New Vector ok ?:" + (normalizedVector.x == 0.6f && normalizedVector.y == 0.8f)); //------------------------------------------------------------------------- // SetScale Vector //------------------------------------------------------------------------- Vec2 testVectorSetScale = new Vec2(3, 4); testVectorSetScale.SetXY(5, 6); Console.WriteLine("SetScale Vector ok ?:" + (testVectorSetScale.x == 5 && testVectorSetScale.y == 6)); }
public static void UnitTest() { Vec2 testVec = new Vec2(2, 4); Vec2 testVec2 = new Vec2(3, 4); float tempFloat = 0; Vec2 temp = testVec + testVec2; Console.WriteLine("Addition correct?: " + (Math.Abs(temp.x - 5) < 0.01f && Math.Abs(temp.y - 8) < 0.01f)); temp = testVec - testVec2; Console.WriteLine("Substraction correct?: " + (Math.Abs(temp.x + 1) < 0.01f && Math.Abs(temp.y) < 0.01f)); temp = 2 * testVec; Console.WriteLine("Multiplication on left correct?: " + (Math.Abs(temp.x - 4) < 0.01f && Math.Abs(temp.y - 8) < 0.01f)); temp = testVec * 2; Console.WriteLine("Multiplication on right correct?: " + (Math.Abs(temp.x - 4) < 0.01f && Math.Abs(temp.y - 8) < 0.01f)); tempFloat = testVec2.Length(); Console.WriteLine("Length correct?: " + (Math.Abs(tempFloat - 5) < 0.01f)); testVec2.Normalize(); Console.WriteLine("Normalize correct?: " + (Math.Abs(testVec2.Length() - 1) < 0.01f && Math.Abs(testVec2.x - 0.6f) < 0.01f && Math.Abs(testVec2.y - 0.8f) < 0.01f)); testVec2 = new Vec2(3, 4); temp = testVec2.Normalized(); Console.WriteLine("Normalized correct?: " + (Math.Abs(temp.Length() - 1) < 0.01f && Math.Abs(temp.x - 0.6f) < 0.01f && Math.Abs(temp.y - 0.8f) < 0.01f)); testVec.SetXY(15, 2); Console.WriteLine("SetXY correct?: " + (Math.Abs(testVec.x - 15) < 0.01f && Math.Abs(testVec.y - 2) < 0.01f)); Console.WriteLine("Deg2Rad correct?: " + (Math.Abs(Deg2Rad(180) - Math.PI) < 0.01f)); Console.WriteLine("Rad2Deg correct?: " + (Math.Abs(Rad2Deg((float)Math.PI) - 180) < 0.01f)); testVec = GetUnitVectorDeg(180); Console.WriteLine("GetUnitVectorDeg correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f)); testVec = GetUnitVectorRad((float)Math.PI); Console.WriteLine("GetUnitVectorRad correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f)); Console.WriteLine("RandomUnitVector correct?: " + (Math.Abs(RandomUnitVector().Length() - 1) < 0.01f)); testVec = RandomUnitVector(); testVec.SetAngleDegrees(270); Console.WriteLine("SetAngleDegrees correct?: " + (Math.Abs(testVec.y + 1) < 0.01f && Math.Abs(testVec.x) < 0.01f)); Console.WriteLine("GetAngleDegrees correct?: " + (Math.Abs(testVec.GetAngleDegrees() - 270) < 0.01f)); testVec.SetAngleRadians((float)Math.PI); Console.WriteLine("SetAngleRadians correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f)); Console.WriteLine("GetAngleRadians correct?: " + (Math.Abs(testVec.GetAngleRadians() - Math.PI) < 0.01f)); testVec.RotateDegrees(90); Console.WriteLine("RotateDegrees correct?: " + (Math.Abs(testVec.GetAngleDegrees() - 270) < 0.01f)); testVec = RandomUnitVector(); testVec.SetAngleDegrees(180); testVec.RotateRadians((float)Math.PI / 2); Console.WriteLine("RotateRadians correct?: " + (Math.Abs(testVec.GetAngleRadians() - (3 * Math.PI) / 2) < 0.01f)); testVec = new Vec2(2, 4); testVec2 = new Vec2(3, 4); Console.WriteLine("Dot correct?: " + ((testVec.Dot(testVec2) - 22) < 0.01f)); Vec2 testVecNormal = testVec2.Normal(); Console.WriteLine("Normal correct?: " + (Math.Abs(testVecNormal.x + 0.8f) < 0.01f && Math.Abs(testVecNormal.y - 0.6f) < 0.01f && Math.Abs(testVecNormal.Length() - 1) < 0.01f)); testVec.Reflect(1, testVec2); Console.WriteLine("Reflect correct?: " + (Math.Abs(testVec.x - 3.28f) < 0.01f && Math.Abs(testVec.y - 3.04f) < 0.01f)); }