SFML community forums

Help => Graphics => Topic started by: B026 on December 02, 2016, 09:08:39 pm

Title: How to compute AABB of circle shape with sf::Transform [SOLVED]
Post by: B026 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
Title: Re: How to compute AABB of circle shape with sf::Transform
Post by: Hapax 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.
Title: Re: How to compute AABB of circle shape with sf::Transform
Post by: Laurent 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.
Title: How to compute AABB of circle shape with sf::Transform
Post by: eXpl0it3r on December 03, 2016, 10:31:57 am
Question is why you'd want an AABB for a circle?
Title: Re: How to compute AABB of circle shape with sf::Transform
Post by: B026 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]