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

Author Topic: Rotation keeps moving entity along X axis  (Read 5700 times)

0 Members and 1 Guest are viewing this topic.

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Rotation keeps moving entity along X axis
« on: May 03, 2015, 01:35:26 pm »
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?



 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation keeps moving entity along X axis
« Reply #1 on: May 03, 2015, 01:39:35 pm »
Set the origin to the center.

And yes, the comma operator in the if is an obvious mistake.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #2 on: May 03, 2015, 01:47:00 pm »
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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Rotation keeps moving entity along X axis
« Reply #3 on: May 03, 2015, 01:55:19 pm »
Do you even know what the comma operator does?  I'm pretty sure it's not what you want.
My guess is you want logical or (||) or logical and (&&) .

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation keeps moving entity along X axis
« Reply #4 on: May 03, 2015, 01:55:52 pm »
As already mentioned here, 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #5 on: May 03, 2015, 02:00:21 pm »
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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation keeps moving entity along X axis
« Reply #6 on: May 03, 2015, 02:09:20 pm »
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. 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 and describe it again?
« Last Edit: May 03, 2015, 02:11:19 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #7 on: May 03, 2015, 02:22:39 pm »
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?
« Last Edit: May 03, 2015, 02:34:34 pm by Garry_pro »

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #8 on: May 03, 2015, 03:07:38 pm »
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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation keeps moving entity along X axis
« Reply #9 on: May 03, 2015, 03:24:58 pm »
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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #10 on: May 03, 2015, 03:40:51 pm »
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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation keeps moving entity along X axis
« Reply #11 on: May 03, 2015, 03:43:14 pm »
Read the linked thread! >:(

Here is the link again: http://en.sfml-dev.org/forums/index.php?topic=5559
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #12 on: May 03, 2015, 04:00:29 pm »
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);
}

}









 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation keeps moving entity along X axis
« Reply #13 on: May 03, 2015, 04:10:39 pm »
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 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 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Garry_pro

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rotation keeps moving entity along X axis
« Reply #14 on: May 03, 2015, 04:29:09 pm »
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.