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

Author Topic: Polar Vectors.  (Read 4910 times)

0 Members and 1 Guest are viewing this topic.

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Polar Vectors.
« on: July 20, 2012, 01:12:22 pm »
Calling an X and Y co-ordinate system a Vector has always confused me... after all, X and Y is a rectangular form, or Cartesian plotted point. Vectors usually have a Direction, and Distance, force, power, whatever, therefore would be more of a Polar co-ordinate system than Cartesian.

In any case, I'm not going to argue about what you call it, nor ask to change it, that isn't my point of coming here.

So... to my point.

I've been working on a new class for what I'm working on. A form of Polar Co-ordinate system. Rather than being true Polar, the Y is inverted, so that Negative Y is up, rather than Positive Y being up, to corrispond to screen co-ordinates, rather than Graph type Co-ordinates. Also, 0 is measured from north, rather from east, to corrispond with sprite rotation in SFML.

Why use such a thing?
Say you're working on a 2D top down persective game. you have an X and a Y movement, but you want the sprite to face the direction of the movement. You'd need too calculate an angle.
No problem, Create a new Polar class object, pass it your X and Y vector co-ordinates, and it'll convert it into an angle (t) (t, because it is a secondary symbol to θ, and i don't think θ is C friendly, plus I have no idea how to type it, this was copied and pasted), and a radial (r)(its a radius co-ordinate).

You might want the oposite type of thing, a throwing game for example. you have a force and direction, and need to calculate an X and Y Vector for your sprite movement. again, no problem, create the Polar object, then use the conversion function to make a Vector from it.

Just a couple of examples.

So far, the class only has 4 functions.
Vector2f ToVector(), Creates a vector from the Polar
FromVector(Vector2f), Creates new Polar co-ordinate from a Vector
Rotate(float Angle), Increments t, then adds or subtracts 360 untill its in the 0 to <360 range
SetRotation(float Angle), Sets t, then puts it in the 360 range.

Then I thought... Why not share it? but not untill its finished.

I am plenty willing to share this code, though you could also say I'm kinda hopeful that you'd like the idea so much, you'd want to include it in the next version of SFML.

I'm also interested in what other functions you think might be useful.

Also, so far it uses floats only. I am a fan of doubles over floats, but the reason I didn't make it a template class is mostly because... well... any kind of int just wouldn't really work for this kind of thing.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Polar Vectors.
« Reply #1 on: July 20, 2012, 01:45:24 pm »
I' m not exactly sure why you get confused, because vectors are defined through their x and y (and z, or even more) component and not through their distance an direction, those are only properties of the vector. E.g. the distance of a vector is the squareroot of the sum of each component squared. The direction is given through the two components. The same principles apply to velocity, force, etc vectors. In a 2D space they are given through the X and the Y component. As for a point, it's just a vector from the origin of the coordination system to the wanted location.

So you're offering some functions for a transformation into polar coordinates. That can be handy but since it's not an elementary function I guess there's no need for including it in SFML. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Polar Vectors.
« Reply #2 on: July 20, 2012, 02:00:35 pm »
Fair enough...

but still, any sugestions?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Polar Vectors.
« Reply #3 on: July 20, 2012, 04:23:39 pm »
Quote
So far, the class only has 4 functions.
Vector2f ToVector(), Creates a vector from the Polar
FromVector(Vector2f), Creates new Polar co-ordinate from a Vector
Rotate(float Angle), Increments t, then adds or subtracts 360 untill its in the 0 to <360 range
SetRotation(float Angle), Sets t, then puts it in the 360 range.
Where is the radius? And why not a single function instead of a class?
sf::Vector2f polarToVector(float angle, float radius);
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Polar Vectors.
« Reply #4 on: July 20, 2012, 05:16:49 pm »
My library Thor provides the class template thor::PolarVector2<T>. It has two members r (radius) and phi (polar angle) and can be implicitly converted to and from SFML vectors. Maybe it helps you.

By the way, I also implement a lot of functions on top of SFML vectors. Sometimes it's just easier to write
sf::Vector2f vector(...);
thor::rotate(vector, 45.f);
instead of constructing a polar vector each time.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Polar Vectors.
« Reply #5 on: July 20, 2012, 05:53:42 pm »
Quote
So far, the class only has 4 functions.
Vector2f ToVector(), Creates a vector from the Polar
FromVector(Vector2f), Creates new Polar co-ordinate from a Vector
Rotate(float Angle), Increments t, then adds or subtracts 360 untill its in the 0 to <360 range
SetRotation(float Angle), Sets t, then puts it in the 360 range.
Where is the radius? And why not a single function instead of a class?
sf::Vector2f polarToVector(float angle, float radius);

Thats not the entire class, just the functions. it has the local members of r and t for radius and angle, plus constructors.
I originally did have it as just a pair of functions, PolarToVector and VectorToPolar, but when I actually tried to put it into practice, I found myself using a structure that contained floats r and t to store my values, and realised it was probably just easier to store it all in 1 class.

Rotate and SetRotation came later when i discovered that after pretty much every "Polar.t += angle;" incrementation, I was using a "while(Polar.t >= 360.f) Polar.t -= 360.f;" check to keep it below 360, so included that in the class as a pair of functions. of course my check that was only for positive incrementations, because thats all I was doing, the function also includes a check for negative incrementations.
so the same code can now be replaced with "Polar.Rotate(angle);" instead.

Here's the entire class definition in my Polar.h file
// Note: This is not a true Polar representation.
// Y is inverted to match screen co-ordinates vs graph style co-ordinates.
// T (Angle) is measured from North vs from East.
// This should make it more useful for calculating sprite rotation from an X and Y movement.
class Polar
{
public :
        Polar();
        Polar(float r, float t);
        Polar(const sf::Vector2f& vector);
        float r; // Radial. aka force or speed
        float t; // Turn, Angular measurement in Degrees
        void Rotate(float angle); // increment t by angle. corrects to 0 - 360.
        void SetRotation(float angle); // sets t to angle. corrects to 0 - 360.
        const sf::Vector2f ToVector();
        void FromVector(const sf::Vector2f& vector);
        void FromVector(float x, float y);
};
 
As said, I purposely limited it to float only, since the only other type I'd use for something like this would be double, and SFML seems to be geared to be using floats over doubles.
« Last Edit: July 20, 2012, 06:06:38 pm by bobingabout »

 

anything