I am having trouble getting my updated position for my objects. Whenever I call .getPosition()
in the main loop, the objects positions remains static despite the object moving. For example, an object that was placed at (500,200), remains at the same position despite continuous translations.
However, whenever I output the position in the Update method, using sf::RectangleShapes getPosition(), the position updates, but the orientation of the coordinate system changes. The zero for the X axis is placed at the red line, while the value 300 for the x axis is placed at the green (The Window is 800 by 600). (See screenshot below). The code looks like this
Monster.h
class Monster : public sf::Drawable, public sf::Transformable
{
public:
void Update();
void Render(sf::RenderWindow & Window);
sf::Vector2f Position();
Monster();
private:
sf::RectangleShape aMonster;
float aWidth;
float aHeight;
float aPosX;
float aPosY;
float aSpeedX;
float aSpeedY;
sf::Vector2f aVelocity;
float aMonster_Right_Side;
float aMonster_Left_Side;
bool amoveRight;
bool amoveLeft;
private:
virtual void draw(sf::RenderTarget & target, sf::RenderStates states) const;
};
Monster.cpp
Monster::Monster() : aPosX(10.0f), aPosY(50.0f), aWidth(30.0f), aHeight(30.0f), aSpeedX(1.0f), aSpeedY(0.0f), aVelocity(aSpeedX, aSpeedY),
amoveLeft(false), amoveRight(true)
{
aMonster.setFillColor(sf::Color::White);
aMonster.setSize(sf::Vector2f(aWidth, aHeight));
}
void Monster::Update()
{
std::cout << "X: " << aMonster.getPosition().x << " Y: " << aMonster.getPosition().y << std::endl; <-- Coordinate system changes. ( 0 for x axis starts at red line, 300 for x-axis at greenline)
aMonster_Right_Side = aMonster.getPosition().x + aWidth;
aMonster_Left_Side = aMonster.getPosition().x;
if(amoveRight == true)
{
amoveLeft = false;
aMonster.move(1.0f, 0.0f);
if(aMonster_Right_Side >= 300.0f) <-- Right edge of screen
{
aMonster.move(0.0f, 10.0f);
amoveLeft = true;
amoveRight = false;
}
}
if(amoveLeft == true)
{
amoveRight = false;
aMonster.move(-1.0f, 0.0f);
if(aMonster_Left_Side <= -50.0f) <-- Left edge of screen
{
aMonster.move(0.0f, 10.0f);
amoveLeft = false;
amoveRight = true;
}
}
}
void Monster::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(aMonster, states);
}
Main.cpp
vector<vector<Monster>> Army_of_Monsters;
vector< Monster > Monsters;
vector<sf::FloatRect> Monster_Position;
Monster monster;
Monsters.push_back(monster);
Monsters.push_back(monster);
Monsters.push_back(monster);
Monsters.push_back(monster);
Monsters.push_back(monster);
.
.
.
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j <= 9 )
{
Army_of_Monsters[i][j].setPosition(k * 50.0f, 50.0f);
Monster_Position.push_back(sf::FloatRect(Army_of_Monsters[i][j].getPosition().x, Army_of_Monsters[i][j].getPosition().y, 30.0f, 30.0f));
}
else if(j > 9 && j <= 19)
{
Army_of_Monsters[i][j].setPosition(k * 50.0f, 100.0f);
Monster_Position.push_back(sf::FloatRect(Army_of_Monsters[i][j].getPosition().x, Army_of_Monsters[i][j].getPosition().y, 30.0f, 30.0f));
}
.
while()
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
Army_of_Monsters[i][j].Update(); <-- Moves the monster objects
}
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
Army_of_Monsters_Position[i][j].left = Army_of_Monsters[i][j].getPosition().x;
Army_of_Monsters_Position[i][j].top = Army_of_Monsters[i][j].getPosition().y;
Army_of_Monsters_Position[i][j].width = 30.0f;
Army_of_Monsters_Position[i][j].height = 30.0f;
}
}
std::cout << "X: " << Army_of_Monsters_Position[0][20].left << " Y: " << Army_of_Monsters_Position[0][49].top << std::endl; <-- Does not update despite call to update
}
Does anybody know what would cause this?