@@ -26,7 +26,10 @@ public class Bridge : Sample
2626 private B2BodyId [ ] m_bodyIds = new B2BodyId [ m_count ] ;
2727 private B2JointId [ ] m_jointIds = new B2JointId [ m_count + 1 ] ;
2828 private float m_frictionTorque ;
29- private float m_gravityScale ;
29+ private float m_constraintHertz ;
30+ private float m_constraintDampingRatio ;
31+ private float m_springHertz ;
32+ private float m_springDampingRatio ;
3033
3134 private static Sample Create ( SampleContext context )
3235 {
@@ -48,15 +51,25 @@ public Bridge(SampleContext context) : base(context)
4851 }
4952
5053 {
51- B2Polygon box = b2MakeBox ( 0.5f , 0.125f ) ;
54+ m_constraintHertz = 60.0f ;
55+ m_constraintDampingRatio = 0.0f ;
56+ m_springHertz = 2.0f ;
57+ m_springDampingRatio = 0.7f ;
58+ m_frictionTorque = 200.0f ;
59+
60+ B2Polygon box = b2MakeBox ( 0.5f , 0.125f ) ;
5261
5362 B2ShapeDef shapeDef = b2DefaultShapeDef ( ) ;
5463 shapeDef . density = 20.0f ;
5564
5665 B2RevoluteJointDef jointDef = b2DefaultRevoluteJointDef ( ) ;
66+ jointDef . enableMotor = true ;
67+ jointDef . maxMotorTorque = m_frictionTorque ;
68+ jointDef . enableSpring = true ;
69+ jointDef . hertz = m_springHertz ;
70+ jointDef . dampingRatio = m_springDampingRatio ;
71+
5772 int jointIndex = 0 ;
58- m_frictionTorque = 200.0f ;
59- m_gravityScale = 1.0f ;
6073
6174 float xbase = - 80.0f ;
6275
@@ -76,8 +89,6 @@ public Bridge(SampleContext context) : base(context)
7689 jointDef . bodyIdB = m_bodyIds [ i ] ;
7790 jointDef . localAnchorA = b2Body_GetLocalPoint ( jointDef . bodyIdA , pivot ) ;
7891 jointDef . localAnchorB = b2Body_GetLocalPoint ( jointDef . bodyIdB , pivot ) ;
79- jointDef . enableMotor = true ;
80- jointDef . maxMotorTorque = m_frictionTorque ;
8192 m_jointIds [ jointIndex ++ ] = b2CreateRevoluteJoint ( m_worldId , ref jointDef ) ;
8293
8394 prevBodyId = m_bodyIds [ i ] ;
@@ -89,8 +100,6 @@ public Bridge(SampleContext context) : base(context)
89100 jointDef . bodyIdB = groundId ;
90101 jointDef . localAnchorA = b2Body_GetLocalPoint ( jointDef . bodyIdA , pivot ) ;
91102 jointDef . localAnchorB = b2Body_GetLocalPoint ( jointDef . bodyIdB , pivot ) ;
92- jointDef . enableMotor = true ;
93- jointDef . maxMotorTorque = m_frictionTorque ;
94103 m_jointIds [ jointIndex ++ ] = b2CreateRevoluteJoint ( m_worldId , ref jointDef ) ;
95104
96105 B2_ASSERT ( jointIndex == m_count + 1 ) ;
@@ -133,15 +142,14 @@ public override void UpdateGui()
133142 {
134143 base . UpdateGui ( ) ;
135144
136- float height = 80 .0f;
145+ float height = 180 .0f;
137146 ImGui . SetNextWindowPos ( new Vector2 ( 10.0f , m_camera . m_height - height - 50.0f ) , ImGuiCond . Once ) ;
138- ImGui . SetNextWindowSize ( new Vector2 ( 240 .0f, height ) ) ;
147+ ImGui . SetNextWindowSize ( new Vector2 ( 320 .0f, height ) ) ;
139148
140149 ImGui . Begin ( "Bridge" , ImGuiWindowFlags . NoResize ) ;
141150
142- // Slider takes half the window
143- ImGui . PushItemWidth ( ImGui . GetWindowWidth ( ) * 0.5f ) ;
144- bool updateFriction = ImGui . SliderFloat ( "Joint Friction" , ref m_frictionTorque , 0.0f , 1000.0f , "%2.f" ) ;
151+ ImGui . PushItemWidth ( ImGui . GetWindowWidth ( ) * 0.6f ) ;
152+ bool updateFriction = ImGui . SliderFloat ( "Joint Friction" , ref m_frictionTorque , 0.0f , 10000.0f , "%2.f" ) ;
145153 if ( updateFriction )
146154 {
147155 for ( int i = 0 ; i <= m_count ; ++ i )
@@ -150,14 +158,40 @@ public override void UpdateGui()
150158 }
151159 }
152160
153- if ( ImGui . SliderFloat ( "Gravity scale ", ref m_gravityScale , - 1 .0f, 1 .0f, "%.1f" ) )
161+ if ( ImGui . SliderFloat ( "Spring hertz ", ref m_springHertz , 0 .0f, 30 .0f, "%.0f" ) )
154162 {
155- for ( int i = 0 ; i < m_count ; ++ i )
163+ for ( int i = 0 ; i <= m_count ; ++ i )
156164 {
157- b2Body_SetGravityScale ( m_bodyIds [ i ] , m_gravityScale ) ;
165+ b2RevoluteJoint_SetSpringHertz ( m_jointIds [ i ] , m_springHertz ) ;
158166 }
159167 }
160168
169+ if ( ImGui . SliderFloat ( "Spring damping" , ref m_springDampingRatio , 0.0f , 2.0f , "%.1f" ) )
170+ {
171+ for ( int i = 0 ; i <= m_count ; ++ i )
172+ {
173+ b2RevoluteJoint_SetSpringDampingRatio ( m_jointIds [ i ] , m_springDampingRatio ) ;
174+ }
175+ }
176+
177+ if ( ImGui . SliderFloat ( "Constraint hertz" , ref m_constraintHertz , 15.0f , 240.0f , "%.0f" ) )
178+ {
179+ for ( int i = 0 ; i <= m_count ; ++ i )
180+ {
181+ b2Joint_SetConstraintTuning ( m_jointIds [ i ] , m_constraintHertz , m_constraintDampingRatio ) ;
182+ }
183+ }
184+
185+ if ( ImGui . SliderFloat ( "Constraint damping" , ref m_constraintDampingRatio , 0.0f , 10.0f , "%.1f" ) )
186+ {
187+ for ( int i = 0 ; i <= m_count ; ++ i )
188+ {
189+ b2Joint_SetConstraintTuning ( m_jointIds [ i ] , m_constraintHertz , m_constraintDampingRatio ) ;
190+ }
191+ }
192+
193+ ImGui . PopItemWidth ( ) ;
194+
161195 ImGui . End ( ) ;
162196 }
163197}
0 commit comments