diff options
| author | victorfisac <victorfisac@gmail.com> | 2015-12-21 21:12:35 +0100 |
|---|---|---|
| committer | victorfisac <victorfisac@gmail.com> | 2015-12-21 21:12:35 +0100 |
| commit | e683fe88b9ab0fde76521a0367cdff4c229ac60c (patch) | |
| tree | f51d6412b2064c555dcb1426d1775fb3b2d49558 /src | |
| parent | 1bcb5ddd505e5c4bdac6f254e931e9c3145be88d (diff) | |
| download | raylib-e683fe88b9ab0fde76521a0367cdff4c229ac60c.tar.gz raylib-e683fe88b9ab0fde76521a0367cdff4c229ac60c.zip | |
Added physics engine-module and example
- Added new physics engine-module with four new data types: Physics,
Transform, Rigidbody and Collider. This library contains functions to
apply physics calculations to a position vector calculating collisions
automatically.
- Fixed some writing mistakes of lighting module.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libraylib.a | bin | 0 -> 432294 bytes | |||
| -rw-r--r-- | src/lighting.c | 2 | ||||
| -rw-r--r-- | src/lighting.h | 2 | ||||
| -rw-r--r-- | src/physics.c | 272 | ||||
| -rw-r--r-- | src/physics.h | 99 | ||||
| -rw-r--r-- | src/raylib.h | 57 |
6 files changed, 430 insertions, 2 deletions
diff --git a/src/libraylib.a b/src/libraylib.a Binary files differnew file mode 100644 index 00000000..5ba9eda7 --- /dev/null +++ b/src/libraylib.a diff --git a/src/lighting.c b/src/lighting.c index 98e1696a..5cf2a2ec 100644 --- a/src/lighting.c +++ b/src/lighting.c @@ -42,7 +42,7 @@ //... //---------------------------------------------------------------------------------- -// Module Functions Definition +// Module Functions Declarations //---------------------------------------------------------------------------------- // Lights functions diff --git a/src/lighting.h b/src/lighting.h index 67a65d45..a35113c3 100644 --- a/src/lighting.h +++ b/src/lighting.h @@ -61,7 +61,7 @@ typedef struct Material { } Material; //---------------------------------------------------------------------------------- -// Module Functions Declaration +// Module Functions Definitions // NOTE: light and material structs uses float pointers instead of vectors to be compatible with SetShaderValue() //---------------------------------------------------------------------------------- // Lights functions diff --git a/src/physics.c b/src/physics.c new file mode 100644 index 00000000..90a8b2e9 --- /dev/null +++ b/src/physics.c @@ -0,0 +1,272 @@ +/********************************************************************************************** +* +* raylib physics engine module - Basic functions to apply physics to 2D objects +* +* Copyright (c) 2015 Victor Fisac and Ramon Santamaria +* +* This software is provided "as-is", without any express or implied warranty. In no event +* will the authors be held liable for any damages arising from the use of this software. +* +* Permission is granted to anyone to use this software for any purpose, including commercial +* applications, and to alter it and redistribute it freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you must not claim that you +* wrote the original software. If you use this software in a product, an acknowledgment +* in the product documentation would be appreciated but is not required. +* +* 2. Altered source versions must be plainly marked as such, and must not be misrepresented +* as being the original software. +* +* 3. This notice may not be removed or altered from any source distribution. +* +**********************************************************************************************/ + +//#define PHYSICS_STANDALONE // NOTE: To use the physics module as standalone lib, just uncomment this line + +#if defined(PHYSICS_STANDALONE) + #include "physics.h" +#else + #include "raylib.h" +#endif + +#include <math.h> +#include <stdio.h> + +//---------------------------------------------------------------------------------- +// Defines and Macros +//---------------------------------------------------------------------------------- +#define MAX_ELEMENTS 1024 // Stored rigidbodies and colliders array length +#define DECIMAL_FIX 0.01f // Decimal margin for collision checks (avoid rigidbodies shake) + +//---------------------------------------------------------------------------------- +// Types and Structures Definition +//---------------------------------------------------------------------------------- +// ... + +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +static Physics physics; +static Collider colliders[MAX_ELEMENTS]; +static Rigidbody rigidbodies[MAX_ELEMENTS]; +static bool collisionChecker = false; + +//---------------------------------------------------------------------------------- +// Module Functions Definition +//---------------------------------------------------------------------------------- +void InitPhysics() +{ + for (int i = 0; i < MAX_ELEMENTS; i++) + { + rigidbodies[i].enabled = false; + rigidbodies[i].mass = 0.0f; + rigidbodies[i].velocity = (Vector2){0, 0}; + rigidbodies[i].acceleration = (Vector2){0, 0}; + rigidbodies[i].isGrounded = false; + rigidbodies[i].isContact = false; + rigidbodies[i].friction = 0.0f; + + colliders[i].enabled = false; + colliders[i].bounds = (Rectangle){0, 0, 0, 0}; + colliders[i].radius = 0; + } +} + +void SetPhysics(Physics settings) +{ + physics = settings; + + // To get good results, gravity needs to be 1:10 from original parameter + physics.gravity = (Vector2){physics.gravity.x / 10, physics.gravity.y / 10}; +} + +void AddCollider(int index, Collider collider) +{ + colliders[index] = collider; +} + +void AddRigidbody(int index, Rigidbody rigidbody) +{ + rigidbodies[index] = rigidbody; +} + +void ApplyPhysics(int index, Vector2 *position) +{ + if (rigidbodies[index].enabled) + { + // Apply gravity + rigidbodies[index].velocity.y += rigidbodies[index].acceleration.y; + rigidbodies[index].velocity.x += rigidbodies[index].acceleration.x; + + rigidbodies[index].velocity.y += physics.gravity.y; + rigidbodies[index].velocity.x += physics.gravity.x; + + // Apply friction to velocity + if (rigidbodies[index].isGrounded) + { + if (rigidbodies[index].velocity.x > DECIMAL_FIX) + { + rigidbodies[index].velocity.x -= rigidbodies[index].friction; + } + else if (rigidbodies[index].velocity.x < -DECIMAL_FIX) + { + rigidbodies[index].velocity.x += rigidbodies[index].friction; + } + else + { + rigidbodies[index].velocity.x = 0; + } + } + + if (rigidbodies[index].velocity.y > DECIMAL_FIX) + { + rigidbodies[index].velocity.y -= rigidbodies[index].friction; + } + else if (rigidbodies[index].velocity.y < -DECIMAL_FIX) + { + rigidbodies[index].velocity.y += rigidbodies[index].friction; + } + else + { + rigidbodies[index].velocity.y = 0; + } + + // Apply friction to acceleration + if (rigidbodies[index].isGrounded) + { + if (rigidbodies[index].acceleration.x > DECIMAL_FIX) + { + rigidbodies[index].acceleration.x -= rigidbodies[index].friction; + } + else if (rigidbodies[index].acceleration.x < -DECIMAL_FIX) + { + rigidbodies[index].acceleration.x += rigidbodies[index].friction; + } + else + { + rigidbodies[index].acceleration.x = 0; + } + } + + if (rigidbodies[index].acceleration.y > DECIMAL_FIX) + { + rigidbodies[index].acceleration.y -= rigidbodies[index].friction; + } + else if (rigidbodies[index].acceleration.y < -DECIMAL_FIX) + { + rigidbodies[index].acceleration.y += rigidbodies[index].friction; + } + else + { + rigidbodies[index].acceleration.y = 0; + } + + // Update position vector + position->x += rigidbodies[index].velocity.x; + position->y -= rigidbodies[index].velocity.y; + + // Update collider bounds + colliders[index].bounds.x = position->x; + colliders[index].bounds.y = position->y; + + // Check collision with other colliders + collisionChecker = false; + rigidbodies[index].isContact = false; + for (int j = 0; j < MAX_ELEMENTS; j++) + { + if (index != j) + { + if (colliders[index].enabled && colliders[j].enabled) + { + if (colliders[index].type == RectangleCollider) + { + if (colliders[j].type == RectangleCollider) + { + if (CheckCollisionRecs(colliders[index].bounds, colliders[j].bounds)) + { + collisionChecker = true; + + if ((colliders[index].bounds.y + colliders[index].bounds.height <= colliders[j].bounds.y) == false) + { + rigidbodies[index].isContact = true; + } + } + } + else + { + if (CheckCollisionCircleRec((Vector2){colliders[j].bounds.x, colliders[j].bounds.y}, colliders[j].radius, colliders[index].bounds)) + { + collisionChecker = true; + } + } + } + else + { + if (colliders[j].type == RectangleCollider) + { + if (CheckCollisionCircleRec((Vector2){colliders[index].bounds.x, colliders[index].bounds.y}, colliders[index].radius, colliders[j].bounds)) + { + collisionChecker = true; + } + } + else + { + if (CheckCollisionCircles((Vector2){colliders[j].bounds.x, colliders[j].bounds.y}, colliders[j].radius, (Vector2){colliders[index].bounds.x, colliders[index].bounds.y}, colliders[index].radius)) + { + collisionChecker = true; + } + } + } + } + } + } + + // Update grounded rigidbody state + rigidbodies[index].isGrounded = collisionChecker; + + // Set grounded state if needed (fix overlap and set y velocity) + if (collisionChecker && rigidbodies[index].velocity.y != 0) + { + position->y += rigidbodies[index].velocity.y; + rigidbodies[index].velocity.y = -rigidbodies[index].velocity.y * rigidbodies[index].bounciness; + } + + if (rigidbodies[index].isContact) + { + position->x -= rigidbodies[index].velocity.x; + rigidbodies[index].velocity.x = rigidbodies[index].velocity.x; + } + } +} + +void SetRigidbodyEnabled(int index, bool state) +{ + rigidbodies[index].enabled = state; +} + +void SetRigidbodyVelocity(int index, Vector2 velocity) +{ + rigidbodies[index].velocity.x = velocity.x; + rigidbodies[index].velocity.y = velocity.y; +} + +void AddRigidbodyForce(int index, Vector2 force) +{ + rigidbodies[index].acceleration.x = force.x * rigidbodies[index].mass; + rigidbodies[index].acceleration.y = force.y * rigidbodies[index].mass; +} + +void SetColliderEnabled(int index, bool state) +{ + colliders[index].enabled = state; +} + +Collider GetCollider(int index) +{ + return colliders[index]; +} + +Rigidbody GetRigidbody(int index) +{ + return rigidbodies[index]; +}
\ No newline at end of file diff --git a/src/physics.h b/src/physics.h new file mode 100644 index 00000000..211f31a2 --- /dev/null +++ b/src/physics.h @@ -0,0 +1,99 @@ +/********************************************************************************************** +* +* raylib physics engine module - Basic functions to apply physics to 2D objects +* +* Copyright (c) 2015 Victor Fisac and Ramon Santamaria +* +* This software is provided "as-is", without any express or implied warranty. In no event +* will the authors be held liable for any damages arising from the use of this software. +* +* Permission is granted to anyone to use this software for any purpose, including commercial +* applications, and to alter it and redistribute it freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you must not claim that you +* wrote the original software. If you use this software in a product, an acknowledgment +* in the product documentation would be appreciated but is not required. +* +* 2. Altered source versions must be plainly marked as such, and must not be misrepresented +* as being the original software. +* +* 3. This notice may not be removed or altered from any source distribution. +* +**********************************************************************************************/ + +#ifndef PHYSICS_H +#define PHYSICS_H + +//---------------------------------------------------------------------------------- +// Defines and Macros +//---------------------------------------------------------------------------------- +// ... + +//---------------------------------------------------------------------------------- +// Types and Structures Definition +//---------------------------------------------------------------------------------- +typedef enum { RectangleCollider, CircleCollider } ColliderType; + +// Physics struct +typedef struct Physics { + bool enabled; + bool debug; // Should be used by programmer for testing purposes + Vector2 gravity; +} Physics; + +// Transform struct +typedef struct Transform { + Vector2 position; + float rotation; + Vector2 scale; +} Transform; + +// Rigidbody struct +typedef struct Rigidbody { + bool enabled; + float mass; + Vector2 acceleration; + Vector2 velocity; + bool isGrounded; + bool isContact; // Avoid freeze player when touching floor + bool applyGravity; + float friction; // 0.0f to 1.0f + float bounciness; // 0.0f to 1.0f +} Rigidbody; + +// Collider struct +typedef struct Collider { + bool enabled; + ColliderType type; + Rectangle bounds; // Just used for RectangleCollider type + int radius; // Just used for CircleCollider type +} Collider; + +#ifdef __cplusplus +extern "C" { // Prevents name mangling of functions +#endif + +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +void InitPhysics(); // Initialize all internal physics values +void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings + +void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot +void AddCollider(int index, Collider collider); // Initialize a new Collider with parameters to internal index slot + +void ApplyPhysics(int index, Vector2 *position); // Apply physics to internal rigidbody, physics calculations are applied to position pointer parameter +void SetRigidbodyEnabled(int index, bool state); // Set enabled state to a defined rigidbody +void SetRigidbodyVelocity(int index, Vector2 velocity); // Set velocity of rigidbody (without considering of mass value) +void AddRigidbodyForce(int index, Vector2 force); // Set rigidbody force (considering mass value) + +void SetColliderEnabled(int index, bool state); // Set enabled state to a defined collider + +Rigidbody GetRigidbody(int index); // Returns the internal rigidbody data defined by index parameter +Collider GetCollider(int index); // Returns the internal collider data defined by index parameter + +#ifdef __cplusplus +} +#endif + +#endif // PHYSICS_H
\ No newline at end of file diff --git a/src/raylib.h b/src/raylib.h index 04ece42e..4eefa3ea 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -433,6 +433,44 @@ typedef enum { // Camera system modes typedef enum { CAMERA_CUSTOM = 0, CAMERA_FREE, CAMERA_ORBITAL, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON } CameraMode; +// Collider types +typedef enum { RectangleCollider, CircleCollider } ColliderType; + +// Physics struct +typedef struct Physics { + bool enabled; + bool debug; // Should be used by programmer for testing purposes + Vector2 gravity; +} Physics; + +// Transform struct +typedef struct Transform { + Vector2 position; + float rotation; + Vector2 scale; +} Transform; + +// Rigidbody struct +typedef struct Rigidbody { + bool enabled; + float mass; + Vector2 acceleration; + Vector2 velocity; + bool isGrounded; + bool isContact; // Avoid freeze player when touching floor + bool applyGravity; + float friction; // 0.0f to 1.0f + float bounciness; // 0.0f to 1.0f +} Rigidbody; + +// Collider struct +typedef struct Collider { + bool enabled; + ColliderType type; + Rectangle bounds; // Just used for RectangleCollider type + int radius; // Just used for CircleCollider type +} Collider; + #ifdef __cplusplus extern "C" { // Prevents name mangling of functions #endif @@ -742,6 +780,25 @@ void SetMaterialSpecularColor(Material *material, Vector3 color); // Set m void SetMaterialGlossiness(Material *material, float glossiness); // Set material glossiness value (recommended values: 0 - 100) void SetMaterialNormalDepth(Material *material, float depth); // Set normal map depth (B component from RGB type map scalar multiplier) +//---------------------------------------------------------------------------------- +// Physics System Functions (engine-module: physics) +//---------------------------------------------------------------------------------- +void InitPhysics(); // Initialize all internal physics values +void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings + +void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot +void AddCollider(int index, Collider collider); // Initialize a new Collider with parameters to internal index slot + +void ApplyPhysics(int index, Vector2 *position); // Apply physics to internal rigidbody, physics calculations are applied to position pointer parameter +void SetRigidbodyEnabled(int index, bool state); // Set enabled state to a defined rigidbody +void SetRigidbodyVelocity(int index, Vector2 velocity); // Set velocity of rigidbody (without considering of mass value) +void AddRigidbodyForce(int index, Vector2 force); // Set rigidbody force (considering mass value) + +void SetColliderEnabled(int index, bool state); // Set enabled state to a defined collider + +Rigidbody GetRigidbody(int index); // Returns the internal rigidbody data defined by index parameter +Collider GetCollider(int index); // Returns the internal collider data defined by index parameter + //------------------------------------------------------------------------------------ // Audio Loading and Playing Functions (Module: audio) //------------------------------------------------------------------------------------ |
