From f5a28d98a3f49ad78769d2cc027de4d13c2a1c77 Mon Sep 17 00:00:00 2001 From: yggdrasil75 Date: Sun, 9 Nov 2025 21:02:10 -0500 Subject: [PATCH] a --- simtools/grid2.cpp | 52 +++++++++++++++++++++++++++++----------------- simtools/sim2.hpp | 3 +++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/simtools/grid2.cpp b/simtools/grid2.cpp index f00c6f8..e2f3106 100644 --- a/simtools/grid2.cpp +++ b/simtools/grid2.cpp @@ -20,10 +20,13 @@ std::vector> gridToPixels(const Sim2& sim, int width, int heig int x = static_cast(pos.x); int y = static_cast(pos.y); + // Flip Y coordinate to make (0,0) bottom-left instead of top-left + int flippedY = height - 1 - y; + // Only draw if within bounds - if (x >= 0 && x < width && y >= 0 && y < height) { + if (x >= 0 && x < width && flippedY >= 0 && flippedY < height) { // Convert Vec4 (RGBA) to Vec3 (RGB) - pixels[y][x] = Vec3(color.x, color.y, color.z); + pixels[flippedY][x] = Vec3(color.x, color.y, color.z); } } @@ -31,35 +34,44 @@ std::vector> gridToPixels(const Sim2& sim, int width, int heig } int main() { - const int FRAME_COUNT = 60; - const float TOTAL_TIME = 1.0f; // 1 second + const int FRAME_COUNT = 120; // Increased frame count for longer animation + const float TOTAL_TIME = 4.0f; // Increased to 4 seconds for more movement const float TIME_STEP = TOTAL_TIME / FRAME_COUNT; // Create output directory std::filesystem::create_directories("output"); - // Create a simulation with 50x30 grid and gravity - Sim2 sim(50, 30, Vec2(0, -9.8f)); + // Create a simulation with 512x512 grid and SCALED gravity + // Scale gravity by ~17x (512/30 ≈ 17) to account for larger grid + Sim2 sim(512, 512, Vec2(0, -9.8f * 17.0f)); - // Create some objects - auto ball1 = sim.createBall(Vec2(10, 25), 3.0f, Vec4(1.0f, 0.5f, 0.0f, 1.0f), 1.0f); // Orange ball - auto ball2 = sim.createBall(Vec2(30, 20), 2.0f, Vec4(0.5f, 0.8f, 1.0f, 1.0f), 0.5f); // Blue ball + // Create some objects - adjust positions and sizes for larger grid + auto ball1 = sim.createBall(Vec2(100, 400), 40.0f, Vec4(1.0f, 0.5f, 0.0f, 1.0f), 1.0f); // Orange ball + auto ball2 = sim.createBall(Vec2(300, 450), 35.0f, Vec4(0.5f, 0.8f, 1.0f, 1.0f), 0.8f); // Blue ball - // Create ground - auto ground = sim.createGround(Vec2(0, 0), 50, Vec4(0.0f, 1.0f, 0.0f, 1.0f)); // Green ground + // Give initial velocities to make movement more visible + ball1->setVelocity(Vec2(50.0f, 0.0f)); // Rightward velocity + ball2->setVelocity(Vec2(-30.0f, 0.0f)); // Leftward velocity + + // Create ground - higher up so we can see falling + auto ground = sim.createGround(Vec2(0, 0), 512, Vec4(0.0f, 1.0f, 0.0f, 1.0f)); // Green ground + + // auto realground = sim.createGround(Vec2(0, 512), 512, Vec4(0.0f, 1.0f, 0.0f, 1.0f)); // Green ground // Create walls - auto leftWall = sim.createWall(Vec2(0, 1), 29, Vec4(0.3f, 0.3f, 0.7f, 1.0f)); // Blue walls - auto rightWall = sim.createWall(Vec2(49, 1), 29, Vec4(0.3f, 0.3f, 0.7f, 1.0f)); + auto leftWall = sim.createWall(Vec2(0, 51), 461, Vec4(0.3f, 0.3f, 0.7f, 1.0f)); // Blue walls (511-50=461) + auto rightWall = sim.createWall(Vec2(511, 51), 461, Vec4(0.3f, 0.3f, 0.7f, 1.0f)); + // Set world bounds - sim.setWorldBounds(Vec2(0, 0), Vec2(49, 29)); + sim.setWorldBounds(Vec2(0, 0), Vec2(511, 511)); - // Simulation parameters - sim.setElasticity(0.8f); // Bouncy - sim.setFriction(0.05f); // Low friction + // Simulation parameters - adjust for more visible movement + sim.setElasticity(0.7f); // Slightly less bouncy + sim.setFriction(0.02f); // Very low friction std::cout << "Rendering " << FRAME_COUNT << " frames over " << TOTAL_TIME << " seconds..." << std::endl; + std::cout << "Gravity: " << sim.getGlobalGravity().y << " px/s²" << std::endl; // Run simulation and save frames for (int frame = 0; frame < FRAME_COUNT; ++frame) { @@ -67,7 +79,7 @@ int main() { sim.update(TIME_STEP); // Convert simulation state to pixel data - auto pixels = gridToPixels(sim, 50, 30); + auto pixels = gridToPixels(sim, 512, 512); // Create filename with zero-padded frame number std::ostringstream filename; @@ -75,7 +87,9 @@ int main() { // Save as BMP if (BMPWriter::saveBMP(filename.str(), pixels)) { - std::cout << "Saved frame " << frame << " to " << filename.str() << std::endl; + if (frame % 10 == 0) { // Only print every 10 frames to reduce spam + std::cout << "Saved frame " << frame << " to " << filename.str() << std::endl; + } } else { std::cerr << "Failed to save frame " << frame << std::endl; } diff --git a/simtools/sim2.hpp b/simtools/sim2.hpp index be5241d..2dafc52 100644 --- a/simtools/sim2.hpp +++ b/simtools/sim2.hpp @@ -322,6 +322,9 @@ private: Vec2 worldPos = objPos + positions[i]; Vec2 gridPos = worldPos.round(); + // Flip Y coordinate to make (0,0) bottom-left + gridPos.y = height - 1 - gridPos.y; + // Only add if within grid bounds if (gridPos.x >= 0 && gridPos.x < width && gridPos.y >= 0 && gridPos.y < height) {