I have a weird issue when it comes to detecting basic collision and frankly, I can't figure out why.
So my movement for my player is in my Player.cs Class and in this class contains a Update() method that is called in my main game loop in Game1.cs. I have it set up so that when the hitbox of my player 'Rectangle Hitbox;' intersects with a collision tile, it switches a boolean called 'inAir' to false. When this boolean is false, the force of gravity should be zero. The code in my Player.cs's Update method is as follows:
public void Update(GameTime gameTime) {
//In my constructor for the Player class
//Position is set equal to a new Vector2(0, 0)
//Velocity is set equal to Vector2.Zero
//Acceleration is set equal to a new Vector2(0, 100.0f);
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
Velocity += Acceleration * deltaTime;
Position += Velocity * deltaTime;
HitBox = new Rectangle(new Point((int)Position.X, (int)Position.Y), new Point(Width, Height));
foreach (SpriteTile t in gameMap.CollisionSprites) {
if (HitBox.Intersects(t.Bounds))
inAir = false;
else if (!HitBox.Intersects(t.Bounds))
inAir = true;
}
if (!inAir) {
Velocity.Y = -Acceleration.Y * deltaTime;
}
}
I also have some simple text displaying on the screen telling me the value of the boolean 'inAir' which will be shown in the image I provide a bit later.
In my Player.cs class above, a SpriteTile Object only stores 2 things, a Texture2D, and a Position which is predetermined when I load the collisions from my Tiled Collision Layer.
The Bounds Method however simply returns a rectangle showing the bounds of the current tile. It is defined as follows:
public Rectangle Bounds {
get {
return new Rectangle(
(int)Position.X,
(int)Position.Y,
Texture.Width,
Texture.Height);
}
}
The puzzling part about all this to me is when I added simple debug code to draw these values onto the screen, and test whether the collision's were working, I got this:
According to the bounding boxes I'm drawing, It should be intersecting, therefore stopping the fall. Even if I got the collision response code wrong, the boolean 'inAir' should be set to true. (Yes true because the string is drawn to the screen like so: "Is Intersecting: " + !Player.inAir meaning that if an intersection were to take place, it would draw to the screen true.
If any more information is needed about my code, let me know and I will edit the post and provide it.
If anyone can please help me figure out where I'm going wrong with such a simple idea's implementation, I would be more then grateful! Thanks guys.