Skip to content

Commit

Permalink
weld joint
Browse files Browse the repository at this point in the history
testing
  • Loading branch information
erincatto committed Sep 4, 2023
1 parent 2325a9f commit 9a212e3
Show file tree
Hide file tree
Showing 12 changed files with 599 additions and 41 deletions.
2 changes: 2 additions & 0 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ BOX2D_API bool b2Shape_TestPoint(b2ShapeId shapeId, b2Vec2 point);

BOX2D_API b2JointId b2World_CreateMouseJoint(b2WorldId worldId, const b2MouseJointDef* 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);

BOX2D_API void b2MouseJoint_SetTarget(b2JointId jointId, b2Vec2 target);
Expand All @@ -85,6 +86,7 @@ BOX2D_API void b2RevoluteJoint_EnableLimit(b2JointId jointId, bool enableLimit);
BOX2D_API void b2RevoluteJoint_EnableMotor(b2JointId jointId, bool enableMotor);
BOX2D_API void b2RevoluteJoint_SetMotorSpeed(b2JointId jointId, float motorSpeed);
BOX2D_API float b2RevoluteJoint_GetMotorTorque(b2JointId jointId, float inverseTimeStep);
BOX2D_API void b2RevoluteJoint_SetMaxMotorTorque(b2JointId jointId, float torque);

/// This function receives shapes found in the AABB query.
/// @return true if the query should continue
Expand Down
46 changes: 46 additions & 0 deletions include/box2d/joint_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,49 @@ static inline struct b2RevoluteJointDef b2DefaultRevoluteJointDef(void)
def.collideConnected = false;
return def;
}

typedef struct b2WeldJointDef
{
/// 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 bodyB angle minus bodyA angle in the reference state (radians).
/// This defines the zero angle for the joint limit.
float referenceAngle;

/// Stiffness expressed as hertz (oscillations per second). Use zero for maximum stiffness.
float linearHertz;
float angularHertz;

/// Damping ratio, non-dimensional. Use 1 for critical damping.
float linearDampingRatio;
float angularDampingRatio;

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

static inline struct b2WeldJointDef b2DefaultWeldJointDef(void)
{
b2WeldJointDef 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.referenceAngle = 0.0f;
def.linearHertz = 0.0f;
def.angularHertz = 0.0f;
def.linearDampingRatio = 1.0f;
def.angularDampingRatio = 1.0f;
def.collideConnected = false;
return def;
}
33 changes: 31 additions & 2 deletions include/box2d/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ extern "C"
#define B2_CLAMP(A, B, C) B2_MIN(B2_MAX(A, B), C)

static const b2Vec2 b2Vec2_zero = {0.0f, 0.0f};
static const b2Vec3 b2Vec3_zero = {0.0f, 0.0f, 0.0f};
static const b2Rot b2Rot_identity = {0.0f, 1.0f};
static const b2Transform b2Transform_identity = {{0.0f, 0.0f}, {0.0f, 1.0f}};
static const b2Mat22 b2Mat22_zero = {{0.0f, 0.0f}, {0.0f, 0.0f}};
static const b2Mat33 b2Mat33_zero = {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};

bool b2IsValid(float a);
bool b2IsValidVec2(b2Vec2 v);
Expand All @@ -43,6 +45,19 @@ static inline float b2Cross(b2Vec2 a, b2Vec2 b)
return a.x * b.y - a.y * b.x;
}

/// Perform the dot product on two 3-vectors.
static inline float b2Dot3(b2Vec3 a, b2Vec3 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}

/// Perform the cross product on two 3-vectors.
static inline b2Vec3 b2Cross3(b2Vec3 a, b2Vec3 b)
{
return B2_LITERAL(b2Vec3){a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}


/// Perform the cross product on a vector and a scalar. In 2D this produces
/// a vector.
static inline b2Vec2 b2CrossVS(b2Vec2 v, float s)
Expand Down Expand Up @@ -305,8 +320,7 @@ static inline b2Mat22 b2GetInverse22(b2Mat22 A)
return B;
}

/// Solve A * x = b, where b is a column vector. This is more efficient
/// than computing the inverse in one-shot cases.
/// Solve A * x = b, where b is a column vector.
static inline b2Vec2 b2Solve22(b2Mat22 A, b2Vec2 b)
{
float a11 = A.cx.x, a12 = A.cy.x, a21 = A.cx.y, a22 = A.cy.y;
Expand All @@ -319,6 +333,21 @@ static inline b2Vec2 b2Solve22(b2Mat22 A, b2Vec2 b)
return x;
}

/// Solve A * x = b, where b is a column vector.
static inline b2Vec3 b2Solve33(b2Mat33 A, b2Vec3 b)
{
float det = b2Dot3(A.cx, b2Cross3(A.cy, A.cz));
if (det != 0.0f)
{
det = 1.0f / det;
}
b2Vec3 x;
x.x = det * b2Dot3(b, b2Cross3(A.cy, A.cz));
x.y = det * b2Dot3(A.cx, b2Cross3(b, A.cz));
x.z = det * b2Dot3(A.cx, b2Cross3(A.cy, b));
return x;
}

#ifdef __cplusplus
}
#endif
13 changes: 13 additions & 0 deletions include/box2d/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ typedef struct b2Vec2
float x, y;
} b2Vec2;

/// 3D vector
typedef struct b2Vec3
{
float x, y, z;
} b2Vec3;

/// 2D rotation
typedef struct b2Rot
{
Expand All @@ -48,6 +54,13 @@ typedef struct b2Mat22
b2Vec2 cx, cy;
} b2Mat22;

/// A 3-by-3 Matrix
typedef struct b2Mat33
{
/// columns
b2Vec3 cx, cy, cz;
} b2Mat33;

/// Axis-aligned bounding box
typedef struct b2AABB
{
Expand Down
2 changes: 1 addition & 1 deletion samples/collection/behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class OverlapRecovery : public Sample

b2Polygon box = b2MakeBox(extent, extent);

int count = 4;
int count = 2;
float fraction = 0.75f;
float y = fraction * extent;
while (count > 0)
Expand Down
Loading

0 comments on commit 9a212e3

Please sign in to comment.