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

Author Topic: revolving a sprite around a circle based on mouse position  (Read 4761 times)

0 Members and 1 Guest are viewing this topic.

helloSunday

  • Newbie
  • *
  • Posts: 2
    • View Profile
revolving a sprite around a circle based on mouse position
« on: August 15, 2021, 06:11:34 am »
Ok, so I'm trying to get a sprite to revolve around the center of another sprite. I want the revolution to sync up(follow) with the mouse position relative to the sprite that is being revolved around.

I'm assuming this is more of a mathematics/geometry question than a SFML specific question, but nonetheless I am trying to accomplish this task is SFML.

My question is: how can this be done?

*I'm not necessarily looking for a specific answer as much as guidance on tools or information that could be used to solve this problem.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: revolving a sprite around a circle based on mouse position
« Reply #1 on: August 15, 2021, 06:51:43 am »
You can use atan2 to compute the angle of rotation between your (center) sprite and position of your mouse.
You can use a transform to rotate around a point.
Create a transform, rotate around the center of your  (center) sprite, use transformPoint to apply the rotation to the center of your revolving sprite. Then move your sprite to the resulting point.

Alternatively you could also compute the vector between your (center) sprite and mouse, normalize it, multiply it by the radius of your revolution, then add it to the center of your (center) sprite to get the position of your revolving sprite.
« Last Edit: August 15, 2021, 06:59:42 am by G. »

helloSunday

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: revolving a sprite around a circle based on mouse position
« Reply #2 on: August 15, 2021, 07:29:59 am »


Alternatively you could also compute the vector between your (center) sprite and mouse, normalize it, multiply it by the radius of your revolution, then add it to the center of your (center) sprite to get the position of your revolving sprite.


This was huge, I was trying to do this on graph paper but couldn't figure it out. Thanks a bunch!

 

anything