private CollisionInfo GetBoxOverlap(Box other) { Vec2 circlePoint = new Vec2(Mathf.Clamp(position.x, other.x - other.halfWidth, other.x + other.halfWidth), Mathf.Clamp(position.y, other.y - other.halfHeight, other.y + other.halfHeight)); Vec2 distance = circlePoint - this.position; if (distance.Length() < radius) { return(new CollisionInfo(distance.NewNormalized(), -(radius - distance.Length()), true)); } return(null); }
private void PlayerAnimation(byte currentAnimation) { //Animation _timePassed += Time.deltaTime; if (_velocity.Length() > 1) { _animationSpeed = 1 + (Time.time / 70 % 10); } currentFrame = (int)_animationSpeed; //Rotation float tempDeltaAngle = 0; float temp = 0; tempDeltaAngle = _velocity.GetAngleDegrees() - rotation % 360; if (tempDeltaAngle % 360 < -6 || tempDeltaAngle % 360 > 6) { if (tempDeltaAngle % 360 < 0) { temp = -6; } else { temp = 6; } if (Math.Abs(tempDeltaAngle % 360) > 180) { temp = 0 - temp; } rotation += temp; } else if (tempDeltaAngle >= -6 && tempDeltaAngle <= -1) { rotation -= 0.75f; } else if (tempDeltaAngle <= 6 && tempDeltaAngle > 1) { rotation += 0.75f; } }
private void EulerIntegration() { _oldPosition = Position; _velocity += _acceleration; if (_velocity.Length() > _maxSpeed) { _velocity.Normalize(); _velocity *= _maxSpeed; } Position += _velocity; SetXY(Position.x, Position.y); }
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)); }