   using sin(int) and cos(int) functions
  I have primitive understanding of sine and cosine waves etc.I am trying to write a program that uses a bitmap the player can rotate and move (ie GTA 1 type movement i guess). I've got the rotation down but i'm trying to make it so it moves depending on the direction its rotated. here's what i have so far (taken *out* of context):int angle = 0;  // bitmap starts facing up, no angleint xmove = 0, ymove = 0; //the rate at which the bitmap travelsint x = 0, y = 0;    // used to display the bitmapwhile(1) // just ignore..{   if(key[KEY_LEFT]) angle--;   if(key[KEY_RIGHT]) angle++;   // theres a few lines relating to allegro here, and making sure the angle doesnt go to high/low.   if(key[KEY_UP]) {x += xmove; y+=ymove;} // three lines rotate and move bitmapSTOP! Here's the problem, im not sure if im to relate sin or cos to x/y. Do i do this :xmove = sin(x); or xmove = cos(x);    ?Also, because the result of sin/cos will be .whatever should i multiply them by i dunno...3 or 4?Thanks.   
  its normally cos(x) and sin(y) for circle   ;  It depends on how many angles you want in a circle. A common and practical value is 256 degrees (as opposed to 360).const double pi = 3.1415926535897932384626433832795;const double scale = 2.0 * pi / 256.0;double move_x = cos(angle * scale);double move_y = sin(angle * scale);   ;Quote:Original post by Anonymous PosterA common and practical value is 256 degrees (as opposed to 360).Only if you're going to use a lookup table. Otherwise, there is no benefit aside from the fact that your angle variable fits in a char.   ;Quote:Original post by FrunyQuote:Original post by Anonymous PosterA common and practical value is 256 degrees (as opposed to 360).Only if you're going to use a lookup table. Otherwise, there is no benefit.Well, you get virtually free handling of wraparound and clipping (especially if it's recorded in a byte-sized integer type). So you don't have to worry about numerical stability or the values going "out of range". Even if you used integers you'd either get a "jump" or start to loose precision (at least for 32-bit floats) after letting the counter run long enough.It's also a compact way of serializing angles, which is nice in case you want/need to use a single unit everywhere. It probably wouldn't hurt to extend it to 1024 degrees however.Oh, and the original post was mine. I was just to lazy to login on this machine..edit: Another non-obvious trick that takes advantage of the wraparound arithmetic is that I was recently able to reuse a simple interpolation class originally used for positions as an interpolated angle by simply by changing the type to a signed byte, and thus getting it to turn the shortest way.   ;  Alright, so im gonna post all the source code cuz im still having a few problems (but the input so far has helped).i hope source is the right tag.<source> #include <allegro.h>#include <math.h> /* TODO (#1#): Initialize allegro */ /* TODO (#2#): Display a bitmap */ /* TODO (#3#): Allow the bitmap to rotate */ /* TODO (#4#): Move the bitmap */ /* TODO (#5#): Add animation */ int main(void){    allegro_init(); // initialize allegroinstall_keyboard(); // ready keyboard for inputset_color_depth(16);    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 400, 400, 0, 0); // create a windowBITMAP *edgar = load_bitmap("edgar-standby.bmp", NULL);    BITMAP *d_buffer = create_bitmap(SCREEN_W, SCREEN_H); // load bitmap and double bufferbool quit = false; // has exit been pressed?    int angle = 0; // angle of bitmap    float x = 0, y = 0; // location of edgar    double xmove = 0, ymove = 0; // movement speed of edgar    const double pi = 3.1415926535897932384626433832795; // constant of pi    const double scale = 2.0 * pi / 360; // speeds up edgar according since cos and sin are decimal numbers    while(!quit)    {   if(key[KEY_ESC]) quit = true; // exit has been pressed    xmove = cos(angle*scale); // determine edgars direction  ymove = sin(angle*scale);    if(key[KEY_LEFT]) angle--; // turning  if(key[KEY_RIGHT]) angle++;  if(key[KEY_UP]) {x += xmove; y += ymove;} // move edgar   rectfill(d_buffer, 0, 0, SCREEN_W, SCREEN_H, 0); // clear back buffer   rotate_sprite(d_buffer, edgar, x, y, itofix(angle)); // draw a rotated edgar  blit(d_buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // update screen    }destroy_bitmap(edgar);return 0; }END_OF_MAIN() </source>I hope that you guys are framiliar with allegro! If not i'll try and explain anything thats native to allegro. Basically, the program works. My bitmap (edgar of ff6...just a place holder) rotates properly and moves properly until he's made more than a 90 degree turn, then precision is lost. Even though he's on a slight tilt to the left, he's gonig a noticable amount to the right!\ <-------- bitmap, it's supposed to start moving to the left now...in the actualy program the tilt is less severe. but it still moves right!EDIT:Oops. I worked out the problem there, the source code i posted differed from what i was using...one a edited slightly. Up there is revised code. Now there's another problem though. When I rotate the bitmap aaaaaaalll the way around and it's *just* beginning to be on a slant upwards it keeps going down! If I change direction it begins to go up but if i keep rotating that way it refuses to go down! I can't explain it too well, hopefully someone has allegro and can fix the problem by compiling my code (with a different bitmap), please![Edited by - Le_Danse_Macabre on December 26, 2005 2:02:08 PM];  Well, as you probably know, the Allegro's rotate_sprite function uses 256 instead of 360 for the max(64 = 90 degrees, 128 = 180 degrees, etc).  So you need to determine which quadrant you are facing and make xmove and ymove have the appropraite sign.  For example, assuming you start out facing positive x, then any angle between 192 and 256 will have a positive xmove and a negative ymove(moving up and to the right).  I think you can figure the rest out from here but if you need some more help just post back and we'll try to get you going.  Hope that helps.-AJEDIT: You also need to make sure then when your angle equals zero and the user wants the angle to go down that you make your angle 256 otherwise it will go to  -1.   ;Quote:Original post by doynaxQuote:Original post by FrunyQuote:Original post by Anonymous PosterA common and practical value is 256 degrees (as opposed to 360).Only if you're going to use a lookup table. Otherwise, there is no benefit.Well, you get virtually free handling of wraparound and clipping (especially if it's recorded in a byte-sized integer type). So you don't have to worry about numerical stability or the values going "out of range". Even if you used integers you'd either get a "jump" or start to loose precision (at least for 32-bit floats) after letting the counter run long enough.It's also a compact way of serializing angles, which is nice in case you want/need to use a single unit everywhere. It probably wouldn't hurt to extend it to 1024 degrees however.Oh, and the original post was mine. I was just to lazy to login on this machine..edit: Another non-obvious trick that takes advantage of the wraparound arithmetic is that I was recently able to reuse a simple interpolation class originally used for positions as an interpolated angle by simply by changing the type to a signed byte, and thus getting it to turn the shortest way.Wouldn’t you get all the same advantages but much better resolution using a 16 bit integer?   ;Quote:Original post by GrainWouldn’t you get all the same advantages but much better resolution using a 16 bit integer?Ponder this: do you really need a 0.0055° angular resolution for a game. If you do, you might as well go ahead and use a float.   ;Quote:Original post by FrunyQuote:Original post by GrainWouldn’t you get all the same advantages but much better resolution using a 16 bit integer?Ponder this: do you really need a 0.0055° angular resolution for a game. If you do, you might as well go ahead and use a float.Perhaps not but its entirely reasonable to expect to need at least 1° resolution and 8 bits doesn’t offer that. And besides a float doesn’t have all those advantages he mentioned.   