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

Author Topic: C++ class related problem with object constructor  (Read 1268 times)

0 Members and 1 Guest are viewing this topic.

SantoZombi

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
C++ class related problem with object constructor
« on: June 28, 2019, 01:03:24 am »
Hello, world!

I'm a c++ newbie trying to code a RPG with a raycaster (based on Lode's tutorial). Everything was going smooth and I've got to the point where I could do the floor and ceiling casting. However, I ran into problems when I decided it was time to split the code in different classes.

The first class I was trying to add was "Player" which will store position, movement and "camera" parameters. Unfortunately, it went from looking like this:


to looking like this:


Using Eclipse's debugger I found that the player object is not constructing itself properly, which I found kind of weird but I guess it could some sort of pointer related stuff (honestly, I have no idea what it could be, other that my lack of knowledge of C++).

This is my class header:
Quote
#ifndef PLAYER_HPP_
#define PLAYER_HPP_

namespace player
{

   class Player {
   public:
      //========================================================================
      //Public attributes=======================================================
      //========================================================================
      //Raycaster parameters====================================================
      //Camera parameters
      unsigned short int cMapID; //Current map
      sf::Vector2f pos; //Initial player's position
      sf::Vector2f dir; //Initial player's direction: {(-1,0) = N, (0,1) = E, (1,0) = S, (0,-1) = W}
      sf::Vector2f cam; //Initial "camera plane": {(0,0.5) = N, (0.5,0) = E, (0,-0.5) = S, (-0.5,0) = W}
      //Auxiliar variables to check movement
      sf::Vector2f oldPos; //Auxiliar variable to evaluate movement
      bool isPlayerMoving = false; //Activate when the player moves
      //Movement & rotation parameters
      float baseSpeed; //Works for 0.16,0.2, 0.24, 0.28, 0.32 //Should try something similar to what I did with rotation
      float d2Wall; //Auxiliar variable to keep the player from moving at a minimum distance to walls
      short int rotFrames; //Number of rotation frames
      short int rotCFrame; //Auxiliar variable to check how many "frames" have passed
      double rotSpeed; //Rotation speed in rad/frame
      //Raycaster parameters====================================================

      //========================================================================
      //Public functions========================================================
      //========================================================================
      Player(unsigned short int _cMapID, sf::Vector2f _pos, sf::Vector2f _dir);
      virtual ~Player();
   };

}

#endif /* PLAYER_HPP_ */
and this is my class implementation file:
Quote
#include "GlobalVariables.hpp"
#include "Player.hpp"

namespace player
{

    Player::Player(unsigned short int _cMapID, sf::Vector2f _pos, sf::Vector2f _dir) {
        //Raycaster parameters====================================================
        //Camera parameters
        unsigned short int cMapID = _cMapID; //Current map
        sf::Vector2f pos = _pos; //Initial player's position
        std::cout << pos.x;
        sf::Vector2f dir = _dir; //Initial player's direction: {(-1,0) = N, (0,1) = E, (1,0) = S, (0,-1) = W}
        //Initial "camera plane": {(0,0.5) = N, (0.5,0) = E, (0,-0.5) = S, (-0.5,0) = W}
        if ( std::abs(dir.x) > std::abs(dir.y) )     sf::Vector2f cam(0, - dir.x / 2.0);
        else                                         sf::Vector2f cam(dir.y / 2.0 , 0);
        //Auxiliar variables to check movement
        sf::Vector2f oldPos = pos; //Auxiliar variable to evaluate movement
        bool isPlayerMoving = false; //Activate when the player moves
        //Movement & rotation parameters
        float baseSpeed = 0.2; //Works for 0.16,0.2, 0.24, 0.28, 0.32 //Should try something similar to what I did with rotation
        float d2Wall = 0.6 / baseSpeed; //Auxiliar variable to keep the player from moving at a minimum distance to walls
        short int rotFrames = 30; //Number of rotation frames
        short int rotCFrame = 0; //Auxiliar variable to check how many "frames" have passed
        double rotSpeed = 0; //Rotation speed in rad/frame
        //Raycaster parameters====================================================

    }

    Player::~Player() {
        // TODO Auto-generated destructor stub
    }

}
This is how I'm calling the object at my main:
Quote
    sf::Vector2f initPos(7.5, 8.5);
    sf::Vector2f initDir(0, - 1);
    player::Player mPlayer(0, initPos, initDir);

Looking at the variables with the debugger, the arguments are passing properly to the constructor (i.e. player.pos seems to update to whatever _pos is). However, when the code goes back to main(), the values passed to the constructor don't carry over. If I modify the header file as follows:
Quote
namespace player
{

    class Player {
    public:
        //========================================================================
        //Public attributes=======================================================
        //========================================================================
        //Raycaster parameters====================================================
        //Camera parameters
        unsigned short int cMapID; //Current map
        sf::Vector2f pos; //Initial player's position
        sf::Vector2f dir; //Initial player's direction: {(-1,0) = N, (0,1) = E, (1,0) = S, (0,-1) = W}
        sf::Vector2f cam; //Initial "camera plane": {(0,0.5) = N, (0.5,0) = E, (0,-0.5) = S, (-0.5,0) = W}
        //Auxiliar variables to check movement
        sf::Vector2f oldPos; //Auxiliar variable to evaluate movement
        bool isPlayerMoving = false; //Activate when the player moves
        //Movement & rotation parameters
        float baseSpeed = 0.2; //Works for 0.16,0.2, 0.24, 0.28, 0.32 //Should try something similar to what I did with rotation
        float d2Wall = 0.6 / baseSpeed; //Auxiliar variable to keep the player from moving at a minimum distance to walls
        short int rotFrames = 30; //Number of rotation frames
        short int rotCFrame = 0; //Auxiliar variable to check how many "frames" have passed
        double rotSpeed = 0; //Rotation speed in rad/frame

        //Raycaster parameters====================================================

        //========================================================================
        //Public functions========================================================
        //========================================================================
        Player(unsigned short int _cMapID, sf::Vector2f _pos, sf::Vector2f _dir);
        virtual ~Player();
    };

}
the predefined parameters are kept.

I've also tried declaring the class in the main file, but it doesn't work either. It seems to only work when I don't encapsulate stuff into a class.

Can someone please help me understand what I am doing wrong?
« Last Edit: June 28, 2019, 01:08:38 am by SantoZombi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: C++ class related problem with object constructor
« Reply #1 on: June 28, 2019, 08:02:07 am »
Please keep in mind that this is the SFML forum. We try to focus on SFML questions & issues; there are better places for basic C++ questions ;)

You're declaring new variables in Player's constructor, instead of using its members.
Laurent Gomila - SFML developer

SantoZombi

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: C++ class related problem with object constructor
« Reply #2 on: June 28, 2019, 05:19:16 pm »
Please keep in mind that this is the SFML forum. We try to focus on SFML questions & issues; there are better places for basic C++ questions ;)

You're declaring new variables in Player's constructor, instead of using its members.

I feel so silly right now. Thanks a lot!

 

anything