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

Author Topic: Calculating position  (Read 3894 times)

0 Members and 1 Guest are viewing this topic.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Calculating position
« on: March 25, 2013, 11:56:16 pm »
Ok, so now I have reversed problem (reverse of my other question).
I have the angle and the vectors lenght and I need x and y (if the vector starts at the coordinates 0,0).
I have tryed something, but it didn't gived the correct results and when I though about it I realized why and I'm just to tired to think about it any more.
So if anyone has any ideas please post.

Not important: I need this because I have a rectangle (sprite) in the middle of the screen, with origin in it's middle and it is rotated by the player. Now I have to get the x,y of the sprite's corners (all 4 of them) to calculate if he can no more rotate in that way or go forwards/backwords.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Calculating position
« Reply #1 on: March 26, 2013, 12:19:40 am »
If I understand you correctly, you just want a conversion from the polar coordinates to the Cartesian coordinates, right?
Well Google is your best friend, once you know what you're looking for, e.g. here.

If not, then I'm not quite sure, what the problem is and you might want to link to the other thread you're referring to, to maybe give a bit more context. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: Calculating position
« Reply #2 on: March 27, 2013, 03:10:30 pm »
Ok so this is what I tryed
double y = tankVL * std::cos(rotate+startR),
x = tankVL * std::sin(rotate+startR);
And it dosen't show the correst cords.
I puted a small rectangle at the resulting coordinates and insted of being at the corner of the sprite it was rotating around it's center (every freme in which I change 'rotate' (I change it by 2) it rotates 120 degress around the center). That gives me an idea to make some special effect around the player when he uses something, but dosen't solve my problem.

my startR var is the starting angle of coordinates position, roration var is what I setRotation of sprite and tankVL is the distance from center. (see the pic)

I made this picture which shows what I'm trying to do.
If I mesed up something in the formula please tell.


[attachment deleted by admin]

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Calculating position
« Reply #3 on: March 27, 2013, 05:13:51 pm »
Look at this. By the way, pick up a book on precalc/trig to learn more about this type of math.

And looking at your picture, remember that angle 0/360 points right, not up. So to get your reference angles go up/down, not left/right.

[attachment deleted by admin]
« Last Edit: March 27, 2013, 05:25:37 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: Calculating position
« Reply #4 on: March 27, 2013, 11:24:41 pm »
That is the same thing other guy pointed me to.
That still dozen't tell me what is wrong with my code. (posted in previus post)

Aster

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Re: Calculating position
« Reply #5 on: March 28, 2013, 12:02:15 am »
If I understood correctly, you essentially want to rotate the sprite in the direction of its movement, correct?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Calculating position
« Reply #6 on: March 28, 2013, 01:44:28 am »
I have the angle and the vectors lenght and I need x and y (if the vector starts at the coordinates 0,0).

Dude we have told you exactly what you need to know to do this.

That still dozen't tell me what is wrong with my code. (posted in previus post)

First that little "snippet" you posted makes no sense whatsoever. And what you are doing seems to have nothing to do with the formulas you were given.

So please clarify what you want, and remember we are not here to write code for you. And please post complete working code.

std::sin(rotate+startR);
 

Just looking at this, why are you seeming to add rotations and then trying to get the sin??? I think you need to go read what the sin function really does.
« Last Edit: March 28, 2013, 01:53:20 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: Calculating position
« Reply #7 on: March 28, 2013, 07:22:33 pm »
Why do you have to be like that man?

Anyway, I figured out the problem, I forgot to convert to radians. It works now.

 

anything