Reputation: 139
I am making a little game in C#/XNA and I have this problem with collision.
I want to make a normal player movement and instead of just checking if for example the Up key is pressed, it also checks if player CanGoTop == true
:
if (keyState.IsKeyDown(Keys.Up) && CanGoTop == true)
{
// bla bla bla
}
I do this for left, right, up, down, and if a boolean is false, the player will not move (there is an else
statment after those 4 statements of movement that sets velocity.x
and .y
to 0)
I made a method inside my Player class:
public void RectangleInteraction(Rectangle anotherRectangle)
{
if (playerRectangle.Bottom <= anotherRectangle.Top &&
playerRectangle.Top >= (anotherRectangle.Top - playerRectangle.Height) &&
playerRectangle.Left <= anotherRectangle.Right &&
playerRectangle.Right >= anotherRectangle.Left)
{
CanGoBot = false;
}
else CanGoBot = true;
if (playerRectangle.Top >= anotherRectangle.Bottom &&
playerRectangle.Bottom <= (anotherRectangle.Bottom + playerRectangle.Height) &&
playerRectangle.Left <= anotherRectangle.Right &&
playerRectangle.Right >= anotherRectangle.Left)
{
CanGoTop = false;
}
else CanGoTop = true;
if (playerRectangle.Top <= anotherRectangle.Bottom &&
playerRectangle.Bottom >= anotherRectangle.Top &&
playerRectangle.Right <= anotherRectangle.Left &&
playerRectangle.Left >= (anotherRectangle.Left - playerRectangle.Width))
{
CanGoRight = false;
}
else CanGoRight = true;
if (playerRectangle.Top <= anotherRectangle.Bottom &&
playerRectangle.Bottom >= anotherRectangle.Top &&
playerRectangle.Left >= anotherRectangle.Right &&
playerRectangle.Right <= (anotherRectangle.Right + playerRectangle.Width))
{
CanGoLeft = false;
}
else CanGoLeft = true;
}
and then in my Game1.cs Update()
method I use my player instance to call that method there. It works for CanGoLeft
but for the other 3 bools, it doesn't.
I really don't know why, here are my 4 screenshoots with InGameMsgs
to help me out, and checking those Messages with my code tells me that the collision logic is good, but something else is wrong. Why does only CanGoLeft
work then?
Upvotes: 0
Views: 378
Reputation: 43876
Your logic seems broken at several points. You should use the Rectangle
's method like Offset
and Intersects
to check if the target Rectangle
s would colide. I think this comes close to your intention (assuming distance
is the value by which you're player moves):
public void RectangleInteraction(Rectangle anotherRectangle)
{
Rectangle down = playerRectangle; down.Offset(0, distance);
Rectangle up = playerRectangle; up.Offset(0, -distance);
Rectangle left = playerRectangle; left.Offset(-distance, 0);
Rectangle right = playerRectangle; right.Offset(distance, 0);
ConGoBot = !down.Intersects(anotherRectanlge);
ConGoTop = !up.Intersects(anotherRectanlge);
ConGoLeft = !left.Intersects(anotherRectanlge);
ConGoRight = !right.Intersects(anotherRectanlge);
}
(This is assuming you're using Microsoft.Xna.Framework.Rectangle
, there are other Rectangle
structs in .net framework, but your question was tagged xna)
Upvotes: 1