-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (115 loc) · 3.8 KB
/
main.cpp
File metadata and controls
140 lines (115 loc) · 3.8 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <SDL2/SDL.h>
#include <iostream>
#include <vector>
const int FPS = 60;
const int frameDelay = 1000 / FPS;
const float dt = 1.0 / FPS;
const float gravity = 9.81f * 100; // pixels per second squared
struct Vec2 {
float x;
float y;
Vec2 operator+(const Vec2& other) const { return {x + other.x, y + other.y}; }
Vec2 operator-(const Vec2& other) const { return {x - other.x, y - other.y}; }
Vec2 operator*(float scalar) const { return {x * scalar, y * scalar}; }
Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
float length() const { return std::sqrt(x*x + y*y); }
Vec2 normalize() const { float len = length(); return len ? Vec2{x/len, y/len} : Vec2{0,0}; }
};
class Object
{
public:
Vec2 position;
Vec2 velocity;
int radius;
float mass;
Object(Vec2 pos, Vec2 vel, int r, int m) {
this->position = pos;
this->velocity = vel;
this->radius = r;
this->mass = m;
}
void accelerate(Vec2 acc) {
this->velocity.x += acc.x * dt;
this->velocity.y += acc.y * dt;
}
void updatePos() {
this->position.x += velocity.x * dt;
this->position.y += velocity.y * dt;
}
void drawCircle(SDL_Renderer* renderer, int centerX, int centerY, int radius) {
for (int w = 0; w < radius * 2; w++) {
for (int h = 0; h < radius * 2; h++) {
int dx = radius - w;
int dy = radius - h;
if ((dx*dx + dy*dy) <= (radius * radius)) {
SDL_RenderDrawPoint(renderer, centerX + dx, centerY + dy);
}
}
}
}
};
std::vector<Object> objs = {
Object({100, 100}, {0, 0}, 30, 7.34767309 * pow(10, 22)),
Object({700, 100}, {0, 0}, 30, 7.34767309 * pow(10, 22))
};
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
Vec2 screenSize;
screenSize.x = 800;
screenSize.y = 600;
SDL_Window* window = SDL_CreateWindow(
"Physics Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
screenSize.x,
screenSize.y,
SDL_WINDOW_SHOWN
);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
Vec2 pos = {50, 50};
Vec2 vel = {0, 0};
int radius = 20;
SDL_Event event;
bool running = true;
while (running) {
Uint32 frameStart = SDL_GetTicks();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
for (auto& obj : objs) {
obj.accelerate({0, gravity});
obj.updatePos();
obj.drawCircle(renderer, obj.position.x, obj.position.y, obj.radius);
// Y-axis
if (obj.position.y - obj.radius < 0) {
obj.position.y = obj.radius;
obj.velocity.y *= -0.95f;
} else if (obj.position.y + obj.radius > screenSize.y) {
obj.position.y = screenSize.y - obj.radius;
obj.velocity.y *= -0.95f;
}
// X-axis
if (obj.position.x - obj.radius < 0) {
obj.position.x = obj.radius;
obj.velocity.x *= -0.95f;
} else if (obj.position.x + obj.radius > screenSize.x) {
obj.position.x = screenSize.x - obj.radius;
obj.velocity.x *= -0.95f;
}
}
SDL_RenderPresent(renderer);
Uint32 frameTime = SDL_GetTicks() - frameStart;
if (frameDelay > frameTime) {
SDL_Delay(frameDelay - frameTime);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}