sin cos etc

Started by
6 comments, last by schattenmaennlein 16 years, 6 months ago
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?
Advertisement
Quote:Original post by schattenmaennlein
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?
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));
thank you, that was quick
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.
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.
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]
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.
shot.x += shot.speed * sin(shot.angle/(180/PI));
shot.y += shot.speed * cos(shot.angle/(180/PI));

This topic is closed to new replies.

Advertisement