# need help with using trig functions

• June 29th, 2012, 11:31 PM
Tdogg
need help with using trig functions
First off I'm new here so I'd like to say nice to meet you all :-h. Now down to business, I'm working on a (2d, birds i view) game but I'm having trouble with interacting with the avatar. I would like to make it so the avatar faces the mouse cursor (so it rotates so the front always is facing the mouse). This is what I have but the code just has the avatar spinning constantly. (Note: I changed some variable names and some of the syntax to make it easier to understand my code without knowledge of the game engine I'm using so any var names or brackets/parenthesis problems aren't an issue.) Thanks in advance for the any help!

Code :

```if (mousesX >= avatarsX) { if(mousesY >= avatarsY) angle = (int)(Math.atan((mousesY - avatarsY) / (mousesX -avatarsX))); else angle = (int)(Math.atan((avatarsY - mousesY) / (mousesX - avatarsX))); } else { if(mousesY >= avatarsY) angle = (int)(Math.atan(mousesY -avatarsY)/(avatarsX - mousesX)); else angle = (int)(Math.atan((avatarsY - mousesY) / (avatarsX - mousesX));   }   if(angle != oldAngle) {( setImage(rotate(getImage() , angle + oldAngle + (180 - (angle + oldAngle)))); //rotate(image, int) rotates image (in this case the avitar) by int degrees clockwise. setImage(image) displays image (the avatar) and getImage() returns the avatars image   oldAngle += angle; }   if (oldAngle == 360) oldAngle -= 360;```
• June 30th, 2012, 07:43 AM
Norm
Re: need help with using trig functions
Can you make a small complete program that compiles, executes and shows the problem?
• July 1st, 2012, 12:57 AM
Tdogg
Re: need help with using trig functions
Sorry, I'm not quite sure what you mean. Are you asking to upload an example program that reproduces the problem?
• July 1st, 2012, 07:47 AM
Norm
Re: need help with using trig functions
Yes, something that could be used for testing the code.