Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » move a bmp by an angle

This thread is locked; no one can reply to it. rss feed Print
move a bmp by an angle
evenzzz
Member #7,276
May 2006

Hello

My BITMAP rotate left and right on keypress left and right, but this BITMAP don't go forward in the right angle on keypress up.

I think the problem is in calculating coordonate of the bitmap here :

x += 1 * cos(angle) ;
y += 1 * sin(angle) ;

someone can help the poor English speaker I'm please :)

1#include <allegro.h>
2#include <time.h>
3#include <math.h>
4 
5#define ECRAN_X 800
6#define ECRAN_Y 600
7 
8#define ERREUR(msg) {\
9 set_gfx_mode(GFX_TEXT,0,0,0,0);\
10 allegro_message("Error : \n%s\n", msg);\
11 allegro_exit();\
12 return 1;\
13}
14 
15 
16int main()
17{
18BITMAP *tank;
19BITMAP *page;
20float angle=0;
21float x, y;
22 
23 allegro_init();
24 install_keyboard();
25
26 set_color_depth(16);
27 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, ECRAN_X, ECRAN_Y, 0, 0) != 0)
28 ERREUR(allegro_error);
29
30 
31 tank=load_bitmap("tank.bmp",NULL);
32 page=create_bitmap(SCREEN_W,SCREEN_H);
33 if (!tank||!page)
34 ERREUR("load image");
35 
36 x=SCREEN_W/2;
37 y=SCREEN_H/2;
38
39 while (1){
40 rest(10);
41 if (key[KEY_ESC] )
42 return(0);
43 if (keypressed()){
44 if (key[KEY_LEFT]){
45 angle-=0.5;
46 }
47 if (key[KEY_RIGHT]){
48 angle+=0.5;
49 }
50 if (key[KEY_UP]){
51 x += 1 * cos(angle) ;
52 y += 1 * sin(angle) ;
53 }
54 }
55 angle=(angle>256)?0:angle;
56 clear_bitmap(page);
57
58 rotate_sprite(page, tank, x, y, ftofix(angle));
59 blit(page,screen,0,0,0,0,SCREEN_W,SCREEN_H);
60 }
61 return 0;
62}
63END_OF_MAIN();

deps
Member #3,858
September 2003
avatar

You have to use radians with sin and cos.

---
Layers are good, all good things have layers, like onions. Good things also stink. - Trezker
I now have a page and a blog!

Steve Terry
Member #1,989
March 2002
avatar

And cos/sin are in 360 degrees, not 256.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

evenzzz
Member #7,276
May 2006

ok

I try this but it dont work :

#define DEGREES(x) int((x)/360.0*0xFFFFFF)
#define RADIANS(x) int((x)/2/M_PI*0xFFFFFF)

so i try this :

x += 10 * (cos((angle * 360 / 256 )) * ( M_PI / 180)) ;
y += 10 * (sin((angle * 360 / 256 )) * ( M_PI / 180)) ;

both dont work :(

CursedTyrant
Member #7,080
April 2006
avatar

Tell me something:

angle * 360 / 256

What the hell does it do?

Try this, but I'm not sure what do you want to do:

float angle, x, y;

x += cos(angle*M_PI/180);
y += sin(angle*M_PI/180);

---------
Signature.
----
[My Website] | [My YouTube Channel]

evenzzz
Member #7,276
May 2006

angle * 360 / 256 -> because allegro d'ont rotate over 256°

"Allegro uses a bizarre angle format for angles in all graphics commands"

miran
Member #2,407
June 2002

It should be angle_in_degrees * 256 / 360 to turn degrees to Allegro degrees. It's not that Allegro doesn't rotate over 256°, Allegro's degrees are on a scale between 0 and 255. So a full circle in Allegro's functions is 256 units which is equivalent to 360 degrees. Sin and cos and other standard math functions of course work in radians. So basically you should keep you variables as radians and when you need to pass them to an Allegro function, convert them from radians to Allegro degrees:

angle_in degrees = angle_in_radians * 180/M_PI;
angle_in_allegro_degrees = angle_in_degrees * 256.0 / 360.0

--
sig used to be here

evenzzz
Member #7,276
May 2006

Thank all i find the answer

if (key[KEY_UP]){
xenemy += enemy_vel * cos(angle) * dt;
yenemy += enemy_vel * sin(angle) * dt;
}

rotate_sprite(buffer, enemy, xenemy, yenemy, itofix((angle/2/M_PI*256)));

it work great !

gnolam
Member #2,030
March 2002
avatar

Steve Terry said:

And cos/sin are in 360 degrees, not 256.

Say what?

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

Go to: