Those conditions look wrong, at a glance. For example:
if (playerRectangle.Top <= anotherRect.Bottom &&
playerRectangle.Bottom >= anotherRect.Top &&
Well, It can, becouse we are on an Y axis.
The top of the Screen is 0 and bottom is idk lets say 1000.
lets say enemy.Rect.Bottom = 500; and enemy.Rect.Top = 450; (Top is less becouse we are on the Y axis, not X)
So if player,Rect.Bottom is >= 450 && player.Rectangle.Top <= 50, Then that means, that if Player is going to move right or left,
he will at some point intersect with enemyRectangle. The Collision logic is good, I made it working, My problem here is, as the
thread title says: Array problem. I have written everything above, and again, I am experiencing a strange thing.
Now I moved my CollisionLogic away from player or entity classes, I made a public static class RectangleCollision with static method called
CollisionAmong() that takes 2 arguments -> 2 rectangles that might intersect, and then if they do the player wont be able to move.
public static class RectangleCollision
{
public static void CollisionAmong(Rectangle oneRect, Rectangle anotherRect)
{
if (oneRect.Bottom >= (anotherRect.Top - 2) && // - 2 becouse they should not intersect becouse of bug issues
oneRect.Top <= anotherRect.Top &&
oneRect.Left < anotherRect.Right &&
oneRect.Right > anotherRect.Left)
{
Player.CanGoBot = false;
}
else Player.CanGoBot = true;
if (oneRect.Top <= (anotherRect.Bottom + 2) && // + 2 becouse they should not intersect becouse of bug issues
oneRect.Bottom >= anotherRect.Bottom &&
oneRect.Left < anotherRect.Right &&
oneRect.Right > anotherRect.Left)
{
Player.CanGoTop = false;
}
else Player.CanGoTop = true;
if (oneRect.Top < anotherRect.Bottom &&
oneRect.Bottom > anotherRect.Top &&
oneRect.Right >= (anotherRect.Left - 2) && // - 2 becouse they should not intersect becouse of bug issues
oneRect.Left < anotherRect.Left)
{
Player.CanGoRight = false;
}
else Player.CanGoRight = true;
if (oneRect.Top <= anotherRect.Bottom &&
oneRect.Bottom >= anotherRect.Top &&
oneRect.Left <= (anotherRect.Right + 2) && // + 2 becouse they should not intersect becouse of bug issues
oneRect.Right > anotherRect.Right)
{
Player.CanGoLeft = false;
}
else Player.CanGoLeft = true;
}
}
CollisionChecking on its own is correct, tested it a lot and was struggling with it for some time and I finally made it good, I like it the way it is now
and I dont want to change it, it is not a problem.
The problem comes now:
As I said, In my
Game1.cs class I made player and 2 things, barrier and enemy.
Entity[] entites;
protected override void Initialize()
{
player = new Player(50, 300);
entites = new Entity[2];
entites[0] = new Entity("enemy",ObjectType.Enemy, 400, 300); // enemy
entites[1] = new Entity("barrier", ObjectType.Object, 150, 200); // barrier
DrawLowTxt = true;
base.Initialize();
}
and I will post this draw method here as well:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DimGray);
spriteBatch.Begin();
map.DrawMap(spriteBatch);
player.Draw(spriteBatch);
foreach (Entity entity in entites)
{
entity.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
Okay now everything works great, there are player barrier and enemy drawn. (and map but it has nothing to do with anything and is not
important right now).
So now, In Game1.cs Update()method I call Collison() method.
Collision method is a method in Game1.cs class which I want to have for
Collision Checks, and now, my Collision() method looks like this:
private void Collision()
{
RectangleCollision.CollisionAmong(player.playerRectangle, entities[0].EntityRect); // 1st statment
RectangleCollision.CollisionAmong(player.playerRectangle, entities[1].EntityRect); // 2nd statemnt
}
As I said many times before, Now when I build and run my game, player will have Collision stuff with barrier only, not enemy
cause barrier is on 2nd stament (as I said before
, I dont know why but only second statement is executed in game, and 1st statment is not working <--- THIS IS THE PROBLEM).
Again, If that Collision method looked like this:
private void Collision()
{
RectangleCollision.CollisionAmong(player.playerRectangle, entities[1].EntityRect); // now 1st stament is checking for collision with entities[1]
RectangleCollision.CollisionAmong(player.playerRectangle, entities[0].EntityRect); // other way around than when it was like above
}
Now player will have Collision with enemy, not barrier, becouse, enemy is on 2nd stament, and Collision wont work on barrier.
I hope that now you guys can realize what is my problem, and maybe someone knows why this is happening :/