Beispiel #1
0
        public bool RayCast(Vector2 origin, Vector2 direction, float maxDistance, out RayCastHit hit, Category collisionMask)
        {
            var smallest = 1.0f;
            //direction.Normalize();
            var found    = false;
            var localHit = default(RayCastHit);

            world.RayCast((f, p, n, fr) =>
            {
                if ((f.CollisionCategories & collisionMask) == f.CollisionCategories)
                {
                    found = true;
                    if (fr < smallest)
                    {
                        smallest = fr;
                    }
                    localHit = new RayCastHit()
                    {
                        distance = fr * maxDistance, hitPoint = p, normal = n, fixture = f
                    };
                    return(fr);
                }
                return(-1);
            }, origin, origin + direction * maxDistance);

            hit = localHit;
            return(found);
        }
Beispiel #2
0
        void SlideDownMaxSlope(RayCastHit hitInfo, ref Vector2 moveAmount)
        {
            var slopeAngle = MathHelper.ToDegrees((float)Math.Acos(Vector2.Dot(hitInfo.normal, UpVector)));

            if (slopeAngle < 90 && slopeAngle > maxSlopeAngle)
            {
                moveAmount.X = (float)(Math.Sign(hitInfo.normal.X) * (Math.Abs(moveAmount.Y) - hitInfo.distance) / Math.Tan(MathHelper.ToRadians(slopeAngle)));

                collisions.slopeAngle  = slopeAngle;
                collisions.slidingDown = true;
                collisions.slopeNormal = hitInfo.normal;
            }
        }