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

Author Topic: World Coordinates not updating  (Read 1567 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
World Coordinates not updating
« on: July 01, 2015, 01:34:40 am »
I am having trouble getting my updated position for my objects. Whenever I call
Code: [Select]
.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
Code: [Select]
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
Code: [Select]
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
Code: [Select]

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?
« Last Edit: July 01, 2015, 07:57:19 am by Laurent »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: World Coordinates not updating
« Reply #1 on: July 01, 2015, 01:41:18 am »
You have 2 Transformable: the Monster object, and its sf::RectangleShape member.
Maybe you're moving the RectangleShape and getting the position of the Monster object.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: World Coordinates not updating
« Reply #2 on: July 01, 2015, 02:41:45 am »
Maybe you're moving the RectangleShape and getting the position of the Monster object.
It might also be the other way around i.e. moving the Monster object and getting the position of the RectangleShape.  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Student555

  • Guest
Re: World Coordinates not updating
« Reply #3 on: July 01, 2015, 04:04:45 am »
Thanks for the solution G.

Question:

In your previous post, you stated
Quote
Maybe you're moving the RectangleShape and getting the position of the Monster object.

I realized that I was moving the sf::RectangleShape, but why is the coordinate system different, compared to the Monster object.
« Last Edit: July 01, 2015, 07:57:43 am by Laurent »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: World Coordinates not updating
« Reply #4 on: July 01, 2015, 07:45:52 am »
The Monster object and its RectangleShape member are 2 different objects, each with their own positions (and scales and rotations...) because they both inherit from sf::Transformable.
However when you draw the RectangleShape (aMonster) you give it the transform from the Monster object (you retrieve it with getTransform()), it makes aMonster take into account both transforms. (its own and the one from the Monster object)
If you set the position of aMonster to 10,10, and the position of the Monster object to 20,25, aMonster would be drawn at 30,35 but its position (with aMonster.getPosition()) would still be 10,10. You could say that the RectangleShape is positionned relatively to the position of the Monster object.

Maybe it seems useless / confusing because you only have one shape and you probably want the shape and the Monster to both have the same position. Imagine your Monster class has 2 shapes, for example a rectangle for the body and a circle for the head, you would set the rectangle and the circle at 2 different positions. Then when you want to move your monster, you wouldn't manipulate the rectangle and the circle directly, but rather the position of the Monster object.

 

anything