1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if (Physics.AABBvsAABB(Crate.AABB, Floor.AABB)) { Crate.Collided = true; Floor.Collided = true; } Crate.Update(gameTime); Floor.Update(gameTime); .... some code Renderer.Draw(Crate.GetRenderPos(), Crate.Texture,Camera.get_transformation()); Renderer.Draw(Floor.GetRenderPos(), Floor.Texture,Camera.get_transformation()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | public enum FacingDirection { Up = 0, Down = 1, Left = 2, Right = 3 } public enum EntityTag { Player = (0 << 1), Enemy = (1 << 1), Crate = (2 << 1), Floor = (3 << 1) } public struct Entity { public bool Active ; public bool Visible ; public bool Collided ; public bool Special; public FacingDirection Direction; public EntityTag Tag; public long Health; public long Stamina; public long MaxHealth; public long MaxStamina; public float HealthRegen; public float StaminaRegen; public Texture2D Texture; public AABB AABB; public Vector2 Dim; public Vector2 Pos; public Vector2 dPos; public Vector2 ddPos; public static Color _Color = Color.White; public static int depth = 0; public Color Mask{ get{ return _Color; } set { _Color = value; } } public void SetDim() { Dim.X = Texture.Width; Dim.Y = Texture.Height; } public void SetAABB() { AABB.Center = Pos; AABB.HalfDim = Dim/2; } public Vector2 GetRenderPos() { return AABB.Center - AABB.HalfDim; } public void Update(GameTime gt) { float dt = (float)gt.ElapsedGameTime.TotalSeconds; AABB.Center = Pos; dPos = Vector2.One; if (Collided) { ddPos = -ddPos * 0.1f; Collided = false; } dPos *= ddPos; Pos += dPos * dt; } } |
1 2 3 4 5 6 7 8 9 | public struct AABB { public Vector2 Center; public Vector2 HalfDim; } public static bool AABBvsAABB(AABB A, AABB B) { return (Math.Abs(A.Center.X - B.Center.X) > (A.HalfDim.X + B.HalfDim.X) || Math.Abs(A.Center.Y - B.Center.Y) > (A.HalfDim.Y + B.HalfDim.Y)) ? false : true; } |
1 2 3 4 5 6 7 8 9 10 | Crate.Update(gameTime); Floor.Update(gameTime); if (Physics.AABBvsAABB(Crate.AABB, Floor.AABB)) { Crate.Collided = true; Floor.Collided = true; Crate.Pos = Crate.PrevPos; Crate.InAir = false; Crate.ddPos = Vector2.Zero; } |