# sf::RectangleShape's position is not same as I set it to

When I try to print my box.getSfShape()'s position it does not print out the position I set it to have.

( The box.getSfShape()'s position is by default set to 0 )

Wrong output : ( printing the position of the box.getSfShape() )

( prints out : X : 0 Y : 0 ((These are the default assigned positions)) )

box.getSfShape().setPosition(box.getBody()->GetPosition().x / b2Meter, box.getBody()->GetPosition().y / b2Meter);


But when I print out the calculation only :

std::cout << "X : " << box.getSfShape().getPosition().x << " Y : " << box.getSfShape().getPosition().y << std::endl;


It prints out a increasing Y value and a X value on 0 like it should do.

So, I'm guessing the call to set the box.getSfShape() position somehow fails.

If you need something more, please tell me what to add :)

• Does getSfShape return by value or by reference? Dec 17, 2015 at 22:11
• @Athosvk It returns by value.
– BiiX
Dec 17, 2015 at 22:15
• There's your error most likely :) I can post an answer, but it's going to be a major copy paste of another answer of mine: gamedev.stackexchange.com/questions/112950/… Dec 17, 2015 at 22:16
• @Athosvk Yes, that was the problem! Have no idea what a struct is, but i'll do some reading! Thank you!
– BiiX
Dec 17, 2015 at 22:20
• I'll clarify in the form an answer Dec 17, 2015 at 22:22

So as I mentioned in the comments, the problem is that getSfShape is returning by copy. Although I answered a very similar problem here, it was more a bit of a language specific case. Structs are not related to this in the context of C++, so I'll slightly rephrase what I posted for the other question.

Due to your getSfShape() returning by value, it returns a copy of the shape. This copy is just another RectangleShape, same values, but it is only a copy of the RectangleShape. Then on the copy that is returned, you call the setPosition. It won't fail and C++ will happily let you do this, but all you did was edit the copy, which you discard straight away!

The solution to this problem, as you seem to have figured out already, is to have getSfShape() return by reference. This gives you back a reference to the shape you actually wanted to edit and then sets the position. So just for clarity, rather than this (assuming the correct types are used):

sf::RectangleShape getSfShape()
{
return shape;
}


You would do something like this:

sf::RectangleShape& getSfShape()
{
return shape;
}


Even a little easier than in C#!

I guess you're not updating the SFML shape position after each call to the box2d world.Step().

What type is your box? Is that part of a third-party library or your own code?

• I made the box class myself, I do update the RectangleShapes position and step the world every frame. Should I add the Box class codes as well?
– BiiX
Dec 14, 2015 at 11:01
• What's your output for std::cout << "X : " << box.getBody()->GetPosition().x / b2Meter << " " << box.getBody()->GetPosition().y / b2Meter << std::endl; ? (The first 10 values for instance.) Oh, and, by the way, be careful about angles: SFML setRotation() requires degrees but box2d works only with radians. Dec 15, 2015 at 7:36
• It quckly counts from 0 and up. It prints out an float or double value.
– BiiX
Dec 15, 2015 at 12:35
• Y value is counting up and X value is always 0.
– BiiX
Dec 15, 2015 at 14:30