-
In my game, when the code to rotate a shape is triggered, it rotates a series of sprites. However, because in my game you need to rotate them a lot at the same rotation, each time they rotate the shapes move to the right slightly for no reason of which I can see. Is there a simple soloution to this? I tried to move them back on the x axis and did set position to each after the rotation, but it simply doesn't work and they still move each time. Have I made an obvious mistake?
if (mousepos.x == 550, mousepos.y == 300)
{
si1.rotate(0.1); //rotate the shape
si2.rotate(0.1); //^
si2.rotate(0.1); //^
}
//how do I stop them moving on the x axis each time the mouse gets to that point?
-
Set the origin to the center.
And yes, the comma operator in the if is an obvious mistake.
-
But if I don't have the comma operator, it doesn't work? :-\ I set the origin prior to the rotation, and also tried it after the rotation, to no avail.
-
Do you even know what the comma operator (http://en.m.wikipedia.org/wiki/Comma_operator) does? I'm pretty sure it's not what you want.
My guess is you want logical or (||) or logical and (&&) .
-
As already mentioned here (http://en.sfml-dev.org/forums/index.php?topic=18068), you really have to learn C++ before using SFML. You're doing basic mistakes that have nothing to do with the library. The comma operator does not do what you want, you want to use AND or OR instead.
-
Well ok ok, I'll investigate that, but I was only following a video tutorial. But please explain how that is causing my entities to move each time?
-
I was only following a video tutorial
Yes, and that's what happens. There's a reason why we discourage most video tutorials ;)
Seriously, there are only very few about SFML that I can recommend in good conscience, e.g. those of Vittorio Romeo. Others, like CodingMadeEasy, have repeatedly shown that their tutorials teach questionable habits, of which people have to get rid in this forum afterwards. Don't hesitate to search the forum for past discussions about them, there are tons of concrete examples and arguments.
And in case you're talking about C++ video tutorials, matters are even worse. I'd say you cannot meaningfully learn the C++ programming language without a good book (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You won't even save time -- eventually you have to learn everything the hard way, which takes much longer.
But please explain how that is causing my entities to move each time?
I don't understand your problem. Could you follow the instructions here (http://en.sfml-dev.org/forums/index.php?topic=5559) and describe it again?
-
Oh, it was CodingMadeEasy (SFML, not C++) I was following, haha ;D
My problem is that every time a shape in my game rotates, it also moves to the right slightly, which I don't want to happen. I have no idea why it does this. The shape has had the origin set outside of the while windowIsOpen loop, which isn't the problem. However every time it rotates (triggered by the mouse being on a position), it moves away from it's origin, I want it to stay there but rotate from the shape's center, if you see what I mean.
Edit: From what I can see, the entity rotates around a center (I may be wrong), how do I change it?
-
I now understand that it rotates around the origin, but I already set the shape's origin. How do I set the origin for the rotation?
-
Read the thread I linked to again. I don't feel like guessing, so please come up with a minimal complete example.
By the way, there's an edit function ;)
-
Sorry, I'm not sure what you mean by a "complete example". Do you mean of my code? To clarify, I would like to know how to set the origin of which the rotation uses.
-
Read the linked thread! >:(
Here is the link again: http://en.sfml-dev.org/forums/index.php?topic=5559
-
I didn't know which thread you meant because you didn't specify which once, hence creating unnesisary posts but nevermind.
sf::RectangleShape rec1(sf::Vector2f(20, 5));
rec1.setorigin(-100, -300);
rec1.setFillColor(sf::Color::Green);
While (window.isOpen())
{
...
window.clear(sf::Color::Cyan);
window.draw(rec1);
sf::Vector2i mousepos = sf::Mouse::getPosition(window);
...
if (mousepos.x == 550, mousepad.y == 300) //note that this seems to work fine with the comma
{
rec1.rotate(-0.5);
}
}
-
I didn't know which thread you meant because you didn't specify which once, hence creating unnesisary posts but nevermind.
Are you even reading our answers?
Could you follow the instructions here (http://en.sfml-dev.org/forums/index.php?topic=5559) and describe it again?
Here's the link. You still haven't read it obviously. Your code is not only incomplete, it doesn't even compile ("While") ::)
And yes, the comma operator in the if is an obvious mistake.
Do you even know what the comma operator (http://en.m.wikipedia.org/wiki/Comma_operator) does? I'm pretty sure it's not what you want.
The comma operator does not do what you want, you want to use AND or OR instead.
But no, you still have the comma operator in your example. Believe us, it is incorrect there, even if it "seems" to work.
It's starting to get a bit tiresome to help you... We've asked for so many things multiple times, and you don't even read our answers and advice carefully. If your next post doesn't contain a complete and minimal example as described in that other thread, that's it from my side.
-
Well whatever, wether you like it or not I HAVEread the post that took you 3 posts to tell me which one it was... In all honesty, you never really tried to answer my original and only question, you only went on about the comma. So I'm not wasting any more time here, I'll move on to somewhere that actually utilises its responses with information that might actually be useful.
-
Whatever your rotation problem is (hard to tell without a usable example - see http://sscce.org/). Your use of the comma operator is obviously flawed and shows that you lack basic understanding of C++. If you are not willing to correct obviously broken code and provide a working example of your problem for people to test, then by all means, waste your time elsewhere.
-
rec1.setorigin(-100, -300);
It is kind of odd that this is the 2nd thread (http://en.sfml-dev.org/forums/index.php?topic=18072.0) I have seen today with this. I guess the other poster followed the same tutorial that Garry_pro did.