Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » what's wrong in this code

This thread is locked; no one can reply to it. rss feed Print
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
avatar

You didn't actually attach anything...

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

J43G3R
Member #20,281
June 2021

oh, sorry I thought I did,can you please check it again. I updated it.Thankyou:)
With this post I have attached a program similar to asteroids.c but there are two differences 1.) it works 2.) it creates only one asteroid.

amarillion
Member #940
January 2001
avatar

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

  • Start with a fresh identity matrix,

  • Rotate with the given omega

  • Translate with the given curr_x and curr_y

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?

--
"Either help out or stop whining" - Evert

amarillion
Member #940
January 2001
avatar

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
EDIT: it worked after I corrected the naively added ptr->omega+=ptr->omega to ptr->omega+=OMEGA.Thankyou so much everyone.....but I still wonder what was wrong with my very first code???

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

amarillion
Member #940
January 2001
avatar

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??
EDIT: ok I got your point now, if I just keep on increasing the value of theta , then after certain time the value will get too large to fit into the variable and our program can crash,so we keep it below 360.Right?

amarillion
Member #940
January 2001
avatar

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

Go to: