diff --git a/include/box3d/box3d.h b/include/box3d/box3d.h index e66fc98..d9f5fdf 100644 --- a/include/box3d/box3d.h +++ b/include/box3d/box3d.h @@ -1482,6 +1482,12 @@ B3_API void b3PogoJoint_SetMaxCompressionForce( b3JointId jointId, float force ) /// Get the max compression force, usually in Newtons. B3_API float b3PogoJoint_GetMaxCompressionForce( b3JointId jointId ); +// Set the unit ground normal. +B3_API void b3PogoJoint_SetNormal( b3JointId jointId, b3Vec3 normal ); + +// Get the unit ground normal. +B3_API b3Vec3 b3PogoJoint_GetNormal( b3JointId jointId ); + /// Get the current length of the pogo. B3_API float b3PogoJoint_GetCurrentLength( b3JointId jointId ); diff --git a/samples/dynamic_mover.cpp b/samples/dynamic_mover.cpp index dd1e9a1..eeeebd7 100644 --- a/samples/dynamic_mover.cpp +++ b/samples/dynamic_mover.cpp @@ -370,9 +370,16 @@ void DynamicMover::Update( float timeStep, b3Vec3 forward, b3Vec3 right, b3Vec2 baseId = m_castResult.bodyId; } - if ( B3_ID_EQUALS( b3Joint_GetBodyA( m_moverJointId ), baseId ) == false ) + // The mover joint can be destroyed if attached to a platform that gets destroyed. + bool moverJointValid = b3Joint_IsValid( m_moverJointId ); + + if ( moverJointValid == false || B3_ID_EQUALS( b3Joint_GetBodyA( m_moverJointId ), baseId ) == false ) { - b3DestroyJoint( m_moverJointId, false ); + if (moverJointValid) + { + b3DestroyJoint( m_moverJointId, false ); + } + CreateMoverJoint( baseId ); } @@ -450,17 +457,6 @@ void DynamicMover::Update( float timeStep, b3Vec3 forward, b3Vec3 right, b3Vec2 m_velocity.z += accelSpeed * desiredDirection.z; } - // The pogo joint is rebuilt every update because it may land on a different body - if ( b3Joint_IsValid( m_pogoJointId ) ) - { - m_pogoLength = b3PogoJoint_GetCurrentLength( m_pogoJointId ); - m_pogoImpulse = b3PogoJoint_GetImpulse( m_pogoJointId ); - m_pogoVelocity = b3PogoJoint_GetCurrentSpeed( m_pogoJointId ); - - b3DestroyJoint( m_pogoJointId, false ); - m_pogoJointId = b3_nullJointId; - } - if ( m_castResult.hit == true ) { float moverMass = b3Body_GetMass( m_moverId ); @@ -523,6 +519,7 @@ void DynamicMover::Update( float timeStep, b3Vec3 forward, b3Vec3 right, b3Vec2 localFrameA.p = b3Body_GetLocalPoint( m_surfaceBodyId, m_castResult.point ); localFrameA.q = b3Quat_identity; b3Joint_SetLocalFrameA( m_pogoJointId, localFrameA ); + b3PogoJoint_SetNormal( m_pogoJointId, m_castResult.normal ); if ( m_jumping ) { @@ -539,7 +536,7 @@ void DynamicMover::Update( float timeStep, b3Vec3 forward, b3Vec3 right, b3Vec2 m_pogoCompressionScale * moverWeight ); b3PogoJoint_SetSpringHertz( m_pogoJointId, m_pogoHertz ); - b3PogoJoint_SetSpringDampingRatio( m_pogoJointId, m_pogoHertz ); + b3PogoJoint_SetSpringDampingRatio( m_pogoJointId, m_pogoDampingRatio ); } } else diff --git a/samples/geometric_mover.cpp b/samples/geometric_mover.cpp index 1e78261..69f159a 100644 --- a/samples/geometric_mover.cpp +++ b/samples/geometric_mover.cpp @@ -279,11 +279,11 @@ void GeometricMover::Update( float timeStep, b3Vec3 forward, b3Vec3 right, b3Vec // Want the mover to collide with allies b3QueryFilter moverFilter = { - .categoryBits = 1, .maskBits = ~0u, .id = 1, .name = "mover_collide" }; + .categoryBits = 1, .maskBits = ~0ull, .id = 1, .name = "mover_collide" }; // The cast should ignore allies b3QueryFilter castFilter = { - .categoryBits = 1, .maskBits = ~2u, .id = 1, .name = "mover_cast" }; + .categoryBits = 1, .maskBits = ~2ull, .id = 1, .name = "mover_cast" }; m_totalIterations = 0; float tolerance = 0.01f; diff --git a/src/mover_joint.c b/src/mover_joint.c index a8c9f7f..aa30049 100644 --- a/src/mover_joint.c +++ b/src/mover_joint.c @@ -242,7 +242,7 @@ void b3DrawMoverJoint( b3DebugDraw* draw, b3World* world, b3JointSim* base, b3Vec3 vA = stateA != NULL ? stateA->linearVelocity : b3Vec3_zero; b3Vec3 vB = stateB != NULL ? stateB->linearVelocity : b3Vec3_zero; - b3Vec3 relativeVelocity = b3Sub( b3Sub( vB, vA ), joint->linearVelocity ); + b3Vec3 relativeVelocity = b3Sub( vB, vA ); b3Vec2 planarVelocity = { b3Dot( relativeVelocity, axisX ), b3Dot( relativeVelocity, axisZ ) }; b3Vec2 targetVelocity = { b3Dot( joint->linearVelocity, axisX ), b3Dot( joint->linearVelocity, axisZ ) }; diff --git a/src/pogo_joint.c b/src/pogo_joint.c index 3bec87a..b0bc495 100644 --- a/src/pogo_joint.c +++ b/src/pogo_joint.c @@ -90,6 +90,21 @@ float b3PogoJoint_GetMaxCompressionForce(b3JointId jointId) return joint->pogoJoint.maxCompressionForce; } +void b3PogoJoint_SetNormal( b3JointId jointId, b3Vec3 normal ) +{ + B3_ASSERT( b3IsValidVec3( normal ) && b3IsNormalized( normal ) ); + b3World* world = b3GetWorld( jointId.world0 ); + B3_REC( world, PogoJointSetNormal, jointId, normal ); + b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint ); + joint->pogoJoint.normal = normal; +} + +b3Vec3 b3PogoJoint_GetNormal( b3JointId jointId ) +{ + b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint ); + return joint->pogoJoint.normal; +} + float b3PogoJoint_GetCurrentLength( b3JointId jointId ) { b3World* world = b3GetWorld( jointId.world0 ); @@ -245,6 +260,7 @@ void b3SolvePogoJoint( b3JointSim* base, b3StepContext* context, bool useBias ) if ( joint->hertz == 0.0f ) { joint->impulse = 0.0f; + joint->velocity = 0.0f; return; } diff --git a/src/recording_ops.inl b/src/recording_ops.inl index d5c82f2..df13ebe 100644 --- a/src/recording_ops.inl +++ b/src/recording_ops.inl @@ -224,6 +224,7 @@ B3_REC_OP( 0x86, PogoJointSetSpringHertz, RET_NONE, ARG( JOINTID, joint ) ARG( F B3_REC_OP( 0x87, PogoJointSetSpringDampingRatio, RET_NONE, ARG( JOINTID, joint ) ARG( F32, dampingRatio ) ) B3_REC_OP( 0x88, PogoJointSetMaxTensionForce, RET_NONE, ARG( JOINTID, joint ) ARG( F32, force ) ) B3_REC_OP( 0x89, PogoJointSetMaxCompressionForce, RET_NONE, ARG( JOINTID, joint ) ARG( F32, force ) ) +B3_REC_OP( 0x8A, PogoJointSetNormal, RET_NONE, ARG( JOINTID, joint ) ARG( VEC3, normal ) ) // Spatial queries. Inputs flow through the manifest (reader side). The hit tail and result are // hand-written in recording.c / recording_replay.c since they are variable length. diff --git a/src/replay.c b/src/replay.c index 0d77d47..78b73df 100644 --- a/src/replay.c +++ b/src/replay.c @@ -1752,6 +1752,11 @@ static void b3RecDispatch_PogoJointSetMaxCompressionForce( b3PogoJoint_SetMaxCompressionForce( b3RecMakeJointId( rdr, a->joint ), a->force ); } +static void b3RecDispatch_PogoJointSetNormal( const b3RecArgs_PogoJointSetNormal* a, b3RecReader* rdr ) +{ + b3PogoJoint_SetNormal( b3RecMakeJointId( rdr, a->joint ), a->normal ); +} + static void b3RecDispatch_StateHash( const b3RecArgs_StateHash* a, b3RecReader* rdr ) { b3World* world = b3GetWorldFromId( rdr->replayWorldId ); diff --git a/src/types.c b/src/types.c index 9caab0a..f5e1ad3 100644 --- a/src/types.c +++ b/src/types.c @@ -153,7 +153,7 @@ b3DebugDraw b3DefaultDebugDraw( void ) }; draw.jointScale = 1.0f; - draw.forceScale = 1.0f; + draw.forceScale = 0.001f; return draw; } diff --git a/test/test_joint.c b/test/test_joint.c index 13ecd22..103e6d7 100644 --- a/test/test_joint.c +++ b/test/test_joint.c @@ -372,6 +372,20 @@ static int TestPogoJoint( void ) b3PogoJoint_SetSpringDampingRatio( jointId, 0.9f ); ENSURE( b3PogoJoint_GetSpringDampingRatio( jointId ) == 0.9f ); + float maxTension = 50.0f * 10.0f * mass; + b3PogoJoint_SetMaxTensionForce( jointId, maxTension ); + ENSURE( b3PogoJoint_GetMaxTensionForce( jointId ) == maxTension ); + + float maxCompression = 60.0f * 10.0f * mass; + b3PogoJoint_SetMaxCompressionForce( jointId, maxCompression ); + ENSURE( b3PogoJoint_GetMaxCompressionForce( jointId ) == maxCompression ); + + b3Vec3 normal = b3Normalize( (b3Vec3){ 0.2f, 1.0f, 0.0f } ); + b3PogoJoint_SetNormal( jointId, normal ); + b3Vec3 gotNormal = b3PogoJoint_GetNormal( jointId ); + ENSURE( gotNormal.x == normal.x && gotNormal.y == normal.y && gotNormal.z == normal.z ); + b3PogoJoint_SetNormal( jointId, b3Vec3_axisY ); + // Starts stretched by one meter ENSURE_SMALL( b3PogoJoint_GetCurrentLength( jointId ) - 3.0f, 1.0e-5f ); diff --git a/test/test_recording.c b/test/test_recording.c index 9c34b54..46f1e30 100644 --- a/test/test_recording.c +++ b/test/test_recording.c @@ -1630,6 +1630,9 @@ static int AllOps( void ) b3PogoJoint_SetRestLength( pogoJointId, 0.9f ); b3PogoJoint_SetSpringHertz( pogoJointId, 4.0f ); b3PogoJoint_SetSpringDampingRatio( pogoJointId, 0.7f ); + b3PogoJoint_SetNormal( pogoJointId, b3Normalize( (b3Vec3){ 0.2f, 1.0f, 0.0f } ) ); + b3PogoJoint_SetMaxTensionForce( pogoJointId, 800.0f ); + b3PogoJoint_SetMaxCompressionForce( pogoJointId, 900.0f ); // World config mutators b3World_SetGravity( worldId, (b3Vec3){ 0.0f, -9.8f, 0.0f } );