The Highway ########### We are trying to build a simulator for a chunk of the highway through downtown Austin. .. note:: This is actually easy: put tons of cars on the road, and never move them. The real highway through Austin is also known as the "world's biggest parking lot". it stretches from San Antonio to North of Dallas! YMMV! As we have been learning, we need to break down our problem into a set of classes, each responsible for one part of our problem. This entire project was inspired by the ``PoolBall`` project, where we have a bunch of balls sitting on a table, moving around and bouncing off of each other and the table edges. If we turn the PoolTable into the actual highway, and turn PoolBalls into cars, we are almost there. However, we really do not want our cars to bounce off of each other, except by accident. Our new simulation will need to address how accidents happen and what to do when they do happen. Highway Class ************* Let's start off by building a class that will model the highway: .. literalinclude:: code/Highway.h :linenos: :caption: include/Highway.h .. warning:: In this version, everything is ``public``. This is a bad idea, and you should decide what to make public, and what to make private. Those decisions may cause you to add additional methods to let users get at the private data. Highway Attributes ================== When you start out designing a class, you begin by thinking about physical things from the world where the problem came from! In our case, we are dealing with a physical highway, which has some number of lanes, each of which is so wide, and the total highway is some length. The simulation we re going to set up will divide that total length into a set of segments to make displaying a long highway easier to do on our skinny screen. A number of the attributes you see here are there to allow us to think in terms of physical units of measure (miles, feet) and map those units into our simulators world (pixels, etc). Highway Methods =============== Besides the obvious constructor, we define a bunch of methods that will help us manage all of those cars we will be adding, and making them move. Notice the method that will "draw" the highway. That routine will need to draw a complete screen, with all cars in their "current" position", every time we call it. .. note:: Actually, we will never call that method. Our "drawScene function, part of the graphics engine, will do that calling. Highway Traffic =============== This class is responsible for managing all the traffic we will see on the screen. We need a way to add new cars who enter the highway, "retire" cars that leave the highway, and make those cars do something fun! For this starting class, we are setting up an array of cars, which is really a bad approach. Given that you have a nice Linked-List class around (you do, don't you?) It would be better to use that to set up our collection of cars. Then we are not constrained by some predefined maximum size. Adding Cars ----------- As a starting point for this simulation, we need to add new cars to the highway entrance. Those cars appear at random times in each lane, and are a random distance apart, going a random speed. Yikes, that is a lot of randomness to deal with. Right! It looks like we need a nice random number generator to help out. We will look at that soon, then we just need to use it wisely. When we create new cars for the simulation, we will use the random number generator to initialize attributes for those cars (something we will see in a bit). However, you cannot just accept those random values. You need ot confirm that what you have makes "sense". One car should not land on top of another car, and a car should not be going 30 MPH faster thn another car directly in front of it. You do not want collisions to happen right away! All of this logic must be included in the code that places a new car in the simulation. Retiring Cars ------------- When a car exits the simulation, we do not need that car again. In an array based design, we can use that index to set up a new car that will enter at the beginning of the highway. In a linked list implementation, we "reuse" the old data node, and reset the attributes so it is a new car. Collision Avoidance ------------------- The real heart of this project is deciding how vehicles behave. We will store behavior attributes in each vehicle object, but it will be the highway class that makes all of the action happen Your challenge is to come up with a set of behaviors that make your simulator act like a real highway. Let's see: * Some drivers go fast * Some drivers go slow * Some drivers want to be in the "fast lane" * Some drivers want to stay in their lane. Some Drivers are willing to "tailgate" * Some drivers want a half mile between them and the car ahead. * Some drivers change lanes "instantly * Some drivers change lanes slowly Finally, every driver reacts to things based on a reaction time. If they are too slow, they may run into something and cause an accident. Then what do we do? (My solution: make turn them back into poolballs, and let them bounce off of each other and the barriers on the sides of the road! Each decision about what to do to move a car is based on attributes we will be recording in each vehicle. The challenge is to use those data items to move a car "realistically": * Does the driver notice all the vehicles around their car? * Are they texting? * If they do not pay attention, and they try to move into another lane, do they hit another car? Fun time! Vehicle Class ************* Now, let's consider a class to manage a single car: .. literalinclude:: code/Vehicle.h :linenos: :caption: include/Vehicle.h Nothing much here. This is much like the class we set up for the PoolBall problem. In this design, we probably intend to draw our cars as small circles on the screen, just like a tiny poolball. If you want to make a car into a rectangle, you need a length and width, and if you want a truck, you might have attributes for the front truck part, and another set for the trailer. Of course, modeling collisions if your vehicles are rectangles is going to be harder to figure out, but that is something you may want to consider. We clearly need more attributes and methods, but this is a start! Moving Forward ************** You have a basic structure to move forward, all you need to do is add the code toget things set up, then turn the simulation loose. Have some fun with this, it can become addicting! As your design progresses, you may find the need to add attributes to these classes, and methods as well. Those decisions are usually made after you try something, and decide you can make things "better". That is what programming is all about.