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

Author Topic: sf::Shape::GetPosition() returns nan  (Read 5292 times)

0 Members and 1 Guest are viewing this topic.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Shape::GetPosition() returns nan
« on: December 14, 2009, 05:33:23 am »
Ahhhh.
I'm trying to get the distance between two sf::Shapes in the latest SVN Build, but when I construct an sf::Shape, set its position, then try to get its x position with GetPosition().x, it gives nan, completely screwing up my simulation. HALP!
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Shape::GetPosition() returns nan
« Reply #1 on: December 14, 2009, 07:46:23 am »
Can you show a complete example?
Laurent Gomila - SFML developer

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Shape::GetPosition() returns nan
« Reply #2 on: December 14, 2009, 03:28:45 pm »
Well, you didn't ask for minimal, so:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <cmath>

class Particle{
    private:
        double myMass;
        double myRadius;
        sf::Shape myShape;
        sf::Vector2f myVelocity;
    public:
        Particle(double newX = 0.f, double newY = 0.f, double newMass = 2.f*3.14159265358979) :
        myMass(newMass),
        myRadius(newMass/(2.f*3.14159265358979)),
        myShape(sf::Shape::Circle(newX, newY, myRadius, sf::Color(255, 255, 255, 255))),
        myVelocity(sf::Vector2f(0.f, 0.f)){
        }
        double GetMass() const{
            return myMass;
        }
        double GetRadius() const{
            return myRadius;
        }
        const sf::Shape *GetShape() const{
            return &myShape;
        }
        sf::Vector2f GetVelocity() const{
            return myVelocity;
        }
        void Move(sf::Vector2f Delta){
            myVelocity += Delta;
        }
        void Update(){
            myShape.Move(myVelocity);
        }
        ~Particle(){
        }
};

std::vector<Particle> Particles;
sf::RenderWindow Window;

int main(){
    Window.Create(sf::VideoMode(512, 512, 32), "Solar System Simulator", sf::Style::Close, sf::ContextSettings(24, 8, 0, 3, 2));
    Window.SetFramerateLimit(50);
    srand(time(NULL));
    for (int i = 0; i < 100; i++){
        Particles.push_back(Particle(rand()%512, rand()%512));
    }
    while (Window.IsOpened()){
        sf::Event Event;
        while (Window.GetEvent(Event)){
            if (Event.Type == sf::Event::Closed || (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape))
                Window.Close();
        }
        Window.Clear();
        for (unsigned int s = 0; s != Particles.size(); s++){
            for (unsigned int t = s+1; t != Particles.size(); t++){
                double DeltaX = Particles.at(t).GetShape()->GetPosition().x - Particles.at(s).GetShape()->GetPosition().x;
                std::cout << Particles.at(t).GetShape()->GetPosition().x << '\n';
                double DeltaY = Particles.at(t).GetShape()->GetPosition().y - Particles.at(s).GetShape()->GetPosition().y;
                double Distance = DeltaX*DeltaX + DeltaY*DeltaY;
                double Mass = Particles.at(s).GetMass() * Particles.at(t).GetMass();
                double Newtons = (6.67428/100000000000.f)*Mass/Distance;
                double Direction = atan2(DeltaY, DeltaX);
                Particles.at(s).Move(sf::Vector2f(-cos(Direction)*Newtons/Particles.at(s).GetMass(), -sin(Direction)*Newtons/Particles.at(s).GetMass()));
                Particles.at(t).Move(sf::Vector2f(cos(Direction)*Newtons/Particles.at(t).GetMass(), sin(Direction)*Newtons/Particles.at(t).GetMass()));
            }
        }
        for (unsigned int i = 0; i != Particles.size(); i++){
            Particles.at(i).Update();
            Window.Draw(*Particles.at(i).GetShape());
        }
        Window.Display();
    }
    return EXIT_SUCCESS;
}

Stupid particle simulations. I don't see what the problem could be, as I have checked over my code several times, so if it is my fault, it's probably a really dumb mistake.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Shape::GetPosition() returns nan
« Reply #3 on: December 14, 2009, 03:45:41 pm »
Quote
Well, you didn't ask for minimal, so

