Some formatting tweaks

This commit is contained in:
raysan5 2016-01-20 19:09:48 +01:00
parent 51c0b61a43
commit c5663ca015
5 changed files with 42 additions and 36 deletions

View File

@ -32,28 +32,30 @@ int main()
int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [physics] example - basic rigidbody");
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}};
Physics worldPhysics = { true, false, (Vector2){ 0, -9.81f } };
// Set internal physics settings
SetPhysics(worldPhysics);
// Object initialization
Transform player = (Transform){(Vector2){(screenWidth - OBJECT_SIZE) / 2, (screenHeight - OBJECT_SIZE) / 2}, 0.0f, (Vector2){OBJECT_SIZE, OBJECT_SIZE}};
AddCollider(PLAYER_INDEX, (Collider){true, RectangleCollider, (Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, 0});
AddCollider(PLAYER_INDEX, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, 0});
AddRigidbody(PLAYER_INDEX, (Rigidbody){true, 1.0f, (Vector2){0, 0}, (Vector2){0, 0}, false, false, true, 0.5f, 1.0f});
// 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(PLAYER_INDEX + 1, (Collider){true, RectangleCollider, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0});
AddCollider(PLAYER_INDEX + 1, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0});
// Object properties initialization
float moveSpeed = 6.0f;
float jumpForce = 4.5f;
float jumpForce = 5.0f;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
@ -67,7 +69,7 @@ int main()
ApplyPhysics(PLAYER_INDEX, &player.position);
// Check jump button input
if(IsKeyDown(KEY_SPACE) && GetRigidbody(PLAYER_INDEX).isGrounded)
if (IsKeyDown(KEY_SPACE) && GetRigidbody(PLAYER_INDEX).isGrounded)
{
// Reset object Y velocity to avoid double jumping cases but keep the same X velocity that it already has
SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){GetRigidbody(PLAYER_INDEX).velocity.x, 0});
@ -77,12 +79,12 @@ int main()
}
// Check movement buttons input
if(IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
{
// Set rigidbody velocity in X based on moveSpeed value and apply the same Y velocity that it already has
SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
}
else if(IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
else if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
{
// Set rigidbody velocity in X based on moveSpeed negative value and apply the same Y velocity that it already has
SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){-moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
@ -110,12 +112,12 @@ int main()
DrawText("Use P to switch DEBUG MODE", (screenWidth - MeasureText("Use P to switch DEBUG MODE", 20)) / 2, screenHeight * 0.3f, 20, LIGHTGRAY);
// Check if debug mode is enabled
if(worldPhysics.debug)
if (worldPhysics.debug)
{
// Draw every internal physics stored collider if it is active
for(int i = 0; i < 2; i++)
for (int i = 0; i < 2; i++)
{
if(GetCollider(i).enabled)
if (GetCollider(i).enabled)
{
DrawRectangleLines(GetCollider(i).bounds.x, GetCollider(i).bounds.y, GetCollider(i).bounds.width, GetCollider(i).bounds.height, GREEN);
}

View File

@ -11,11 +11,11 @@
#include "raylib.h"
#define MAX_OBJECTS 5
#define OBJECTS_OFFSET 150
#define MAX_OBJECTS 5
#define OBJECTS_OFFSET 150
#define FORCE_INTENSITY 250.0f // Customize by user
#define FORCE_RADIUS 100 // Customize by user
#define FORCE_INTENSITY 250.0f // Customize by user
#define FORCE_RADIUS 100 // Customize by user
int main()
{
@ -25,7 +25,7 @@ int main()
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
@ -36,17 +36,20 @@ int main()
// Objects initialization
Transform objects[MAX_OBJECTS];
for(int i = 0; i < MAX_OBJECTS; i++)
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});
AddCollider(i, (Collider){true, COLLIDER_RECTANGLE, (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});
AddCollider(MAX_OBJECTS, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0});
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
@ -57,19 +60,19 @@ int main()
// 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++)
for (int i = 0; i < MAX_OBJECTS; i++)
{
ApplyPhysics(i, &objects[i].position);
}
// Check foce button input
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
AddForceAtPosition(GetMousePosition(), FORCE_INTENSITY, FORCE_RADIUS);
}
// Check debug mode toggle button input
if(IsKeyPressed(KEY_P))
if (IsKeyPressed(KEY_P))
{
// Update program physics value
worldPhysics.debug = !worldPhysics.debug;
@ -86,21 +89,21 @@ int main()
ClearBackground(RAYWHITE);
// Check if debug mode is enabled
if(worldPhysics.debug)
if (worldPhysics.debug)
{
// Draw every internal physics stored collider if it is active (floor included)
for(int i = 0; i < MAX_OBJECTS + 1; i++)
for (int i = 0; i < MAX_OBJECTS + 1; i++)
{
if(GetCollider(i).enabled)
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)
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))
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);
}
@ -114,7 +117,7 @@ int main()
else
{
// Draw objects
for(int i = 0; i < MAX_OBJECTS; i++)
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);
}

View File

@ -183,9 +183,9 @@ void ApplyPhysics(int index, Vector2 *position)
{
if (colliders[index].enabled && colliders[j].enabled)
{
if (colliders[index].type == RectangleCollider)
if (colliders[index].type == COLLIDER_RECTANGLE)
{
if (colliders[j].type == RectangleCollider)
if (colliders[j].type == COLLIDER_RECTANGLE)
{
if (CheckCollisionRecs(colliders[index].bounds, colliders[j].bounds))
{
@ -207,7 +207,7 @@ void ApplyPhysics(int index, Vector2 *position)
}
else
{
if (colliders[j].type == RectangleCollider)
if (colliders[j].type == COLLIDER_RECTANGLE)
{
if (CheckCollisionCircleRec((Vector2){colliders[index].bounds.x, colliders[index].bounds.y}, colliders[index].radius, colliders[j].bounds))
{

View File

@ -32,7 +32,8 @@
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef enum { RectangleCollider, CircleCollider } ColliderType;
// Collider types
typedef enum { COLLIDER_CIRCLE, COLLIDER_RECTANGLE, COLLIDER_CAPSULE } ColliderType;
// Physics struct
typedef struct Physics {

View File

@ -463,12 +463,12 @@ typedef struct {
typedef enum { CAMERA_CUSTOM = 0, CAMERA_FREE, CAMERA_ORBITAL, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON } CameraMode;
// Collider types
typedef enum { RectangleCollider, CircleCollider } ColliderType;
typedef enum { COLLIDER_CIRCLE, COLLIDER_RECTANGLE, COLLIDER_CAPSULE } ColliderType;
// Physics struct
typedef struct Physics {
bool enabled;
bool debug; // Should be used by programmer for testing purposes
bool debug; // Should be used by programmer for testing purposes
Vector2 gravity;
} Physics;
@ -496,8 +496,8 @@ typedef struct Rigidbody {
typedef struct Collider {
bool enabled;
ColliderType type;
Rectangle bounds; // Just used for RectangleCollider type
int radius; // Just used for CircleCollider type
Rectangle bounds; // Used for COLLIDER_RECTANGLE and COLLIDER_CAPSULE
int radius; // Used for COLLIDER_CIRCLE and COLLIDER_CAPSULE
} Collider;
#ifdef __cplusplus