REVIEWED: physac module and examples #1525
This commit is contained in:
parent
b845f3886a
commit
677f420bf0
6 changed files with 535 additions and 666 deletions
|
@ -1,24 +1,19 @@
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* Physac - Physics demo
|
* raylib [physac] example - physics demo
|
||||||
*
|
*
|
||||||
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
|
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||||
* NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Use the following line to compile:
|
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
|
||||||
*
|
*
|
||||||
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static /
|
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
|
||||||
* -lraylib -lpthread -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm /
|
|
||||||
* -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
|
|
||||||
*
|
|
||||||
* Copyright (c) 2016-2018 Victor Fisac
|
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#define PHYSAC_NO_THREADS
|
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
@ -29,12 +24,11 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||||
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics demo");
|
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics demo");
|
||||||
|
|
||||||
// Physac logo drawing position
|
// Physac logo drawing position
|
||||||
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
||||||
int logoY = 15;
|
int logoY = 15;
|
||||||
bool needsReset = false;
|
|
||||||
|
|
||||||
// Initialize physics and default physics bodies
|
// Initialize physics and default physics bodies
|
||||||
InitPhysics();
|
InitPhysics();
|
||||||
|
@ -55,25 +49,17 @@ int main(void)
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Delay initialization of variables due to physics reset async
|
UpdatePhysics(); // Update physics system
|
||||||
RunPhysicsStep();
|
|
||||||
|
|
||||||
if (needsReset)
|
if (IsKeyPressed('R')) // Reset physics system
|
||||||
{
|
{
|
||||||
|
ResetPhysics();
|
||||||
|
|
||||||
floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
|
floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
|
||||||
floor->enabled = false;
|
floor->enabled = false;
|
||||||
|
|
||||||
circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
|
circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
|
||||||
circle->enabled = false;
|
circle->enabled = false;
|
||||||
|
|
||||||
needsReset = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset physics input
|
|
||||||
if (IsKeyPressed('R'))
|
|
||||||
{
|
|
||||||
ResetPhysics();
|
|
||||||
needsReset = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Physics body creation inputs
|
// Physics body creation inputs
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* Physac - Physics friction
|
* raylib [physac] example - physics friction
|
||||||
*
|
*
|
||||||
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
|
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||||
* NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Use the following line to compile:
|
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
|
||||||
*
|
*
|
||||||
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static /
|
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
|
||||||
* -lraylib -lpthread -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm /
|
|
||||||
* -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
|
|
||||||
*
|
|
||||||
* Copyright (c) 2016-2018 Victor Fisac
|
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#define PHYSAC_NO_THREADS
|
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
@ -29,7 +24,7 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||||
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics friction");
|
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics friction");
|
||||||
|
|
||||||
// Physac logo drawing position
|
// Physac logo drawing position
|
||||||
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
||||||
|
@ -73,9 +68,9 @@ int main(void)
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
RunPhysicsStep();
|
UpdatePhysics(); // Update physics system
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics system
|
||||||
{
|
{
|
||||||
// Reset dynamic physics bodies position, velocity and rotation
|
// Reset dynamic physics bodies position, velocity and rotation
|
||||||
bodyA->position = (Vector2){ 35, screenHeight*0.6f };
|
bodyA->position = (Vector2){ 35, screenHeight*0.6f };
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* Physac - Physics movement
|
* raylib [physac] example - physics movement
|
||||||
*
|
*
|
||||||
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
|
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||||
* NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Use the following line to compile:
|
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
|
||||||
*
|
*
|
||||||
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static /
|
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
|
||||||
* -lraylib -lpthread -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm /
|
|
||||||
* -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
|
|
||||||
*
|
|
||||||
* Copyright (c) 2016-2018 Victor Fisac
|
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#define PHYSAC_NO_THREADS
|
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
#define VELOCITY 0.5f
|
#define VELOCITY 0.5f
|
||||||
|
@ -31,7 +26,7 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||||
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics movement");
|
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics movement");
|
||||||
|
|
||||||
// Physac logo drawing position
|
// Physac logo drawing position
|
||||||
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
||||||
|
@ -66,9 +61,9 @@ int main(void)
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
RunPhysicsStep();
|
UpdatePhysics(); // Update physics system
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
{
|
{
|
||||||
// Reset movement physics body position, velocity and rotation
|
// Reset movement physics body position, velocity and rotation
|
||||||
body->position = (Vector2){ screenWidth/2, screenHeight/2 };
|
body->position = (Vector2){ screenWidth/2, screenHeight/2 };
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* Physac - Physics restitution
|
* raylib [physac] example - physics restitution
|
||||||
*
|
*
|
||||||
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
|
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||||
* NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Use the following line to compile:
|
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
|
||||||
*
|
*
|
||||||
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static /
|
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
|
||||||
* -lraylib -lpthread -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm /
|
|
||||||
* -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
|
|
||||||
*
|
|
||||||
* Copyright (c) 2016-2018 Victor Fisac
|
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#define PHYSAC_NO_THREADS
|
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
@ -29,7 +24,7 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||||
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics restitution");
|
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics restitution");
|
||||||
|
|
||||||
// Physac logo drawing position
|
// Physac logo drawing position
|
||||||
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
||||||
|
@ -62,9 +57,9 @@ int main(void)
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
RunPhysicsStep();
|
UpdatePhysics(); // Update physics system
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
{
|
{
|
||||||
// Reset circles physics bodies position and velocity
|
// Reset circles physics bodies position and velocity
|
||||||
circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
|
circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
|
||||||
|
@ -124,6 +119,7 @@ int main(void)
|
||||||
DestroyPhysicsBody(circleB);
|
DestroyPhysicsBody(circleB);
|
||||||
DestroyPhysicsBody(circleC);
|
DestroyPhysicsBody(circleC);
|
||||||
DestroyPhysicsBody(floor);
|
DestroyPhysicsBody(floor);
|
||||||
|
|
||||||
ClosePhysics(); // Unitialize physics
|
ClosePhysics(); // Unitialize physics
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* Physac - Body shatter
|
* raylib [physac] example - physics shatter
|
||||||
*
|
*
|
||||||
* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
|
* This example has been created using raylib 1.5 (www.raylib.com)
|
||||||
* NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Use the following line to compile:
|
* This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
|
||||||
*
|
*
|
||||||
* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static /
|
* Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
|
||||||
* -lraylib -lpthread -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm /
|
|
||||||
* -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
|
|
||||||
*
|
|
||||||
* Copyright (c) 2016-2018 Victor Fisac
|
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#define PHYSAC_IMPLEMENTATION
|
#define PHYSAC_IMPLEMENTATION
|
||||||
#define PHYSAC_NO_THREADS
|
|
||||||
#include "physac.h"
|
#include "physac.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
@ -29,12 +24,11 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||||
InitWindow(screenWidth, screenHeight, "Physac [raylib] - Body shatter");
|
InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics shatter");
|
||||||
|
|
||||||
// Physac logo drawing position
|
// Physac logo drawing position
|
||||||
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
int logoX = screenWidth - MeasureText("Physac", 30) - 10;
|
||||||
int logoY = 15;
|
int logoY = 15;
|
||||||
bool needsReset = false;
|
|
||||||
|
|
||||||
// Initialize physics and default physics bodies
|
// Initialize physics and default physics bodies
|
||||||
InitPhysics();
|
InitPhysics();
|
||||||
|
@ -49,31 +43,23 @@ int main(void)
|
||||||
// Main game loop
|
// Main game loop
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
{
|
{
|
||||||
// Update
|
|
||||||
RunPhysicsStep();
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Delay initialization of variables due to physics reset asynchronous
|
UpdatePhysics(); // Update physics system
|
||||||
if (needsReset)
|
|
||||||
{
|
if (IsKeyPressed('R')) // Reset physics input
|
||||||
// Create random polygon physics body to shatter
|
|
||||||
CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
|
|
||||||
needsReset = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsKeyPressed('R')) // Reset physics input
|
|
||||||
{
|
{
|
||||||
ResetPhysics();
|
ResetPhysics();
|
||||||
needsReset = true;
|
|
||||||
|
CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
|
||||||
{
|
{
|
||||||
// Note: some values need to be stored in variables due to asynchronous changes during main thread
|
|
||||||
int count = GetPhysicsBodiesCount();
|
int count = GetPhysicsBodiesCount();
|
||||||
for (int i = count - 1; i >= 0; i--)
|
for (int i = count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
PhysicsBody currentBody = GetPhysicsBody(i);
|
PhysicsBody currentBody = GetPhysicsBody(i);
|
||||||
|
|
||||||
if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);
|
if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1065
src/physac.h
1065
src/physac.h
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue