Add hello world project to show how to use ReactPhysics3D in your project

This commit is contained in:
Daniel Chappuis 2020-05-13 19:07:04 +02:00
parent 075f89e91b
commit c1facfc682
4 changed files with 73 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# Changelog # Changelog
## Develop ## Version 0.8.0
### Added ### Added
@ -25,10 +25,12 @@
- In the EventListener, the onContact() and onTrigger() method now reports the type of event (start, stay, exit) for each contact. This way the user can know whether it's a new contact or not or when two colliders are not in contact anymore. - In the EventListener, the onContact() and onTrigger() method now reports the type of event (start, stay, exit) for each contact. This way the user can know whether it's a new contact or not or when two colliders are not in contact anymore.
- A DebugRenderer class has been added in order to display debug info (colliders, AABBs, contacts, ...) using graphics primities (lines, triangles). - A DebugRenderer class has been added in order to display debug info (colliders, AABBs, contacts, ...) using graphics primities (lines, triangles).
- A RigidBody::applyForceAtLocalPosition() method has been added to apply a force at a given position of the rigid body in local-space. - A RigidBody::applyForceAtLocalPosition() method has been added to apply a force at a given position of the rigid body in local-space.
- The CMakeLists.txt file of the library has been refactored to use modern CMake. The targets are now exported when you install the library so that you can import the library with the find_package(ReactPhysics3D) function in your own CMakeLists.txt file
- A Hello World project has been added to show a very simple project that uses the ReactPhysics3D library
### Fixed ### Fixed
- Issues [#125](https://github.com/DanielChappuis/reactphysics3d/issues/125) and [#106](https://github.com/DanielChappuis/reactphysics3d/issues/106) have been fixed. - Issues [#125](https://github.com/DanielChappuis/reactphysics3d/issues/125) and [#106](https://github.com/DanielChappuis/reactphysics3d/issues/106) with CMake install of the library have been fixed
- Issue [#141](https://github.com/DanielChappuis/reactphysics3d/issues/141) with limits of hinge and slider joints has been fixed. - Issue [#141](https://github.com/DanielChappuis/reactphysics3d/issues/141) with limits of hinge and slider joints has been fixed.
### Changed ### Changed

View File

@ -254,7 +254,10 @@ target_compile_features(reactphysics3d PUBLIC cxx_std_11)
set_target_properties(reactphysics3d PROPERTIES CXX_EXTENSIONS OFF) set_target_properties(reactphysics3d PROPERTIES CXX_EXTENSIONS OFF)
# Compile with warning messages # Compile with warning messages
target_compile_options(reactphysics3d PRIVATE -Wall) target_compile_options(reactphysics3d PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4> # for Visual Studio
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -pedantic> # Other compilers
)
# Library headers # Library headers
target_include_directories(reactphysics3d PUBLIC target_include_directories(reactphysics3d PUBLIC

18
helloworld/CMakeLists.txt Normal file
View File

@ -0,0 +1,18 @@
# This is the CMakeLists.txt file of the "hello world" project
# in order to show a simple executable that uses the ReactPhysics3D library
# Minimum cmake version required
cmake_minimum_required(VERSION 3.8)
# Import the ReactPhysics3D library that you have installed on your computer using
# the "make install" command
find_package(ReactPhysics3D REQUIRED)
# Project
project(HelloWorld)
# Create the executable
add_executable(helloworld Main.cpp)
# Link with the ReactPhysics3D library
target_link_libraries(helloworld ReactPhysics3D::ReactPhysics3D)

47
helloworld/Main.cpp Normal file
View File

@ -0,0 +1,47 @@
/*
* This is a "hello world" project with a basic example of how to use
* the ReactPhysics3D library in your project. This example is a simple
* rigid body that is falling down because of gravity.
*/
// Libraries
#include <reactphysics3d/reactphysics3d.h>
#include <iostream>
// ReactPhysics3D namespace
using namespace reactphysics3d;
// Main function
int main(int argc, char** argv) {
// First you need to create the PhysicsCommon object. This is a factory module
// that you can use to create physics world and other objects. It is also responsible
// for logging and memory management
PhysicsCommon physicsCommon;
// Create a physics world
PhysicsWorld* world = physicsCommon.createPhysicsWorld();
// Create a rigid body in the world
Vector3 position(0, 20, 0);
Quaternion orientation = Quaternion::identity();
Transform transform(position, orientation);
RigidBody* body = world->createRigidBody(transform);
const decimal timeStep = 1.0f / 60.0f;
// Step the simulation a few steps
for (int i=0; i < 20; i++) {
world->update(timeStep);
// Ge the updated position of the body
const Transform& transform = body->getTransform();
const Vector3& position = transform.getPosition();
// Display the position of the body
std::cout << "Body Position: (" << position.x << ", " << position.y << ", " << position.z << ")" << std::endl;
}
return 0;
}