Skip to content

Commit d1ce06b

Browse files
authored
Wind (#980)
- added b2Shape_ApplyWind and sample - added b2Body_WakeTouching and sample - b2DestroyJoint now takes a flag to wake the connected bodies - fixed bug in b2HashSet and b2SetItem no longer holds a copy of the hash - added more samples and unit tests #978
1 parent 5e6cedd commit d1ce06b

26 files changed

+1074
-83
lines changed

benchmark/amd7950x_sse2/washer.csv

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
threads,ms
2-
1,8001.88
3-
2,4492.59
4-
3,3213.91
5-
4,2543.84
6-
5,2112.88
7-
6,1832.15
8-
7,1641.22
9-
8,1498.33
2+
1,7934.7
3+
2,4531.48
4+
3,3218.58
5+
4,2523.46
6+
5,2100.84
7+
6,1842.67
8+
7,1638.05
9+
8,1529.17

benchmark/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static void MinProfile( b2Profile* p1, const b2Profile* p2 )
140140

141141
// Run benchmark 3 with 4 workers and repeat 20 times. Record the step times.
142142
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=3 -r=20 -s
143-
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=8 -b=5
143+
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=8 -b=7
144144

145145
// Run benchmark 3 with 4 workers and run once. Disable continuous collision. Record the step times.
146146
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=3 -r=1 -nc -s

include/box2d/box2d.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ B2_API bool b2Body_IsAwake( b2BodyId bodyId );
397397
/// which can be expensive and possibly unintuitive.
398398
B2_API void b2Body_SetAwake( b2BodyId bodyId, bool awake );
399399

400+
/// Wake bodies touching this body. Works for static bodies.
401+
B2_API void b2Body_WakeTouching( b2BodyId bodyId );
402+
400403
/// Enable or disable sleeping for this body. If sleeping is disabled the body will wake.
401404
B2_API void b2Body_EnableSleep( b2BodyId bodyId, bool enableSleep );
402405

@@ -677,6 +680,8 @@ B2_API b2MassData b2Shape_ComputeMassData( b2ShapeId shapeId );
677680
/// todo need sample
678681
B2_API b2Vec2 b2Shape_GetClosestPoint( b2ShapeId shapeId, b2Vec2 target );
679682

683+
B2_API void b2Shape_ApplyWindForce( b2ShapeId shapeId, b2Vec2 wind, float drag, float lift, bool wake );
684+
680685
/// Chain Shape
681686

682687
/// Create a chain shape
@@ -717,8 +722,8 @@ B2_API bool b2Chain_IsValid( b2ChainId id );
717722
* @{
718723
*/
719724

720-
/// Destroy a joint
721-
B2_API void b2DestroyJoint( b2JointId jointId );
725+
/// Destroy a joint. Optionally wake attached bodies.
726+
B2_API void b2DestroyJoint( b2JointId jointId, bool wakeAttached );
722727

723728
/// Joint identifier validation. Provides validation for up to 64K allocations.
724729
B2_API bool b2Joint_IsValid( b2JointId id );

samples/car.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ void Car::Despawn()
112112
{
113113
assert( m_isSpawned == true );
114114

115-
b2DestroyJoint( m_rearAxleId );
116-
b2DestroyJoint( m_frontAxleId );
115+
b2DestroyJoint( m_rearAxleId, false );
116+
b2DestroyJoint( m_frontAxleId, false );
117117
b2DestroyBody( m_rearWheelId );
118118
b2DestroyBody( m_frontWheelId );
119119
b2DestroyBody( m_chassisId );
@@ -261,8 +261,8 @@ void Truck::Despawn()
261261
{
262262
assert( m_isSpawned == true );
263263

264-
b2DestroyJoint( m_rearAxleId );
265-
b2DestroyJoint( m_frontAxleId );
264+
b2DestroyJoint( m_rearAxleId, false );
265+
b2DestroyJoint( m_frontAxleId, false );
266266
b2DestroyBody( m_rearWheelId );
267267
b2DestroyBody( m_frontWheelId );
268268
b2DestroyBody( m_chassisId );

samples/doohickey.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ void Doohickey::Despawn()
8989
{
9090
assert( m_isSpawned == true );
9191

92-
b2DestroyJoint( m_axleId1 );
93-
b2DestroyJoint( m_axleId2 );
94-
b2DestroyJoint( m_sliderId );
92+
b2DestroyJoint( m_axleId1, false );
93+
b2DestroyJoint( m_axleId2, false );
94+
b2DestroyJoint( m_sliderId, false );
9595

9696
b2DestroyBody( m_wheelId1 );
9797
b2DestroyBody( m_wheelId2 );

samples/sample.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ void Sample::MouseDown( b2Vec2 p, int button, int mod )
355355
{
356356
// This acts like angular friction
357357
float lever = sqrtf( massData.rotationalInertia / massData.mass );
358-
jointDef.maxVelocityTorque = 1.0f * lever * mg;
358+
jointDef.maxVelocityTorque = 0.25f * lever * mg;
359359
}
360360

361361
m_mouseJointId = b2CreateMotorJoint( m_worldId, &jointDef );
@@ -367,7 +367,7 @@ void Sample::MouseUp( b2Vec2 p, int button )
367367
{
368368
if ( B2_IS_NON_NULL( m_mouseJointId ) && button == GLFW_MOUSE_BUTTON_1 )
369369
{
370-
b2DestroyJoint( m_mouseJointId );
370+
b2DestroyJoint( m_mouseJointId, true );
371371
m_mouseJointId = b2_nullJointId;
372372

373373
b2DestroyBody( m_mouseBodyId );

samples/sample_benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ class BenchmarkSleep : public Sample
935935
uint64_t ticks = b2GetTicks();
936936

937937
// This will wake the island
938-
b2DestroyJoint( jointId );
938+
b2DestroyJoint( jointId, true );
939939
m_wakeTotal += b2GetMillisecondsAndReset( &ticks );
940940

941941
// Put the island back to sleep. It must be split because a constraint was removed.

samples/sample_bodies.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,3 +1040,72 @@ class SetVelocity : public Sample
10401040
};
10411041

10421042
static int sampleSetVelocity = RegisterSample( "Bodies", "Set Velocity", SetVelocity::Create );
1043+
1044+
class WakeTouching : public Sample
1045+
{
1046+
public:
1047+
explicit WakeTouching( SampleContext* context )
1048+
: Sample( context )
1049+
{
1050+
if ( m_context->restart == false )
1051+
{
1052+
m_context->camera.m_center = { 0.0f, 4.0f };
1053+
m_context->camera.m_zoom = 8.0f;
1054+
}
1055+
1056+
{
1057+
b2BodyDef bodyDef = b2DefaultBodyDef();
1058+
m_groundId = b2CreateBody( m_worldId, &bodyDef );
1059+
1060+
b2Segment segment = { { -20.0f, 0.0f }, { 20.0f, 0.0f } };
1061+
b2ShapeDef shapeDef = b2DefaultShapeDef();
1062+
b2CreateSegmentShape( m_groundId, &shapeDef, &segment );
1063+
}
1064+
1065+
b2Polygon box = b2MakeBox( 0.5f, 0.5f );
1066+
1067+
b2ShapeDef shapeDef = b2DefaultShapeDef();
1068+
shapeDef.density = 1.0f;
1069+
1070+
b2BodyDef bodyDef = b2DefaultBodyDef();
1071+
bodyDef.type = b2_dynamicBody;
1072+
1073+
float x = -1.0f * ( m_count - 1 );
1074+
1075+
for ( int i = 0; i < m_count; ++i )
1076+
{
1077+
bodyDef.position = { x, 4.0f };
1078+
b2BodyId bodyId = b2CreateBody( m_worldId, &bodyDef );
1079+
b2CreatePolygonShape( bodyId, &shapeDef, &box );
1080+
x += 2.0f;
1081+
}
1082+
}
1083+
1084+
void UpdateGui() override
1085+
{
1086+
float fontSize = ImGui::GetFontSize();
1087+
float height = 5.0f * fontSize;
1088+
ImGui::SetNextWindowPos( ImVec2( 0.5f * fontSize, m_camera->m_height - height - 2.0f * fontSize ), ImGuiCond_Once );
1089+
ImGui::SetNextWindowSize( ImVec2( 10.0f * fontSize, height ) );
1090+
1091+
ImGui::Begin( "Wake Touching", nullptr, ImGuiWindowFlags_NoResize );
1092+
1093+
if ( ImGui::Button( "Wake Touching" ) )
1094+
{
1095+
b2Body_WakeTouching( m_groundId );
1096+
}
1097+
1098+
ImGui::End();
1099+
}
1100+
1101+
static Sample* Create( SampleContext* context )
1102+
{
1103+
return new WakeTouching( context );
1104+
}
1105+
1106+
static constexpr int m_count = 10;
1107+
1108+
b2BodyId m_groundId;
1109+
};
1110+
1111+
static int sampleWakeTouching = RegisterSample( "Bodies", "Wake Touching", WakeTouching::Create );

samples/sample_determinism.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class FallingHinges : public Sample
4646
}
4747
else
4848
{
49-
DrawTextLine( "sleep step = %d, hash = 0x%08x", m_data.sleepStep, m_data.hash );
49+
DrawTextLine( "sleep step = %d, hash = 0x%08X", m_data.sleepStep, m_data.hash );
5050
}
5151
}
5252

samples/sample_events.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ class JointEvent : public Sample
20562056
{
20572057
int index = (int)(intptr_t)event->userData;
20582058
assert( 0 <= index && index < e_count );
2059-
b2DestroyJoint( event->jointId );
2059+
b2DestroyJoint( event->jointId, true );
20602060
m_jointIds[index] = b2_nullJointId;
20612061
}
20622062
}

0 commit comments

Comments
 (0)