I have been trying to make a vector class to handle particles in a particle system I am making. The problem I have is when I try to change the dir it does not do what it is supposed to. If I keep pressing up it will just bounce back from between two directions same with down. But if I go up down repeatedly it changes a little bit but not much. I cannot figure out why this is happening.
This is my class

struct sPosition{float x,y;};

class cVector{
    float Mag;
    sPosition Pos;
    sPosition Dir;

    public:
    cVector(float PosX = 0.0f,float PosY = 0.0f, float Magn = 0.0f, float Degrees = 0.0f){
        Pos.x = PosX;
        Pos.y = PosY;
        SetDirD(Degrees);
        Mag = Magn;
    }
    float GetPosX(){return Pos.x;}
    float GetPosY(){return Pos.y;}
    float GetDirX(){return Dir.x;}
    float GetDirY(){return Dir.y;}
    float GetDirR(){return float (atan2(Dir.x,Dir.y * -1.0f));}
    float GetMag(){return Mag;}

    void SetPos(float PosX,float PosY){Pos.x = PosX; Pos.y = PosY;}
    void SetDir(float DirX,float DirY){Dir.x = DirX; Dir.y = DirY;}
    void SetDirR(float Rads){
        Dir.x = float (cos(Rads));
        Dir.y = float (sin(Rads) * -1.0);
    }
    void SetDirD(float Degs){
        Dir.x = float (cos(DegToRad<float>(Degs)));
        Dir.y = float (sin(DegToRad<float>(Degs)) * -1.0);
    }
    void SetMag(float Magn){Mag = Magn;}


};

this is my main.cpp just in case it is useful. I am using sfml graphics library btw.

#include <SFML/Graphics.hpp>
#include "Vectors.h"
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

int main(){
    cVector VectA(200.0f,300.0f,100.0f,45.0f);
    // Create the main rendering window


    // Start game loop
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event)){
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
                VectA.SetDirR(VectA.GetDirR() + 0.1f);
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
                VectA.SetDirR(VectA.GetDirR() - 0.1f);

        }

        // Clear the screen (fill it with black color)
        App.Clear();
        App.Draw(sf::Shape::Line(VectA.GetPosX(),VectA.GetPosY(),
                                 VectA.GetPosX() + (VectA.GetDirX() * VectA.GetMag()),
                                 VectA.GetPosY() + (VectA.GetDirY() * VectA.GetMag()),
                                 2.0f,sf::Color::Red));

        App.Display();
    }

    return EXIT_SUCCESS;
}

Recommended Answers

All 2 Replies

Your order of arguments for atan2 is wrong. It should be (y, x), not (x, y).

Your order of arguments for atan2 is wrong. It should be (y, x), not (x, y).

ooh thank you, didn't know that I was wrestling with this for a while lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.