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

Author Topic: Setting Sprite on a circle around the player in an angle to mouseposition  (Read 3497 times)

0 Members and 1 Guest are viewing this topic.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Hey there!
Got a pretty much mathematical problem here I couldn't figure out after hours of trying...maybe some1 can help me.

Let me explain...


So I want to set the object's position to a coord on the circle.
The circle's coord should be calculated using the angle between the player and the mouse-position.

Hope you know what I'm talking about.

Greets,
Finn

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #1 on: September 29, 2012, 05:09:04 pm »
Don't post in General Discussions, the right forum would be Help -> General. Here is the image with a link that works:


First, take the vector from the player to the mouse. Then, set its length to the circle radius. Add the result to the player's position, and you have the object position.

If you don't know how to calculate this stuff, take a look at trigonometry and basic vector algebra. Alternatively, you could also use the vector functions in my Thor library :P
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #2 on: September 29, 2012, 05:27:43 pm »
Calculate the linear function between the mouse and the player and set a radius of your choice. The you tell the object to go as far as it can within the line given by the function you calculated without going beyond the radius of the circle (it works amazing with polar coordinates if you know how to use them).

It's pretty much the same answer Nexus gave you, just maybe a bit more explaining. You can implement your own polar vectors (they're not strictly necessary, but give you a powerful tool for things you can't do with carthesian vectors) or use the one's given by Thor. In my opinion it's very easy to implement them and you will be forced to read a bit in order to get them right, so implementing them yourself might be a little better in that aspect.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #3 on: September 29, 2012, 05:58:36 pm »
Oh you guys are fast :D

Some code would be helpfull though :-/

P = Playercoordinate
M = Mousecoordinate

First off getting PM by M-P.
sf::Vector2<float> pmVector;//player to mouse vector
pmVector.x = MouseX - PlayerX;
pmVector.y = MouseY - PlayerY;
 

Right?
What now?

Greets,
Finn

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #4 on: September 29, 2012, 06:14:02 pm »
Well we're not here to program for you, also as you mentioned this is mainly a mathematical problem and the transformation from math to code should be quite obvious once you understood what the math does.
So sit down, take your time, think about it, read stuff on math if you don't get it and then implement it and if you got stuck there come back here. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #5 on: September 29, 2012, 06:25:09 pm »
Yeah that's the problem...I'm thinking about this problem for hours and didn't come to any solution yet...guess I just got a brainlock or something :D

I would really appreciate if you'd show the the mathematical steps I have to do with examples!

Greets,
Finn

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #6 on: September 29, 2012, 06:43:42 pm »
The thing is that mathematically there are many ways to do that, for example, most people wouldn't use polar vectors for something like that, whereas I find it easier using them. The complexity in giving an answer that would be the best for you is that there is no map to it, you make it as you learn. The solution you are most comfortable with will most likely be the best (for you) unless it's inefficient.

This link has some basic linear algebra that might help you solve it: http://www.nhn.ou.edu/walkup/demonstrations/WebTutorials/VectorIntroduction.htm

Check also for the concepts of vector length. The calculation of an angle given two points is something that can be done in many ways and depending on your reference point it may vary entirely. For something like that I tend to do everything in the origin and move it to the position it should be (which is what most do if I'm not mistaken). Polar Vectors (I must be getting you tired with this) give you an easy way to calculate angles, so I'd go there. But then again, what you think it's best is not necessarily what I think it's best. In order for you to know yourself there is no choice but to read...
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Re: Setting Sprite on a circle around the player in an angle to mouseposition
« Reply #7 on: September 29, 2012, 07:06:49 pm »
Okay I think now I got something that's very close to what I want.

My vectors consist of x and y coordinates
O = Object-Vector
M = Mouse-Vector
P = Player-Vector
r = radius

and this is what my algo comes down to:

O = P + r * (P - M) / (|P| - |M|)
 

It works flawless...except that it doesn't gimme the coords of the circle-side facing the mouse...instead it gives me the coordinates on the other side of the circle. Any ideas on how to fix it?

Greets,
Finn

PS: You helped me alot so far, you're great! :o)

EDIT: GOT IT!! Changed to :
Code: [Select]
O = P - r * (P - M) / (|P| - |M|)
« Last Edit: September 29, 2012, 07:14:58 pm by Finn »

 

anything