You've just convinced me to never forget to mention it :lol:

Can you at least show me where the NAN occurs? Are you sure that it's not your calculations that produce it? I see very large numbers in your code.
Laurent Gomila - SFML developer

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Shape::GetPosition() returns nan
« Reply #4 on: December 15, 2009, 12:07:22 am »
Quote from: "Laurent"
Quote
Well, you didn't ask for minimal, so

You've just convinced me to never forget to mention it :lol:

Can you at least show me where the NAN occurs? Are you sure that it's not your calculations that produce it? I see very large numbers in your code.
std::cout << Particles.at(t).GetShape()->GetPosition().x << '\n'; gives me nan for each and every object it checks.

EDIT: Just changed the for loop towards the beginning to this:
Code: [Select]
   for (int i = 0; i < 100; i++){
        Particles.push_back(Particle(rand()%512, rand()%512));
        std::cout << Particles.at(i).GetShape()->GetPosition().x << '\n';
    }

and commented these lines:
Code: [Select]
               Particles.at(s).Move(sf::Vector2f(-cos(Direction)*Newtons/Particles.at(s).GetMass(), -sin(Direction)*Newtons/Particles.at(s).GetMass()));
                Particles.at(t).Move(sf::Vector2f(cos(Direction)*Newtons/Particles.at(t).GetMass(), sin(Direction)*Newtons/Particles.at(t).GetMass()));

And now the new std::cout at the beginning is giving 0s for all of the x positions, but the Particles are drawn in random places with any x position from 0-511.
I use the latest build of SFML2

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Shape::GetPosition() returns nan
« Reply #5 on: December 15, 2009, 04:45:02 am »
I found the problem. You have a terrible sf::Shape::Circle constructor. It works, but it doesn't set the position to the x and y coordinates I give it. It just creates a circle using x and y as center coordinates for the local point coordinates, instead of centering the local point coordinates around 0,0 and Setting the Circle's Position to (x,y). Very misleading system you have there, Laurent.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Shape::GetPosition() returns nan
« Reply #6 on: December 15, 2009, 07:38:05 am »
Quote
It works, but it doesn't set the position to the x and y coordinates I give it. It just creates a circle using x and y as center coordinates for the local point coordinates, instead of centering the local point coordinates around 0,0 and Setting the Circle's Position to (x,y). Very misleading system you have there, Laurent.

It may look misleading now for you, but if you think about it it makes perfect sense. The global position has nothing to do with the points that you define, just like other drawables (text and sprite).
If it was like you suggest, how would it handle an arbitrary polygon? Would it move its global coordinates to its center? What happens when you add a new point, all the points get changed to match the new center of the shape?

sf::Shape is a very simple system, you have local points and global transformations applied to these points, it does nothing more than what you define. Having a system like yours would be misleading.
If you want your circle to be centered around (0, 0) then just do this ;)
Laurent Gomila - SFML developer

DdR_Dan

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::Shape::GetPosition() returns nan
« Reply #7 on: January 17, 2010, 05:49:41 am »
I think my problem is similar to the one here but I don't understand the answer.  I just want to get the x and y distance between the center of a sprite and the center of a circle.  I've been trying to use GetPosition and GetCenter, but I think there's something I don't understand about the system...  :wink:

I want to learn the right way to do this:
Cursor.GetPosition().x-Ball.GetPosition().x;

Edit:  If it matters, I want to use the distances for the direction for the ball to move.  I'm planning on taking the x and y distances, dividing by the total length and multiplying by a speed to get the ball to move towards the cursor at a specific speed.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Shape::GetPosition() returns nan
« Reply #8 on: January 17, 2010, 10:44:34 am »
Just create your circle with (0, 0) as the center, then use SetPosition and GetPosition and everything will be fine ;)
Laurent Gomila - SFML developer

DdR_Dan

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::Shape::GetPosition() returns nan
« Reply #9 on: January 17, 2010, 05:21:21 pm »
Since this is my first time using SFML and doing graphics, it feels awesome to see something work.  Thank you very much.  :D

 

anything