2015-12-21 23:12:35 +03:00
/*******************************************************************************************
*
2016-01-03 19:54:06 +03:00
* raylib [ physac ] physics example - Basic rigidbody
2015-12-21 23:12:35 +03:00
*
2016-02-13 19:09:53 +03:00
* This example has been created using raylib 1.4 ( www . raylib . com )
2015-12-21 23:12:35 +03:00
* raylib is licensed under an unmodified zlib / libpng license ( View raylib . h for details )
*
2016-02-13 19:09:53 +03:00
* Copyright ( c ) 2016 Victor Fisac and Ramon Santamaria ( @ raysan5 )
2015-12-21 23:12:35 +03:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "raylib.h"
# define OBJECT_SIZE 50
# define PLAYER_INDEX 0
int main ( )
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800 ;
int screenHeight = 450 ;
InitWindow ( screenWidth , screenHeight , " raylib [physics] example - basic rigidbody " ) ;
2016-01-20 21:09:48 +03:00
2016-02-13 19:09:53 +03:00
InitPhysics ( 3 ) ; // Initialize physics system with maximum physic objects
2015-12-21 23:12:35 +03:00
// Object initialization
Transform player = ( Transform ) { ( Vector2 ) { ( screenWidth - OBJECT_SIZE ) / 2 , ( screenHeight - OBJECT_SIZE ) / 2 } , 0.0f , ( Vector2 ) { OBJECT_SIZE , OBJECT_SIZE } } ;
2016-01-20 21:09:48 +03:00
AddCollider ( PLAYER_INDEX , ( Collider ) { true , COLLIDER_RECTANGLE , ( Rectangle ) { player . position . x , player . position . y , player . scale . x , player . scale . y } , 0 } ) ;
2015-12-21 23:12:35 +03:00
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 } } ;
2016-01-20 21:09:48 +03:00
AddCollider ( PLAYER_INDEX + 1 , ( Collider ) { true , COLLIDER_RECTANGLE , ( Rectangle ) { floor . position . x , floor . position . y , floor . scale . x , floor . scale . y } , 0 } ) ;
2015-12-21 23:12:35 +03:00
// Object properties initialization
float moveSpeed = 6.0f ;
2016-01-20 21:09:48 +03:00
float jumpForce = 5.0f ;
2016-02-13 19:09:53 +03:00
bool physicsDebug = false ;
2016-01-20 21:09:48 +03:00
SetTargetFPS ( 60 ) ;
2015-12-21 23:12:35 +03:00
//--------------------------------------------------------------------------------------
// 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)
ApplyPhysics ( PLAYER_INDEX , & player . position ) ;
// Check jump button input
2016-01-20 21:09:48 +03:00
if ( IsKeyDown ( KEY_SPACE ) & & GetRigidbody ( PLAYER_INDEX ) . isGrounded )
2015-12-21 23:12:35 +03:00
{
// 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 } ) ;
// Add jumping force in Y axis
AddRigidbodyForce ( PLAYER_INDEX , ( Vector2 ) { 0 , jumpForce } ) ;
}
// Check movement buttons input
2016-01-20 21:09:48 +03:00
if ( IsKeyDown ( KEY_RIGHT ) | | IsKeyDown ( KEY_D ) )
2015-12-21 23:12:35 +03:00
{
// 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 } ) ;
}
2016-01-20 21:09:48 +03:00
else if ( IsKeyDown ( KEY_LEFT ) | | IsKeyDown ( KEY_A ) )
2015-12-21 23:12:35 +03:00
{
// 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 } ) ;
}
// Check debug mode toggle button input
2016-02-13 19:09:53 +03:00
if ( IsKeyPressed ( KEY_P ) ) physicsDebug = ! physicsDebug ;
2015-12-21 23:12:35 +03:00
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing ( ) ;
ClearBackground ( RAYWHITE ) ;
// Draw information
DrawText ( " Use LEFT / RIGHT to MOVE and SPACE to JUMP " , ( screenWidth - MeasureText ( " Use LEFT / RIGHT to MOVE and SPACE to JUMP " , 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 ) ;
// Check if debug mode is enabled
2016-02-13 19:09:53 +03:00
if ( physicsDebug )
2015-12-21 23:12:35 +03:00
{
// Draw every internal physics stored collider if it is active
2016-01-20 21:09:48 +03:00
for ( int i = 0 ; i < 2 ; i + + )
2015-12-21 23:12:35 +03:00
{
2016-01-20 21:09:48 +03:00
if ( GetCollider ( i ) . enabled )
2015-12-21 23:12:35 +03:00
{
DrawRectangleLines ( GetCollider ( i ) . bounds . x , GetCollider ( i ) . bounds . y , GetCollider ( i ) . bounds . width , GetCollider ( i ) . bounds . height , GREEN ) ;
}
}
}
else
{
2016-02-13 19:09:53 +03:00
// Draw player and floor
2015-12-21 23:12:35 +03:00
DrawRectangleRec ( ( Rectangle ) { player . position . x , player . position . y , player . scale . x , player . scale . y } , GRAY ) ;
DrawRectangleRec ( ( Rectangle ) { floor . position . x , floor . position . y , floor . scale . x , floor . scale . y } , BLACK ) ;
}
EndDrawing ( ) ;
//----------------------------------------------------------------------------------
}
// De-Initialization
2016-02-13 19:09:53 +03:00
//--------------------------------------------------------------------------------------
UnloadPhysics ( ) ; // Unload physic objects
2015-12-21 23:12:35 +03:00
CloseWindow ( ) ; // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0 ;
}