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

Author Topic: Mouse rotation in top-down shooter  (Read 5243 times)

0 Members and 1 Guest are viewing this topic.

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Mouse rotation in top-down shooter
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mouse rotation in top-down shooter
« Reply #1 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)
Laurent Gomila - SFML developer

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Mouse rotation in top-down shooter
« Reply #2 on: March 15, 2012, 03:36:27 pm »
Okay, thank you. I'm not really good at maths(I'm in junior high school).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Mouse rotation in top-down shooter
« Reply #3 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Mouse rotation in top-down shooter
« Reply #4 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

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Mouse rotation in top-down shooter
« Reply #5 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 }

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Mouse rotation in top-down shooter
« Reply #6 on: March 15, 2012, 05:54:00 pm »
It seems like you're too lazy in general :P
  • Concerning the vector algebra, it's your decision, but you definitely need it in game programming. If you learn it now, you'll have less problems in school ;)
  • Both installing SFML 1.6 and SFML 2.0 is explained very well in the tutorials, we can't speak about "no proper install guide".
  • The code you showed is wrong. Also this is explained in the tutorials, but you have to actually read them.
  • Generally, you should know that as a programmer, you often have to search informations on your own (concerning documentation, programming language, specific techniques, ...). No one will teach you them, so you must learn them by yourself.
I don't want to offend you, consider those points as good advice :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Mouse rotation in top-down shooter
« Reply #7 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 :)

 

anything