new 'toss just dropped
This commit is contained in:
@ -1,46 +1,91 @@
|
|||||||
#error "Not implemented."
|
#ifndef PHYSICS_HEADER
|
||||||
|
#define PHYSICS_HEADER
|
||||||
|
|
||||||
#if 0
|
class BlockProjectile {
|
||||||
const float G = 9.8f;
|
public:
|
||||||
const float mass = 10.0f;
|
const int block_size = 30;
|
||||||
const float x0 = 400.0f;
|
|
||||||
const float y0 = 500.0f;
|
|
||||||
|
|
||||||
float v0 = 50.0f;
|
const float G = 98.0f * 2;
|
||||||
float angle = 45.0f;
|
|
||||||
float v = v0;
|
|
||||||
float x = x0;
|
|
||||||
float y = y0;
|
|
||||||
float t0 = GetTime();
|
|
||||||
|
|
||||||
vector<Vector2> trajectory;
|
Vector2 display_offset;
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 velocity;
|
||||||
|
double last_update;
|
||||||
|
|
||||||
float dt = GetTime() - t0;
|
/* Used for tracing the block visually,
|
||||||
v = (G/mass) * dt;
|
* not for calculations
|
||||||
x = x0 + (v0*sin(radians(angle + 90.0f))) * dt;
|
*/
|
||||||
y = y0 + (v0*cos(radians(angle + 90.0f)) + G*dt) * dt * v;
|
vector<Vector2> trajectory;
|
||||||
|
|
||||||
trajectory.push_back((Vector2) { .x = x, .y = y });
|
void reset(void) {
|
||||||
|
position = { 0, 0 };
|
||||||
|
velocity.y = (rand() % 400) + 3;
|
||||||
|
velocity.x = (rand() % (150*2)) - 150;
|
||||||
|
trajectory.clear();
|
||||||
|
}
|
||||||
|
|
||||||
if (y > screenHeight) {
|
BlockProjectile() {
|
||||||
v0 = (rand() % 100) + 20.0f;
|
display_offset.x = (screenWidth / 2) - (block_size - 2);
|
||||||
angle = rand() % 180;
|
display_offset.y = screenHeight - 100 - (block_size - 2);
|
||||||
v = v0;
|
reset();
|
||||||
y = y0;
|
}
|
||||||
x = x0;
|
|
||||||
t0 = GetTime();
|
|
||||||
trajectory.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
------
|
double speed(Vector2 velocity) {
|
||||||
|
return sqrt(pow(velocity.x, 2) + pow(velocity.y, 2));
|
||||||
|
}
|
||||||
|
|
||||||
for (const auto &v : trajectory) {
|
void display(void) {
|
||||||
DrawPixel(v.x, v.y, BLUE);
|
for (auto &p : trajectory) {
|
||||||
}
|
DrawPixelV(
|
||||||
|
(Vector2) {
|
||||||
|
.x = p.x + display_offset.x,
|
||||||
|
.y = p.y + display_offset.y,
|
||||||
|
},
|
||||||
|
BLUE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
DrawCircleV((Vector2){x, y}, 10, RED);
|
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;
|
||||||
|
|
||||||
DrawText("Stone Trajectory Simulation", 10, 10, 20, DARKGRAY);
|
|
||||||
DrawText(TextFormat("Time: %.2f s", dt), 10, 40, 20, DARKGRAY);
|
|
||||||
DrawText(TextFormat("Velocity: %.2f Angle: %.2f", v, angle), 10, 70, 20, DARKGRAY);
|
|
||||||
#endif
|
#endif
|
||||||
|
4
main.cpp
4
main.cpp
@ -24,8 +24,8 @@ using namespace std;
|
|||||||
*/
|
*/
|
||||||
//#include "Block1D.hpp"
|
//#include "Block1D.hpp"
|
||||||
//#include "BouncyBlock1D.hpp"
|
//#include "BouncyBlock1D.hpp"
|
||||||
#include "Block2D.hpp"
|
//#include "Block2D.hpp"
|
||||||
//#inline "BlockProjectile.hpp"
|
#include "BlockProjectile.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
InitWindow(screenWidth, screenHeight, "Physics Simulation");
|
InitWindow(screenWidth, screenHeight, "Physics Simulation");
|
||||||
|
Reference in New Issue
Block a user