Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kinematic bodies #53

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "box2d/api.h"
#include "box2d/geometry.h"
#include "box2d/id.h"
#include "box2d/joint_types.h"
#include "box2d/timer.h"
Expand Down Expand Up @@ -41,16 +42,51 @@ BOX2D_API void b2World_DestroyBody(b2BodyId bodyId);

BOX2D_API b2Vec2 b2Body_GetPosition(b2BodyId bodyId);
BOX2D_API float b2Body_GetAngle(b2BodyId bodyId);
BOX2D_API void b2Body_SetTransform(b2BodyId bodyId, b2Vec2 position, float angle);

BOX2D_API b2Vec2 b2Body_GetLocalPoint(b2BodyId bodyId, b2Vec2 globalPoint);
BOX2D_API b2Vec2 b2Body_GetWorldPoint(b2BodyId bodyId, b2Vec2 localPoint);

BOX2D_API void b2Body_SetTransform(b2BodyId bodyId, b2Vec2 position, float angle);
BOX2D_API b2Vec2 b2Body_GetLinearVelocity(b2BodyId bodyId);
BOX2D_API float b2Body_GetAngularVelocity(b2BodyId bodyId);
BOX2D_API void b2Body_SetLinearVelocity(b2BodyId bodyId, b2Vec2 linearVelocity);
BOX2D_API void b2Body_SetAngularVelocity(b2BodyId bodyId, float angularVelocity);

BOX2D_API b2BodyType b2Body_GetType(b2BodyId bodyId);
BOX2D_API void b2Body_SetType(b2BodyId bodyId, b2BodyType type);

/// Get the mass of the body (kilograms)
BOX2D_API float b2Body_GetMass(b2BodyId bodyId);

/// Get the inertia tensor of the body. In 2D this is a single number. (kilograms * meters^2)
BOX2D_API float b2Body_GetInertiaTensor(b2BodyId bodyId);

/// Get the center of mass position of the body in local space.
BOX2D_API b2Vec2 b2Body_GetLocalCenterOfMass(b2BodyId bodyId);

/// Get the center of mass position of the body in world space.
BOX2D_API b2Vec2 b2Body_GetWorldCenterOfMass(b2BodyId bodyId);

/// Override the body's mass properties. Normally this is computed automatically using the
/// shape geometry and density. This information is lost if a shape is added or removed or if the
/// body type changes.
BOX2D_API void b2Body_SetMassData(b2MassData massData);

/// Is this body awake?
BOX2D_API void b2Body_IsAwake(b2BodyId bodyId);

/// Wake a body from sleep. This wakes the entire island the body is touching.
BOX2D_API void b2Body_Wake(b2BodyId bodyId);

/// Is this body enabled?
BOX2D_API bool b2Body_IsEnabled(b2BodyId bodyId);

/// Disable a body by removing it completely from the simulation
BOX2D_API void b2Body_Disable(b2BodyId bodyId);

/// Enable a body by adding it to the simulation
BOX2D_API void b2Body_Enable(b2BodyId bodyId);

/// Create a shape and attach it to a body. Contacts are not created until the next time step.
/// @warning This function is locked during callbacks.
BOX2D_API b2ShapeId b2Body_CreateCircle(b2BodyId bodyId, const b2ShapeDef* def, const b2Circle* circle);
Expand All @@ -62,6 +98,7 @@ BOX2D_API b2BodyId b2Shape_GetBody(b2ShapeId shapeId);
BOX2D_API bool b2Shape_TestPoint(b2ShapeId shapeId, b2Vec2 point);

BOX2D_API b2JointId b2World_CreateMouseJoint(b2WorldId worldId, const b2MouseJointDef* def);
BOX2D_API b2JointId b2World_CreatePrismaticJoint(b2WorldId worldId, const b2PrismaticJointDef* def);
BOX2D_API b2JointId b2World_CreateRevoluteJoint(b2WorldId worldId, const b2RevoluteJointDef* def);
BOX2D_API b2JointId b2World_CreateWeldJoint(b2WorldId worldId, const b2WeldJointDef* def);
BOX2D_API void b2World_DestroyJoint(b2JointId jointId);
Expand All @@ -78,7 +115,7 @@ BOX2D_API float b2RevoluteJoint_GetMotorTorque(b2JointId jointId, float inverseT
BOX2D_API void b2RevoluteJoint_SetMaxMotorTorque(b2JointId jointId, float torque);
BOX2D_API b2Vec2 b2RevoluteJoint_GetConstraintForce(b2JointId jointId);

/// This function receives shapes found in the AABB query.
/// This function receives shapes found in the AABB query.
/// @return true if the query should continue
typedef bool b2QueryCallbackFcn(b2ShapeId shapeId, void* context);

Expand All @@ -87,7 +124,6 @@ typedef bool b2QueryCallbackFcn(b2ShapeId shapeId, void* context);
/// @param aabb the query box.
BOX2D_API void b2World_QueryAABB(b2WorldId worldId, b2AABB aabb, b2QueryCallbackFcn* fcn, void* context);


/// Advanced API for testing and special cases

/// Enable/disable sleep.
Expand Down
2 changes: 2 additions & 0 deletions include/box2d/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ typedef struct b2MassData
/// The rotational inertia of the shape about the local origin.
float I;

/// TODO_ERIN remove geometry info from this

/// Distance from shape centroid to closest point on perimeter.
float minExtent;

Expand Down
75 changes: 72 additions & 3 deletions include/box2d/joint_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct b2MouseJointDef
float damping;
} b2MouseJointDef;

