Repository navigation
Expand file tree
/
Copy pathbouncing_ball.cpp.cpp
More file actions
73 lines (49 loc) · 1.67 KB
/
Copy pathbouncing_ball.cpp.cpp
File metadata and controls
73 lines (49 loc) · 1.67 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <windows.h>
#include <iomanip>
struct SimulationResult {
double totaltime{0.0};
double VelocityY{0.0};
};
SimulationResult FreeFall (double StartHeight, double Gravity) {
double PositionY = StartHeight;
double VelocityY = 0;
double deltatime = 0.1;
double totaltime = 0;
std::cout<<std::fixed<<" Time | Height | Velocity\n";
bool IsSimulating = true;
double restitution = 0.7;
while (IsSimulating) {
VelocityY = VelocityY + (deltatime * Gravity);
PositionY = PositionY + (VelocityY * deltatime);
totaltime = totaltime + deltatime;
if (PositionY <= 0) {
PositionY = 0;
if (VelocityY < 0.9 && VelocityY > -0.9) {
VelocityY = 0.0;
IsSimulating = false;
}
else if (VelocityY < 0.0) {
VelocityY = - VelocityY * restitution;
}
}
std::cout<<std::fixed<<" "<<totaltime<<" | "<<PositionY<<" | "<<VelocityY<<"\n";
Sleep(100);
}
SimulationResult result;
result.totaltime = totaltime;
result.VelocityY = VelocityY;
return result;
}
int main() {
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
double StartHeight = 100;
double Gravity = -9.8;
SimulationResult data = FreeFall(StartHeight, Gravity);
std::cout<<"Total time to fall:"<<data.totaltime<<"\n";
std::cout<<"Total velocity:"<<data.VelocityY<<"\n";
std::cout<<"\nPress Enter to exit:";
std::cin.get();
return 0;
}