Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to compute AABB of circle shape with sf::Transform [SOLVED]  (Read 2285 times)

0 Members and 1 Guest are viewing this topic.

B026

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
How to compute AABB of circle shape with sf::Transform [SOLVED]
« on: December 02, 2016, 09:08:39 pm »
Hi guys, so, I'm developing a CircleShape class for collisions and it's defined by radius only. I have other classes such as EdgeShape and PolygonShape.

In EdgeShape and PolygonShape I transform the verteices that define them and then calculate minX, maxX, minY and maxY to create de AABB (someone like sf::FloatRect). Everything works perfectly.

The problem comes computing the AABB from the CircleShape. I tried with this code:
    AABB CircleShape::getAABB (const sf::Transform &transform)
   {
       return transform.transformRect (sf::FloatRect (0, 0, radius * 2.f, radius * 2.f));
   }
 

but when I rotate it, its width and height increase, and that is not correct.

Thanks!!   ;D
« Last Edit: December 03, 2016, 08:35:05 pm by B026 »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to compute AABB of circle shape with sf::Transform
« Reply #1 on: December 03, 2016, 01:24:40 am »
The rectangle axis-aligned bounding box contained the circle that you create (the FloatRect) - when rotated - results in a rotated rectangle and the axis-aligned bounding box of the rotated rectangle is larger than the unrotated rectangle.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to compute AABB of circle shape with sf::Transform
« Reply #2 on: December 03, 2016, 10:11:29 am »
I don't know if there's an "exact" method for computing the bounding box of a transformed circle, but at least you can do the same as for your other shapes: convert it to a polygon (that's what you do for drawing it anyway, right?) and apply the same process.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10830
    • View Profile
    • development blog
    • Email
How to compute AABB of circle shape with sf::Transform
« Reply #3 on: December 03, 2016, 10:31:57 am »
Question is why you'd want an AABB for a circle?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

B026

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How to compute AABB of circle shape with sf::Transform
« Reply #4 on: December 03, 2016, 08:34:49 pm »
Question is why you'd want an AABB for a circle?

Hahaha, you're question made me feel like a fool. XD. So well, I'm trying to check collisions using AABB method. Maybe is easier check collisions between a circle and an AABB instead of compute the AABB of a circle, but I didn't think that when I was trying to achieve it.

I don't know if there's an "exact" method for computing the bounding box of a transformed circle, but at least you can do the same as for your other shapes: convert it to a polygon (that's what you do for drawing it anyway, right?) and apply the same process.

I think in that option but maybe is not very efficient. Well, I thought create a regular polygon with a big number of vertex.

The rectangle axis-aligned bounding box contained the circle that you create (the FloatRect) - when rotated - results in a rotated rectangle and the axis-aligned bounding box of the rotated rectangle is larger than the unrotated rectangle.

You're right. Thanks. If you scale the sf::Transform, you will scale the circle radius too.

Well, finally i solved it.

I must to transform the position of the circle and then scale its radius. [SOLVED]

 

anything