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

Author Topic: sf::Circle<typename T>  (Read 3011 times)

0 Members and 3 Guests are viewing this topic.

Njifra

  • Guest
sf::Circle<typename T>
« on: March 15, 2014, 03:57:27 pm »
My idea is that SFML has something similar to sf::Rect<typename T> that is based on circle (sf::Circle<typename T>)...
So what I mean, is to have sf::Circle<typename T> class that holds middle position(X, Y) and radius.
Meh... coding says more then my words :D

Here is how it should look:
namespace sf
{
        template <typename T>
        class Circle
        {
        public:
                T x, y;
                T radius;

                Circle<T>()
                {
                        this->x      = T(0);
                        this->y      = T(0);
                        this->radius = T(0);
                }

                Circle<T>(T _x, T _y, T _radius)
                {
                        this->x      = _x + _radius;
                        this->y      = _y + _radius;
                        this->radius = _radius;
                }

                Circle<T>(sf::CircleShape cShape)
                {
                        this->x      = T(cShape.getRadius() + cShape.getPosition().x);
                        this->y      = T(cShape.getRadius() + cShape.getPosition().y);
                        this->radius = T(cShape.getRadius());
                }

                bool intersects(sf::Circle<T> circle)
                {
                        bool ret = true;
                        if (sqrt(pow(this->x - circle.x, 2) + pow(this->y - circle.y, 2)) >= this->radius + circle.radius)
                                ret = false;

                        return ret;
                }

                bool contains(sf::Vector2<T> vecT)
                {
                        bool ret = false;
                        if (sqrt(float(pow(float(this->x - vecT.x), (float)2) + pow(float(this->y - vecT.y), (float)2))) <= this->radius)
                                ret = true;

                        return ret;
                }
        };

        typedef sf::Circle<int> IntCircle;
        typedef sf::Circle<double> DoubleCircle;
        typedef sf::Circle<float> FloatCircle;
        typedef sf::Circle<unsigned int> UintCircle;
}
 

It probably says all...
And here is small example of usage:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <typeinfo>
#include "SFML\Graphics.hpp"
#include "SFML\System.hpp"
#include "SFML\Window.hpp"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
        sf::RenderWindow window;
        window.create(sf::VideoMode(600, 400), "FloatCircle");

        sf::CircleShape sfCircle(20);
        sfCircle.setPosition(100, 100);
        sfCircle.setFillColor(sf::Color::Blue);

        sf::FloatCircle myCircle(sfCircle);
        // or:
        // sf::FloatCircle myCircle(100, 100, 20);
        // ---------------------------------------
        // or:
        // sf::FloatCircle myCircle;
        // myCircle.radius = 20;
        // myCircle.x = 100 + myCircle.radius;
        // myCircle.y = 100 + myCircle.radius;

        while(window.isOpen())
        {
                sf::Event event;
                 while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                 }

                if (myCircle.contains((sf::Vector2f)sf::Mouse::getPosition(window)))
                         sfCircle.setFillColor(sf::Color::Red); //Collision.
                else
                         sfCircle.setFillColor(sf::Color::Blue); //No collision.

                window.clear();
                window.draw(sfCircle);
                window.display();
         }

        return 0;
}
 

I hope it helps.
Well its not that good idea, but its easy to use.
Btw, sorry for my bad english.
;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Circle<typename T>
« Reply #1 on: March 15, 2014, 04:00:32 pm »
Well its not that good idea
Why do you propose it then? :P

sf::Rect is different, it clearly has its uses inside SFML: texture rects, views, bounds, ...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Njifra

  • Guest
Re: sf::Circle<typename T>
« Reply #2 on: March 15, 2014, 04:05:29 pm »
Beacuse, maybe someone doesent know how to do circle collision detection or check if point is inside of circle...
And it would be awesome if it is already upgraded in the SFML....
Yea.... I really dont know why I proposed it....

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Circle<typename T>
« Reply #3 on: March 15, 2014, 04:08:49 pm »
Beacuse, maybe someone doesent know how to do circle collision detection or check if point is inside of circle...
I agree that such a class can be useful, but collision detection is not SFML's application field. For such code snippets, we have the Wiki ;)

I also have some suggestions for your code:
  • Compare the squares, not square roots
  • Use the constructor initializer list
  • Don't construct from circle shapes (if you really want to provide this, a global function might be better -- or even two, one of them for the opposite direction)
  • Pass class parameters by const-reference, not value
  • Instead of bool ret = false; if (expr) ret = true; return ret; write return expr;
  • Omit the <T> at constructor definitions
« Last Edit: March 15, 2014, 04:18:09 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Njifra

  • Guest
Re: sf::Circle<typename T>
« Reply #4 on: March 15, 2014, 04:17:07 pm »
Ok.
Sorry guys for making stupid ideas... But I never have good and useful ideas, so I thought this one will be awesome....
Sorry...
Do you guys have any ideas?  XD

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Circle<typename T>
« Reply #5 on: March 15, 2014, 04:19:46 pm »
Sorry guys for making stupid ideas... But I never have good and useful ideas, so I thought this one will be awesome....
No need to feel sorry. The idea is not bad, but it's not something that should be part of SFML.

Do you guys have any ideas?  XD
Believe me, there are more than enough of them :D
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything