From da9f31ee67f2cb36c0af3ee54b33f3448e917d26 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 20 Dec 2024 00:35:59 +0100 Subject: [PATCH] new 'toss just dropped --- BlockProjectile.hpp | 115 ++++++++++++++++++++++++++++++-------------- main.cpp | 4 +- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/BlockProjectile.hpp b/BlockProjectile.hpp index 0a76900..297daf2 100644 --- a/BlockProjectile.hpp +++ b/BlockProjectile.hpp @@ -1,46 +1,91 @@ -#error "Not implemented." +#ifndef PHYSICS_HEADER +#define PHYSICS_HEADER -#if 0 - const float G = 9.8f; - const float mass = 10.0f; - const float x0 = 400.0f; - const float y0 = 500.0f; +class BlockProjectile { + public: + const int block_size = 30; - float v0 = 50.0f; - float angle = 45.0f; - float v = v0; - float x = x0; - float y = y0; - float t0 = GetTime(); + const float G = 98.0f * 2; - vector trajectory; + Vector2 display_offset; + Vector2 position; + Vector2 velocity; + double last_update; - float dt = GetTime() - t0; - v = (G/mass) * dt; - x = x0 + (v0*sin(radians(angle + 90.0f))) * dt; - y = y0 + (v0*cos(radians(angle + 90.0f)) + G*dt) * dt * v; + /* Used for tracing the block visually, + * not for calculations + */ + vector 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) { - v0 = (rand() % 100) + 20.0f; - angle = rand() % 180; - v = v0; - y = y0; - x = x0; - t0 = GetTime(); - 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)); + } - for (const auto &v : trajectory) { - DrawPixel(v.x, v.y, BLUE); - } + void display(void) { + 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 diff --git a/main.cpp b/main.cpp index 6bf71dd..934b185 100644 --- a/main.cpp +++ b/main.cpp @@ -24,8 +24,8 @@ using namespace std; */ //#include "Block1D.hpp" //#include "BouncyBlock1D.hpp" -#include "Block2D.hpp" -//#inline "BlockProjectile.hpp" +//#include "Block2D.hpp" +#include "BlockProjectile.hpp" int main() { InitWindow(screenWidth, screenHeight, "Physics Simulation");