sin cos etc

This topic is 4089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have two points(playersprite, mouse), birdperspective, 2d. Whats the most efficient way to calculate the angle in which one point has to rotate from his starting position to face the other one?

Share on other sites
Quote:
 Original post by schattenmaennleinI have two points(playersprite, mouse), birdperspective, 2d.Whats the most efficient way to calculate the angle in which one point has to rotate from his starting position to face the other one?
I wouldn't worry too much about what method is 'most efficient' (unless for some reason you're performing this calculation thousands of times per update). Anyway:
vector2 diff = B-A;// To compute the absolute angle that orients an object at point// A towards point B:float angle = atan2(diff.y,diff.x);// To compute the relative angle that rotates an object at point// A into alignment with point B:float angle = atan2(perp_dot(object.forward,diff),dot(object.forward,diff));

Share on other sites
thank you, that was quick

Share on other sites
if you are still looking at this, then here:
http://www.kirupa.com/forum/showpost.php?p=2216146&postcount=30
for top down games it can prove very useful. In my top down games (since that's all I program) having the player rotate slowly adds a lot to gameplay.

Share on other sites
Other question:

now I want to move a point in the matrix in a direction angle.
I could do this very comfortable with the opengltranslatefunction but I think this will cause problems later for the collision detection.
I cant think of a pretty algorithm, so how do you do this?

a good theoritical resource for calculations like this is also welcome.

Share on other sites
I thought about it, it should work with sin and cos, I just have to negate the quadratic developement of the number so I think its something like:
x += speed * sqrt(sin(shotangle));
y += speed * sqrt(cos(shotangle));
but I still lack the formula *experimenting*

edit:
i seem to be thinking wrong, i thought the sqrt-function of sin should have a steady developement but that does not work inside my calculator.

(ah and just for safety means
float angle = atan2(perp_dot(object.forward,diff),dot(object.forward,diff)); <- nedds to be mulitplied by 180/Pi because atan gives its value in radian)

edit2: ugh, the sqrt thing does not work because cos and sin dont have quadratic developements, I think. I believe I have to multiply cos by sin or something along those lines *experimenting*

[Edited by - schattenmaennlein on September 29, 2007 5:22:59 PM]

Share on other sites
cos is for x sin is for y

the functions use radians. Stop using or even thinking in degrees it will save you a lot of time.

Share on other sites
shot.x += shot.speed * sin(shot.angle/(180/PI));
shot.y += shot.speed * cos(shot.angle/(180/PI));

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• Forum Statistics

• Total Topics
633653
• Total Posts
3013152
×