Advertisement
konalisp

collision

Feb 20th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1.         public void ResolveCollisions() {
  2.             List<Entity> elist = GetCollisionsList(this.BoundingSprite);
  3.             if (elist.Count > 0) {
  4.                 foreach (Entity e in elist) {
  5.                     //Entity e = elist[0];
  6.                     CollisionPart ep = (CollisionPart)e.SignalPart("CollisionPart");
  7.                     //where to move
  8.                     float moveX = 0.0f;
  9.                     float moveY = 0.0f;
  10.                     //left/right/top/bottom
  11.                     float l =  ep.BoundingSprite.Position.X - (this.BoundingSprite.Position.X + this.BoundingSprite.Texture.Size.X);
  12.                     float r = (ep.BoundingSprite.Position.X + ep.BoundingSprite.Texture.Size.X) - this.BoundingSprite.Position.X;
  13.                     float t =  ep.BoundingSprite.Position.Y - (this.BoundingSprite.Position.Y + this.BoundingSprite.Texture.Size.Y);
  14.                     float b = (ep.BoundingSprite.Position.Y + ep.BoundingSprite.Texture.Size.Y) - this.BoundingSprite.Position.Y;
  15.                     // find the offset of both sides
  16.                     moveX = Math.Abs(l) < r ? l : r;
  17.                     moveY = Math.Abs(t) < b ? t : b;
  18.                     //speed at which entities will move out of each other (for cooler effect)
  19.                     //um, never mind, using delta time causes things to get pushed by
  20.                     //other entities when they shouldn't be able to.
  21.                     float s = 1; // Globals.DeltaTime.AsSeconds() * 26.0f;
  22.                     // only use whichever offset is the smallest
  23.                     if (Math.Abs(moveX) < Math.Abs(moveY))
  24.                         this.HorizontalPosition += moveX * s; else
  25.                         this.VerticlePosition   += moveY * s;
  26.                         //making these negative is very funny
  27.                         //multiplying the values by deltatime has a cool slowdown effect
  28.                 }
  29.                 this.UpdateBounding();
  30.             }
  31.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement