Exemplo n.º 1
0
        // custom method to do some extra work after the collision check
        protected override void UpdateEntityState( CollisionCheckResult result )
        {

            base.UpdateEntityState( result );

            _againstWall = result.XAxis;
        }
Exemplo n.º 2
0
        public CollisionCheckResult Check(Vector2 currentPosition, Vector2 positionDelta, Vector2 objectSize)
        {
            Result = new CollisionCheckResult()
            {
                Position = currentPosition
            };

            _currentPosition = currentPosition.Ceiling();
            _positionDelta   = positionDelta.Ceiling();
            _objectSize      = objectSize.Ceiling();

            _finalPosition = new Vector2(_positionDelta.X, _positionDelta.Y);

            // short-circuit if there is nothing to do
            if (positionDelta == Vector2.Zero)
            {
                return(Result);
            }

            // determine if we need to perform multiple checks, e.g. the entitys proposed
            // position is more than one tile away.
            var steps = (float)Math.Ceiling(Math.Max(Math.Abs(_positionDelta.X), Math.Abs(_positionDelta.Y)) / _map.TileSize);

            if (steps > 1)
            {
                var stepDelta = _positionDelta / steps;

                if (stepDelta.X > 0 || stepDelta.Y > 0)
                {
                    for (var i = 0; i < steps; i++)
                    {
                        PeekStep(stepDelta, i);

                        // if the trace finds a collision anywhere along the way, zero out the offending part of the vector
                        if (Result.XAxis)
                        {
                            stepDelta.X     = 0;
                            positionDelta.X = 0;
                        }
                        if (Result.YAxis)
                        {
                            stepDelta.Y     = 0;
                            positionDelta.Y = 0;
                        }
                    }
                }
            }
            else
            {
                PeekStep(positionDelta, 0f);
            }

            return(Result);
        }
Exemplo n.º 3
0
        public CollisionCheckResult Check( Vector2 currentPosition, Vector2 positionDelta, Vector2 objectSize  )
        {
            Result = new CollisionCheckResult() { Position = currentPosition };

            _currentPosition = currentPosition.Ceiling();
            _positionDelta = positionDelta.Ceiling();
            _objectSize = objectSize.Ceiling();

            _finalPosition = new Vector2( _positionDelta.X, _positionDelta.Y );

            // short-circuit if there is nothing to do
            if( positionDelta == Vector2.Zero )
                return Result;

            // determine if we need to perform multiple checks, e.g. the entitys proposed
            // position is more than one tile away.
            var steps = (float)Math.Ceiling( Math.Max( Math.Abs( _positionDelta.X ), Math.Abs( _positionDelta.Y ) ) / _map.TileSize );

            if( steps > 1 )
            {
                var stepDelta = _positionDelta / steps;

                if( stepDelta.X > 0 || stepDelta.Y > 0 )
                {
                    for( var i = 0; i < steps; i++ )
                    {
                        PeekStep( stepDelta, i );

                        // if the trace finds a collision anywhere along the way, zero out the offending part of the vector
                        if( Result.XAxis )
                        {
                            stepDelta.X = 0;
                            positionDelta.X = 0;
                        }
                        if( Result.YAxis )
                        {
                            stepDelta.Y = 0;
                            positionDelta.Y = 0;
                        }
                    }
                }
            }
            else
            {
                PeekStep( positionDelta, 0f );
            }

            return Result;
        }
Exemplo n.º 4
0
        protected virtual void UpdateEntityState( CollisionCheckResult result )
        {
            IsStanding = false;
            if( result.YAxis )
            {
                IsStanding = Velocity.Y > 0;
                Velocity = new Vector2( Velocity.X, 0 );
            }

            if( result.XAxis )
                Velocity = new Vector2( 0, Velocity.Y );

            Position = result.Position;
        }