Thread: Star Simulator

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    Star Simulator

    Hello all,

    I've been trying to make a program that would simulate star movement. The source is included, although I doubt anyone will want to go through the whole thing. The program pretty much works fine, except for one thing, that being that almost every single time it's run, it crashes. I couldn't find any reason for it; sometimes it would crash right away, sometimes in the first second, sometimes in the second second, and sometimes (albeit rarely) it would run without any problems, but it wouldn't work as it was supposed to. I managed to find out that the error is located when the program generates the values for "velX" and "velY"; usually they're fine, but from time to time on a completely random basis the value of one "object" goes completely wrong....you'll find out what I'm talking about when you try it out. I have no idea what's going on with it, so I hope one of you do :-). Oh, before I forget, I'm using CodeBlocks, so that means the MiniGW compiler.

    While I'm at it, I also wanted to ask; as you can see, I had to use period signs and spaces to display the stars on the screen, which makes it very inelegant-looking and also limits the space. I was wondering, is there any way I can use single pixels for this job? I spent most of one day trying to find a solution; I tried SetPixel(), but when linking the compiler gave the error "Undefined reference to _SePixel@16". I tried installing SDL, which ended catastrophically, due to my inability to figure out how to install a library. I tried all the examples/links (except Alegro, which I think would end the same way SDL did) in this and other threads. I tried VGA. None of them worked. Any tips...?

    Thanks in advance for any help,

    G4B3
    Last edited by G4B3; 05-10-2008 at 08:07 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Assuming your running windows, theres a tutorial on how to setup SDL for codeblocks here:
    http://lazyfoo.net/SDL_tutorials/les...ocks/index.php

    Otherwise if you are using Linux, you should be able to install it all with a single comand. For debian based distros its something like:
    sudo apt-get install sdl1.2-dev

    I dont really have time to go over your code at the moment, but someone else should help. Good luck with it.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Yes, I'm using Windows. Thanks a million :-)

    Hope someone will have a sec for the code itself

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Comment out the calls to objMove(); and objDisplay(); to see how long it runs for. If it crashes in the same way, start looking at objBehave();

    If that's OK, add in objMove() and let it run again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    I'll try, thanks....but my guess is that it really is with objMove()...problem is, it still doesn't explain why the Random Generator is acting up at completely random times...

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    okay, it's for certain, the problem is in objMove(). At least I know where to focus my attention ) thanks

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    OKay, I managed to isolate the problem to this line

    object[numObj].life = object[field[object[numObj].coordX][object[numObj].coordY]].life = field[object[numObj].coordX][object[numObj].coordY] = 0;

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Negative that's not your problem. Your "objects" velocities can be very large. There is a good possibility that they will move more than MAXHIGHT spaces past the end of your array.

    In objMove, instead of checking "if (object.x >= MAXHIGHT)" you should be doing "while (object.x >= MAXHIGHT)". Change it similarly for Y.

    Also, in objBehave you have:

    Code:
            if(!--object[numObj].life) {
                field[object[numObj].coordX][object[numObj].coordX] = FALSE;
                continue;
            }
    Shouldn't that be coordY? Making those changes cause this to work fine for me.

    [edit]
    Ok after looking at the output of the program for a while, I went and read the rest of your description about what it's supposed to be doing. You're going to have to qualify "pretty much works fine" because I don't think this is working at all. Unless by "works fine" you mean "outputs something".
    [/edit]
    Last edited by arpsmack; 05-10-2008 at 03:46 PM.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Funny, I changed it to "while" even before I read this Still doesn't seem to solve the problem...for some unknown reason, I keep getting negative values for coordX/coordY....

    thanks for pointing out the coordX error.... :-)

    and that was a joke, of course it doesn't work it's nowhere near "pretty much works fine" but I'm a lot farther then I was this morning

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Oh there was another thing I changed but forgot to mention.

    What if your X and Y coords drop below 0 (i.e. your object is moving up and to the left).

    You need to check if X < 0, and if so add MAXHIGHT. Likewise for Y.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    oooooooooooooooh, THAT's where the problem was. I edited it so I was checking for under zero, but I didn't relise that I was always subtracting MAXLENGTH/HEIGHT even when it was under zero....that's the problem Thanks man I'm pretty sure it should work now :-)

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    okay, so I changed it to while, and it works, but now it's taking forever for some reason...any ideas?

    edit: I found out why....it's because, from time to time, coordX and coordY are equal to -2147483646 or some such huge (or small, to be more precise) number...

    edit2: The problem resides in velX and velY...for some reason they're completely giberish....but only sometimes, and I can't find out when...when I checked the output of the random generator at the very begining, they were fine, but when I access them int the objGrav() function, they're not....
    Last edited by G4B3; 05-11-2008 at 02:48 AM.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    And it's working. Finally. The problem was, how surprisingly, the lack of an "if" preventing the program from searching for values outside the array. Along the way I also found another problem, which wouldn't affect the programs functionality, but the program wouldn't work the way it was supposed to.

    I'd like to thank everyone for their help :-) Here's the finished source. I'm still going to edit it some, cause I need to add realistic values for velocities and stuff, and then I want to remake it using SDL so it actually looks a little acceptable :-) But all that in due time. Thanks again
    Last edited by G4B3; 05-11-2008 at 05:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with cache simulator!
    By dtogers123 in forum C Programming
    Replies: 3
    Last Post: 04-30-2008, 06:18 PM
  2. Random question simulator
    By Pickpocket in forum C Programming
    Replies: 9
    Last Post: 12-04-2006, 07:13 AM
  3. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  4. Star Wars Kid: Episode II.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 54
    Last Post: 07-31-2003, 04:41 PM
  5. Star Wars or Star Trek
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 09-28-2001, 08:33 AM