8 directions
neil dwyer

Why are there only eight directions? I am assuming it has something to do with allegro degrees and the player1_rotate. My bitmap will rotate all 255 degrees but if I press forward it will only go North, South, East, West, NE, NW, SE, or SW. Sorry for all of my noob questions but I am a noob. Do I have to do the trig in radians and the normal trig functions?

1#include <allegro.h>
2#include <stdlib.h>
3 
4#define speed 1
5 
6int player1_x = 320;
7int player1_y = 240;
8int player2_x;
9int player2_y;
10int player1_rotate;
11int player2_rotate;
12int mickeyx;
13int mickeyy;
14int player1_vy;
15 
16void player1_movement()
17{
18 if(key[KEY_A])
19 {
20 player1_x -= speed;
21 }
22 if(key[KEY_D])
23 {
24 player1_x += speed;
25 }
26 if(key[KEY_W])
27 {
28 player1_y -= speed * fixtoi(fcos(itofix(player1_rotate)));
29 player1_x += speed * fixtoi(fsin(itofix(player1_rotate)));
30 }
31 if(key[KEY_S])
32 {
33 player1_y += speed;
34 }
35 get_mouse_mickeys(&mickeyx, &mickeyy);
36 
37 player1_rotate += mickeyx / 5;
38 
39}
40 
41int main(int argc, char *argv[])
42{
43 allegro_init();
44 install_keyboard();
45 install_mouse();
46 set_mouse_speed(1,1);
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, ftofix(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}
71END_OF_MAIN()

Thanks.

gnolam

You're converting the results of sin and cos [-1, 1] into integers.

Just dump all the fixed point and use floats like normal people already. :P

neil dwyer

how many radians is one allegro degree. It's like 40 something but I want to be exact.

Elverion

There are 2PI radians in a circle, or 256 Allegro degrees. That means there would be roughly 0.025 radians in 1 Allegro degree.

I think the problem with there only being 8 directions is due to rounding. If you are using itofix, that means you are taking radians (which are float), and rounding them to ints. The loss of data would cause problems like what you had stated.

neil dwyer

yes, but does it round to the nearest 16th whole number?

Johan Halmén

player1_x and player1_y should be floats, not ints. And the thing you add to them should be float, not int.

speed * fixtoi(fcos(itofix(player1_rotate)))

turns into -1, 0 or 1. So when you add that to both x and y, you get only 8 directions.

neil dwyer

ok

Thread #586208. Printed from Allegro.cc