craftworkgames / MonoGame.Extended

Extensions to make MonoGame more awesome
http://www.monogameextended.net/
Other
1.44k stars 324 forks source link

Collision and Drawing with negative Width and Height not working #747

Open PalladiumPeet opened 2 years ago

PalladiumPeet commented 2 years ago

Hello, i came across an issue with negative Width and Height while drawing RectangleF or using it for Collision Detection. For example, when the shooting direction is to the upperleft corner starting by the player position, there will be a negative direction.

public void Draw(SpriteBatch spriteBatch)
{
    var a = new RectangleF(100, 100, -20, -20);
    spriteBatch.DrawRectangle(a, Color.White, 3);

    var b = new RectangleF(150, 100, 20, 20);
    spriteBatch.DrawRectangle(b, Color.White, 3);

    var c = new RectangleF(200, 100, -20, 20);
    spriteBatch.DrawRectangle(c, Color.White, 3);

    //this one is totally weird
    var d = new RectangleF(250, 100, 20, -20);
    spriteBatch.DrawRectangle(d, Color.White, 3);
}

this draws to this: grafik

and for Collision if have this example, which is always false

var shape1 = new RectangleF(32, 32, -32, -32);
var shape2 = new RectangleF(16, 16, -32, -32);

Assert.True(shape1.Intersects(shape2));

and when adding a ICollsionActor-Class with such a RectangleF with negative Width or Height to the CollisionComponent, i will never collide with anything.

I have looked through the other open issues and through the MG-Forum, but couldn't find anything of help on this topic. Please excuse me, if this isn't an issue (and i'm too stupid) or someone already knows about it.

Thanks in advance

Edit: Formatting Edit2: Have inserted the wrong code before, corrected it

kaltinril commented 2 years ago

@PalladiumPeet I know your post is old, but I"m looking through things while trying to understand my own question and figured I'd take a stab at giving a response.

I'm just a user like you, but to me, I don't understand why you have negative width and height values? How can you draw something that has no width, or rather, has inverted width?

At the bottom, I've linked to what appear to be the code for this RectangleF, but again, I don't think this is a bug, to me, a 2D object would never have a negative width. If you want to "flip" the image, that's not default rectangle behavior. An inverted (negative) width or height would basically be impossible in normal 2D environment.

Possible work-around: You could make a Helper method, or Create your own Rectangle class that uses RectangleF, or create an Extension Method to add some sort of "AdjustForNegativeDistances". Whichever you end use choosing, you'd take X, Y, Width, Height, and then you could "re-calculate" the X as being the MIN (clamp?) of X or X + Width, and Y being the MIN of Y or Y + Height. Then depending on which it is, you'd set the real width and height. One quick way would be.

  1. Helper Method: This would be the easiest, but you have no control over the width and height after they've been created.
  2. Extension Method: This would be more complicated, but you still have no direct control over the width and height after they've been created.
  3. Your Rectangle Class: This might be the best solution. You'd create a class that just has an instance of RectangleF, then you can intercept calls by creating methods that just pass along that functionality to RectangleF. This is a basically just a wrapper class around RectangleF.

https://github.com/craftworkgames/MonoGame.Extended/blob/a3373ac26d90c9801b71b55679f1199fa4ec22a6/src/cs/MonoGame.Extended/Math/RectangleF.cs#L146

https://github.com/craftworkgames/MonoGame.Extended/blob/a3373ac26d90c9801b71b55679f1199fa4ec22a6/src/cs/MonoGame.Extended/Math/RectangleF.cs#L65