Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace WindowsGame4
- {
- class PhysicsEngine
- {
- public void HandleCollissions(LevelManager level, PlayerObject player)
- {
- // adjust the player worldposition according to collission etc.
- if (player.Velocity.X != 0)
- {
- player.WorldPositionX += player.Velocity.X;
- CalculateCollissions(level, player, PhysicsDirection.horizontal);
- }
- if (player.Velocity.Y != 0)
- {
- player.WorldPositionY += player.Velocity.Y;
- CalculateCollissions(level, player, PhysicsDirection.vertical);
- }
- }
- public void CalculateCollissions(LevelManager level, PlayerObject player, PhysicsDirection direction)
- {
- foreach (Layer layer in level.Layers)
- {
- if (layer != null)
- {
- foreach (Scene2DTile tile in layer.TileGrid)
- {
- if (tile != null)
- {
- // if the distance between the objects is lower than 100
- if (Math.Sqrt(Math.Pow(Math.Abs(tile.WorldPosition.X - player.WorldPosition.X), 2) + Math.Pow(Math.Abs(tile.WorldPosition.Y - player.WorldPosition.Y), 2)) < 100)
- {
- if (player.Boundingbox.Intersects(tile.BoundingBox))
- {
- #region//PER PIXEL COLLISSION
- if (layer.CollissionType == LayerCollissionType.perPixel)
- {
- if (direction == PhysicsDirection.horizontal)
- {
- if (player.Velocity.X > 0) // the player comes FROM THE LEFT and collides with the tile
- {
- while (IntersectPixelsTileAndSprite(tile, player))
- {
- player.WorldPositionX -= 1;
- }
- }
- else //if (player.Velocity.X < 0) // the player comes FROM THE RIGHT and collides with the tile
- {
- while (IntersectPixelsTileAndSprite(tile, player))
- {
- player.WorldPositionX += 1;
- }
- }
- }
- else
- {
- if (player.Velocity.Y > 0) // the player comes FROM ABOVE and collides with the tile
- {
- while (IntersectPixelsTileAndSprite(tile, player))
- {
- player.WorldPositionY -= 1;
- player.PositionState = PositionState.onGround;
- player.VelocityY = 0;
- }
- }
- else //if (player.Velocity.Y < 0) // the player comes FROM UNDER and collides with the tile
- {
- while (IntersectPixelsTileAndSprite(tile, player))
- {
- player.WorldPositionY += 1;
- player.VelocityY = 0;
- }
- }
- }
- }
- #endregion
- }
- }
- }
- }
- }
- }
- }
- public bool IntersectPixelsTileAndSprite(Scene2DTile tile, PlayerObject player)
- {
- // Find the bounds of the rectangle intersection
- // That is: find the rectangle, in which the textures intersect
- int top = Math.Max(tile.BoundingBox.Top, player.Boundingbox.Top);
- int bottom = Math.Min(tile.BoundingBox.Bottom, player.Boundingbox.Bottom);
- int left = Math.Max(tile.BoundingBox.Left, player.Boundingbox.Left);
- int right = Math.Min(tile.BoundingBox.Right, player.Boundingbox.Right);
- //Now we create the colorarrays from the tiles, to compare
- Color[] dataA = SourceFromTextureToColor(tile.Spritesheet, tile.SourceRectangle);
- Color[] dataB = WholeTextureToColor(player.Sprite);
- // Check every point within the intersection bounds
- for (int y = top; y < bottom; y++)
- {
- for (int x = left; x < right; x++)
- {
- // Get the color of both pixels at this point
- Color colorA = dataA[(x - tile.BoundingBox.Left) +
- (y - tile.BoundingBox.Top) * tile.BoundingBox.Width];
- Color colorB = dataB[(x - player.Boundingbox.Left) +
- (y - player.Boundingbox.Top) * player.Boundingbox.Width];
- // If both pixels are not completely transparent,
- if (colorA.A != 0 && colorB.A != 0)
- {
- // then an intersection has been found
- return true;
- }
- }
- }
- // No intersection found
- return false;
- }
- public Color[] SourceFromTextureToColor(Texture2D spritesheet, Rectangle sourceRectangle)
- {
- Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height];
- spritesheet.GetData(0, sourceRectangle, data, 0, data.Length);
- return data;
- }
- public Color[] WholeTextureToColor(Texture2D sprite)
- {
- Color[] data = new Color[sprite.Width * sprite.Height];
- sprite.GetData(data);
- return data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement