diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 92 | ||||
| -rw-r--r-- | src/physics.cpp | 27 | ||||
| -rw-r--r-- | src/physics.hpp | 14 |
3 files changed, 89 insertions, 44 deletions
diff --git a/src/main.cpp b/src/main.cpp index 7af702a..50ccd47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,51 +1,59 @@ #include "raylib.h" +#include "raymath.h" #include "resources.hpp" #include "renderer.hpp" +#include "physics.hpp" +#include <iostream> + //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ int main(void) { - // Initialization - //-------------------------------------------------------------------------------------- - const int screenWidth = 800; - const int screenHeight = 800; - - InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); - - SetTargetFPS(60); // Set our game to run at 60 frames-per-second - //-------------------------------------------------------------------------------------- - - auto ship = Renderer::Sprite("kenneySpaceShooter", (Rectangle){1365,1696,198,188}, (Vector2){19.80/2,18.80/2}); - auto rock = Renderer::Sprite("kenneySpaceShooter", (Rectangle){0,800,440,442}, (Vector2){44.00/2, 44.20/2}); - - // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key - { - // Update - //---------------------------------------------------------------------------------- - // TODO: Update your variables here - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(BLACK); - - DrawText("Space!", 190, 200, 20, LIGHTGRAY); - //DrawTexture(Resources::useTexture("ship"), 0, 0, WHITE); - ship.draw(100,100, 0.1); - rock.draw(750,750, 0.1); - - EndDrawing(); - //---------------------------------------------------------------------------------- - } - - // De-Initialization - //-------------------------------------------------------------------------------------- - CloseWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- - - return 0; + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 800; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + auto ship_sprite = Renderer::Sprite("kenneySpaceShooter", (Rectangle){1365,1696,198,188}, (Vector2){19.80/2,18.80/2}); + auto rock_sprite = Renderer::Sprite("kenneySpaceShooter", (Rectangle){0,800,440,442}, (Vector2){44.00/2, 44.20/2}); + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + for (int i = 0; i < 50000; i++) { + Physics::step(0.001); + } + std::cout << std::to_string(Vector2Distance(Physics::ship.position, Physics::rock.position)) << std::endl; + BeginDrawing(); + + ClearBackground(BLACK); + + DrawText("Space!", 190, 200, 20, LIGHTGRAY); + //DrawTexture(Resources::useTexture("ship"), 0, 0, WHITE); + ship_sprite.draw(Physics::ship.position.x,Physics::ship.position.y, 0.1); + rock_sprite.draw(Physics::rock.position.x,Physics::rock.position.y, 0.1); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; } diff --git a/src/physics.cpp b/src/physics.cpp index 23b21e4..d8045a5 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -1,7 +1,30 @@ -#include "physics.hpp" #include <math.h> +#include "physics.hpp" +#include "raylib.h" +#include "raymath.h" namespace Physics { - //const float gravitational_constant = 6.67e-11; + // using 1 as the const + //const float gravitational_constant = 6.67;//e-11; + //grav = grav_const × (r_mass / distance^2) + //force = s_mass * grav + //force = s_mass * (grav_const * (r_mass / distance^2)) + + Body::Body(Vector2 position, Vector2 velocity, float mass):position(position), velocity(velocity), mass(mass) { + } + + Body ship = Body((Vector2){200,400}, (Vector2){0,0.5}, 1); + Body rock = Body((Vector2){400,400}, (Vector2){0,0}, 50); + + //0.01667 + void step(float deltaTime) { + // calculate velocity change + //pow((rock.position.x - ship.position.x),2) + pow((rock.position.y - ship.position.y),2); + float distance = Vector2DistanceSqr(ship.position, rock.position); + Vector2 force = Vector2Normalize(Vector2Subtract(rock.position, ship.position)); + ship.velocity = Vector2Add(ship.velocity, Vector2Scale(force, ship.mass * (rock.mass / (distance/*this is pre squared*/)) * deltaTime)); + // calculate position change + ship.position = Vector2Add(ship.position, Vector2Scale(ship.velocity, deltaTime)); + } } diff --git a/src/physics.hpp b/src/physics.hpp index fa90c1d..a5b3b83 100644 --- a/src/physics.hpp +++ b/src/physics.hpp @@ -1,5 +1,19 @@ #pragma once +#include "raylib.h" namespace Physics { + class Body { + public: + Vector2 position; + Vector2 velocity; + float mass; + Body(Vector2 position, Vector2 velocity, float mass); + }; + + extern Body ship;// = Body((Vector2){50,50}, (Vector2){0,1}, 1); + extern Body rock;// = Body((Vector2){400,400}, (Vector2){0,0}, 50); + + //0.01667 + void step(float deltaTime); } |
