I want player1 to move at its rotation angle when pressing forward(w). When I implement the trigonometry, which I am fairly new to, the player doesn't rotate.
| 1 | #include <allegro.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <iostream.h> |
| 4 | |
| 5 | #define speed 2 |
| 6 | |
| 7 | int player1_x = 320; |
| 8 | int player1_y = 240; |
| 9 | int player2_x; |
| 10 | int player2_y; |
| 11 | int player1_rotate; |
| 12 | int player2_rotate; |
| 13 | int mickeyx; |
| 14 | int mickeyy; |
| 15 | int player1_vy; |
| 16 | |
| 17 | int player1_movement() |
| 18 | { |
| 19 | if(key[KEY_A]) |
| 20 | { |
| 21 | player1_x -= speed; |
| 22 | } |
| 23 | if(key[KEY_D]) |
| 24 | { |
| 25 | player1_x += speed; |
| 26 | } |
| 27 | if(key[KEY_W]) |
| 28 | { |
| 29 | player1_y -= speed * fsin(player1_rotate); |
| 30 | } |
| 31 | if(key[KEY_S]) |
| 32 | { |
| 33 | player1_y += speed * fsin(player1_rotate - 255/2); |
| 34 | } |
| 35 | get_mouse_mickeys(&mickeyx, &mickeyy); |
| 36 | |
| 37 | player1_rotate += mickeyx; |
| 38 | } |
| 39 | |
| 40 | int main(int argc, char *argv[]) |
| 41 | { |
| 42 | allegro_init(); |
| 43 | install_keyboard(); |
| 44 | install_mouse(); |
| 45 | set_color_depth(16); |
| 46 | set_gfx_mode(GFX_AUTODETECT,640,480,0,0); |
| 47 | |
| 48 | |
| 49 | BITMAP *buffer = create_bitmap(640,480); |
| 50 | |
| 51 | BITMAP *player1 = load_bitmap("player.bmp", NULL); |
| 52 | |
| 53 | BITMAP *player2 = load_bitmap("player.bmp", NULL); |
| 54 | |
| 55 | while(!key[KEY_ESC]) |
| 56 | { |
| 57 | player1_movement(); |
| 58 | rotate_sprite(buffer, player1, player1_x, player1_y,player1_rotate); |
| 59 | blit(buffer, screen,0,0,0,0,640,480); |
| 60 | clear_bitmap(buffer); |
| 61 | } |
| 62 | |
| 63 | destroy_bitmap(player1); |
| 64 | destroy_bitmap(player2); |
| 65 | destroy_bitmap(buffer); |
| 66 | |
| 67 | return 0; |
| 68 | |
| 69 | } |
| 70 | END_OF_MAIN() |
You're using a number of fixed point math routines, but not converting your ints to fixed point. (Also, player1_movement() is defined as returning int, so you should either return something or change it to void). Like so:
| 1 | #include <allegro.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <iostream.h> |
| 4 | |
| 5 | #define speed 2 |
| 6 | |
| 7 | int player1_x = 320; |
| 8 | int player1_y = 240; |
| 9 | int player2_x; |
| 10 | int player2_y; |
| 11 | int player1_rotate; |
| 12 | int player2_rotate; |
| 13 | int mickeyx; |
| 14 | int mickeyy; |
| 15 | int player1_vy; |
| 16 | |
| 17 | int player1_movement() |
| 18 | { |
| 19 | if(key[KEY_A]) |
| 20 | { |
| 21 | player1_x -= speed; |
| 22 | } |
| 23 | if(key[KEY_D]) |
| 24 | { |
| 25 | player1_x += speed; |
| 26 | } |
| 27 | if(key[KEY_W]) |
| 28 | { |
| 29 | player1_y -= speed * fixtoi(fsin(itofix(player1_rotate))); |
| 30 | } |
| 31 | if(key[KEY_S]) |
| 32 | { |
| 33 | player1_y += speed * fixtoi(fsin(itofix(player1_rotate - 255/2))); |
| 34 | } |
| 35 | get_mouse_mickeys(&mickeyx, &mickeyy); |
| 36 | |
| 37 | player1_rotate += mickeyx; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int main(int argc, char *argv[]) |
| 43 | { |
| 44 | allegro_init(); |
| 45 | install_keyboard(); |
| 46 | install_mouse(); |
| 47 | set_color_depth(16); |
| 48 | set_gfx_mode(GFX_AUTODETECT,640,480,0,0); |
| 49 | |
| 50 | |
| 51 | BITMAP *buffer = create_bitmap(640,480); |
| 52 | |
| 53 | BITMAP *player1 = load_bitmap("player.bmp", NULL); |
| 54 | |
| 55 | BITMAP *player2 = load_bitmap("player.bmp", NULL); |
| 56 | |
| 57 | while(!key[KEY_ESC]) |
| 58 | { |
| 59 | player1_movement(); |
| 60 | rotate_sprite(buffer, player1, player1_x, player1_y, itofix(player1_rotate)); |
| 61 | blit(buffer, screen,0,0,0,0,640,480); |
| 62 | clear_bitmap(buffer); |
| 63 | } |
| 64 | |
| 65 | destroy_bitmap(player1); |
| 66 | destroy_bitmap(player2); |
| 67 | destroy_bitmap(buffer); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | END_OF_MAIN() |
Rotates as nice as you please.
#include <iostream.h>
It's #include <iostream>, iostream.h is obsolete.
Ok, its rotating right now, thank you for that, but it still isn't going in the direction it is facing when I press forward.
Edit: Nevermind, stupid me forgot to do the trig for the x value.:P