static inline struct b2MouseJointDef b2DefaultMouseJointDef(void)
static inline b2MouseJointDef b2DefaultMouseJointDef(void)
{
b2MouseJointDef def = {0};
def.bodyIdA = b2_nullBodyId;
Expand All @@ -47,6 +47,73 @@ static inline struct b2MouseJointDef b2DefaultMouseJointDef(void)
return def;
}

/// Prismatic joint definition. This requires defining a line of
/// motion using an axis and an anchor point. The definition uses local
/// anchor points and a local axis so that the initial configuration
/// can violate the constraint slightly. The joint translation is zero
/// when the local anchor points coincide in world space.
typedef struct b2PrismaticJointDef
{
/// The first attached body.
b2BodyId bodyIdA;

/// The second attached body.
b2BodyId bodyIdB;

/// The local anchor point relative to bodyA's origin.
b2Vec2 localAnchorA;

/// The local anchor point relative to bodyB's origin.
b2Vec2 localAnchorB;

/// The local translation unit axis in bodyA.
b2Vec2 localAxisA;

/// The constrained angle between the bodies: bodyB_angle - bodyA_angle.
float referenceAngle;

/// Enable/disable the joint limit.
bool enableLimit;

/// The lower translation limit, usually in meters.
float lowerTranslation;

/// The upper translation limit, usually in meters.
float upperTranslation;

/// Enable/disable the joint motor.
bool enableMotor;

/// The maximum motor torque, usually in N-m.
float maxMotorForce;

/// The desired motor speed in radians per second.
float motorSpeed;

/// Set this flag to true if the attached bodies should collide.
bool collideConnected;
} b2PrismaticJointDef;

/// Use this to initialize your joint definition
static inline b2PrismaticJointDef b2DefaultPrismaticJointDef(void)
{
b2PrismaticJointDef def = {0};
def.bodyIdA = b2_nullBodyId;
def.bodyIdB = b2_nullBodyId;
def.localAnchorA = B2_LITERAL(b2Vec2){0.0f, 0.0f};
def.localAnchorB = B2_LITERAL(b2Vec2){0.0f, 0.0f};
def.localAxisA = B2_LITERAL(b2Vec2){1.0f, 0.0f};
def.referenceAngle = 0.0f;
def.enableLimit = false;
def.lowerTranslation = 0.0f;
def.upperTranslation = 0.0f;
def.enableMotor = false;
def.maxMotorForce = 0.0f;
def.motorSpeed = 0.0f;
def.collideConnected = false;
return def;
}

/// Revolute joint definition. This requires defining an anchor point where the
/// bodies are joined. The definition uses local anchor points so that the
/// initial configuration can violate the constraint slightly. You also need to
Expand Down Expand Up @@ -98,7 +165,8 @@ typedef struct b2RevoluteJointDef
bool collideConnected;
} b2RevoluteJointDef;

static inline struct b2RevoluteJointDef b2DefaultRevoluteJointDef(void)
/// Use this to initialize your joint definition
static inline b2RevoluteJointDef b2DefaultRevoluteJointDef(void)
{
b2RevoluteJointDef def = {0};
def.bodyIdA = b2_nullBodyId;
Expand Down Expand Up @@ -146,7 +214,8 @@ typedef struct b2WeldJointDef
bool collideConnected;
} b2WeldJointDef;

static inline struct b2WeldJointDef b2DefaultWeldJointDef(void)
/// Use this to initialize your joint definition
static inline b2WeldJointDef b2DefaultWeldJointDef(void)
{
b2WeldJointDef def = {0};
def.bodyIdA = b2_nullBodyId;
Expand Down
8 changes: 7 additions & 1 deletion include/box2d/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ static inline b2Vec2 b2CrossSV(float s, b2Vec2 v)
return B2_LITERAL(b2Vec2){-s * v.y, s * v.x};
}

/// Get a right pointing perpendicular vector. Equivalent to b2CrossVS(v, 1.0f).
/// Get a left pointing perpendicular vector. Equivalent to b2CrossSV(1.0f, v)
static inline b2Vec2 b2LeftPerp(b2Vec2 v)
{
return B2_LITERAL(b2Vec2){-v.y, v.x};
}

/// Get a right pointing perpendicular vector. Equivalent to b2CrossVS(v, 1.0f)
static inline b2Vec2 b2RightPerp(b2Vec2 v)
{
return B2_LITERAL(b2Vec2){v.y, -v.x};
Expand Down
5 changes: 3 additions & 2 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ set(BOX2D_SAMPLES
collection/benchmark_many_tumblers.cpp
collection/benchmark_pyramid.cpp
collection/benchmark_tumbler.cpp
collection/behavior.cpp
collection/sample_continuous1.cpp
collection/sample_robustness.cpp
collection/sample_bodies.cpp
collection/sample_continuous.cpp
collection/sample_distance.cpp
collection/sample_dynamic_tree.cpp
collection/sample_hull.cpp
Expand Down
Loading