MonoGame source Rectangle not working

Hello, so,
I have ran into an issue where, I’m trying to add collisions into my game, to do this I need to have a relative rectangle,

so, in a simple case it works,

_spriteBatch.Draw(DoomWalkUp1, PlayerHitbox, Color.White);

when i test this with intersections, it works fine, however when I apply it to my main character, he doesn’t even spawn in, nor does he move or anything, (I debug his coordinates)

Main which doesn’t work -

_spriteBatch.Draw(CurrentFrame, CharacterPosition, PlayerHitbox, Color.White, 0f, new Vector2(DoomWalkUp1.Width/2, DoomWalkUp1.Height/2),
Vector2.One, SpriteEffects.None, 0f);

Main which works -

_spriteBatch.Draw(CurrentFrame, CharacterPosition, null, Color.White, 0f, new Vector2(DoomWalkUp1.Width/2, DoomWalkUp1.Height/2),
Vector2.One, SpriteEffects.None, 0f);

notice how in the one that works, I do not use a source rectangle, and instead I use ‘null’

where the source rectangle is declared in load content

PlayerHitbox = new Rectangle(CharacterPosX, CharacterPosY, 46, 58);

So, does anyone know how to apply the rectangle to the main character without causing issues?

The SpriteBatch Draw method that takes a source rectangle parameter is for a region within the image to draw to screen. I.e. if you had a sprite sheet with many images on it, and you wanted to select one specific region (source rectangle) of that texture to draw, you’d use the source rectangle parameter to “clip” that one out instead of drawing the whole texture. It has absolutely nothing to do with collision in any form, that is something you’d handle in the Update loop before drawing anything.

1 Like

Thanks for your response, that makes sense, however, in that case how would I link two rectangles to check for intersections or collisions, for example,

if(Rectangle1.Intersects(Rectangle2))
{
//do something
{

So, you need one rectangle to represent the position of your character.

And a SEPERATE rectangle, (some call it a source rectangle, I call it a stencil rectangle), to indicate what part of a larger image to draw INSIDE the first rectangle.

Collision is detected using the first rectangle, because IT contains data on where your hero is… The second rectangle, what he looks like, is not important, for this…

MY previous answer was unclear… Your characters POSITION rectangle, should check collision against the colliding objects position rectangle…

  • The stencil, or source as you call it, does not matter for collision detections.