SFML community forums

Help => General => Topic started by: zoran404 on March 25, 2013, 11:56:16 pm

Title: Calculating position
Post by: zoran404 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.
Title: Re: Calculating position
Post by: eXpl0it3r 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 (http://www.mathsisfun.com/polar-cartesian-coordinates.html).

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. ;)
Title: Re: Calculating position
Post by: zoran404 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]
Title: Re: Calculating position
Post by: zsbzsb on March 27, 2013, 05:13:51 pm
Look at this (http://imageshack.us/photo/my-images/856/mathm.png/). 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]
Title: Re: Calculating position
Post by: zoran404 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)
Title: Re: Calculating position
Post by: Aster 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?
Title: Re: Calculating position
Post by: zsbzsb 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.
Title: Re: Calculating position
Post by: zoran404 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.