Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Forgotten mana...

This thread is locked; no one can reply to it. rss feed Print
Forgotten mana...
Don Freeman
Member #5,110
October 2004
avatar

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));
////////////////////////////////////////////////////////////////

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

ReyBrujo
Moderator
January 2001
avatar

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

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Don Freeman
Member #5,110
October 2004
avatar

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...

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Arthur Kalliokoski
Second in Command
February 2005
avatar

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

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

amarillion
Member #940
January 2001
avatar

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;

--
Martijn van Iersel | My Blog | Sin & Cos | Tegel tilemap editor | TINS 2017

OICW
Member #4,069
November 2003
avatar

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.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

amarillion
Member #940
January 2001
avatar

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

--
Martijn van Iersel | My Blog | Sin & Cos | Tegel tilemap editor | TINS 2017

Go to: