Exemple #1
0
        // vertexes: Convex polygon vertexes with clockwise rotation only.
        public static LWShape Create(Point position, List <Point> vertexes)
        {
            LWShape lwShape = new LWShape();

            lwShape.SetPosition(position);
            lwShape.SetShape(vertexes);
            return(lwShape);
        }
Exemple #2
0
        // 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);
        }
Exemple #3
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());
        }
Exemple #4
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());
        }
Exemple #5
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());
        }
Exemple #6
0
 // this shape is colliding a target or not
 public bool Collided(LWShape target)
 {
     return(Collided(new Vector(), target));
 }
Exemple #7
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);
        }