The members of the structure return inf. C++



  • Class which contains the object of the structure vector2d

    class Foo {
    

    public:
    Foo() {
    acceleration_force = vector2d();
    //acceleration_force(); не видит конструктора вообще
    cout << "acc x: " << acceleration_force.x << " y: " << acceleration_force.y << endl;
    }
    private:
    vector2d acceleration_force;
    };

    My structure vector2d

    #ifndef VECTORS_H
    #define VECTORS_H

    struct vector2d {

    vector2d(float y, float x) : y(y), x(x) {}
    vector2d() : y(-1), x(0) {}
    
    float y, x;
    

    };

    #endif /* VECTORS_H /

    It's working. acc x: inf y: -inf

    Although the same code works successfully https://ideone.com/Xj2SVb

    I'll take it out with help. ncurses Function printw


    I seem to have a problem with the signs and references, as the debager shows everything right.

    Function AddAcceleration:

    void AddAcceleration(vector2d direction, float value) {
    acceleration_force = (direction.normal()value) * mass;
    }

    Function vector2d::normal()

    vector2d normal() {
    vector2d result(this);
    result.x = result.length() / result.x;
    result.y = result.length() / result.y;
    return result;
    }

    Function GetForce:

    vector2d GetForce() {
    return acceleration_force;
    }

    Function vector2d::lenght:

    float length() {
    return sqrt(yy+xx);
    }

    Removed classes Foo:

    private:
    float mass;
    vector2d acceleration_force;
    float speed;
    vector2d friction;
    vector2d force;
    vector2d direction;

    GetMass()GetSpeed() Just ghetters.

    Section of the code that turns the vector values on the screen:

    foo = Foo();
    foo.AddAcceleration(vector2d(-1,0), 2);
    printw("%f, %f", mov.GetForce().x, mov.GetForce().y);

    Also, please see the classes where I overloaded the cameras. and + structures vector2d♪ They don't work either, I understand.

    vector2d operator+(const vector2d& right) const {
    vector2d result(*this); // Make a copy of myself.
    result.x += right.x;
    result.y += right.y;
    return result;
    }

    vector2d operator*(const float right) const {
    vector2d result(*this);
    result.x *= right;
    result.y *= right;
    return result;
    }



  • I figured out what the problem is. inf-inf
    When х = 0, y = -1, considered length sqrt( -1 * -1 + 0 * 0) == 1, variable x becomes x = 1 / x = 1 / 0 = inf, further the length is already considered as sqrt ( -1 * -1 * inf * inf)♪ Plus, we had to split the coordinates for the length of the vector. And multiplying. mass Yeah. nanOh, well. mass not initiated


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2