Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
PalladiumPeet opened this issue Nov 26, 2021 · 1 comment
Open

Comments

@PalladiumPeet
Copy link

PalladiumPeet commented Nov 26, 2021

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
Copy link

@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.

public RectangleF(float x, float y, float width, float height)

public float Right => X + Width;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants