private void CheckObjectCollisions(ref CollisionCheckResult result) { Rectangle bounds = BoundingRectangle; foreach (var obj in _engine.GetCollidables()) { //dont collide with yourself,stupid if (obj == this) { continue; } Vector2 depth = bounds.GetIntersectionDepth(obj.BoundingRectangle); if (depth != Vector2.Zero) { //if both objects are using the fastest collision check type - this is enough if (this.CollCheckLevel == CollisionCheckType.Rectangle && obj.CollCheckLevel == CollisionCheckType.Rectangle) { result.HitImpassable = true; result.CollidedObject = obj; } else { if (Collider.DoPerPixelCollision(bounds, _textureData, obj.BoundingRectangle, obj.GetTextureData())) { result.HitImpassable = true; result.CollidedObject = obj; } } } } }
protected virtual CollisionCheckResult DoCollisionCheck() { var result = new CollisionCheckResult { PositionBeforeUpdate = _positionBeforeUpdate, PositionOfCollision = Position }; CheckTileCollisions(ref result); //if we are clear of tiles, check game objects - performance consuming if (!result.HitImpassable) { CheckObjectCollisions(ref result); } return(result); }
protected override void HandleCollisionsInternal(GameTime gameTime, CollisionCheckResult collisions) { }
protected abstract void HandleCollisionsInternal(GameTime gameTime, CollisionCheckResult collisions);
private void CheckTileCollisions(ref CollisionCheckResult result) { Rectangle bounds = BoundingRectangle; int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width); int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // Reset flag to search for ground collision. bool isOnGround = false; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { GameObject collidedTile = null; // If this tile is collidable, TileCollision collision = _engine.Level.GetCollision(x, y, out collidedTile); if (collision != TileCollision.Passable) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = _engine.Level.GetBounds(x, y); Vector2 depth = bounds.GetIntersectionDepth(tileBounds); if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); result.HitImpassable = true; result.CollidedObject = collidedTile; // Resolve the collision along the shallow axis. if (absDepthY < absDepthX) { // If we crossed the top of a tile, we are on the ground. if (_previousBounds.Bottom <= tileBounds.Top) { isOnGround = true; } // Resolve the collision along the Y axis. Position = new Vector2(Position.X, Position.Y + depth.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } else { // Resolve the collision along the X axis. Position = new Vector2(Position.X + depth.X, Position.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } } } } // Save the new bounds bottom. _previousBounds = bounds; result.IsOnGround = isOnGround; }
protected override void HandleCollisionsInternal(GameTime gameTime, CollisionCheckResult collisions) { if (collisions.HitImpassable) { //_engine.ExplosionMaster.AddExplosion(Explosion,gameTime); Kill(); if (collisions.CollidedObject != null) //if what we hit isn't just a tile or screen border { //TODO USE ROTATION collisions.CollidedObject.OnGotHit(new HitData{Rotation = Rotation,HitPosition = Position,HitTime = gameTime, Damage = 1}); } } }
private void CheckTileCollisions(ref CollisionCheckResult result) { Rectangle bounds = BoundingRectangle; int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width); int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // Reset flag to search for ground collision. bool isOnGround = false; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { GameObject collidedTile = null; // If this tile is collidable, TileCollision collision = _engine.Level.GetCollision(x, y, out collidedTile); if (collision != TileCollision.Passable) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = _engine.Level.GetBounds(x, y); Vector2 depth = bounds.GetIntersectionDepth(tileBounds); if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); result.HitImpassable = true; result.CollidedObject = collidedTile; // Resolve the collision along the shallow axis. if (absDepthY < absDepthX) { // If we crossed the top of a tile, we are on the ground. if (_previousBounds.Bottom <= tileBounds.Top) isOnGround = true; // Resolve the collision along the Y axis. Position = new Vector2(Position.X, Position.Y + depth.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } else { // Resolve the collision along the X axis. Position = new Vector2(Position.X + depth.X, Position.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } } } } // Save the new bounds bottom. _previousBounds = bounds; result.IsOnGround = isOnGround; }
protected override void HandleCollisionsInternal(GameTime gameTime, CollisionCheckResult collisions) { Hit = collisions.HitImpassable; }
private void CheckObjectCollisions(ref CollisionCheckResult result) { Rectangle bounds = BoundingRectangle; foreach (var obj in _engine.GetCollidables()) { //dont collide with yourself,stupid if(obj==this) continue; Vector2 depth = bounds.GetIntersectionDepth(obj.BoundingRectangle); if (depth != Vector2.Zero) { //if both objects are using the fastest collision check type - this is enough if (this.CollCheckLevel == CollisionCheckType.Rectangle && obj.CollCheckLevel == CollisionCheckType.Rectangle) { result.HitImpassable = true; result.CollidedObject = obj; } else { if (Collider.DoPerPixelCollision(bounds, _textureData, obj.BoundingRectangle, obj.GetTextureData())) { result.HitImpassable = true; result.CollidedObject = obj; } } } } }
protected virtual CollisionCheckResult DoCollisionCheck() { var result = new CollisionCheckResult {PositionBeforeUpdate = _positionBeforeUpdate, PositionOfCollision = Position}; CheckTileCollisions(ref result); //if we are clear of tiles, check game objects - performance consuming if(!result.HitImpassable) CheckObjectCollisions(ref result); return result; }
protected override void HandleCollisionsInternal(GameTime gameTime, CollisionCheckResult collisions) { IsOnGround = collisions.IsOnGround; // If the collision stopped us from moving, reset the velocity to zero. if (Position.X == collisions.PositionBeforeUpdate.X) Velocity.X = 0; if (Position.Y == collisions.PositionBeforeUpdate.Y) Velocity.Y = 0; if (!IsDead && IsOnGround) { if (Math.Abs(Velocity.X) - 0.02f > 0) { //TODO run _botAnimation.PlayAnimation(_lowerIdle); _topAnimation.PlayAnimation(_upperIdle); } else { _botAnimation.PlayAnimation(_lowerIdle); _topAnimation.PlayAnimation(_upperIdle); } } // Clear input. movement = 0.0f; isJumping = false; isFiring = false; }
protected override void HandleCollisionsInternal(GameTime gameTime, CollisionCheckResult collisions) { throw new NotImplementedException(); }