Added scale factor for accurate calculations.
This commit is contained in:
52
ray.c
52
ray.c
@@ -4,12 +4,14 @@
|
|||||||
|
|
||||||
// Changes:
|
// Changes:
|
||||||
// Reversing gravity makes it work?!!!
|
// Reversing gravity makes it work?!!!
|
||||||
|
// Added scale factor, mathed the shit out of that.
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
const float g = 9.81; // Gravity is inversed cause raylib uses top right corner as origin
|
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_x = 100; // Starting X (pixels)
|
||||||
float obj_y = 100; // Starting Y (pixels)
|
float obj_y = 100; // Starting Y (pixels)
|
||||||
float obj_vel_x = 50; // Velocity X (pixels/second)
|
float obj_vel_x = 50; // Velocity X (pixels/second)
|
||||||
@@ -21,26 +23,26 @@ float rect_size = 40; // Size of the rectangle
|
|||||||
int main(void) {
|
int main(void) {
|
||||||
const int screenWidth = 800;
|
const int screenWidth = 800;
|
||||||
const int screenHeight = 600;
|
const int screenHeight = 600;
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "Physics Engine - Falling Rectangle");
|
InitWindow(screenWidth, screenHeight, "WrldBox // ray.c");
|
||||||
SetTargetFPS(60); // 60 frames per second
|
SetTargetFPS(60); // 60 frames per second
|
||||||
|
|
||||||
float deltaTime = 0.0f;
|
float deltaTime = 0.0f;
|
||||||
bool isSimulating = true;
|
bool isSimulating = true;
|
||||||
|
|
||||||
printf(">= SIMULATION PARAMETERS =<\n");
|
printf(">= SIMULATION PARAMETERS =<\n");
|
||||||
printf("g= %f\nX= %.2f Y= %.2f GROUND_Y= %.2f\nVEL_X= %.2f VEL_Y= %.2f\n\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);
|
g, obj_x, obj_y, ground_y, obj_vel_x, obj_vel_y);
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
// Calculate delta time (time since last frame)
|
// Calculate delta time (time since last frame)
|
||||||
deltaTime = GetFrameTime();
|
deltaTime = GetFrameTime();
|
||||||
|
|
||||||
// Control simulation with spacebar
|
// Control simulation with spacebar
|
||||||
if (IsKeyPressed(KEY_SPACE)) {
|
if (IsKeyPressed(KEY_SPACE)) {
|
||||||
isSimulating = !isSimulating;
|
isSimulating = !isSimulating;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset with R key
|
// Reset with R key
|
||||||
if (IsKeyPressed(KEY_R)) {
|
if (IsKeyPressed(KEY_R)) {
|
||||||
obj_x = 100;
|
obj_x = 100;
|
||||||
@@ -51,74 +53,74 @@ int main(void) {
|
|||||||
isSimulating = true;
|
isSimulating = true;
|
||||||
printf("\n>= SIMULATION RESET =<\n");
|
printf("\n>= SIMULATION RESET =<\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update physics if simulating and object is above ground
|
// Update physics if simulating and object is above ground
|
||||||
if (isSimulating && obj_y + rect_size/2 < ground_y) {
|
if (isSimulating && obj_y + rect_size/2 < ground_y) {
|
||||||
// Your original physics logic with deltaTime
|
// Your original physics logic with deltaTime
|
||||||
obj_vel_y += deltaTime * g; // v = u + at
|
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_y += obj_vel_y * deltaTime; // s = ut + 1/2 at^2 is handled by this
|
||||||
obj_x += obj_vel_x * deltaTime;
|
obj_x += obj_vel_x * deltaTime;
|
||||||
|
|
||||||
time_step += deltaTime;
|
time_step += deltaTime;
|
||||||
|
|
||||||
// Real-time console output (optional)
|
// Real-time console output (optional)
|
||||||
if ((int)(time_step * 60) % 30 == 0) { // Print every ~0.5 seconds
|
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",
|
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);
|
time_step, obj_x, obj_y, obj_vel_x, obj_vel_y);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clamp to ground (prevent going through)
|
// Clamp to ground (prevent going through)
|
||||||
if (obj_y + rect_size/2 >= ground_y) {
|
if (obj_y + rect_size/2 >= ground_y) {
|
||||||
obj_y = ground_y - rect_size/2;
|
obj_y = ground_y - rect_size/2;
|
||||||
if (isSimulating) {
|
if (isSimulating) {
|
||||||
printf("\n\n>= Object Hit Ground =<\n");
|
printf("\n\n>= Object Hit Ground =<\n");
|
||||||
printf("X: %.2f, Y: %.2f, VEL_X: %.2f, VEL_Y: %.2f TIME: %.2f\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);
|
obj_x, obj_y, obj_vel_x, obj_vel_y, time_step);
|
||||||
isSimulating = false;
|
isSimulating = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep rectangle in screen bounds horizontally
|
// Keep rectangle in screen bounds horizontally
|
||||||
if (obj_x - rect_size/2 < 0) obj_x = rect_size/2;
|
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;
|
if (obj_x + rect_size/2 > screenWidth) obj_x = screenWidth - rect_size/2;
|
||||||
|
|
||||||
// Drawing
|
// Drawing
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
// Draw ground line
|
// Draw ground line
|
||||||
DrawLine(0, ground_y, screenWidth, ground_y, DARKGRAY);
|
DrawLine(0, ground_y, screenWidth, ground_y, DARKGRAY);
|
||||||
|
|
||||||
// Draw ground label
|
// Draw ground label
|
||||||
DrawText("GROUND", 10, ground_y + 5, 20, DARKGRAY);
|
DrawText("GROUND", 10, ground_y + 5, 20, DARKGRAY);
|
||||||
|
|
||||||
// Draw the falling rectangle
|
// Draw the falling rectangle
|
||||||
Rectangle rect = { obj_x - rect_size/2, obj_y - rect_size/2, rect_size, rect_size };
|
Rectangle rect = { obj_x - rect_size/2, obj_y - rect_size/2, rect_size, rect_size };
|
||||||
DrawRectangleRec(rect, RED);
|
DrawRectangleRec(rect, RED);
|
||||||
DrawRectangleLinesEx(rect, 2, MAROON);
|
DrawRectangleLinesEx(rect, 2, MAROON);
|
||||||
|
|
||||||
// Draw info text
|
// Draw info text
|
||||||
DrawText(TextFormat("Time: %.2f s", time_step), 10, 10, 20, DARKGRAY);
|
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("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("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);
|
DrawText(TextFormat("Status: %s", isSimulating ? "SIMULATING" : "PAUSED"), 10, 85, 20, isSimulating ? GREEN : RED);
|
||||||
|
|
||||||
// Draw controls help
|
// Draw controls help
|
||||||
DrawText("Controls:", screenWidth - 200, 10, 20, DARKGRAY);
|
DrawText("Controls:", screenWidth - 200, 10, 20, DARKGRAY);
|
||||||
DrawText("SPACE: Pause/Resume", screenWidth - 200, 35, 15, DARKGRAY);
|
DrawText("SPACE: Pause/Resume", screenWidth - 200, 35, 15, DARKGRAY);
|
||||||
DrawText("R: Reset", screenWidth - 200, 55, 15, DARKGRAY);
|
DrawText("R: Reset", screenWidth - 200, 55, 15, DARKGRAY);
|
||||||
DrawText("ESC: Exit", screenWidth - 200, 75, 15, DARKGRAY);
|
DrawText("ESC: Exit", screenWidth - 200, 75, 15, DARKGRAY);
|
||||||
|
|
||||||
// Draw velocity vector (optional visualization)
|
// Draw velocity vector (optional visualization)
|
||||||
Vector2 velocityEnd = { obj_x + obj_vel_x * 0.1f, obj_y + obj_vel_y * 0.1f };
|
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);
|
DrawLineEx((Vector2){obj_x, obj_y}, velocityEnd, 3, BLUE);
|
||||||
DrawText("Velocity Vector", obj_x + 5, obj_y - 10, 15, BLUE);
|
DrawText("Velocity Vector", obj_x + 5, obj_y - 10, 15, BLUE);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user