-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMIGRATION_GUIDE.html
More file actions
481 lines (422 loc) · 15.5 KB
/
MIGRATION_GUIDE.html
File metadata and controls
481 lines (422 loc) · 15.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Migration Guide - MFS Engine Documentation</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}
header {
text-align: center;
margin-bottom: 3rem;
color: white;
}
.logo {
font-size: 3rem;
font-weight: bold;
margin-bottom: 0.5rem;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.tagline {
font-size: 1.2rem;
opacity: 0.9;
margin-bottom: 1rem;
}
.badges {
display: flex;
justify-content: center;
gap: 0.5rem;
flex-wrap: wrap;
margin-bottom: 2rem;
}
.badge {
background: rgba(255,255,255,0.2);
padding: 0.3rem 0.8rem;
border-radius: 20px;
font-size: 0.9rem;
backdrop-filter: blur(10px);
}
.content {
background: white;
border-radius: 15px;
padding: 2rem;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}
.section {
margin-bottom: 2.5rem;
}
.section h2 {
color: #4a5568;
margin-bottom: 1rem;
font-size: 1.8rem;
border-bottom: 3px solid #667eea;
padding-bottom: 0.5rem;
}
.doc-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1.5rem;
margin-top: 1.5rem;
}
.doc-card {
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 10px;
padding: 1.5rem;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.doc-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #667eea, #764ba2);
}
.doc-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}
.doc-card h3 {
color: #2d3748;
margin-bottom: 0.5rem;
font-size: 1.3rem;
}
.doc-card p {
color: #718096;
margin-bottom: 1rem;
font-size: 0.95rem;
}
.doc-card a {
color: #667eea;
text-decoration: none;
font-weight: 500;
display: inline-flex;
align-items: center;
gap: 0.5rem;
}
.doc-card a:hover {
color: #764ba2;
}
.quick-start {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
border-radius: 15px;
padding: 2rem;
margin-bottom: 2rem;
}
.quick-start h2 {
color: white;
border-bottom: 3px solid rgba(255,255,255,0.3);
}
.code-block {
background: rgba(0,0,0,0.2);
border-radius: 8px;
padding: 1rem;
margin: 1rem 0;
font-family: 'Monaco', 'Consolas', monospace;
font-size: 0.9rem;
overflow-x: auto;
}
.features-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.feature {
background: rgba(255,255,255,0.1);
padding: 1rem;
border-radius: 8px;
backdrop-filter: blur(10px);
}
.feature h4 {
margin-bottom: 0.5rem;
font-size: 1.1rem;
}
.feature ul {
list-style: none;
font-size: 0.9rem;
opacity: 0.9;
}
.feature li:before {
content: "✓ ";
color: #4ade80;
font-weight: bold;
}
footer {
text-align: center;
margin-top: 3rem;
padding: 2rem;
color: rgba(255,255,255,0.8);
}
@media (max-width: 768px) {
.container {
padding: 1rem;
}
.logo {
font-size: 2rem;
}
.doc-grid {
grid-template-columns: 1fr;
}
}
/* Additional styles for markdown content */
h1, h2, h3, h4, h5, h6 {
color: #2d3748;
margin-top: 2rem;
margin-bottom: 1rem;
}
h1 {
font-size: 2.5rem;
border-bottom: 3px solid #667eea;
padding-bottom: 0.5rem;
}
h2 {
font-size: 2rem;
border-bottom: 2px solid #e2e8f0;
padding-bottom: 0.3rem;
}
h3 {
font-size: 1.5rem;
}
h4 {
font-size: 1.3rem;
}
p {
margin-bottom: 1rem;
line-height: 1.7;
}
ul, ol {
margin-bottom: 1rem;
padding-left: 2rem;
}
li {
margin-bottom: 0.5rem;
}
code {
background: #f7fafc;
padding: 0.2rem 0.4rem;
border-radius: 4px;
font-family: 'Monaco', 'Consolas', monospace;
font-size: 0.9rem;
color: #e53e3e;
}
pre {
background: #2d3748;
color: #e2e8f0;
padding: 1rem;
border-radius: 8px;
overflow-x: auto;
margin: 1rem 0;
font-family: 'Monaco', 'Consolas', monospace;
font-size: 0.9rem;
}
pre code {
background: none;
padding: 0;
color: inherit;
}
blockquote {
border-left: 4px solid #667eea;
padding-left: 1rem;
margin: 1rem 0;
color: #4a5568;
font-style: italic;
}
table {
width: 100%;
border-collapse: collapse;
margin: 1rem 0;
}
th, td {
border: 1px solid #e2e8f0;
padding: 0.75rem;
text-align: left;
}
th {
background: #f7fafc;
font-weight: 600;
}
.back-link {
display: inline-block;
margin-bottom: 2rem;
color: #667eea;
text-decoration: none;
font-weight: 500;
}
.back-link:hover {
color: #764ba2;
}
</style>
</head>
<body>
<div class="container">
<header>
<div class="logo">🎮 MFS Engine</div>
<div class="tagline">Multi-Platform Game Engine Documentation</div>
<div class="badges">
<span class="badge">Production Ready</span>
<span class="badge">Zig 0.12.0</span>
<span class="badge">Cross-Platform</span>
<span class="badge">Ray Tracing</span>
<span class="badge">Open Source</span>
</div>
</header>
<div class="content">
<a href="index.html" class="back-link">← Back to Documentation Index</a>
<div class="markdown-content">
<h1>Migration Guide: Enhanced Physics System</h1>
<p>This document explains how to migrate from the standard physics system to the enhanced physics system in the MFS engine.</p>
<h2>Why Migrate?</h2>
<p>The enhanced physics system provides several significant improvements:</p>
<ul>
<li>Full rigid body dynamics with proper rotational physics</li>
<li>Improved collision detection with spatial partitioning (O(n+m) instead of O(n²))</li>
<li>Enhanced constraint system with multiple constraint types</li>
<li>Better performance through object sleeping and optimized algorithms</li>
<li>More accurate collision response with contact point generation</li>
</ul>
<h2>Basic Migration Steps</h2>
<h3>1. Import the Enhanced Physics Module</h3>
<p>Replace your physics import with the improved version: <pre><code class="zig">// Old import
const physics = @import("physics/physics.zig");
// New import
const physics = @import("physics/physics_improved.zig");</code></pre></p>
<h3>2. Update World Initialization</h3>
<p>The world initialization remains similar, but you can take advantage of additional configuration options: <pre><code class="zig">// Create physics config with enhanced options
var config = physics.PhysicsConfig{
.gravity = physics.Vec3f{ 0, -9.81, 0, 0 },
.enable_sleeping = true, // Enable object sleeping
.collision_iterations = 3, // More iterations for better stability
.constraint_iterations = 6, // More constraint iterations
.spatial_cell_size = 2.0, // Cell size for spatial partitioning
};
// Initialize physics world
var world = try physics.World.init(allocator, config);
defer world.deinit();</code></pre></p>
<h3>3. Work with Rigid Bodies</h3>
<p>Use the dedicated rigid body creation functions: <pre><code class="zig">// Create a box rigid body
const box_idx = try world.createRigidBodyBox(
physics.Vec3f{ 0, 5, 0, 0 }, // position
physics.Vec3f{ 1.0, 1.0, 1.0, 0.0 }, // size
2.0 // mass
);
// Create a sphere rigid body
const sphere_idx = try world.createRigidBodySphere(
physics.Vec3f{ 2, 5, 0, 0 }, // position
1.0, // radius
1.5 // mass
);</code></pre></p>
<h3>4. Use Advanced Constraints</h3>
<p>The enhanced system provides a constraint manager with multiple constraint types: <pre><code class="zig">// Access the constraint manager (it's an optional, so use if check)
if (world.constraint_manager) |*cm| {
// Create a distance constraint
const distance_constraint = try cm.addDistance(.{
.object_index_a = object_a_idx,
.object_index_b = object_b_idx,
.distance = 2.0,
.compliance = 0.0001,
});
// Create a position constraint
const position_constraint = try cm.addPosition(.{
.object_index = object_idx,
.target_position = physics.Vec3f{ 0, 10, 0, 0 },
.stiffness = 0.5,
});
// Create an angle constraint
const angle_constraint = try cm.addAngle(.{
.object_index_a = object_a_idx,
.object_index_b = object_b_idx,
.target_angle = 0.5,
.stiffness = 0.2,
});
}</code></pre></p>
<h3>5. Apply Forces to Rigid Bodies</h3>
<p>For advanced force application to rigid bodies: <pre><code class="zig">// Get access to the rigid body manager
if (world.rigid_body_manager) |*rbm| {
// Get a rigid body by object index
if (rbm.getRigidBody(object_idx)) |rb| {
// Apply force at a specific point
rb.applyForceAtPoint(
physics.Vec3f{ 10, 0, 0, 0 }, // force direction & magnitude
physics.Vec3f{ 0, 1, 0, 0 }, // point of application (relative to center)
&world.objects[object_idx] // reference to the physical object
);
}
}</code></pre></p>
<h3>6. Collision Filtering</h3>
<p>The enhanced system supports collision filtering with groups and masks: <pre><code class="zig">// Set collision group and mask (bitfields)
world.objects[object_idx].collision_group = 0x01; // Group 1
world.objects[object_idx].collision_mask = 0x02; // Can collide with group 2
// Another object that can collide with the above
world.objects[other_idx].collision_group = 0x02; // Group 2
world.objects[other_idx].collision_mask = 0x01; // Can collide with group 1</code></pre></p>
<h3>7. Update and Simulation Loop</h3>
<p>The update function remains the same: <pre><code class="zig">// Main game loop
while (running) {
// Calculate delta time
const dt = timer.lap();
// Update physics
world.update(dt);
// Get physics stats for monitoring
const stats = world.getPerformanceStats();
}</code></pre></p>
<h2>Advanced Features</h2>
<h3>Object Sleeping</h3>
<p>Objects automatically sleep when they come to rest, saving CPU time. To manually control: <pre><code class="zig">// Wake up a sleeping object
world.objects[object_idx].wake();
// Force an object to sleep
world.objects[object_idx].sleep();</code></pre></p>
<h3>Performance Monitoring</h3>
<p>The enhanced system includes performance statistics: <pre><code class="zig">const stats = world.getPerformanceStats();
std.debug.print("Active objects: {d}, Update time: {d}ns\n",
.{stats.active_objects, stats.update_time_ns});
std.debug.print("Collision time: {d}ns, Constraint time: {d}ns\n",
.{stats.collision_time_ns, stats.constraint_time_ns});</code></pre></p>
<h3>Enhanced Spring Constraints</h3>
<p>The improved system supports more advanced spring options: <pre><code class="zig">if (world.constraint_manager) |*cm| {
const spring = try cm.addSpring(.{
.object_index_a = object_a_idx,
.object_index_b = object_b_idx,
.rest_length = 2.0,
.stiffness = 10.0,
.damping = 0.3,
.min_length = 1.0, // Spring can't compress beyond this
.max_length = 3.0, // Spring can't stretch beyond this
.bidirectional = false, // Only applies force when stretched (not compressed)
.break_threshold = 4.0, // Spring breaks if stretched too far
});
}</code></pre></p>
<h2>Example Implementation</h2>
<p>See <code>src/physics/examples/advanced_physics_demo.zig</code> for a complete example that demonstrates the features of the enhanced physics system.</p>
<h2>Common Issues</h2>
<p>1. <strong>Object Tunneling</strong>: If fast-moving objects pass through thin objects, increase the <code>collision_iterations</code> or reduce the fixed timestep. 2. <strong>Unstable Constraints</strong>: If constraints seem unstable, try increasing the <code>constraint_iterations</code> or adjusting the compliance values. 3. <strong>Performance Concerns</strong>: Use object sleeping, appropriate collision groups/masks, and monitor the performance stats to identify bottlenecks. 4. <strong>Rigid Body Rotation</strong>: Remember that rigid body physics requires more computational resources than simple particles, so use only where necessary.</p>
</div>
</div>
<footer>
<p>© 2024 MFS Engine - Built with ❤️ and Zig</p>
<p>Production-ready, cross-platform game engine for next-generation applications</p>
</footer>
</div>
</body>
</html>