// this shape move by velocity will collide a target or not public bool Collided(Vector velocity, LWShape target) { foreach (LineSegment lineSegment in _shape) { if (target.IsWithIn(lineSegment.from + velocity)) { return(true); } } return(false); }
private void CalculateCollision(Vector velocity, LWShape target) { if (target == null) { return; } Distance min = new Distance { x = float.MaxValue, y = float.MaxValue }; Point collisionPoint = Point.CreateInvalidPoint(); LineSegment collisionLine = null; foreach (LineSegment originalLineSegment in _shape) { foreach (LineSegment targetLineSegment in target.GetSegments()) { Point originalPoint = originalLineSegment.from; if (target.IsWithIn(originalPoint) && !target.IsWithIn(originalPoint + velocity)) { continue; } LineSegment trajectory = LineSegment.Create( originalPoint, originalPoint + velocity ); Point intersection = LWCollide.Math.Intersection(trajectory, targetLineSegment); if (intersection.IsInvalidPoint()) { continue; } if (Distance.Create(originalPoint, intersection).GetPower() < min.GetPower()) { collisionPoint = intersection; min = Distance.Create(originalPoint, intersection); } collisionLine = targetLineSegment.Clone(); } } // does not collide if (collisionPoint.IsInvalidPoint() || collisionLine == null) { _cache.Register( _shape, target.GetSegments(), _position, target.GetPosition(), velocity, collisionPoint.Clone(), min.Clone(), Vector.Create(), Vector.Create()); return; } float primaryVectorLength = (float)System.Math.Sqrt((double)min.GetPower()); float secondaryVectorLength = (float)System.Math.Sqrt((double)velocity.GetPower()) - primaryVectorLength; Vector primaryVector = velocity * (float)(primaryVectorLength / System.Math.Sqrt((double)velocity.GetPower())); Vector secondaryVector = LWCollide.Math.GetLineVector(velocity, collisionLine) * secondaryVectorLength; _cache.Register( _shape, target.GetSegments(), _position, target.GetPosition(), velocity, collisionPoint.Clone(), min.Clone(), primaryVector, secondaryVector); }