0
\$\begingroup\$

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 :)

| improve this question | | | | |
\$\endgroup\$
  • \$\begingroup\$ Does getSfShape return by value or by reference? \$\endgroup\$ – Athos vk Dec 17 '15 at 22:11
  • \$\begingroup\$ @Athosvk It returns by value. \$\endgroup\$ – BiiX Dec 17 '15 at 22:15
  • 1
    \$\begingroup\$ 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/… \$\endgroup\$ – Athos vk Dec 17 '15 at 22:16
  • \$\begingroup\$ @Athosvk Yes, that was the problem! Have no idea what a struct is, but i'll do some reading! Thank you! \$\endgroup\$ – BiiX Dec 17 '15 at 22:20
  • \$\begingroup\$ I'll clarify in the form an answer \$\endgroup\$ – Athos vk Dec 17 '15 at 22:22
3
\$\begingroup\$

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#!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

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?

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – BiiX Dec 14 '15 at 11:01
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – A. Breust Dec 15 '15 at 7:36
  • \$\begingroup\$ It quckly counts from 0 and up. It prints out an float or double value. \$\endgroup\$ – BiiX Dec 15 '15 at 12:35
  • \$\begingroup\$ Y value is counting up and X value is always 0. \$\endgroup\$ – BiiX Dec 15 '15 at 14:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.