Hitboxes and Rotations

I'm having some minor problems with working with rotated hitboxes on projectiles.
Let me give some information on how my hitboxes generally work:

- All projectiles have a hitbox of either of the two:
- A custom hitbox decided by custom width and height (used for projectiles that use sprites that have some glow or other effects that shouldn't be included in the actual hitbox).
OR
- A hitbox that is identical to the sprite the projectile uses.

For the second type of hitbox, there are no particular issues, but for the first type, it causes some weird problems - the reason why I consider it to be weird is because of my lack of math understanding.

Here's the code I'm using for the hitboxes and the actual visible sprites (noted as Animation):

Hitbox Property (will always be an empty rectangle if not set as the second type of hitbox)
  1.        /// <summary>
  2.         /// The hitbox rectangle. Can be null.
  3.         /// </summary>
  4.         public Rectangle Hitbox
  5.         {
  6.             get {
  7.                 Rectangle rect = _hitbox;
  8.                 Vector2 offset = GetOffset(rect.Width, rect.Height);
  9.                 rect.X = (int)Position.X - (int)offset.X;
  10.                 rect.Y = (int)Position.Y - (int)offset.Y;
  11.                 return rect;
  12.             }
  13.             set
  14.             {
  15.                 _hitbox = value;
  16.             }
  17.         }
  18.         Rectangle _hitbox = new Rectangle();
Expand to see the code.


The actual hitbox. As shown, this will be set to the sprite's hitbox if the above hitbox returns empty.
  1. public Rectangle HitboxFinal
  2.         {
  3.             get
  4.             {
  5.                 // If a hitbox is provided: check if intersecting
  6.                 if (_hitbox != new Rectangle())
  7.                     return Hitbox;
  8.                 else if (Animation != null)
  9.                     return new Rectangle((int)Position.X - (int)GetOffset(Animation.Width, Animation.Height).X,
  10.                         (int)Position.Y - (int)GetOffset(Animation.Width, Animation.Height).Y, Animation.Width,
  11.                         Animation.Height);
  12.                 else
  13.                     return new Rectangle((int)Position.X - 16,
  14.                         (int)Position.Y - 16, 32, 32);
  15.             }
  16.         }
Expand to see the code.


The GetOffset() method. I reckon this is where I am doing something wrong (since, you know, math..).
  1.        public Vector2 GetOffset(int width, int height)
  2.         {
  3.             // If not rotation: return vector
  4.             if (Effect.Properties.ContainsKey("rotate"))
  5.             {
  6.                 if (Effect.Properties["rotate"].Value == "false")
  7.                     return new Vector2(width / 2, height / 2);
  8.             }else
  9.                 return new Vector2(width / 2, height / 2);
  10.  
  11.             // Set up the rotated rectangle
  12.             RotatedRectangle rect = new RotatedRectangle(
  13.                 new Rectangle((int)Position.X, (int)Position.Y, width, height), Angle);
  14.             rect.Origin = new Vector2(width / 2, height);
  15.  
  16.             // Set up the offset
  17.             Vector2 offset = new Vector2();
  18.  
  19.             // Set the X-offset
  20.             offset.X = (float)Math.Sqrt(Math.Pow(rect.UpperLeftCorner().X - rect.UpperRightCorner().X, 2) +
  21.                 Math.Pow(rect.UpperLeftCorner().Y - rect.UpperLeftCorner().Y, 2)) / 2f;
  22.             offset.Y = (float)Math.Sqrt(Math.Pow(rect.UpperLeftCorner().X - rect.LowerLeftCorner().X, 2) +
  23.                 Math.Pow(rect.UpperLeftCorner().Y - rect.LowerLeftCorner().Y, 2)) / 2f;
  24.  
  25.             // Return the offset
  26.             return offset;
  27.         }
Expand to see the code.


To explain, the "Position" vector is the center point of the projectile.
However, when the sprite rotates, the actual width and height of the sprite changes, and the hitbox seems to incorrectly function when its smaller than the sprite itself.
The code for this works relatively well, but has some slight inaccuracy which I don't really want to have there.

If anyone sees any obvious mistakes in the code, I'd appreciate any pointers.

Thanks!
It looks like you're making this harder than it is, each entity has a sprite and a hit box, it may be easier to give the entity a default hit box for the entity based on sprite and then on initialisation have a function to reset the entity's hitbox, that way you don't need to muck around with calculating if it already has one.

On the collision check (should be done after every step) you can rotate the point you're checking around the centre of the collision rectangle in the opposite angle of the entity's rotation, that will give you a correct grid to do an isInsideRectangle() call, the hardest part is the rotating back but there are plenty of examples on Google
Thing is, not all actions have a sprite associated with them, and many of these sprites have outer edges that shouldn't be included with their hit box.
Thing is, if a projectile rotates, the hitbox has to rotate in the same fashion, and to make this accurate I have to figure out the width and height (on aligned planes) after the rotation has occured.

Such as if a sprite is at the size 100 x 50 when it's being fired straight up (0 degrees), it would have the dimensions of 50 x 100 when its fired to the left or to the right, and in between those angles it would gradually change between those two values. But the hit box assigned to it could be 80 x 30 which then should align from the center point. When pointing straight up this is a simple calculation:

sprite position = Effect X - Width / 2 , Effect Y - Height / 2
hit box position = Effect X - Box Width / 2 , Effect Y - Box Height / 2

Makes the hitbox perfectly centered within the sprite and creating the result that I want.
However once the sprite gets rotated, the Width and Height value is different, but the actual values stored into those properties do not.
This is where the core of the problem lies.

I feel that the code does most of what it should do, but the hit boxes are slightly inaccurate, and I just want to get that sorted out.

If I misunderstood your proposition, please do clarify in better detail so I might understand it properly. If anything simpler would do the same and result in better detection, I wouldn't say no to it!

Thanks for the reply.
I did try to explain about the rotation.

Here's code to eat.
  1.  
  2. struct Rectangle
  3. {
  4.     float width;
  5.     float height;
  6. };
  7.  
  8. struct Vector2D
  9. {
  10.     float x;
  11.     float y;
  12. };
  13.  
  14. class Hitbox
  15. {
  16. private:
  17.     Rectangle bounding;
  18.     Vector2D offset;
  19. public:
  20.     Hitbox(Rectangle _bounding, Vector2D _offset) // Constructor
  21.     {
  22.         bounding = _bounding;
  23.         offset = _offset;
  24.     };
  25.  
  26.     Vector2D rotateAboutPivot(Vector2D point, Vector2D pivot, float radian)
  27.     {
  28.         float sinRad = sin(radian);  
  29.         float cosRad = cos(radian);  
  30.  
  31.         // Translate point back to origin:  
  32.         point.x -= pivot.x;  
  33.         point.y -= pivot.y;  
  34.  
  35.         // Rotate point  
  36.         Vector2D finalPoint;
  37.         finalPoint.x = point.x * cosRad - point.y * sinRad;  
  38.         finalPoint.y = point.x * sinRad + point.y * cosRad;
  39.  
  40.         // Translate point back to original offset:
  41.         finalPoint.x += pivot.x;  
  42.         finalPoint.y += pivot.y;
  43.  
  44.         return finalPoint;
  45.     };
  46.    
  47.     bool checkHit(Vector2D point, Vector2D entityPosition, float entityAngle)
  48.     {
  49.         float width2 = width / 2.0f;
  50.         float height2 = height / 2.0f;
  51.        
  52.         point = rotateAboutPivot(point, entityPosition, entityAngle * 0.0174532925f);
  53.        
  54.         // Check if point x and y are within the bounding box
  55.         if ((point.x < offset.x + point.x + width2)
  56.         && point.x > offset.x - point.x - width2)
  57.         point.y < offset.y + point.y + height2)
  58.         point.y > offset.y - point.y - height2))
  59.         {
  60.             return true;
  61.         }
  62.         return false;
  63.     };
  64. };
  65.  
Expand to see the code.


Hope you can make sense of that for your language here.
Thanks for the code. That is probably going to be very useful.
Although, since all my angle values are always in radians, I assume the "entityAngle * 0.0174532925f" can just be changed into "entityAngle" with no conversion.

Thanks for this, looks just about right.
Oh yes it should be -entityAngle so it is rotated the other direction to match the bounding box that is always in fixed position, otherwise it is sending the point further round the bounding box.

There is also probably a way to eliminate the sin and cos to improve performance, lookup tables is one but something tells me you can replace that with some thing else entirely that would perform better.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=77067