The online racing simulator
Unusual problem (VB6) [solved]
(15 posts, started )
Unusual problem (VB6) [solved]
Hi

Not sure how to best describe this problem but here goes:

I've got a loop of code that runs to accelerate a vehicle from a standing start to terminal velocity, which self terminates either once a certain condition is met, or once it runs for a certain number iterations. This works fine when the time step for the loop is a tenth or a hundreth of a second, but runs into a problem when the minimum time step (one two thousandth of a second) is used.

The problem is that it seems to freeze, by that I mean once it reaches a certain point, all the values being calculated stop changing, with the exception of the distance travelled, and I can't figure out why. It doesn't happen at a certain iteration, or when any parameter reaches a certain value, and no errors are received.

I've no ideas why the problem only occurs with the smallest timestep, it's exactly the same code, only the loop runs for longer. I've ruled out the loop count being a problem itself. All the values being calculated are, essentially, the same regardless of the time step. Basically I've ruled out everything I can think of.

Anybody have any ideas? If you need any more info, or example, just ask.

Help much appreciated.

Cheers
Could you post your code? Been a few years since I last used Vb6 but I might be able to help
#3 - Stuff
Hrm.. I don't have much experience with loops really. I try to avoid them as much as possible because they are some of the worst offenders of lock-ups. Never heard of any problems with any time intervals with them. Not even sure how a time interval comes into play with the loop?

For a solution, maybe a DoEvents will help. Giving your program a chance at something else while the loop is going. Other than that, maybe try to accomplish the same goal without the loop? Hmm..
Sorry, the timestep is just one variable involved in the acceleration of a vehicle, e.g. you calculate it at 0.0 seconds, then again at 0.01 seconds, then again at 0.02 seconds, and so on. The smaller the time interval, the more accurate the simulation is, and thus the more times the loop will run.

It has nothing to do with the actual passing of realtime.

In this case, my timestep means the loop is running nearly a quarter of a million times, so I don't fancy using the break feature and watching the variables change every time the loop, erm, loops.

Nor do I fancy posting the code since it's around 200 lines, and uses lots of variables and types that are defined elsewhere in the program.

What's DoEvents, I've never tried that?
#5 - amp88
Just to clarify...when you change the frequency of the calculation (i.e every tenth, hundredth etc of a second) you're not actually determining at which point in real time the calculation is performed? Is that correct?
without seeing code, i would take a stab in the dark and say a floating point error is happening somewhere. does the elapsed time increase as it should be?
DoEvents forces to process pending events, and may help - could be that the loop updating pace is too fast for the rest of your program to keep up and it looks like it freezes.

IIRC there was a timer component in VB6, I'd suggest to use that instead of a loop. Do whatever you're doing in your loop inside the Tick event of the timer.
#8 - Stuff
Yeah, like BurnOut said, DoEvents is a keyword that allows your program to process other stuff (like pending events) in the middle of other stuff (like loops). MSDN linkage

If something like that doesn't work then look into other ways of counting or looping. Check out VBspeed for some articles on the fastest way to do things, including their class, CTiming (under Rules). It may help with ideas on how to count an interval differently using API calls.
Thanks for the tips, I'll look into it.

To shed some more detail on my problem, I've saved the outputs of most of the variables calculated while the loop runs to some CSV files. You can see that in tests 1a and 2a - both with the 0.01s timestep - everything runs fine. Tests 1b and 2b - both using the 0.0005s timestep - "freeze" after at a certain point, I've put that row number in the filename. Btw they are only outputting every 5th line stored or the csv would have more than 65,536 lines in it and you wouldn't be able to open the rest of the file.

If you wish, you can download them here - 4,250KB zip.
edit: nvm, sorry.
Correct me if I'm wrong, but does'ent the LFS engine only update it self on the max 100 times a second? Try using a .001 time stamp, that should allow for max precision, while not wasting cycles.
The LFS physics outer loop runs at 100Hz (includes things like collision detection), but inside that is an inner loop which runs 20 times (moving thet car, deforming tyres, etc), giving an effective 2000Hz physics rate. I'd like my program to be able to work at the same rate.

I would agree though, the difference between 10Hz and 100Hz is a lot (even for my simple purposes), while the difference between 100Hz and 2kHz is quite small (again, for my purposes).

I could probably get around this problem by reducing my 2000Hz option to 500Hz, but that does help me answer this mysterious question, I'm quite perplexed.

Edit: Did a little testing, with a timestep of 0.002 (500Hz) there is no problem, but 0.001 (1kHz) gives the same problem as 0.0005 (2kHz). If I can't cure the problem (which will be the case unless I can work out what's going wrong) I'll just have to make do.
Could you re-write the loop for testing purposes to use a For Next loop. You could then test how long it takes it to run say 1000 times when going flat out.
Maybe your loop is just too slow in VB6 and by calling it after a set time you are re-calling it before it has finished the last pass. You might then have 100's or 1000's of attempts running concurrently which would effectively stall the process?

Maxim
Eh, I knew this would be hard to explain. The sub is only called once, the loop within it is exactly the same no matter what the value of the timestep variable, only it runs for the same amount of virtual time, so a smaller timestep means the loop runs through more times. The loop freezes up at a different iteration depending on certain variables, so it's not a problem (directly related) to the ever increasing loop count.

I'm probably not using the best terminology to explain things, the majority of my VB6 knowledge is self taught.

Thanks for suggestions though folks.
Aha, solved the little bugger last night. I'll keep this short rather than rambling on and on:

With a smaller timestep, all the calculated variables are the same, however since less "time" is passing between each iteration, the difference in speed is getting smaller. Since I'm accelerating vehicle up to top speed, the difference in speed is almost nothing, in fact eventually even too small to flip the LSB in a single precision floating point variable. A quick replace all to double precision and the problem went away. Although overkill to leave all 580+ floats at double precision, I undid that and now the 8 or so key variables are at double, and all works like a charm.

Woohoo.

Unusual problem (VB6) [solved]
(15 posts, started )
FGED GREDG RDFGDR GSFDG