Advertisement
Guest User

Untitled

a guest
Nov 1st, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7.  
  8. namespace WindowsGame4
  9. {
  10.     class PhysicsEngine
  11.     {
  12.         public void HandleCollissions(LevelManager level, PlayerObject player)
  13.         {
  14.             // adjust the player worldposition according to collission etc.
  15.             if (player.Velocity.X != 0)
  16.             {
  17.                 player.WorldPositionX += player.Velocity.X;
  18.                 CalculateCollissions(level, player, PhysicsDirection.horizontal);
  19.             }
  20.             if (player.Velocity.Y != 0)
  21.             {
  22.                 player.WorldPositionY += player.Velocity.Y;
  23.                 CalculateCollissions(level, player, PhysicsDirection.vertical);
  24.             }
  25.         }
  26.  
  27.         public void CalculateCollissions(LevelManager level, PlayerObject player, PhysicsDirection direction)
  28.         {
  29.             foreach (Layer layer in level.Layers)
  30.             {
  31.                 if (layer != null)
  32.                 {
  33.                     foreach (Scene2DTile tile in layer.TileGrid)
  34.                     {
  35.                         if (tile != null)
  36.                         {
  37.                             // if the distance between the objects is lower than 100  
  38.                             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)
  39.                             {
  40.                                 if (player.Boundingbox.Intersects(tile.BoundingBox))
  41.                                 {
  42.                                     #region//PER PIXEL COLLISSION
  43.                                     if (layer.CollissionType == LayerCollissionType.perPixel)
  44.                                     {
  45.                                         if (direction == PhysicsDirection.horizontal)
  46.                                         {
  47.                                             if (player.Velocity.X > 0) // the player comes FROM THE LEFT and collides with the tile
  48.                                             {
  49.                                                 while (IntersectPixelsTileAndSprite(tile, player))
  50.                                                 {
  51.                                                     player.WorldPositionX -= 1;
  52.                                                 }
  53.                                             }
  54.                                             else //if (player.Velocity.X < 0) // the player comes FROM THE RIGHT and collides with the tile
  55.                                             {
  56.                                                 while (IntersectPixelsTileAndSprite(tile, player))
  57.                                                 {
  58.                                                     player.WorldPositionX += 1;
  59.                                                 }
  60.                                             }
  61.                                         }
  62.                                         else
  63.                                         {
  64.                                             if (player.Velocity.Y > 0) // the player comes FROM ABOVE and collides with the tile
  65.                                             {
  66.                                                 while (IntersectPixelsTileAndSprite(tile, player))
  67.                                                 {
  68.                                                     player.WorldPositionY -= 1;
  69.                                                     player.PositionState = PositionState.onGround;
  70.                                                     player.VelocityY = 0;
  71.                                                 }
  72.                                             }
  73.                                             else  //if (player.Velocity.Y < 0) // the player comes FROM UNDER and collides with the tile
  74.                                             {
  75.                                                 while (IntersectPixelsTileAndSprite(tile, player))
  76.                                                 {
  77.                                                     player.WorldPositionY += 1;
  78.                                                     player.VelocityY = 0;
  79.                                                 }
  80.                                             }
  81.                                         }
  82.                                     }
  83.                                     #endregion
  84.  
  85.                                 }
  86.                             }
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.  
  93.         public bool IntersectPixelsTileAndSprite(Scene2DTile tile, PlayerObject player)
  94.         {
  95.             // Find the bounds of the rectangle intersection
  96.             // That is: find the rectangle, in which the textures intersect
  97.             int top     = Math.Max(tile.BoundingBox.Top, player.Boundingbox.Top);
  98.             int bottom  = Math.Min(tile.BoundingBox.Bottom, player.Boundingbox.Bottom);
  99.             int left    = Math.Max(tile.BoundingBox.Left, player.Boundingbox.Left);
  100.             int right   = Math.Min(tile.BoundingBox.Right, player.Boundingbox.Right);
  101.  
  102.             //Now we create the colorarrays from the tiles, to compare
  103.             Color[] dataA = SourceFromTextureToColor(tile.Spritesheet, tile.SourceRectangle);
  104.             Color[] dataB = WholeTextureToColor(player.Sprite);
  105.  
  106.             // Check every point within the intersection bounds
  107.             for (int y = top; y < bottom; y++)
  108.             {
  109.                 for (int x = left; x < right; x++)
  110.                 {
  111.                     // Get the color of both pixels at this point
  112.                     Color colorA = dataA[(x - tile.BoundingBox.Left) +
  113.                                          (y - tile.BoundingBox.Top) * tile.BoundingBox.Width];
  114.                     Color colorB = dataB[(x - player.Boundingbox.Left) +
  115.                                          (y - player.Boundingbox.Top) * player.Boundingbox.Width];
  116.  
  117.                     // If both pixels are not completely transparent,
  118.                     if (colorA.A != 0 && colorB.A != 0)
  119.                     {
  120.                         // then an intersection has been found
  121.                         return true;
  122.                     }
  123.                 }
  124.             }
  125.             // No intersection found
  126.             return false;
  127.         }
  128.  
  129.         public Color[] SourceFromTextureToColor(Texture2D spritesheet, Rectangle sourceRectangle)
  130.         {
  131.             Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height];
  132.             spritesheet.GetData(0, sourceRectangle, data, 0, data.Length);
  133.             return data;
  134.         }
  135.  
  136.         public Color[] WholeTextureToColor(Texture2D sprite)
  137.         {
  138.             Color[] data = new Color[sprite.Width * sprite.Height];
  139.             sprite.GetData(data);
  140.             return data;
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement