
-----------------------------------
dudeofgods
Sat May 21, 2011 3:49 pm

Making a traffic simulator
-----------------------------------
Hey guys I am making a simple traffic simulator for my computer class, however i am stumped on how to implement turns and multiple cars into my program. 
Can anyone help?? 

Thanks!!

-----------------------------------
HRI
Sat May 21, 2011 9:27 pm

RE:Making a traffic simulator
-----------------------------------
Well the very first thing I'd do is get rid of that horrible flickering :(
You can do so via a combination of setscreen ("offscreenonly") and View.Update.

Next, I notice you use 360 degree arcs for circles. drawfilloval might be easier.

Instead of drawing 4 specific cars, why not create an array of cars, have the procedure to draw a car take in the position etc as arguments, and have a variable for each saying whether they're on the screen or not (whether to draw them). To be honest, I would personally create a record with this info and make an array of that record, as that basically lets you store each car's position, direction, visibility, whatever you want.


type Car :
    record
        %variables go here
    end record

var randomCar : Car
randomCar.colour := brightred
% you can access the variables via the dot operator


As for turns, the only thing I can say is check whether the car is in the right spot to make a turn, and figure out a trigonometric relationship you can use to draw the car at a certain angle as it turns. Or you can try using pictures and Pic.Rotate, although the one time I used that it had some weird clipping issues :p

EDIT: Almost forgot, NEVER USE PROCESSES :P 
There's a good thread in the tutorials section explaining why. Also, I think it might be making that View.Update still cause flickering as both are running at once.

To resolve the issue (again, this would be easier with an array or preferably record), store the position of everything, draw it once in your main loop, and then use View.Update once after everything is drawn. I also think you forgot the "offscreenonly" part, because View.Update is a bit useless without it :p

-----------------------------------
Tony
Sat May 21, 2011 9:57 pm

RE:Making a traffic simulator
-----------------------------------
HRI gives good advice here. If you implement everything for a single car in an array of size 1, it is trivial to increase the size of array and have more cars without writing (much?) additional code.

-----------------------------------
dudeofgods
Sun May 22, 2011 10:14 am

Re: Making a traffic simulator
-----------------------------------
Thanks, HRI

you have very good idea's. However, i am still a noob at Turing so, i think i will not use array's and stick with the function pic.rotate that you just told me about.

Thanks alot,

                Dudeofgods

-----------------------------------
Raknarg
Sun May 22, 2011 2:28 pm

RE:Making a traffic simulator
-----------------------------------
Although your free to do that, it's probably much better if you at least try arrays first. Not just because it's good for your program, it's a very helpful thing to learn for programming. I'd recommend at least looking into it, it'll put you a step ahead of your class.

-----------------------------------
dudeofgods
Mon May 23, 2011 6:11 pm

Re: RE:Making a traffic simulator
-----------------------------------
Although your free to do that, it's probably much better if you at least try arrays first. Not just because it's good for your program, it's a very helpful thing to learn for programming. I'd recommend at least looking into it, it'll put you a step ahead of your class.

 Ya!!  I tried it with arrays today, and it made the program so much simpler and less complex than it was before.

thanks,
          dudeofgods
