I'm doing programming on my game, but I'm stuck. I'm trying to implement and abuse like control scheme into my game. I have to bitmaps the player and the players arms. The math is all correct but the problem I'm having is that the players arm wont constantly rotate, it just stays at the angel that the mouse was at from when the program first runs. I have the line of code that controls rotation in the while main loop, what am i doing wrong??
| 1 | float radians; |
| 2 | int cx; |
| 3 | int cy; |
| 4 | |
| 5 | BITMAP *arms = load_bitmap("arms.bmp", NULL); |
| 6 | |
| 7 | // Calculate center of character |
| 8 | cx = player_image[n]->w/2; |
| 9 | cy = player_image[n]->h/2; |
| 10 | |
| 11 | radians = atan2(mouse_y - cy, mouse_x - cx) + (AL_PI/2.0); |
| 12 | |
| 13 | fixed allegro_degrees = ftofix( radians * 255.0 / (2.0 * AL_PI )) & 0xffffff; |
| 14 | |
| 15 | |
| 16 | |
| 17 | // Main loop |
| 18 | while (!key[KEY_ESC]) |
| 19 | { |
| 20 | |
| 21 | oldpy = player->y; |
| 22 | oldpx = player->x; |
| 23 | |
| 24 | rotate_sprite(screen, arms, (player->x-mapxoff)+1, (player->y-mapyoff+1)+15, allegro_degrees); |
Thanks
You never update allegro_degrees.
Can you give me a code snippet for that, i wrote this small app, and thats where I'm getting most of the code for the arm to mouse linkage. But I dont update allegro_degrees in it.
This is that other app, "Ascheme"
| 1 | |
| 2 | #include <allegro.h> |
| 3 | #include <math.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | void init(); |
| 7 | void deinit(); |
| 8 | |
| 9 | int main() { |
| 10 | init(); |
| 11 | |
| 12 | int x, y; |
| 13 | |
| 14 | // Load tank sprite |
| 15 | BITMAP *tank = load_bitmap("tank.bmp", NULL); |
| 16 | |
| 17 | // Calculate center of the screen |
| 18 | x = SCREEN_W/2; |
| 19 | y = SCREEN_H/2; |
| 20 | |
| 21 | // Draw tank at starting location |
| 22 | // draw_sprite(screen, tank, x, y); |
| 23 | |
| 24 | float radians; |
| 25 | // fixed allegro_degrees; |
| 26 | |
| 27 | while (!key[KEY_ESC]) { |
| 28 | /* put your code here */ |
| 29 | |
| 30 | vline(screen, SCREEN_W/2, 0, 600, makecol(0,0,255)); |
| 31 | hline(screen, 0, SCREEN_H/2, 800, makecol(0,0,255)); |
| 32 | |
| 33 | show_mouse(screen); |
| 34 | |
| 35 | radians = atan2(mouse_y - y, mouse_x - x) + (AL_PI/2.0); |
| 36 | |
| 37 | fixed allegro_degrees = ftofix( radians * 255.0 / (2.0 * AL_PI )) & 0xffffff; |
| 38 | |
| 39 | rotate_sprite(screen, tank, x-(tank->w/2), y-(tank->h/2), allegro_degrees); |
| 40 | |
| 41 | // Display angle |
| 42 | textprintf_ex(screen, font, 10, 400, -1, makecol(0,0,0),"Angle Degrees %d", allegro_degrees); |
| 43 | textprintf_ex(screen, font, 10, 410, -1, makecol(0,0,0),"Mouse X position %d", mouse_x); |
| 44 | textprintf_ex(screen, font, 10, 420, -1, makecol(0,0,0),"Mouse Y position %d", mouse_y); |
| 45 | } |
| 46 | |
| 47 | deinit(); |
| 48 | return 0; |
| 49 | } |
| 50 | END_OF_MAIN() |
| 51 | |
| 52 | void init() { |
| 53 | int depth, res; |
| 54 | allegro_init(); |
| 55 | depth = desktop_color_depth(); |
| 56 | if (depth == 0) depth = 32; |
| 57 | set_color_depth(depth); |
| 58 | res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); |
| 59 | if (res != 0) { |
| 60 | allegro_message(allegro_error); |
| 61 | exit(-1); |
| 62 | } |
| 63 | |
| 64 | install_timer(); |
| 65 | install_keyboard(); |
| 66 | install_mouse(); |
| 67 | /* add other initializations here */ |
| 68 | } |
| 69 | |
| 70 | void deinit() { |
| 71 | clear_keybuf(); |
| 72 | /* add other deinitializations here */ |
| 73 | } |
In the layter snippet the allegro_degrees are counted inside the while loop.. and in the first not.