SFML community forums

Help => Graphics => Topic started by: iHope on March 15, 2012, 01:39:52 pm

Title: Mouse rotation in top-down shooter
Post by: iHope on March 15, 2012, 01:39:52 pm
Hi, I'm currently trying to make a top-down shooter. I have done kinda a lot of stuff into it, but now I'm stuck at very annoying little problem. I need to make mouse rotation, again I have no idea how to do this. The idea is that the player would look at the cursor. I could rotate the player using cursor so you could use WASD to move around.
Title: Mouse rotation in top-down shooter
Post by: Laurent on March 15, 2012, 02:00:07 pm
What's your problem exactly?

This is quite simple to do:
Code: [Select]
sf::Vector2i cursor = sf::Mouse::getPosition(window);
sf::Vector2f worldCursor = window.convertCoords(cursor.x, cursor.y);
sf::Vector2f direction = worldCursor - player.getPosition();
player.setRotation(std::atan2(direction.y, direction.x));

(SFML 2)
Title: Mouse rotation in top-down shooter
Post by: iHope on March 15, 2012, 03:36:27 pm
Okay, thank you. I'm not really good at maths(I'm in junior high school).
Title: Mouse rotation in top-down shooter
Post by: Nexus on March 15, 2012, 05:19:45 pm
Vector algebra is essential for serious game programming, I'm sure there are good pages on the internet to learn it. You could also start with Wikipedia ;)
Title: Mouse rotation in top-down shooter
Post by: iHope on March 15, 2012, 05:24:31 pm
Yeah, thats true. I'm just too lazy to do so, because if vector algebra is going to be learned in school, I won't try to learn it now. <- English would be a lot more practical :D
Title: Mouse rotation in top-down shooter
Post by: iHope on March 15, 2012, 05:44:34 pm
Argh, I didn't look that code sample too well. The window isn't moving so the window is still, no moving around(Isn't sf::view used for that?). Then I'm using SFML 1.6(Too retard to install without proper install guide). And by the way, do I the code sample when mouse is moved?
Code: [Select]
if(sf::Event::MouseMoved)
{ //Do the code sample }
Title: Mouse rotation in top-down shooter
Post by: Nexus on March 15, 2012, 05:54:00 pm
It seems like you're too lazy in general :P
I don't want to offend you, consider those points as good advice :)
Title: Mouse rotation in top-down shooter
Post by: iHope on March 15, 2012, 06:37:27 pm
You're true about my laziness and I know I shouldn't be so lazy. And I know all those advices are true. Personally I hate installing different libraries, I think it's more harder than programming actually. BUT I'm gonna grow myself a spine and install SFML 2.

EDIT: Got SFML2 working :)