|
what's wrong in this code |
J43G3R
Member #20,281
June 2021
|
I am trying to make a game called blasteroids, which requires displaying floating and rotating asteroids on screen. I have written a code hoping it would generate NR_ASTEROIDS no of asteroids on screen ,each appearing on a random coordinate on screen(at program start) and bounce around the screen with SPEED_X speed in x and SPEED_Y speed in y direction while also rotating continuously with OMEGA angular velocity, but I am getting completely different and absurd result. functions required to translate and rotate asteroids(declared in ./include/animation_funcs.h header file) are defined in translate_asteroid.c.Main source file is asteroids.c[everything is in the attachments]. |
SiegeLord
Member #7,827
October 2006
|
You didn't actually attach anything... "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
J43G3R
Member #20,281
June 2021
|
oh, sorry I thought I did,can you please check it again. I updated it.Thankyou:) |
amarillion
Member #940
January 2001
|
I don't know what the bug is exactly, but... Wouldn't it be simpler to re-calculate the transformation matrix each time, instead of trying to update it frame by frame? So each time you draw an asteroid
I think the code would be a lot less complicated this way, and much easier to spot the bug. -- |
J43G3R
Member #20,281
June 2021
|
I tried what amarillion said and I am attaching the modified version of asteroids.c and translate_asteroid.c. Now the asteroids behave as expected for the first few seconds, but then everything becomes hairy...They start bouncing with ever increasing speed and then suddenly disappear!!! |
Elias
Member #358
May 2000
|
translate_asteroid_x(ptr->trans,SC_WIDTH,&(ptr->x),&(ptr->speed_x)); translate_asteroid_y(ptr->trans,SC_HEIGHT,&(ptr->y),&(ptr->speed_x)); Are you sure you want to pass speed_x both times? -- |
amarillion
Member #940
January 2001
|
Ok, I debugged your code and I see two problems. The main one is this line: ptr->omega += ptr->omega; This will double the rotation angle each turn, eventually reaching infinity, at which point the transformation matrix will become meaningless and nothing is drawn anymore. Probably you'd want a constant angular momentum, so you can do something like this instead: ptr->omega += 0.1; Then there is a lesser bug here: translate_asteroid_x(ptr->trans,SC_WIDTH,&(ptr->x),&(ptr->speed_x)); translate_asteroid_y(ptr->trans,SC_HEIGHT,&(ptr->y),&(ptr->speed_x)); Note that you use ptr->speed_x for both x and y translation. If you fix that then it should work a lot better. -- |
J43G3R
Member #20,281
June 2021
|
Oooooops!!!but still the asteroids vanish after few seconds |
Edgar Reynaldo
Major Reynaldo
May 2007
|
double OMEGA = 1.0f; theta += OMEGA iff TIMER_EVENT theta = fmod(theta , 360.0);
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
amarillion
Member #940
January 2001
|
J43G3R said: but I still wonder what was wrong with my very first code??? I haven't checked, but my guess is that small rounding differences build up over time. You tried to keep state in two places: in the transformation matrix, as well as the coordinates (x, y, and omega). Over time, small rounding differences build up, especially because the omega gets crazy large. My advice: don't try to keep two parallel states because you can't sync them perfectly. Derive the matrix from the coordinates each frame, this is simpler and won't cause syncing issues. -- |
J43G3R
Member #20,281
June 2021
|
Edgar Reynaldo, sorry but I am not quite sure whether I understood what u said, can you please explain a bit about it?? |
amarillion
Member #940
January 2001
|
Yes, that is indeed what Edgar meant. But I think you're using radians, so it's better to keep it below 2 * PI = 6.282 -- |
J43G3R
Member #20,281
June 2021
|
amarillion 😅😅 yes I figured the 2 pi part, thanks anyways |
|