physics/BlockProjectile.hpp
2024-12-20 00:35:59 +01:00

92 lines
2.2 KiB
C++

#ifndef PHYSICS_HEADER
#define PHYSICS_HEADER
class BlockProjectile {
public:
const int block_size = 30;
const float G = 98.0f * 2;
Vector2 display_offset;
Vector2 position;
Vector2 velocity;
double last_update;
/* Used for tracing the block visually,
* not for calculations
*/
vector<Vector2> trajectory;
void reset(void) {
position = { 0, 0 };
velocity.y = (rand() % 400) + 3;
velocity.x = (rand() % (150*2)) - 150;
trajectory.clear();
}
BlockProjectile() {
display_offset.x = (screenWidth / 2) - (block_size - 2);
display_offset.y = screenHeight - 100 - (block_size - 2);
reset();
}
double speed(Vector2 velocity) {
return sqrt(pow(velocity.x, 2) + pow(velocity.y, 2));
}
void display(void) {
for (auto &p : trajectory) {
DrawPixelV(
(Vector2) {
.x = p.x + display_offset.x,
.y = p.y + display_offset.y,
},
BLUE
);
}
DrawCircleV(display_offset, 7, GREEN);
DrawRectangle(
position.x + display_offset.x - (block_size/2),
position.y + display_offset.y - (block_size/2),
block_size,
block_size,
RED
);
// HUD
DrawText("Stone Trajectory Simulation", 10, 10, 20, DARKGRAY);
DrawText(TextFormat("Time: %.2f s", last_update), 10, 40, 20, DARKGRAY);
DrawText(TextFormat("Speed: %.2f", speed(velocity)), 10, 70, 20, DARKGRAY);
}
void control(Vector2 click) {
/* NOTE: this module is automatic*/;
}
void update(void) {
if (position.y + display_offset.y > screenHeight) {
reset();
return;
}
trajectory.push_back(position);
double update_time = GetTime();
Vector2 delta;
delta.x = velocity.x * (update_time - last_update);
delta.y = velocity.y * (update_time - last_update);
position.x += delta.x;
position.y -= delta.y;
velocity.y -= G * (update_time - last_update);
last_update = update_time;
}
} content;
#endif