MECH 415 Midterm Review Part II

Question #1

a) Write a class called car with the following public member variables:

double Px, Py; // position of the car (global coordinates)

double Vx, Vy; // velocity of the car (global coordinates)

double R; // radius of the car (assume it’s approximately spherical)

mesh *Pm; // pointer to a mesh object for drawing the car

It has the following public member functions:

b) A constructor with an argument for each member variable except for Pm. For Pm a string with the name of the mesh file_name is supplied.

c) A destructor.

d) get_input(char Q[]) - this function reads the keyboard and sets the velocities as follows:

key Q[0] gives Vx = -1

key Q[1] gives Vx = 1

otherwise Vx = 0

key Q[2] gives Vy = -1

key Q[3] gives Vy = 1

otherwise Vy = 0

Note the 1D array Q[] can be used to configure inputs for various keys. For example Q[] = {‘A’,’B’,’C’,’D’} would configure the a, b, c, and d keys for input to move the car.

e) sim_step(dt) –this function updates the states Px, Py using Euler’s method as follows:

Px += Vx*dt;

Py += Vy*dt;

f) draw() – this function draws the car using the x-file “car.x” (in graphics example #5 folder) with random initial position parameters in the range -10 to 10 for Px and Py. The yaw angle, in order to point in the velocity direction, is given by atan2(Vy,Vx). You should double check the order of the arguments for atan2 since some compilers reverse them.

g) set_view() – this function sets the view to 1st person perspective with respect to the car, pointing in the forward direction just behind the car (see graphics example #5 for an example).

h) Write a draw_3D_graphics() function to illustrate/test the car class. The program should set the view point, check inputs, simulate a step (dt = 0.01), and draw the car.

i) Organize the car class and functions into the files “car.h” and “car.cpp”.

Question #3

a) Develop a class called World with the following public member variables:

car *car1; // a dynamic car object

car *car2; // a dynamic car object

It has the following public member functions:

b) A constructor with arguments for each constructor of car1 and car2. In general, it’s recommended to use a different mesh object for each vehicle object, otherwise you need to be careful since the position variables will also be shared.

c) A destructor.

d) get_input() - this function reads the keyboard and sets the inputs of car1 and car2 as follows:

LEFT/RIGHT : Vx (-/+) car 1

DOWN/UP : Vy (-/+) car 1

‘A’ / ‘D’ : Vx (-/+) car 2

‘S’ / ‘W’ : Vy (-/+) car 2

e) sim_step(dt) – a function that performs a simulation step for each car.

f) draw() – a function that draws each car.

g) Write a draw_3D_graphics() function to illustrate/test the World class.Use the latest graphics_example3_new project to develop your program. You might want to initially try a 2D view in the x-y plane.

h) Organize the World class and functions into the files “world.h” and “world.cpp”.