Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 50 additions & 44 deletions docs/tutorial/ATutASimpleSim.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ y-coordinate again reaches 0.

Solving for t (using the quadratic formula), we get the time of impact:

![equation_analytic_t_impact](images/equation_analytic_t_impact.png)
```math
-v_y0 - sqrt(v_y0^2 - 2*g*y0)
t_impact = -----------------------------
g
```

---
<a id=a-cannonball-simulation-without-trick></a>
Expand All @@ -88,57 +92,59 @@ Solving for t (using the quadratic formula), we get the time of impact:
```c
/* Cannonball without Trick */

#include <stdio.h>
#include <math.h>
#include <stdio.h>

int main (int argc, char * argv[]) {

/* Declare variables used in the simulation */
double pos[2]; double pos_orig[2] ;
double vel[2]; double vel_orig[2] ;
double acc[2];
double init_angle ;
double init_speed ;
double time ;
int impact;
double impactTime;

/* Initialize data */
pos[0] = 0.0 ; pos[1] = 0.0 ;
vel[0] = 0.0 ; vel[1] = 0.0 ;
acc[0] = 0.0 ; acc[1] = -9.81 ;
time = 0.0 ;
init_angle = M_PI/6.0 ;
init_speed = 50.0 ;
impact = 0;

/* Do initial calculations */
pos_orig[0] = pos[0] ;
pos_orig[1] = pos[1] ;
vel_orig[0] = cos(init_angle)*init_speed ;
vel_orig[1] = sin(init_angle)*init_speed ;
int main(void)
{
/* Initial conditions */
const double acc[2] = {0.0, -9.81}; // acceleration in m/s^2
const double init_angle = M_PI / 6.0; // initial angle in radians
const double init_speed = 50.0; // initial speed in m/s
const double time_step = 0.01; // time step in seconds

const double init_pos[2] = {0.0, 0.0}; // initial position in meters
const double init_vel[2] // initial velocity in m/s
= {
cos(init_angle) * init_speed,
sin(init_angle) * init_speed,
};

/* Initialize simulation state */
double pos[2] = {init_pos[0], init_pos[1]}; // current position in meters
double vel[2] = {init_vel[0], init_vel[1]}; // current velocity in m/s
double sim_time = 0.0; // current simulation time in seconds
double impact_time = 0.0; // time of impact in seconds
int impact = 0; // flag indicating whether an impact has occurred

printf("time, pos[0], pos[1], vel[0], vel[1]\n");

/* Run simulation */
printf("time, pos[0], pos[1], vel[0], vel[1]\n" );
while ( !impact ) {
vel[0] = vel_orig[0] + acc[0] * time ;
vel[1] = vel_orig[1] + acc[1] * time ;
pos[0] = pos_orig[0] + (vel_orig[0] + 0.5 * acc[0] * time) * time ;
pos[1] = pos_orig[1] + (vel_orig[1] + 0.5 * acc[1] * time) * time ;
printf("%7.2f, %10.6f, %10.6f, %10.6f, %10.6f\n", time, pos[0], pos[1], vel[0], vel[1] );
if (pos[1] < 0.0) {
impact = 1;
impactTime = (- vel_orig[1] -
sqrt(vel_orig[1] * vel_orig[1] - 2.0 * pos_orig[1])
) / -9.81;
pos[0] = impactTime * vel_orig[0];
while (!impact) {
vel[0] = init_vel[0] + acc[0] * sim_time;
vel[1] = init_vel[1] + acc[1] * sim_time;

pos[0] = init_pos[0] + (init_vel[0] + 0.5 * acc[0] * sim_time) * sim_time;
pos[1] = init_pos[1] + (init_vel[1] + 0.5 * acc[1] * sim_time) * sim_time;

printf("%7.2f, %10.6f, %10.6f, %10.6f, %10.6f\n", sim_time, pos[0], pos[1], vel[0], vel[1]);

if (pos[1] < 0.0) { // check for impact
impact_time
= (-init_vel[1] - sqrt(init_vel[1] * init_vel[1] - 2.0 * acc[1] * init_pos[1]))
/ acc[1];

pos[0] = init_pos[0] + (init_vel[0] + 0.5 * acc[0] * impact_time) * impact_time;
pos[1] = 0.0;

impact = 1;
}
time += 0.01 ;

sim_time += time_step;
}

/* Shutdown simulation */
printf("Impact time=%lf position=%lf\n", impactTime, pos[0]);
printf("Impact time=%f position=%f\n", impact_time, pos[0]);

return 0;
}
Expand All @@ -147,7 +153,7 @@ int main (int argc, char * argv[]) {
If we compile and run the program in listing 1:

```bash
% cc cannon.c -o cannon -lm
% cc cannon.c -o cannon
% ./cannon
```

Expand Down
Loading
Loading