Add initial prototype.

This commit is contained in:
Rod Kay
2022-07-31 17:34:54 +10:00
commit 54a53b2ac0
1421 changed files with 358874 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
add_executable(unit_test
doctest.h
hello_world.cpp
collision_test.cpp
joint_test.cpp
math_test.cpp
world_test.cpp
)
set_target_properties(unit_test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
target_link_libraries(unit_test PUBLIC box2d)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES doctest.h
hello_world.cpp collision_test.cpp joint_test.cpp math_test.cpp world_test.cpp )

View File

@@ -0,0 +1,81 @@
// MIT License
// Copyright (c) 2020 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "box2d/box2d.h"
#include "doctest.h"
#include <stdio.h>
// Unit tests for collision algorithms
DOCTEST_TEST_CASE("collision test")
{
SUBCASE("polygon mass data")
{
const b2Vec2 center(100.0f, -50.0f);
const float hx = 0.5f, hy = 1.5f;
const float angle1 = 0.25f;
// Data from issue #422. Not used because the data exceeds accuracy limits.
//const b2Vec2 center(-15000.0f, -15000.0f);
//const float hx = 0.72f, hy = 0.72f;
//const float angle1 = 0.0f;
b2PolygonShape polygon1;
polygon1.SetAsBox(hx, hy, center, angle1);
const float absTol = 2.0f * b2_epsilon;
const float relTol = 2.0f * b2_epsilon;
CHECK(b2Abs(polygon1.m_centroid.x - center.x) < absTol + relTol * b2Abs(center.x));
CHECK(b2Abs(polygon1.m_centroid.y - center.y) < absTol + relTol * b2Abs(center.y));
b2Vec2 vertices[4];
vertices[0].Set(center.x - hx, center.y - hy);
vertices[1].Set(center.x + hx, center.y - hy);
vertices[2].Set(center.x - hx, center.y + hy);
vertices[3].Set(center.x + hx, center.y + hy);
b2PolygonShape polygon2;
polygon2.Set(vertices, 4);
CHECK(b2Abs(polygon2.m_centroid.x - center.x) < absTol + relTol * b2Abs(center.x));
CHECK(b2Abs(polygon2.m_centroid.y - center.y) < absTol + relTol * b2Abs(center.y));
const float mass = 4.0f * hx * hy;
const float inertia = (mass / 3.0f) * (hx * hx + hy * hy) + mass * b2Dot(center, center);
b2MassData massData1;
polygon1.ComputeMass(&massData1, 1.0f);
CHECK(b2Abs(massData1.center.x - center.x) < absTol + relTol * b2Abs(center.x));
CHECK(b2Abs(massData1.center.y - center.y) < absTol + relTol * b2Abs(center.y));
CHECK(b2Abs(massData1.mass - mass) < 20.0f * (absTol + relTol * mass));
CHECK(b2Abs(massData1.I - inertia) < 40.0f * (absTol + relTol * inertia));
b2MassData massData2;
polygon2.ComputeMass(&massData2, 1.0f);
CHECK(b2Abs(massData2.center.x - center.x) < absTol + relTol * b2Abs(center.x));
CHECK(b2Abs(massData2.center.y - center.y) < absTol + relTol * b2Abs(center.y));
CHECK(b2Abs(massData2.mass - mass) < 20.0f * (absTol + relTol * mass));
CHECK(b2Abs(massData2.I - inertia) < 40.0f * (absTol + relTol * inertia));
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,112 @@
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "box2d/box2d.h"
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include <stdio.h>
// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
// There are no graphics for this example. Box2D is meant to be used
// with your rendering engine in your game engine.
DOCTEST_TEST_CASE("hello world")
{
// Define the gravity vector.
b2Vec2 gravity(0.0f, -10.0f);
// Construct a world object, which will hold and simulate the rigid bodies.
b2World world(gravity);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world.CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// The extents are the half-widths of the box.
groundBox.SetAsBox(50.0f, 10.0f);
// Add the ground fixture to the ground body.
groundBody->CreateFixture(&groundBox, 0.0f);
// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(0.0f, 4.0f);
b2Body* body = world.CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.0f);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
// Set the box density to be non-zero, so it will be dynamic.
fixtureDef.density = 1.0f;
// Override the default friction.
fixtureDef.friction = 0.3f;
// Add the shape to the body.
body->CreateFixture(&fixtureDef);
// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
float timeStep = 1.0f / 60.0f;
int32 velocityIterations = 6;
int32 positionIterations = 2;
b2Vec2 position = body->GetPosition();
float angle = body->GetAngle();
// This is our little game loop.
for (int32 i = 0; i < 60; ++i)
{
// Instruct the world to perform a single step of simulation.
// It is generally best to keep the time step and iterations fixed.
world.Step(timeStep, velocityIterations, positionIterations);
// Now print the position and angle of the body.
position = body->GetPosition();
angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
// When the world destructor is called, all bodies and joints are freed. This can
// create orphaned pointers, so be careful about your world management.
CHECK(b2Abs(position.x) < 0.01f);
CHECK(b2Abs(position.y - 1.01f) < 0.01f);
CHECK(b2Abs(angle) < 0.01f);
}

View File

@@ -0,0 +1,106 @@
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "box2d/box2d.h"
#include "doctest.h"
#include <stdio.h>
DOCTEST_TEST_CASE("joint reactions")
{
b2Vec2 gravity(0, -10.0f);
b2World world = b2World(gravity);
b2BodyDef bodyDef;
b2Body* ground = world.CreateBody(&bodyDef);
b2CircleShape circle;
circle.m_radius = 1.0f;
b2FixtureDef fixtureDef;
// Disable collision
fixtureDef.filter.maskBits = 0;
fixtureDef.density = 1.0f;
fixtureDef.shape = &circle;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(-2.0f, 3.0f);
b2Body* bodyA = world.CreateBody(&bodyDef);
b2Body* bodyB = world.CreateBody(&bodyDef);
b2Body* bodyC = world.CreateBody(&bodyDef);
b2MassData massData;
circle.ComputeMass(&massData, fixtureDef.density);
const float mg = massData.mass * gravity.y;
bodyA->CreateFixture(&fixtureDef);
bodyB->CreateFixture(&fixtureDef);
bodyC->CreateFixture(&fixtureDef);
b2DistanceJointDef distanceJointDef;
distanceJointDef.Initialize(ground, bodyA, bodyDef.position + b2Vec2(0.0f, 4.0f), bodyDef.position);
distanceJointDef.minLength = distanceJointDef.length;
distanceJointDef.maxLength = distanceJointDef.length;
b2PrismaticJointDef prismaticJointDef;
prismaticJointDef.Initialize(ground, bodyB, bodyDef.position, b2Vec2(1.0f, 0.0f));
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.Initialize(ground, bodyC, bodyDef.position);
b2DistanceJoint* distanceJoint = (b2DistanceJoint*)world.CreateJoint(&distanceJointDef);
b2PrismaticJoint* prismaticJoint = (b2PrismaticJoint*)world.CreateJoint(&prismaticJointDef);
b2RevoluteJoint* revoluteJoint = (b2RevoluteJoint*)world.CreateJoint(&revoluteJointDef);
const float timeStep = 1.f / 60.f;
const float invTimeStep = 60.0f;
const int32 velocityIterations = 6;
const int32 positionIterations = 2;
world.Step(timeStep, velocityIterations, positionIterations);
const float tol = 1e-5f;
{
b2Vec2 F = distanceJoint->GetReactionForce(invTimeStep);
float T = distanceJoint->GetReactionTorque(invTimeStep);
CHECK(F.x == 0.0f);
CHECK(b2Abs(F.y + mg) < tol);
CHECK(T == 0.0f);
}
{
b2Vec2 F = prismaticJoint->GetReactionForce(invTimeStep);
float T = prismaticJoint->GetReactionTorque(invTimeStep);
CHECK(F.x == 0.0f);
CHECK(b2Abs(F.y + mg) < tol);
CHECK(T == 0.0f);
}
{
b2Vec2 F = revoluteJoint->GetReactionForce(invTimeStep);
float T = revoluteJoint->GetReactionTorque(invTimeStep);
CHECK(F.x == 0.0f);
CHECK(b2Abs(F.y + mg) < tol);
CHECK(T == 0.0f);
}
}

View File

@@ -0,0 +1,54 @@
// MIT License
// Copyright (c) 2020 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "box2d/box2d.h"
#include "doctest.h"
#include <stdio.h>
DOCTEST_TEST_CASE("math test")
{
SUBCASE("sweep")
{
// From issue #447
b2Sweep sweep;
sweep.localCenter.SetZero();
sweep.c0.Set(-2.0f, 4.0f);
sweep.c.Set(3.0f, 8.0f);
sweep.a0 = 0.5f;
sweep.a = 5.0f;
sweep.alpha0 = 0.0f;
b2Transform transform;
sweep.GetTransform(&transform, 0.0f);
DOCTEST_REQUIRE_EQ(transform.p.x, sweep.c0.x);
DOCTEST_REQUIRE_EQ(transform.p.y, sweep.c0.y);
DOCTEST_REQUIRE_EQ(transform.q.c, cosf(sweep.a0));
DOCTEST_REQUIRE_EQ(transform.q.s, sinf(sweep.a0));
sweep.GetTransform(&transform, 1.0f);
DOCTEST_REQUIRE_EQ(transform.p.x, sweep.c.x);
DOCTEST_REQUIRE_EQ(transform.p.y, sweep.c.y);
DOCTEST_REQUIRE_EQ(transform.q.c, cosf(sweep.a));
DOCTEST_REQUIRE_EQ(transform.q.s, sinf(sweep.a));
}
}

View File

@@ -0,0 +1,73 @@
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "box2d/box2d.h"
#include "doctest.h"
#include <stdio.h>
static bool begin_contact = false;
class MyContactListener : public b2ContactListener
{
public:
void BeginContact(b2Contact* contact)
{
begin_contact = true;
}
};
DOCTEST_TEST_CASE("begin contact")
{
b2World world = b2World(b2Vec2(0.0f, -10.0f));
MyContactListener listener;
world.SetContactListener(&listener);
b2CircleShape circle;
circle.m_radius = 5.f;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
b2Body* bodyA = world.CreateBody(&bodyDef);
b2Body* bodyB = world.CreateBody(&bodyDef);
bodyA->CreateFixture(&circle, 0.0f);
bodyB->CreateFixture(&circle, 0.0f);
bodyA->SetTransform(b2Vec2(0.f, 0.f), 0.f);
bodyB->SetTransform(b2Vec2(100.f, 0.f), 0.f);
const float timeStep = 1.f / 60.f;
const int32 velocityIterations = 6;
const int32 positionIterations = 2;
world.Step(timeStep, velocityIterations, positionIterations);
CHECK(world.GetContactList() == nullptr);
CHECK(begin_contact == false);
bodyB->SetTransform(b2Vec2(1.f, 0.f), 0.f);
world.Step(timeStep, velocityIterations, positionIterations);
CHECK(world.GetContactList() != nullptr);
CHECK(begin_contact == true);
}