# Face the mouse[Solved]

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

## Recommended Posts

How would i make an image rotate to always be facing the mouse? using blitz3D. [Edited by - Barkanaxen on October 30, 2007 4:00:35 PM]

Thank Youu!

##### Share on other sites
Quote:
 Original post by BarkanaxenThat didnt work, It wasnt what i needed sorry but thank you ToohrVyk for trying
Can you explain why 'atan2' didn't work for you? Did you try it? What were the results? Can you post your code?

##### Share on other sites
Well i just went back to it and looked at it a bit harder and tried it but i dont see how i would implememnt this into my code.thats using drawing functions for lines and stuff, i personally cant piece out the part that does what i want.
Thank you

##### Share on other sites
Quote:
 Original post by BarkanaxenWell i just went back to it and looked at it a bit harder and tried it but i dont see how i would implememnt this into my code.thats using drawing functions for lines and stuff, i personally cant piece out the part that does what i want.Thank you
I don't know anything about Blitz3D, but typically you'd do something like this:
Vector2 diff = mouse_position - object_position;float angle = atan2(diff.y, diff.x);object_image.render(object_position, angle);



##### Share on other sites
Yea i have been trying different things but i really only understand Blitz and Gamemaker. i can understand but not programm in C. I cant translate that into blitz. well i can i know what it does but how would i do that in blitz coding.lol
Thanks =)

##### Share on other sites
Quote:
 Original post by BarkanaxenYea i have been trying different things but i really only understand Blitz and Gamemaker. i can understand but not programm in C. I cant translate that into blitz. well i can i know what it does but how would i do that in blitz coding.lol Thanks =)

Quote:
 Vector2 diff = mouse_position - object_position;float angle = atan2(diff.y, diff.x);object_image.render(object_position, angle);

Maybe I can translate it into some English and then maybe you could understand it.

Basicaly that is taking a difference vector, and it is assigning it to the difference between mouse_position-object_position. That is pretty self explanatory of what those variables mean and represent. They are both vectors though.

Next comes the angle at which something is. The y and the x points are what is plugged into the function.

Next might be what could take a little bit. That is just rendering the object you want (hence the object_image [that's the object you want rendered] instance) to the objects position (hence the object_position vector), at the angle you presented it. Blitz3D may not have a function to draw things and takes an angle. If it does then there you go.

If you don't understand what vectors are, then I suggest you just do a quick Google search and study up on them. Pretty important when it comes to game programming.

I hope this helps at least some what!

##### Share on other sites
I think i can get it now man, Thank u for taking the time to help. I respect that =). Along with everyone else who tried as well =). Thanks everyone!

##### Share on other sites
Here is my problem now, I have all that set up and completely understand it but What i have to do to rotate it is create a strip for rotation like with a certain amount of frames. well i made one with 36 frames. i load it into the game and here is what i typed

loads the strip with 36 frames that are 34 by 34 pixels. and starting at frame 0.

"
diffx=mousex-Player.Actor\x
diffy=mousey-Player.Actor\y
dir=ATan2(diffy,diffx)
Player.Actor\direction=dir
"
Blitz 3d version of what waas shown to me.

"
drawimage Player.Actor\image,Player.Actor\x,Player.Actor\y,Player.Actor\direction
"
wich draws the image at its x and y in that direction. well the thing is the direction isnt supposed to be an angle. It needs to be a frame number but atan2 gives u negative numbers and numbers that are higher than 36. so if anyone understands this then please help. It would be much appreciated.

##### Share on other sites
atan gives you values higher then 36 and negatives becouse atan2 result is an angle in degrees [-180;180].. so now you must convert it to frame number..

dir = ATan2( diffy, diffx )

if dir < 0 then dir = 360 - dir

dir = dir / 10

[Edited by - dopelganger on October 30, 2007 4:31:20 AM]

##### Share on other sites
Most likely, atan2 will return values in radians, ranging from -PI to PI. Converting these to one of your directional frames goes like this:

angle_in_radians = atan2( diffy, diffx )
frame = angle_in_radians * 36 / (2*PI) + 0.5

The 0.5 is added so it'll round to the nearest frame instead of rounding down.

##### Share on other sites
I got it to work thank you all for all ur help. I had to flip it to say
"if dir<0 then dir=dir-360"
But i got it to work and im happy now =). Thanks!