Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 8 directions

This thread is locked; no one can reply to it. rss feed Print
8 directions
neil dwyer
Member #7,237
May 2006

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
Member #2,030
March 2002
avatar

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

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

neil dwyer
Member #7,237
May 2006

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

Elverion
Member #6,239
September 2005
avatar

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.

--
SolarStrike Software - MicroMacro home - Automation software.

neil dwyer
Member #7,237
May 2006

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

Johan Halmén
Member #1,550
September 2001

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

neil dwyer
Member #7,237
May 2006

ok

Go to: