public static int Compare(CollisionResult a, CollisionResult b) { return a.Distance.CompareTo(b.Distance); }
public static int Compare(CollisionResult a, CollisionResult b) { return(a.Distance.CompareTo(b.Distance)); }
/// <summary> /// Determine all collisions that will happen as the given actor moves. /// </summary> /// <param name="actor">The actor that is moving.</param> /// <param name="movement">The actor's movement vector this update.</param> /// <remarks>The results are stored in the cached list.</remarks> public void Collide(Actor actor, Vector2 movement) { collisionResults.Clear(); if (actor == null) { throw new ArgumentNullException("actor"); } if (actor.Dead || (actor.Collidable == false)) { return; } // determine the movement direction and scalar float movementLength = movement.Length(); if (movementLength <= 0f) { return; } // check each actor foreach (Actor checkActor in actors) { if ((actor == checkActor) || checkActor.Dead || !checkActor.Collidable) { continue; } // calculate the target vector Vector2 checkVector = checkActor.Position - actor.Position; float distanceBetween = checkVector.Length() - (checkActor.Radius + actor.Radius); // check if they could possibly touch no matter the direction if (movementLength < distanceBetween) { continue; } // determine how much of the movement is bringing the two together float movementTowards = Vector2.Dot(movement, checkVector); // check to see if the movement is away from each other if (movementTowards < 0f) { continue; } if (movementTowards < distanceBetween) { continue; } CollisionResult result = new CollisionResult(); result.Distance = distanceBetween; result.Normal = Vector2.Normalize(checkVector); result.Actor = checkActor; collisionResults.Add(result); } }
/// <summary> /// Determine all collisions that will happen as the given actor moves. /// </summary> /// <param name="actor">The actor that is moving.</param> /// <param name="movement">The actor's movement vector this update.</param> /// <remarks>The results are stored in the cached list.</remarks> public void Collide(Actor actor, Vector2 movement) { collisionResults.Clear(); if (actor == null) { throw new ArgumentNullException("actor"); } if (actor.Dead || (actor.Collidable == false)) { return; } // determine the movement direction and scalar float movementLength = movement.Length(); if (movementLength <= 0f) { return; } // check each actor foreach (Actor checkActor in actors) { if ((actor == checkActor) || checkActor.Dead || !checkActor.Collidable) { continue; } // calculate the target vector Vector2 checkVector = checkActor.Position - actor.Position; float distanceBetween = checkVector.Length() - (checkActor.Radius + actor.Radius); // check if they could possibly touch no matter the direction if (movementLength < distanceBetween) { continue; } // determine how much of the movement is bringing the two together float movementTowards = Vector2.Dot(movement, checkVector); // check to see if the movement is away from each other if (movementTowards < 0f) { continue; } if (movementTowards < distanceBetween) { continue; } CollisionResult result = new CollisionResult(); result.Distance = distanceBetween; result.Normal = Vector2.Normalize(checkVector); result.Actor = checkActor; collisionResults.Add(result); } }