diff options
| author | Ray <raysan5@gmail.com> | 2016-01-04 12:40:54 +0100 |
|---|---|---|
| committer | Ray <raysan5@gmail.com> | 2016-01-04 12:40:54 +0100 |
| commit | 546c727520d8116132e57fd5e007dd7d873ba9a1 (patch) | |
| tree | 9626741431535dbfdb518d35f553c8184392a23c /examples | |
| parent | 3a739c38c591687d0e298a85f1e7bfe4666076bd (diff) | |
| parent | 1656d17b22b362e54710b7164638464e02bd7e5a (diff) | |
| download | raylib-546c727520d8116132e57fd5e007dd7d873ba9a1.tar.gz raylib-546c727520d8116132e57fd5e007dd7d873ba9a1.zip | |
Merge pull request #69 from victorfisac/develop
Improved lighting and physac engine modules and added new example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/lighting_blinn_phong.c | 4 | ||||
| -rw-r--r-- | examples/physics_basic_rigidbody.c | 2 | ||||
| -rw-r--r-- | examples/physics_rigidbody_force.c | 141 | ||||
| -rw-r--r-- | examples/physics_rigidbody_force.png | bin | 0 -> 18510 bytes |
4 files changed, 144 insertions, 3 deletions
diff --git a/examples/lighting_blinn_phong.c b/examples/lighting_blinn_phong.c index 48949b03..d4ff548a 100644 --- a/examples/lighting_blinn_phong.c +++ b/examples/lighting_blinn_phong.c @@ -31,7 +31,7 @@ int main() // Model initialization Vector3 position = { 0.0, 0.0, 0.0 }; Model model = LoadModel("resources/model/dwarf.obj"); - // Shader shader = LoadShader("resources/shaders/phong.vs", "resources/shaders/phong.fs"); + Shader shader = LoadShader("resources/shaders/phong.vs", "resources/shaders/phong.fs"); SetModelShader(&model, shader); // Shader locations initialization @@ -154,7 +154,7 @@ int main() Begin3dMode(camera); - DrawModel(model, position, 0.1f, (Color){255 * blinnMaterial.diffuseColor[0], 255 * blinnMaterial.diffuseColor[1], 255 * blinnMaterial.diffuseColor[2], 255}); + DrawModel(model, position, 4.0f, (Color){255 * blinnMaterial.diffuseColor[0], 255 * blinnMaterial.diffuseColor[1], 255 * blinnMaterial.diffuseColor[2], 255}); DrawSphere((Vector3){directionalLight.position[0], directionalLight.position[1], directionalLight.position[2]}, 1, YELLOW); diff --git a/examples/physics_basic_rigidbody.c b/examples/physics_basic_rigidbody.c index 2f3fffbc..17d6564f 100644 --- a/examples/physics_basic_rigidbody.c +++ b/examples/physics_basic_rigidbody.c @@ -1,6 +1,6 @@ /******************************************************************************************* * -* raylib [physics] example - Basic rigidbody +* raylib [physac] physics example - Basic rigidbody * * Welcome to raylib! * diff --git a/examples/physics_rigidbody_force.c b/examples/physics_rigidbody_force.c new file mode 100644 index 00000000..726e7c67 --- /dev/null +++ b/examples/physics_rigidbody_force.c @@ -0,0 +1,141 @@ +/******************************************************************************************* +* +* raylib [physac] physics example - Rigidbody forces +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#define MAX_OBJECTS 5 +#define OBJECTS_OFFSET 150 + +#define FORCE_INTENSITY 250.0f // Customize by user +#define FORCE_RADIUS 100 // Customize by user + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [physics] example - rigidbodies forces"); + SetTargetFPS(60); // Enable v-sync + InitPhysics(); // Initialize internal physics values (max rigidbodies/colliders available: 1024) + + // Physics initialization + Physics worldPhysics = {true, false, (Vector2){0, -9.81f}}; + + // Set internal physics settings + SetPhysics(worldPhysics); + + // Objects initialization + Transform objects[MAX_OBJECTS]; + for(int i = 0; i < MAX_OBJECTS; i++) + { + objects[i] = (Transform){(Vector2){75 + OBJECTS_OFFSET * i, (screenHeight - 50) / 2}, 0.0f, (Vector2){50, 50}}; + AddCollider(i, (Collider){true, RectangleCollider, (Rectangle){objects[i].position.x, objects[i].position.y, objects[i].scale.x, objects[i].scale.y}, 0}); + AddRigidbody(i, (Rigidbody){true, 1.0f, (Vector2){0, 0}, (Vector2){0, 0}, false, false, true, 0.5f, 0.5f}); + } + + // Floor initialization + // NOTE: floor doesn't need a rigidbody because it's a static physic object, just a collider to collide with other dynamic colliders (with rigidbody) + Transform floor = (Transform){(Vector2){0, screenHeight * 0.8f}, 0.0f, (Vector2){screenWidth, screenHeight * 0.2f}}; + AddCollider(MAX_OBJECTS, (Collider){true, RectangleCollider, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0}); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + + // Update object physics + // NOTE: all physics detections and reactions are calculated in ApplyPhysics() function (You will live happier :D) + for(int i = 0; i < MAX_OBJECTS; i++) + { + ApplyPhysics(i, &objects[i].position); + } + + // Check foce button input + if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + { + AddForceAtPosition(GetMousePosition(), FORCE_INTENSITY, FORCE_RADIUS); + } + + // Check debug mode toggle button input + if(IsKeyPressed(KEY_P)) + { + // Update program physics value + worldPhysics.debug = !worldPhysics.debug; + + // Update internal physics value + SetPhysics(worldPhysics); + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Check if debug mode is enabled + if(worldPhysics.debug) + { + // Draw every internal physics stored collider if it is active (floor included) + for(int i = 0; i < MAX_OBJECTS + 1; i++) + { + if(GetCollider(i).enabled) + { + // Draw collider bounds + DrawRectangleLines(GetCollider(i).bounds.x, GetCollider(i).bounds.y, GetCollider(i).bounds.width, GetCollider(i).bounds.height, GREEN); + + // Check if current collider is not floor + if(i < MAX_OBJECTS) + { + // Draw lines between mouse position and objects if they are in force range + if(CheckCollisionPointCircle(GetMousePosition(), (Vector2){GetCollider(i).bounds.x + GetCollider(i).bounds.width / 2, GetCollider(i).bounds.y + GetCollider(i).bounds.height / 2}, FORCE_RADIUS)) + { + DrawLineV(GetMousePosition(), (Vector2){GetCollider(i).bounds.x + GetCollider(i).bounds.width / 2, GetCollider(i).bounds.y + GetCollider(i).bounds.height / 2}, RED); + } + } + } + } + + // Draw radius circle + DrawCircleLines(GetMousePosition().x, GetMousePosition().y, FORCE_RADIUS, RED); + } + else + { + // Draw objects + for(int i = 0; i < MAX_OBJECTS; i++) + { + DrawRectangleRec((Rectangle){objects[i].position.x, objects[i].position.y, objects[i].scale.x, objects[i].scale.y}, GRAY); + } + + // Draw floor + DrawRectangleRec((Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, BLACK); + } + + + // Draw help messages + DrawText("Use LEFT MOUSE BUTTON to create a force in mouse position", (screenWidth - MeasureText("Use LEFT MOUSE BUTTON to create a force in mouse position", 20)) / 2, screenHeight * 0.20f, 20, LIGHTGRAY); + DrawText("Use P to switch DEBUG MODE", (screenWidth - MeasureText("Use P to switch DEBUG MODE", 20)) / 2, screenHeight * 0.3f, 20, LIGHTGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/physics_rigidbody_force.png b/examples/physics_rigidbody_force.png Binary files differnew file mode 100644 index 00000000..48afa91b --- /dev/null +++ b/examples/physics_rigidbody_force.png |
