Hello,
I try to make that my sprite will rotate to mouse position, but it doesnt move.
(I use old allegro)
[code]
player.angle = atan2(player.x-player.y, mouse_x-mouse_y);
rotate_sprite(buf,ppistol, player.x , player.y , ftofix( (player.angle*256)/360.0 ) );
[/code]
---
I found similar thread on this forum, and was trying to make like there, but I still got problem, because it rotates well, but mouse is behind player.
const double rad_to_deg = 180.0/M_PI;
const double deg_to_adeg = 32.0/45.0;
const double rad_to_adeg = 128.0/M_PI;
player.angle = -atan2( mouse_x - player.x, mouse_y-player.y);
rotate_sprite( buf , ppistol , player.x, player.y , ftofix( player.angle*rad_to_adeg ) ); //(player.angle*256)/360.0) );
You should really read your post at least once after posted, at least unless you learn how to use the forum's formatting.
I found similar thread on this forum, and was trying to make like there, but I still got problem, because it rotates well, but mouse is behind player.
So put it above the "player"? o0
Use a custom mouse perhaps? (blit some image on mouse coordinates)
That's not what I mean, but I just rotated texture at 180 degrees and now works perfectly, but I don't know how to move player to mouse direction.
It moves in strange ways
What do you want to do exactly ?
According to my crystal ball (which is not really working fine ya know):
1) You have a point that represent the player with x and y
2) You use the mouse pointer and accordingly rotate the player's sprite for it to follow the angle between the mouse coordinate and the player's coordinate
3) You want to press W for making the player move in direction of the mouse
Main.cpp
main.h
player files
I want to move player to mouse direction, after pressing W key.
--
Btw, your crystal ball works fine, that's what I'm trying to do.
I don't know how to correct your code. I was lazy and I found it better to give you a working example.
I attached source and (alleg4.4) binary.
The sprite is rotating accordingly to the mouse position, and go in direction of the mouse when pressing SPACE.
You were in need of rotation forumulae for the rotating part, but the move is only a vector story.
I just calculated the normalised vector between mouse / player and used it at each frame.
Works perfectly! Thanks a lot!
I think I understand (mostly) how does it work.
Oh, what about it?
if( vx || vy )
It means:
if vx or vy is different than 0 then... ?
nvrmnd
I am not sure if it covers all the cases, but that if( vx ||vy ) is there to ensure that you'll never have a zero size (length) and thus not dividing by 0 in the next steps.
Literately it means vx != 0 or vy != 0
One more question
How to change this code, if I want to move to the left or right?
{"name":"infof.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/1\/4162db84b71cdf01c513ad41d140d0d9.jpg","w":324,"h":326,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/1\/4162db84b71cdf01c513ad41d140d0d9"}
You have to do a little bit of vector math.
The matrix representation of the general 2D transformation looks like this:
x' = x cos(t) - y sin(t)
y' = x sin(t) + y cos(t)
where (x,y) are the components of the original vector and (x', y') are the transformed components.
If t = 90 degrees, then cos(90) = 0 and sin(90) = 1. Substituting and multiplying it out gives:
x' = -y
y' = +x
So, basically you'll have to modify it that way:
In that modification, I first test if there was a key pressed or a mouse button. We don't need to compute anything if there was no action.
If I have an action, I first start to compute the vector going from object (sprite) to mouse pointer. When I have it, I apply it differently if it's SPACE, mouse button 1 or 2, accordingly to the explanation from stackoverflow.
I attached you the whole thing again. Use SPACE to move to the mouse_pointer, and mouse button left/right for a perpendicular move against it.
That's all that I wanted to know!
Thanks a lot!