-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.cpp
56 lines (53 loc) · 1.24 KB
/
physics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "physics.h"
#include <iostream>
void Object::log_time(float t)
{
time_log.push_back(time);
}
void Object::log_height(float h)
{
height_log.push_back(height);
}
float Object::get_data()
{
return height, velocity, weight;
}
float Object::get_time()
{
return time;
}
void Object::log_velocity(float v)
{
velocity_log.push_back(velocity);
}
void Object::set_data(float h, float w, float tv)
{
height = h;
weight = w;
terminal_velocity = tv;
}
void Object::calculate()
{
float original_height = height;
while (height > 0)
{
float air_resistance;
if (velocity < terminal_velocity)
air_resistance = (weight * gravity * velocity) / terminal_velocity;
else
air_resistance = weight * gravity;
float net_force = weight * gravity - air_resistance;
if (net_force < 0) net_force = 0; // prevent net force from becoming negative
velocity += net_force * 0.001;
height -= velocity * 0.001;
time += 0.001;
log_time(time);
log_height(height);
log_velocity(velocity);
if (height > original_height)
{
std::cout << "Error: Object is ascending" << std::endl;
break;
}
}
}