コード例 #1
0
        public Point GetCollisionPoint(Vector velocity, LWShape target)
        {
            if (target == null)
            {
                return(Point.CreateInvalidPoint());
            }

            if (_cache.isRegistered(_shape,
                                    target.GetSegments(),
                                    _position,
                                    target.GetPosition(),
                                    velocity))
            {
                return(_cache.GetCollisionPoint());
            }

            CalculateCollision(velocity, target);
            return(_cache.GetCollisionPoint());
        }
コード例 #2
0
        public Vector GetSecondaryVelocity(Vector velocity, LWShape target)
        {
            if (target == null)
            {
                return(Vector.Create());
            }

            if (_cache.isRegistered(_shape,
                                    target.GetSegments(),
                                    _position,
                                    target.GetPosition(),
                                    velocity))
            {
                return(_cache.GetSecondaryVelocity());
            }

            CalculateCollision(velocity, target);
            return(_cache.GetSecondaryVelocity());
        }
コード例 #3
0
        public Distance GetCollisionDistance(Vector velocity, LWShape target)
        {
            if (target == null)
            {
                return(Distance.Create());
            }

            if (_cache.isRegistered(_shape,
                                    target.GetSegments(),
                                    _position,
                                    target.GetPosition(),
                                    velocity))
            {
                return(_cache.GetCollisionDistance());
            }

            CalculateCollision(velocity, target);
            return(_cache.GetCollisionDistance());
        }
コード例 #4
0
        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);
        }