public void KnowWhenThereIsNoCollision() { var originalVector = Vector2.right; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(CollisionAlignment.NoSplit, newVectors.Alignment); }
public void KnowWhenThereIsANonOpposedCollision(Vector2 originalVector, Vector2 collisionNormal) { this.collisionEvent.percentToHit = 0.5f; this.collisionEvent.normal = collisionNormal; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(CollisionAlignment.NonOpposed, newVectors.Alignment); }
public void KnowWhenOriginalVectorAndCollisionNormalAreAligned(Vector2 originalVector, Vector2 collisionNormal) { this.collisionEvent.percentToHit = 0.4f; this.collisionEvent.normal = collisionNormal; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(CollisionAlignment.Aligned, newVectors.Alignment); }
public void ResultInTheSecondReturnedVectorBeingPerpindicularToTheNormal(Vector2 originalVector, Vector2 collisionNormal, Vector2 reflectedVector) { this.collisionEvent.percentToHit = 0.4f; this.collisionEvent.normal = collisionNormal; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.Less(Vector2.Distance(reflectedVector.normalized, newVectors.Second.normalized), Common.FloatTolerance); }
public void ResultInTheFirstReturnedVectorAndTheOriginalVectorPointingTheSameDirection(Vector2 originalVector, Vector2 collisionNormal) { this.collisionEvent.percentToHit = 0.4f; this.collisionEvent.normal = collisionNormal; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.Less(Vector2.Distance(originalVector.normalized, newVectors.First.normalized), Common.FloatTolerance); }
public void ReturnsSecondVectorAsZeroIfThereIsNoCollision() { var originalVector = new Vector2(4, 2); this.collisionEvent.percentToHit = 1; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(0, newVectors.Second.magnitude, Common.FloatTolerance); }
public void ReturnsSecondVectorAsZeroIfCollisionIsOpposite() { var originalVector = new Vector2(4, 2); this.collisionEvent.percentToHit = 0.4f; this.collisionEvent.normal = originalVector * -1f; var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(0, newVectors.Second.magnitude, Common.FloatTolerance); }
public void ReturnsFirstVectorWithMagnitudeProportionalToCollisionTime(float timeToCollision, float normalRotation) { var originalVector = new Vector2(1, 3); this.collisionEvent.percentToHit = timeToCollision; this.collisionEvent.normal = (originalVector * -1f).Rotate(normalRotation); var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(timeToCollision, newVectors.First.magnitude / originalVector.magnitude, Common.FloatTolerance); }
public void ReturnsSecondVectorWithMagnitudeProportionalToCollisionTimeIfNotOpposite(float timeToCollision, float normalRotation) { var originalVector = new Vector2(4, 2); this.collisionEvent.percentToHit = timeToCollision; this.collisionEvent.normal = (originalVector * -1f).Rotate(normalRotation); var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); var expectedMagnitude = Mathf.Cos((90 - Mathf.Abs(normalRotation)) * Mathf.Deg2Rad) * (1 - timeToCollision); Assert.AreEqual(expectedMagnitude, newVectors.Second.magnitude / originalVector.magnitude, Common.FloatTolerance); }
private void ApplyVelocity(Vector2 velocity, bool isInputVelocity, int recurseDepth = 0) { var collision = new BoxProjection(this.Collider.bounds, this.Position, velocity).Project(); var adjustmentVectors = VectorSplitter.Split(velocity, collision); this.Position += adjustmentVectors.First; if (collision) { if (this.lastCollisionNormal == Vector2.zero || collision.normal.y > this.lastCollisionNormal.y) { this.lastCollisionNormal = collision.normal; } if ((Vector2.Angle(collision.normal, Vector2.up) < 45 && adjustmentVectors.Alignment == CollisionAlignment.Opposed) || collision.normal.y < 0) { // Ground / Ceiling if (!isInputVelocity) { this.Velocity = Vector2.zero; } } else { // Anything else if (!isInputVelocity) { this.Velocity = this.Velocity.Project(adjustmentVectors.Second); } if (adjustmentVectors.Second.sqrMagnitude > 0) { // HACK: Just give up. We can't seem to get this right. if (recurseDepth >= 3) { this.Position += adjustmentVectors.Second; } else { this.ApplyVelocity(adjustmentVectors.Second, isInputVelocity, recurseDepth + 1); } } } } else if (isInputVelocity) { this.ClampToSlopes(velocity); } }
public void ReturnOriginalVectorWhenThereIsNoCollision(Vector2 originalVector, Vector2 goalVector) { var newVectors = VectorSplitter.Split(originalVector, this.collisionEvent); Assert.AreEqual(goalVector, newVectors.First); }