This commit is contained in:
yggdrasil75
2025-11-09 21:02:10 -05:00
parent f2b6286580
commit f5a28d98a3
2 changed files with 36 additions and 19 deletions

View File

@@ -20,10 +20,13 @@ std::vector<std::vector<Vec3>> gridToPixels(const Sim2& sim, int width, int heig
int x = static_cast<int>(pos.x); int x = static_cast<int>(pos.x);
int y = static_cast<int>(pos.y); int y = static_cast<int>(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 // 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) // 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<std::vector<Vec3>> gridToPixels(const Sim2& sim, int width, int heig
} }
int main() { int main() {
const int FRAME_COUNT = 60; const int FRAME_COUNT = 120; // Increased frame count for longer animation
const float TOTAL_TIME = 1.0f; // 1 second const float TOTAL_TIME = 4.0f; // Increased to 4 seconds for more movement
const float TIME_STEP = TOTAL_TIME / FRAME_COUNT; const float TIME_STEP = TOTAL_TIME / FRAME_COUNT;
// Create output directory // Create output directory
std::filesystem::create_directories("output"); std::filesystem::create_directories("output");
// Create a simulation with 50x30 grid and gravity // Create a simulation with 512x512 grid and SCALED gravity
Sim2 sim(50, 30, Vec2(0, -9.8f)); // 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 // Create some objects - adjust positions and sizes for larger grid
auto ball1 = sim.createBall(Vec2(10, 25), 3.0f, Vec4(1.0f, 0.5f, 0.0f, 1.0f), 1.0f); // Orange ball 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(30, 20), 2.0f, Vec4(0.5f, 0.8f, 1.0f, 1.0f), 0.5f); // Blue 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 // Give initial velocities to make movement more visible
auto ground = sim.createGround(Vec2(0, 0), 50, Vec4(0.0f, 1.0f, 0.0f, 1.0f)); // Green ground 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 // Create walls
auto leftWall = sim.createWall(Vec2(0, 1), 29, Vec4(0.3f, 0.3f, 0.7f, 1.0f)); // Blue walls 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(49, 1), 29, Vec4(0.3f, 0.3f, 0.7f, 1.0f)); auto rightWall = sim.createWall(Vec2(511, 51), 461, Vec4(0.3f, 0.3f, 0.7f, 1.0f));
// Set world bounds // Set world bounds
sim.setWorldBounds(Vec2(0, 0), Vec2(49, 29)); sim.setWorldBounds(Vec2(0, 0), Vec2(511, 511));
// Simulation parameters // Simulation parameters - adjust for more visible movement
sim.setElasticity(0.8f); // Bouncy sim.setElasticity(0.7f); // Slightly less bouncy
sim.setFriction(0.05f); // Low friction sim.setFriction(0.02f); // Very low friction
std::cout << "Rendering " << FRAME_COUNT << " frames over " << TOTAL_TIME << " seconds..." << std::endl; 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 // Run simulation and save frames
for (int frame = 0; frame < FRAME_COUNT; ++frame) { for (int frame = 0; frame < FRAME_COUNT; ++frame) {
@@ -67,7 +79,7 @@ int main() {
sim.update(TIME_STEP); sim.update(TIME_STEP);
// Convert simulation state to pixel data // 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 // Create filename with zero-padded frame number
std::ostringstream filename; std::ostringstream filename;
@@ -75,7 +87,9 @@ int main() {
// Save as BMP // Save as BMP
if (BMPWriter::saveBMP(filename.str(), pixels)) { 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 { } else {
std::cerr << "Failed to save frame " << frame << std::endl; std::cerr << "Failed to save frame " << frame << std::endl;
} }

View File

@@ -322,6 +322,9 @@ private:
Vec2 worldPos = objPos + positions[i]; Vec2 worldPos = objPos + positions[i];
Vec2 gridPos = worldPos.round(); 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 // Only add if within grid bounds
if (gridPos.x >= 0 && gridPos.x < width && if (gridPos.x >= 0 && gridPos.x < width &&
gridPos.y >= 0 && gridPos.y < height) { gridPos.y >= 0 && gridPos.y < height) {