I am using the SFML library to create a 2D grid. i use grid cords so I can place my players on the grid:

I use struct to hold information of the nodes

struct Node


std::pair <int, int> node;

bool iswall = false;


I created the Grid class function where I set the position.

void Gridspot::setEnemyPosition(int i, int j)


Enemytank.node = std::make_pair(i,j);


I created the enemy in a different class

class AIEnemy




void Update();

sf::RectangleShape getRect();

void setNodePosition(sf::RectangleShape \* pos);

void AutoMotion(int col, int row);


i entered the properties of the enemy(size, colour etc.) in its constructor.



ect.setSize(sf::Vector2f(25.f, 25.f));

sf::Vector2f origin = rect.getSize();

rect.setOrigin(sf::Vector2f(origin.x /2 , origin.y / 2));



and I set the sfml shape in the grid by getting the node cord in the parameter and getting its position in the window

void AIEnemy::setNodePosition(sf::RectangleShape * pos)


sf::Vector2f origin = pos->getSize();

rect.setPosition(pos->getPosition().x+ origin.x /2, pos->getPosition().y + origin.y / 2);


in the main :

if (i == pAInode.first && j == pAInode.second)




now, I did not set the position in the constructor because I needed to set it in the game loop where im creating the visual display of the grid.

problem is that when I now call the update function:

void AIEnemy::Update()


bottom = rect.getPosition().y + rect.getSize().y;

left = rect.getPosition().x;

right = rect.getPosition().x + rect.getSize().x;

top = rect.getPosition().y;


it returns 0,0 even when I set the node position elsewhere. I know this is due to me not setting it in the constructor so my question is: is there a way i can set the position after creating the object?

  • 2
    \$\begingroup\$ Reading your question, I'm confused, there are multiple pieces that do not seem to fit together. Where is rect declared, what is it? Could you provide a minimum complete verifiable example, or at least some more complete code? \$\endgroup\$ – Vaillancourt Feb 17 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.