Line data Source code
1 : // physics.slang — per-particle-type physics steps, split into its own
2 : // module to demonstrate that `-trace-coverage` attributes counters
3 : // correctly across multiple source files. The LCOV report produced
4 : // by the demo will contain separate `SF:` records for this file and
5 : // `simulate.slang`, each with its own per-line hit counts.
6 :
7 : public struct Particle
8 : {
9 : public float3 position;
10 : public float3 velocity;
11 : public uint type;
12 : public uint flags;
13 : };
14 :
15 : public float3 applyGravity(float3 velocity, float dt)
16 : {
17 688 : return velocity + float3(0.0, -9.8, 0.0) * dt;
18 : }
19 :
20 : public void stepFluid(inout Particle p, float dt)
21 : {
22 528 : p.velocity = applyGravity(p.velocity, dt);
23 : // Viscous drag proportional to speed.
24 176 : float speed = length(p.velocity);
25 176 : if (speed > 0.01)
26 176 : p.velocity -= normalize(p.velocity) * 0.5 * dt;
27 176 : p.position += p.velocity * dt;
28 : }
29 :
30 : public void stepGas(inout Particle p, float dt)
31 : {
32 : // Gas rises buoyantly; no gravity pull, only ambient drag.
33 504 : p.velocity += float3(0.0, 2.5, 0.0) * dt;
34 168 : p.velocity *= 0.98;
35 168 : p.position += p.velocity * dt;
36 : }
37 :
38 : public void stepSolid(inout Particle p, float dt)
39 : {
40 : // Solids fall under gravity; bounce on the floor.
41 504 : p.velocity = applyGravity(p.velocity, dt);
42 168 : p.position += p.velocity * dt;
43 168 : if (p.position.y < 0.0)
44 : {
45 0 : p.position.y = 0.0;
46 0 : p.velocity.y = -p.velocity.y * 0.6; // inelastic bounce
47 : }
48 : }
|