72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#ifndef PHYSICS_HEADER
|
|
#define PHYSICS_HEADER
|
|
|
|
class Block2d {
|
|
public:
|
|
Vector2 position = {screenWidth/2, screenHeight/2};
|
|
Vector2 direction = {0, 0};
|
|
const float max_speed = 1200;
|
|
Vector2 speed = {0, 0};
|
|
double last_update;
|
|
|
|
Block2d(void) {
|
|
last_update = GetTime();
|
|
}
|
|
|
|
void accelerate(float * v, float extra) {
|
|
if (abs(*v + extra) < max_speed) {
|
|
*v = abs(*v + extra);
|
|
} else {
|
|
*v = max_speed;
|
|
}
|
|
}
|
|
|
|
void control(Vector2 click) {
|
|
direction = diff(position, click);
|
|
Vector2 speed_addition = {
|
|
direction.x,
|
|
direction.y,
|
|
};
|
|
accelerate(&speed.x, speed_addition.x);
|
|
accelerate(&speed.y, speed_addition.y);
|
|
direction = (Vector2) {
|
|
.x = signum(direction.x),
|
|
.y = signum(direction.y),
|
|
};
|
|
}
|
|
|
|
void decelerate(float * v) {
|
|
if (*v - 1.1f > 0) {
|
|
*v -= 1.1f;
|
|
} else {
|
|
*v = 0;
|
|
}
|
|
}
|
|
|
|
void update(void) {
|
|
double update_time = GetTime();
|
|
position.x -= speed.x * (update_time - last_update) * direction.x;
|
|
position.y -= speed.y * (update_time - last_update) * direction.y;
|
|
|
|
decelerate(&speed.x);
|
|
decelerate(&speed.y);
|
|
|
|
last_update = update_time;
|
|
}
|
|
|
|
void display(void) {
|
|
// Block
|
|
DrawRectangle(position.x, position.y, 30, 30, RED);
|
|
|
|
// Hud
|
|
DrawText(TextFormat("X: %5.2f", position.x), 10, 10, 20, DARKGRAY);
|
|
DrawText(TextFormat("Y: %5.2f", position.y), 10, 40, 20, DARKGRAY);
|
|
DrawText(TextFormat("Speed X: %5.2f", speed.x), 10, 70, 20, DARKGRAY);
|
|
DrawText(TextFormat("Speed Y: %5.2f", speed.y), 10, 100, 20, DARKGRAY);
|
|
}
|
|
|
|
|
|
} content;
|
|
|
|
#endif
|