Forgotten mana...
Don Freeman

Ok...this topic has been punished enough, but I can't find it using allegro/forums/search at the moment.

Questions is:

I am rotating a sprite using allegro's rotate_sprite(...) function. I want the image to move in the same direction as the angle is. I've done this before, but it's been a long time ago. I know it's got something to do with converting radians to degrees...converting degrees to allegro degrees...conjuring magical dragons...and all that, but I can't remember.

////////////////////////////////////////////////////////////////
// in the update routine:
xVel = cos( angle ) * vel;
yVel = sin( angle ) * vel;
x += xVel;
y += yVel;
////////////////////////////////////////////////////////////////
// then in drawing routine:
rotate_sprite(pDest,pBmp,x+pBmp->w/2,y+pBmp->h/2,ftofix(angle));
////////////////////////////////////////////////////////////////

ReyBrujo

Sin & Cos: The Programmer's Pals!, see the Using atan2(): homing missiles section.

Don Freeman

Here is my code sample:

The image is a simple triangle with a white line facing north.

1//////////////////////////////////////////////////////////////////////////////////////////////////
2Player::Player()
3{
4 x = 0.0f;
5 y = 0.0f;
6 vel = 0.0f;
7 xVel = 0.0f;
8 yVel = 0.0f;
9 angle = GetRadians(0.0f);
10 pWeapon = new Weapon;
11 pBmp = NULL;
12}
13Player::~Player()
14{
15 if ( pWeapon )
16 delete pWeapon;
17 pWeapon = NULL;
18 if ( pBmp )
19 destroy_bitmap(pBmp);
20 pBmp = NULL;
21}
22void Player::Display( BITMAP *pDest )
23{
24 if ( !pBmp )
25 return;
26 if ( !pDest )
27 return;
28 rotate_sprite(pDest,pBmp,x+pBmp->w/2,y+pBmp->h/2,ftofix(GetDegrees(angle)*(40.74366543153)));
29}
30void Player::Update( void )
31{
32 xVel = cos( GetDegrees( angle ) ) * vel;
33 yVel = sin( GetDegrees( angle ) ) * vel;
34 x += xVel;
35 y += yVel;
36 pWeapon->Update(*this);
37}
38// Degrees = radians * 180 / PI
39// Radians = degrees * PI / 180
40// const float Rad2AllegroDeg = 40.74366543153;
41// float radian = whatever;
42// float AllegroDeg = radians * Rad2AllegroDeg;
43//////////////////////////////////////////////////////////////////////////////////////////////////
44float GetDegrees( float radians )
45{
46 return (radians * 180 / PI);
47}
48//////////////////////////////////////////////////////////////////////////////////////////////////
49float GetRadians( float degrees )
50{
51 return (degrees * PI / 180);
52}
53//////////////////////////////////////////////////////////////////////////////////////////////////

I know I am just doing a conversion wrong somewhere...

Arthur Kalliokoski

angle = 1.0; //radians
angle *= 180.0/M_PI; //now it's degrees
angle *= 65536.0; //now it's suitable for rotate_sprite

That should work, money back guarantee

amarillion

Arthur: that doesn't work. rotate_sprite needs a value between 0 and 255.

  xVel = cos( GetDegrees( angle ) ) * vel;
  yVel = sin( GetDegrees( angle ) ) * vel;

These functions take radians, not degrees. Assuming angle is in radians (can't deduce that from the code), you can use it directly here without converting.

And you can use this (shorter) code:

  rotate_sprite(pDest,pBmp,x+pBmp->w/2,y+pBmp->h/2, ftofix(angle * 128 / PI));

edit:
Here are they all:

1//radians to degrees:
2float degrees = radians * 180 / PI;
3 
4//degrees to radians:
5float radians = degrees * PI / 180;
6 
7//degrees to allegro degrees
8fix al_deg = ftofix (degrees * 256 / 360);
9 
10//allegro degrees to degrees
11float degrees = fixtof (al_deg) * 360 / 256;
12 
13//radians to allegro degrees
14fix al_deg = ftofix (radians * 128 / PI);
15 
16//allegro degrees to radians
17float radians = fixtof (al_deg) * PI / 128;

OICW

But you need to convert radians into the fixed angle. Fixed angle is number between 0-255 and therefore you must multiple angle in radians by radtofix() constant. I advice you to have angle in fixed format because it will remove you many headaches.

amarillion

OICW: Only one conversion is necessary in the code above, i.e. when using rotate_sprite. All the other functions use radians.

Thread #566564. Printed from Allegro.cc