// ray.c A-0-i by team wholeworldcoding. // working physics simulation with 1 object, limited playground and fixed parameters // This is P7MJ, out. // Changes: // Reversing gravity makes it work?!!! // Added scale factor, mathed the shit out of that. #include #include #include "raylib.h" const float scale=43.7445319335; // We must find a scale factor, as gravity is calculated based on PIXELS rather than METERS. We say the character is 3 ft so we divide 40 pixels by the equvalent amount of meters for 3ft which is .9144. so 40/.9144 to get what one pixel is in terms of meters. Left as a variable because this will probably be recalculated eventually. const float g = 9.81*scale; // Gravity is inversed cause raylib uses top right corner as origin. float obj_x = 100; // Starting X (pixels) float obj_y = 100; // Starting Y (pixels) float obj_vel_x = 50; // Velocity X (pixels/second) float obj_vel_y = 0; // Velocity Y (pixels/second) float ground_y = 550; // Ground at 550 pixels (assuming 600px screen height) float time_step = 0.0f; float rect_size = 40; // Size of the rectangle int main(void) { const int screenWidth = 800; const int screenHeight = 600; InitWindow(screenWidth, screenHeight, "WrldBox // ray.c"); SetTargetFPS(60); // 60 frames per second float deltaTime = 0.0f; bool isSimulating = true; printf(">= SIMULATION PARAMETERS =<\n"); printf("g= %f\nX= %.2f Y= %.2f GROUND_Y= %.2f\nVEL_X= %.2f VEL_Y= %.2f\n\n", g, obj_x, obj_y, ground_y, obj_vel_x, obj_vel_y); while (!WindowShouldClose()) { // Calculate delta time (time since last frame) deltaTime = GetFrameTime(); // Control simulation with spacebar if (IsKeyPressed(KEY_SPACE)) { isSimulating = !isSimulating; } // Reset with R key if (IsKeyPressed(KEY_R)) { obj_x = 100; obj_y = 100; obj_vel_x = 50; obj_vel_y = 0; time_step = 0.0f; isSimulating = true; printf("\n>= SIMULATION RESET =<\n"); } // Update physics if simulating and object is above ground if (isSimulating && obj_y + rect_size/2 < ground_y) { // Your original physics logic with deltaTime obj_vel_y += deltaTime * g; // v = u + at obj_y += obj_vel_y * deltaTime; // s = ut + 1/2 at^2 is handled by this obj_x += obj_vel_x * deltaTime; time_step += deltaTime; // Real-time console output (optional) if ((int)(time_step * 60) % 30 == 0) { // Print every ~0.5 seconds printf("\r[%.2fs] X: %.2f Y: %.2f VEL_X: %.2f VEL_Y: %.2f", time_step, obj_x, obj_y, obj_vel_x, obj_vel_y); fflush(stdout); } } // Clamp to ground (prevent going through) if (obj_y + rect_size/2 >= ground_y) { obj_y = ground_y - rect_size/2; if (isSimulating) { printf("\n\n>= Object Hit Ground =<\n"); printf("X: %.2f, Y: %.2f, VEL_X: %.2f, VEL_Y: %.2f TIME: %.2f\n", obj_x, obj_y, obj_vel_x, obj_vel_y, time_step); isSimulating = false; } } // Keep rectangle in screen bounds horizontally if (obj_x - rect_size/2 < 0) obj_x = rect_size/2; if (obj_x + rect_size/2 > screenWidth) obj_x = screenWidth - rect_size/2; // Drawing BeginDrawing(); ClearBackground(RAYWHITE); // Draw ground line DrawLine(0, ground_y, screenWidth, ground_y, DARKGRAY); // Draw ground label DrawText("GROUND", 10, ground_y + 5, 20, DARKGRAY); // Draw the falling rectangle Rectangle rect = { obj_x - rect_size/2, obj_y - rect_size/2, rect_size, rect_size }; DrawRectangleRec(rect, RED); DrawRectangleLinesEx(rect, 2, MAROON); // Draw info text DrawText(TextFormat("Time: %.2f s", time_step), 10, 10, 20, DARKGRAY); DrawText(TextFormat("Position: (%.1f, %.1f)", obj_x, obj_y), 10, 35, 20, DARKGRAY); DrawText(TextFormat("Velocity: (%.1f, %.1f)", obj_vel_x, obj_vel_y), 10, 60, 20, DARKGRAY); DrawText(TextFormat("Status: %s", isSimulating ? "SIMULATING" : "PAUSED"), 10, 85, 20, isSimulating ? GREEN : RED); // Draw controls help DrawText("Controls:", screenWidth - 200, 10, 20, DARKGRAY); DrawText("SPACE: Pause/Resume", screenWidth - 200, 35, 15, DARKGRAY); DrawText("R: Reset", screenWidth - 200, 55, 15, DARKGRAY); DrawText("ESC: Exit", screenWidth - 200, 75, 15, DARKGRAY); // Draw velocity vector (optional visualization) Vector2 velocityEnd = { obj_x + obj_vel_x * 0.1f, obj_y + obj_vel_y * 0.1f }; DrawLineEx((Vector2){obj_x, obj_y}, velocityEnd, 3, BLUE); DrawText("Velocity Vector", obj_x + 5, obj_y - 10, 15, BLUE); EndDrawing(); } CloseWindow(); return 0; }