Hello, I’m not sure if this is the right place to ask but I have a lasting problem with rectangle.Intersects() method in monogame.
I’m using a combination of Tiled and Monogame together, and have used the following logic to implement a collision system.
foreach (var o in map.ObjectGroups[“collision”].Objects)
{
collisionObjects.Add(new Rectangle((int)o.X + bounds.X, (int)o.Y + bounds.Y, (int)o.Width, (int)o.Height));
}
=for loading the objects that will collide and,
foreach (var rect in _collisionObjects)
{
if (rect.Intersects(Rectangle))
{
Position = initPos;
Velocity = Vector2.Zero;
}
else
{
Position += Velocity;
Velocity = Vector2.Zero;
}
}
=for stopping the player when an obstacle is detected.
It does work fine in some aspects but the problem is that the size of the rectangle to which it collides is always off by 1, or half a pixel which results in the size of the obstacle not equating to the dimensions it was originally created as.
I have attempted to debug this problem by adding the following code
foreach (var VARIABLE in _collisionObjects)
{
spriteBatch.DrawRectangle(VARIABLE, Color.Azure);
}
This line(s) of code draws the rectangle of the obstacle that is in _collisionObjects.
I wouldn’t have had a hard time if the offsets were constant across all obstacles, but problem is that they all vary and seems to carry no pattern.
I know monogame isn’t joking with me and there’s a 100% certainty that I’m the one at fault here but I couldn’t get a grasp of why and how the obstacle dimension could go off and I thought I really would want some help or insights from you guys.
Please help me…