-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDrop.cs
More file actions
354 lines (278 loc) · 9.5 KB
/
Drop.cs
File metadata and controls
354 lines (278 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System.Collections.Generic;
using Box2D.NET.Shared;
using Silk.NET.GLFW;
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.B2Worlds;
using static Box2D.NET.Shared.Humans;
namespace Box2D.NET.Samples.Samples.Continuous;
public class Drop : Sample
{
private static readonly int SampleDrop = SampleFactory.Shared.RegisterSample("Continuous", "Drop", Create);
private List<B2BodyId> m_groundIds = new List<B2BodyId>();
private List<B2BodyId> m_bodyIds = new List<B2BodyId>();
private Human m_human;
private int m_frameSkip;
private int m_frameCount;
private bool m_continuous;
private bool m_speculative;
private static Sample Create(SampleContext context)
{
return new Drop(context);
}
public Drop(SampleContext context) : base(context)
{
if (m_context.restart == false)
{
m_camera.center = new B2Vec2(0.0f, 1.5f);
m_camera.zoom = 3.0f;
m_context.enableSleep = false;
m_context.debugDraw.drawJoints = false;
}
#if FALSE
{
b2BodyDef bodyDef = b2DefaultBodyDef();
b2BodyId groundId = b2CreateBody( m_worldId, &bodyDef );
b2ShapeDef shapeDef = b2DefaultShapeDef();
float w = 0.25f;
int count = 40;
float x = -0.5f * count * w;
float h = 0.05f;
for ( int j = 0; j <= count; ++j )
{
b2Polygon box = b2MakeOffsetBox( w, h, { x, -h }, b2Rot_identity );
b2CreatePolygonShape( groundId, &shapeDef, &box );
x += w;
}
}
#endif
m_human = new Human();
m_frameSkip = 0;
m_frameCount = 0;
m_continuous = true;
m_speculative = true;
Scene1();
}
void Clear()
{
for (int i = 0; i < m_bodyIds.Count; ++i)
{
b2DestroyBody(m_bodyIds[i]);
}
m_bodyIds.Clear();
if (m_human.isSpawned)
{
DestroyHuman(ref m_human);
}
}
void CreateGround1()
{
for (int i = 0; i < m_groundIds.Count; ++i)
{
b2DestroyBody(m_groundIds[i]);
}
m_groundIds.Clear();
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
float w = 0.25f;
int count = 40;
B2Segment segment = new B2Segment(new B2Vec2(-0.5f * count * w, 0.0f), new B2Vec2(0.5f * count * w, 0.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
m_groundIds.Add(groundId);
}
void CreateGround2()
{
for (int i = 0; i < m_groundIds.Count; ++i)
{
b2DestroyBody(m_groundIds[i]);
}
m_groundIds.Clear();
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
float w = 0.25f;
int count = 40;
float x = -0.5f * count * w;
float h = 0.05f;
for (int j = 0; j <= count; ++j)
{
B2Polygon box = b2MakeOffsetBox(0.5f * w, h, new B2Vec2(x, 0.0f), b2Rot_identity);
b2CreatePolygonShape(groundId, shapeDef, box);
x += w;
}
m_groundIds.Add(groundId);
}
void CreateGround3()
{
for (int i = 0; i < m_groundIds.Count; ++i)
{
b2DestroyBody(m_groundIds[i]);
}
m_groundIds.Clear();
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
float w = 0.25f;
int count = 40;
B2Segment segment = new B2Segment(new B2Vec2(-0.5f * count * w, 0.0f), new B2Vec2(0.5f * count * w, 0.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
segment = new B2Segment(new B2Vec2(3.0f, 0.0f), new B2Vec2(3.0f, 8.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
m_groundIds.Add(groundId);
}
// ball
void Scene1()
{
Clear();
CreateGround2();
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(0.0f, 4.0f);
bodyDef.linearVelocity = new B2Vec2(0.0f, -100.0f);
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.125f);
b2CreateCircleShape(bodyId, shapeDef, circle);
m_bodyIds.Add(bodyId);
m_frameCount = 1;
}
// ruler
void Scene2()
{
Clear();
CreateGround1();
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(0.0f, 4.0f);
bodyDef.rotation = b2MakeRot(0.5f * B2_PI);
bodyDef.linearVelocity = new B2Vec2(0.0f, 0.0f);
bodyDef.angularVelocity = -0.5f;
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
B2Polygon box = b2MakeBox(0.75f, 0.01f);
b2CreatePolygonShape(bodyId, shapeDef, box);
m_bodyIds.Add(bodyId);
m_frameCount = 1;
}
// ragdoll
void Scene3()
{
Clear();
CreateGround2();
float jointFrictionTorque = 0.03f;
float jointHertz = 1.0f;
float jointDampingRatio = 0.5f;
CreateHuman(ref m_human, m_worldId, new B2Vec2(0.0f, 40.0f), 1.0f, jointFrictionTorque, jointHertz, jointDampingRatio, 1, B2UserData.Empty, true);
m_frameCount = 1;
}
void Scene4()
{
Clear();
CreateGround3();
float a = 0.25f;
B2Polygon box = b2MakeSquare(a);
B2ShapeDef shapeDef = b2DefaultShapeDef();
float offset = 0.01f;
for (int i = 0; i < 5; ++i)
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
float shift = (i % 2 == 0 ? -offset : offset);
bodyDef.position = new B2Vec2(2.5f + shift, a + 2.0f * a * i);
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
m_bodyIds.Add(bodyId);
b2CreatePolygonShape(bodyId, shapeDef, box);
}
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.125f);
shapeDef.density = 4.0f;
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(-7.7f, 1.9f);
bodyDef.linearVelocity = new B2Vec2(200.0f, 0.0f);
bodyDef.isBullet = true;
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
b2CreateCircleShape(bodyId, shapeDef, circle);
m_bodyIds.Add(bodyId);
}
m_frameCount = 1;
}
public override void Keyboard(Keys key)
{
switch ((Keys)key)
{
case Keys.Keypad1:
Scene1();
break;
case Keys.Keypad2:
Scene2();
break;
case Keys.Keypad3:
Scene3();
break;
case Keys.Keypad4:
Scene4();
break;
case Keys.Keypad5:
Clear();
m_continuous = !m_continuous;
break;
case Keys.V:
Clear();
m_speculative = !m_speculative;
b2World_EnableSpeculative(m_worldId, m_speculative);
break;
case Keys.S:
m_frameSkip = m_frameSkip > 0 ? 0 : 60;
break;
default:
base.Keyboard(key);
break;
}
}
public override void Step()
{
#if FALSE
ImGui.SetNextWindowPos( new Vector2( 0.0f, 0.0f ) );
ImGui.SetNextWindowSize( new Vector2( float( m_context.g_camera.m_width ), float( m_context.g_camera.m_height ) ) );
ImGui.SetNextWindowBgAlpha( 0.0f );
ImGui.Begin( "DropBackground", nullptr,
ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.AlwaysAutoResize |
ImGuiWindowFlags.NoScrollbar );
ImDrawList* drawList = ImGui.GetWindowDrawList();
string ContinuousText = m_continuous && m_speculative ? "Continuous ON" : "Continuous OFF";
drawList->AddText( m_context.g_draw.m_largeFont, m_context.g_draw.m_largeFont->FontSize, { 40.0f, 40.0f }, IM_COL32_WHITE, ContinuousText );
if ( m_frameSkip > 0 )
{
drawList->AddText( m_context.g_draw.m_mediumFont, m_context.g_draw.m_mediumFont->FontSize, { 40.0f, 40.0f + 64.0f + 20.0f },
IM_COL32( 200, 200, 200, 255 ), "Slow Time" );
}
ImGui.End();
#endif
//if (m_frameCount == 165)
//{
// m_context.pause = true;
// m_frameSkip = 30;
//}
m_context.enableContinuous = m_continuous;
if ((m_frameSkip == 0 || m_frameCount % m_frameSkip == 0) && m_context.pause == false)
{
base.Step();
}
else
{
bool pause = m_context.pause;
m_context.pause = true;
base.Step();
m_context.pause = pause;
}
m_frameCount += 1;
}
}