Question: Task C. Velocity and moving objects Lets consider an object moving in 3D space. We already know how to describe its position. (We will be

Task C. Velocity and moving objects

Lets consider an object moving in 3D space. We already know how to describe its position. (We will be assuming metric system, thus distances will be implicitly measured in meters and time in seconds.)

The objects velocity can be represented in the same 3D coordinate system as its displacement per second in the coordinates x, y, and z:

Coord3D vel = {5, -3, 1}; // x, y, z components of the velocity 

When moving at constant velocity vv, the objects new position after the elapsed time dtdt can be computed as

x?=x+vx?dt,x?=x+vx?dt,y?=y+vy?dt,y?=y+vy?dt,z?=z+vz?dt.z?=z+vz?dt.

Lets implement it. In the same program, write a function

void move(Coord3D *ppos, Coord3D *pvel, double dt); 

which gets the position and the velocity of an object and has to compute objects new coordinates after the time interval dt. The function does not return any values, instead, it should update the objects position ppos with its new position coordinates.

Because we pass the coordinates Coord3D * ppos as a pointer, all changes to the fields of the struct pointed by ppos, will affect the original structure you pass into the function, not its local copy. Example:

int main() { Coord3D pos = {0, 0, 100.0}; Coord3D vel = {1, -5, 0.2}; move(&pos, &vel, 2.0); // struct pos gets changed cout << pos.x << " " << pos.y << " " << pos.z << endl; // prints: 2 -10 100.4 } 

Notice that we are not passing anything by reference: We pass the address, &pos, and the function manipulates the original struct pos, because it knows its address in the memory.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!