Skip to content

Commit

Permalink
null joint sample
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Oct 18, 2024
1 parent ed78f91 commit f0849d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions samples/sample_joints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,59 @@ class MotorJoint : public Sample

static int sampleMotorJoint = RegisterSample( "Joints", "Motor Joint", MotorJoint::Create );

// This sample shows how to use a null joint to prevent collision between two bodies.
// This is more specific than filters. It also shows that sleeping is coupled by the null joint.
class NullJoint : public Sample
{
public:
explicit NullJoint( Settings& settings )
: Sample( settings )
{
if ( settings.restart == false )
{
g_camera.m_center = { 0.0f, 7.0f };
g_camera.m_zoom = 25.0f * 0.4f;
}

{
b2BodyId groundId;
b2BodyDef bodyDef = b2DefaultBodyDef();
groundId = b2CreateBody( m_worldId, &bodyDef );
b2ShapeDef shapeDef = b2DefaultShapeDef();
b2Segment segment = { { -20.0f, 0.0f }, { 20.0f, 0.0f } };
b2CreateSegmentShape( groundId, &shapeDef, &segment );
}

{
b2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = b2_dynamicBody;
bodyDef.position = { -4.0f, 2.0f };
b2BodyId bodyId1 = b2CreateBody( m_worldId, &bodyDef );

b2Polygon box = b2MakeSquare( 2.0f );
b2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape( bodyId1, &shapeDef, &box );

bodyDef.position = { 4.0f, 2.0f };
b2BodyId bodyId2 = b2CreateBody( m_worldId, &bodyDef );
b2CreatePolygonShape( bodyId2, &shapeDef, &box );

b2NullJointDef jointDef = b2DefaultNullJointDef();
jointDef.bodyIdA = bodyId1;
jointDef.bodyIdB = bodyId2;

b2CreateNullJoint( m_worldId, &jointDef );
}
}

static Sample* Create( Settings& settings )
{
return new NullJoint( settings );
}
};

static int sampleNullJoint = RegisterSample( "Joints", "Null Joint", NullJoint::Create );

class RevoluteJoint : public Sample
{
public:
Expand Down
2 changes: 1 addition & 1 deletion src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ extern float b2_lengthUnitsPerMeter;
// to move by a small amount without triggering a tree adjustment.
// This is in meters.
// @warning modifying this can have a significant impact on performance
#define b2_aabbMargin ( 0.02f * b2_lengthUnitsPerMeter )
#define b2_aabbMargin ( 0.1f * b2_lengthUnitsPerMeter )

// todo testing
#define b2_aabbVelocityScale 0.0f
Expand Down

0 comments on commit f0849d1

Please sign in to comment.