diff --git a/examples/hello_world/main.cpp b/examples/hello_world/main.cpp new file mode 100644 index 0000000..2760e6d --- /dev/null +++ b/examples/hello_world/main.cpp @@ -0,0 +1,128 @@ +/* +* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net +* +* 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. +*/ + +#include + +// We don't care for a profiler. This definition does nothing. +bool b3PushProfileScope(const char* name) +{ + return false; +} + +// We don't care for a profiler. This definition does nothing. +void b3PopProfileScope() +{ + +} + +// This example shows how to setup and run a simple simulation +// using Bounce. +int main(int argc, char** argv) +{ + // The world gravity. + const b3Vec3 gravity(0.0f, -9.8f, 0.0f); + + // The fixed time step size. + const float32 timeStep = 1.0f / 60.0f; + + // Number of iterations for the velocity constraint solver. + const u32 velocityIterations = 8; + + // Number of iterations for the position constraint solver. + const u32 positionIterations = 2; + + // The world-> We allocate it using the heap but you can to it + // on the stack if the stack is sufficiently large. + b3World* world = new b3World(); + world->SetGravity(gravity); + + // Create a static ground body at the world origin. + b3BodyDef groundDef; + b3Body* ground = world->CreateBody(groundDef); + + // Create a box positioned at the world origin and + // aligned with the world frame. + b3BoxHull groundBox; + + // Set the ground box dimensions using a linear scale transform. + b3Transform scale; + scale.position.SetZero(); + scale.rotation = b3Diagonal(10.0f, 1.0f, 10.0f); + groundBox.SetTransform(scale); + + // Create the box physics wrapper. + b3HullShape groundShape; + groundShape.m_hull = &groundBox; + + // Add the box to the ground body. + b3ShapeDef groundBoxDef; + groundBoxDef.shape = &groundShape; + ground->CreateShape(groundBoxDef); + + // Create a dynamic body. + b3BodyDef bodyDef; + bodyDef.type = e_dynamicBody; + + // Position the body 10 meters high from the world origin. + bodyDef.position.Set(0.0f, 10.0f, 0.0f); + + // Set the initial angular velocity to pi radians (180 degrees) per second. + bodyDef.angularVelocity.Set(0.0f, B3_PI, 0.0f); + + b3Body* body = world->CreateBody(bodyDef); + + // Create a unit box positioned at the world origin and + // aligned with the world frame. + b3BoxHull bodyBox; + bodyBox.SetIdentity(); + + // Create the box physics wrapper. + b3HullShape bodyShape; + bodyShape.m_hull = &bodyBox; + + // Add the box to the body. + b3ShapeDef bodyBoxDef; + bodyBoxDef.shape = &bodyShape; + bodyBoxDef.density = 1.0f; + body->CreateShape(bodyBoxDef); + + // Run a small game loop of 60 frames length. + for (u32 i = 0; i < 60; ++i) + { + // Perform a time step of the world in this frame. + world->Step(timeStep, velocityIterations, positionIterations); + + // Read the body position and orientation in this frame. + b3Vec3 position = body->GetPosition(); + b3Quat orientation = body->GetOrientation(); + + // Decode the axis and angle of rotation about it from the quaternion. + b3Vec3 axis; + float32 angle; + orientation.GetAxisAngle(&axis, &angle); + + // Visualize the body state in this frame. + printf("position = %.2f %.2f %.2f\n", position.x, position.y, position.z); + printf("axis = %.2f %.2f %.2f, angle = %.2f\n\n", axis.x, axis.y, axis.z, angle); + } + + // Now destroy the bodies since the world manages their lifetime. + delete world; + + return 0; +} \ No newline at end of file diff --git a/src/testbed/framework/debug_draw.cpp b/examples/testbed/framework/debug_draw.cpp similarity index 100% rename from src/testbed/framework/debug_draw.cpp rename to examples/testbed/framework/debug_draw.cpp diff --git a/include/testbed/framework/debug_draw.h b/examples/testbed/framework/debug_draw.h similarity index 100% rename from include/testbed/framework/debug_draw.h rename to examples/testbed/framework/debug_draw.h diff --git a/src/testbed/framework/main.cpp b/examples/testbed/framework/main.cpp similarity index 100% rename from src/testbed/framework/main.cpp rename to examples/testbed/framework/main.cpp diff --git a/include/testbed/framework/mat44.h b/examples/testbed/framework/mat44.h similarity index 100% rename from include/testbed/framework/mat44.h rename to examples/testbed/framework/mat44.h diff --git a/src/testbed/framework/profiler.cpp b/examples/testbed/framework/profiler.cpp similarity index 100% rename from src/testbed/framework/profiler.cpp rename to examples/testbed/framework/profiler.cpp diff --git a/include/testbed/framework/profiler.h b/examples/testbed/framework/profiler.h similarity index 100% rename from include/testbed/framework/profiler.h rename to examples/testbed/framework/profiler.h diff --git a/src/testbed/framework/test.cpp b/examples/testbed/framework/test.cpp similarity index 100% rename from src/testbed/framework/test.cpp rename to examples/testbed/framework/test.cpp diff --git a/src/testbed/framework/test_entries.cpp b/examples/testbed/framework/test_entries.cpp similarity index 100% rename from src/testbed/framework/test_entries.cpp rename to examples/testbed/framework/test_entries.cpp diff --git a/include/testbed/tests/angular_motion.h b/examples/testbed/tests/angular_motion.h similarity index 100% rename from include/testbed/tests/angular_motion.h rename to examples/testbed/tests/angular_motion.h diff --git a/include/testbed/tests/body_types.h b/examples/testbed/tests/body_types.h similarity index 100% rename from include/testbed/tests/body_types.h rename to examples/testbed/tests/body_types.h diff --git a/include/testbed/tests/box_stack.h b/examples/testbed/tests/box_stack.h similarity index 100% rename from include/testbed/tests/box_stack.h rename to examples/testbed/tests/box_stack.h diff --git a/include/testbed/tests/capsule_and_hull_collision.h b/examples/testbed/tests/capsule_and_hull_collision.h similarity index 100% rename from include/testbed/tests/capsule_and_hull_collision.h rename to examples/testbed/tests/capsule_and_hull_collision.h diff --git a/include/testbed/tests/capsule_collision.h b/examples/testbed/tests/capsule_collision.h similarity index 100% rename from include/testbed/tests/capsule_collision.h rename to examples/testbed/tests/capsule_collision.h diff --git a/include/testbed/tests/capsule_distance.h b/examples/testbed/tests/capsule_distance.h similarity index 100% rename from include/testbed/tests/capsule_distance.h rename to examples/testbed/tests/capsule_distance.h diff --git a/include/testbed/tests/capsule_stack.h b/examples/testbed/tests/capsule_stack.h similarity index 100% rename from include/testbed/tests/capsule_stack.h rename to examples/testbed/tests/capsule_stack.h diff --git a/include/testbed/tests/character_test.h b/examples/testbed/tests/character_test.h similarity index 100% rename from include/testbed/tests/character_test.h rename to examples/testbed/tests/character_test.h diff --git a/include/testbed/tests/cloth_test.h b/examples/testbed/tests/cloth_test.h similarity index 100% rename from include/testbed/tests/cloth_test.h rename to examples/testbed/tests/cloth_test.h diff --git a/include/testbed/tests/cluster_test.h b/examples/testbed/tests/cluster_test.h similarity index 100% rename from include/testbed/tests/cluster_test.h rename to examples/testbed/tests/cluster_test.h diff --git a/include/testbed/tests/collide_test.h b/examples/testbed/tests/collide_test.h similarity index 100% rename from include/testbed/tests/collide_test.h rename to examples/testbed/tests/collide_test.h diff --git a/include/testbed/tests/distance_test.h b/examples/testbed/tests/distance_test.h similarity index 100% rename from include/testbed/tests/distance_test.h rename to examples/testbed/tests/distance_test.h diff --git a/include/testbed/tests/hinge_chain.h b/examples/testbed/tests/hinge_chain.h similarity index 100% rename from include/testbed/tests/hinge_chain.h rename to examples/testbed/tests/hinge_chain.h diff --git a/include/testbed/tests/hinge_motor.h b/examples/testbed/tests/hinge_motor.h similarity index 100% rename from include/testbed/tests/hinge_motor.h rename to examples/testbed/tests/hinge_motor.h diff --git a/include/testbed/tests/hull_collision.h b/examples/testbed/tests/hull_collision.h similarity index 100% rename from include/testbed/tests/hull_collision.h rename to examples/testbed/tests/hull_collision.h diff --git a/include/testbed/tests/jenga.h b/examples/testbed/tests/jenga.h similarity index 100% rename from include/testbed/tests/jenga.h rename to examples/testbed/tests/jenga.h diff --git a/include/testbed/tests/linear_motion.h b/examples/testbed/tests/linear_motion.h similarity index 100% rename from include/testbed/tests/linear_motion.h rename to examples/testbed/tests/linear_motion.h diff --git a/include/testbed/tests/mesh_contact_test.h b/examples/testbed/tests/mesh_contact_test.h similarity index 100% rename from include/testbed/tests/mesh_contact_test.h rename to examples/testbed/tests/mesh_contact_test.h diff --git a/include/testbed/tests/multiple_shapes.h b/examples/testbed/tests/multiple_shapes.h similarity index 100% rename from include/testbed/tests/multiple_shapes.h rename to examples/testbed/tests/multiple_shapes.h diff --git a/include/testbed/tests/newton_cradle.h b/examples/testbed/tests/newton_cradle.h similarity index 100% rename from include/testbed/tests/newton_cradle.h rename to examples/testbed/tests/newton_cradle.h diff --git a/include/testbed/tests/pyramid.h b/examples/testbed/tests/pyramid.h similarity index 100% rename from include/testbed/tests/pyramid.h rename to examples/testbed/tests/pyramid.h diff --git a/include/testbed/tests/pyramids.h b/examples/testbed/tests/pyramids.h similarity index 100% rename from include/testbed/tests/pyramids.h rename to examples/testbed/tests/pyramids.h diff --git a/include/testbed/tests/quadric_shapes.h b/examples/testbed/tests/quadric_shapes.h similarity index 100% rename from include/testbed/tests/quadric_shapes.h rename to examples/testbed/tests/quadric_shapes.h diff --git a/include/testbed/tests/quickhull_test.h b/examples/testbed/tests/quickhull_test.h similarity index 100% rename from include/testbed/tests/quickhull_test.h rename to examples/testbed/tests/quickhull_test.h diff --git a/include/testbed/tests/ragdoll.h b/examples/testbed/tests/ragdoll.h similarity index 100% rename from include/testbed/tests/ragdoll.h rename to examples/testbed/tests/ragdoll.h diff --git a/include/testbed/tests/ray_cast.h b/examples/testbed/tests/ray_cast.h similarity index 100% rename from include/testbed/tests/ray_cast.h rename to examples/testbed/tests/ray_cast.h diff --git a/include/testbed/tests/sensor_test.h b/examples/testbed/tests/sensor_test.h similarity index 100% rename from include/testbed/tests/sensor_test.h rename to examples/testbed/tests/sensor_test.h diff --git a/include/testbed/tests/shape_stack.h b/examples/testbed/tests/shape_stack.h similarity index 100% rename from include/testbed/tests/shape_stack.h rename to examples/testbed/tests/shape_stack.h diff --git a/include/testbed/tests/sphere_stack.h b/examples/testbed/tests/sphere_stack.h similarity index 100% rename from include/testbed/tests/sphere_stack.h rename to examples/testbed/tests/sphere_stack.h diff --git a/include/testbed/tests/spring.h b/examples/testbed/tests/spring.h similarity index 100% rename from include/testbed/tests/spring.h rename to examples/testbed/tests/spring.h diff --git a/include/testbed/tests/test.h b/examples/testbed/tests/test.h similarity index 100% rename from include/testbed/tests/test.h rename to examples/testbed/tests/test.h diff --git a/include/testbed/tests/thin.h b/examples/testbed/tests/thin.h similarity index 100% rename from include/testbed/tests/thin.h rename to examples/testbed/tests/thin.h diff --git a/include/testbed/tests/varying_friction.h b/examples/testbed/tests/varying_friction.h similarity index 100% rename from include/testbed/tests/varying_friction.h rename to examples/testbed/tests/varying_friction.h diff --git a/include/testbed/tests/varying_restitution.h b/examples/testbed/tests/varying_restitution.h similarity index 100% rename from include/testbed/tests/varying_restitution.h rename to examples/testbed/tests/varying_restitution.h diff --git a/include/bounce/common/memory/stack_allocator.h b/include/bounce/common/memory/stack_allocator.h index 2ac323f..db64bb8 100644 --- a/include/bounce/common/memory/stack_allocator.h +++ b/include/bounce/common/memory/stack_allocator.h @@ -21,9 +21,9 @@ #include -// Allocate 10 MiB from the stack. +// Allocate 1 MiB from the stack. // Increase as you want. -const u32 b3_maxStackSize = B3_MiB(10); +const u32 b3_maxStackSize = B3_MiB(1); // A stack allocator. class b3StackAllocator diff --git a/include/bounce/dynamics/body.h b/include/bounce/dynamics/body.h index 7358953..2b049cf 100644 --- a/include/bounce/dynamics/body.h +++ b/include/bounce/dynamics/body.h @@ -116,6 +116,12 @@ public: // However, manipulating a body transform during the simulation may cause non-physical behaviour. void SetTransform(const b3Vec3& position, const b3Vec3& axis, float32 angle); + // Get the position of the world body origin. + b3Vec3 GetPosition() const; + + // Get the orientation of the world body frame. + b3Quat GetOrientation() const; + // Get the gravity scale of the body. One is used by default. float32 GetGravityScale() const; @@ -382,6 +388,16 @@ inline void b3Body::SetTransform(const b3Vec3& position, const b3Vec3& axis, flo SynchronizeShapes(); } +inline b3Vec3 b3Body::GetPosition() const +{ + return m_sweep.worldCenter; +} + +inline b3Quat b3Body::GetOrientation() const +{ + return m_sweep.orientation; +} + inline b3Vec3 b3Body::GetLocalVector(const b3Vec3& vector) const { return b3MulT(m_xf.rotation, vector); diff --git a/include/bounce/dynamics/time_step.h b/include/bounce/dynamics/time_step.h index a4546fb..6afe01c 100644 --- a/include/bounce/dynamics/time_step.h +++ b/include/bounce/dynamics/time_step.h @@ -53,7 +53,7 @@ enum b3LimitState // Return the Steiner's matrix given the displacement vector from the old // center of rotation to the new center of rotation. -// The result equals to transpose( skew(v) ) * skew(v) or diagonal(v^2) - outer(v) +// The result equals to transpose( skew(v) ) * skew(v) or diagonal(v^2) - outer(v, v) inline b3Mat33 b3Steiner(const b3Vec3& v) { float32 xx = v.x * v.x; diff --git a/premake5.lua b/premake5.lua index 9687702..064f155 100644 --- a/premake5.lua +++ b/premake5.lua @@ -7,8 +7,12 @@ solution_name = "bounce" working_dir = "." solution_dir = "build/" external_dir = "external/" -inc_dir = "include/" -src_dir = "src/" +bounce_inc_dir = "include/" +bounce_src_dir = "src/" +examples_inc_dir = "examples/" +examples_src_dir = "examples/" +tests_inc_dir = "tests/" +tests_src_dir = "tests/" obj_dir = "/obj/" bin_dir = "/bin/" @@ -48,15 +52,15 @@ solution (solution_name) kind "StaticLib" language "C++" location ( solution_dir .. action ) - includedirs { inc_dir } + includedirs { bounce_inc_dir } vpaths { [""] = "bounce" } buildoptions { "-std=c++11" } -- require C++11 files { - inc_dir .. "/bounce/**.h", - inc_dir .. "/bounce/**.inl", - src_dir .. "/bounce/**.cpp" + bounce_inc_dir .. "/bounce/**.h", + bounce_inc_dir .. "/bounce/**.inl", + bounce_src_dir .. "/bounce/**.cpp" } project "glad" kind "StaticLib" @@ -178,14 +182,14 @@ solution (solution_name) kind "ConsoleApp" language "C++" location ( solution_dir .. action ) - includedirs { external_dir, inc_dir } + includedirs { external_dir, bounce_inc_dir, examples_inc_dir } vpaths { ["Headers"] = "**.h", ["Sources"] = "**.cpp" } buildoptions { "-std=c++11" } -- GNU/GCC C++11 files { - inc_dir .. "/testbed/**.h", - src_dir .. "/testbed/**.cpp" + examples_inc_dir .. "/testbed/**.h", + examples_src_dir .. "/testbed/**.cpp" } links { "glfw", "glad", "imgui", "bounce" } @@ -201,7 +205,22 @@ solution (solution_name) "xcb-dri2", "xcb-dri3", "xcb-present", "xcb-sync", "xshmfence", "Xxf86vm", "Xfixes", "Xext", "X11", "pthread", "xcb", "Xau", "Xdmcp" } - + + project "hello_world" + kind "ConsoleApp" + language "C++" + location ( solution_dir .. action ) + includedirs { bounce_inc_dir, examples_inc_dir } + vpaths { ["Headers"] = "**.h", ["Sources"] = "**.cpp" } + buildoptions { "-std=c++11" } -- GNU/GCC C++11 + + files + { + examples_inc_dir .. "/hello_world/**.h", + examples_src_dir .. "/hello_world/**.cpp" + } + + links { "bounce" } -- build if os.is "windows" then diff --git a/readme.md b/readme.md index 08deedd..1a950e1 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ Bounce is a 3D physics engine for games and interactive applications. To get started with Bounce see readme.txt. -Here is how to control the Testbed. The Testbed is a collection of tests and examples that helps the author debug features and is not part of the library. +Here is how to control the Testbed. The Testbed is a collection of non-unit tests and examples that helps the author debug features and is not part of the library. Camera (as in Maya): diff --git a/tests/ignore.txt b/tests/ignore.txt new file mode 100644 index 0000000..e69de29