flutter3d
Showcase Changelog 62 packages API reference

Collision shapes: the source

since 0.5.1 Physics and particles

Regions the guide quotes: shapes (line 103), check (line 220), probe (line 222).

1/// The five collision shapes, standing in a row, each with the flutter3d
2/// mesh that draws roughly the same volume beside it.
3///
4/// Quoted by `collision_shapes.md` and shown whole in the Source tab.
5library;
6
7import 'dart:typed_data';
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_physics/flutter3d_physics.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
12import 'package:flutter3d_showcase/src/demo/run_physics.dart';
14/// The `collision-shapes` page: a box, a sphere, a capsule, a wedge and a
15/// height field in a row, each beside the mesh that draws it. Its check is
16/// that a probe finds only the sphere and that all five are drawn.
17final class CollisionShapesDemo extends ShowcaseDemo {
18 late final CollisionWorld _world;
19 late final Collider _sphereCollider;
21 final List<Collider> _hits = <Collider>[];
23 @override
24 void configureView(DemoContext context) {
25 context.orbit
26 ..distance = 10.0
27 ..pitch = 0.2
28 ..yaw = 0.5;
29 }
31 /// A box with one edge cut away, matching `CollisionWedge(halfExtents,
32 /// uphill: WedgeUphill.positiveX)`: full height at `+x`, tapering to an
33 /// edge at `-x`.
34 MeshData _wedgeMesh(Vector3 half) {
35 final MeshBuilder builder = MeshBuilder(VertexLayout.standard);
36 Vector3 v(double x, double y, double z) => Vector3(x, y, z);
37 final int a = builder.addVertex(position: v(-half.x, -half.y, -half.z));
38 final int b = builder.addVertex(position: v(half.x, -half.y, -half.z));
39 final int c = builder.addVertex(position: v(half.x, half.y, -half.z));
40 final int d = builder.addVertex(position: v(-half.x, -half.y, half.z));
41 final int e = builder.addVertex(position: v(half.x, -half.y, half.z));
42 final int g = builder.addVertex(position: v(half.x, half.y, half.z));
43 builder
44 ..addTriangle(a, c, b) // back
45 ..addTriangle(d, e, g) // front
46 ..addQuad(a, b, e, d) // floor
47 ..addQuad(b, c, g, e) // top wall
48 ..addQuad(a, d, g, c); // slope
49 return builder.build();
50 }
52 /// A small bumpy grid, matching the heights fed to a `CollisionHeightfield`
53 /// of the same `columns`, `rows` and `cellSize`.
54 MeshData _terrainMesh(
55 int columns,
56 int rows,
57 double cellSize,
58 Float32List heights,
59 ) {
60 final MeshBuilder builder = MeshBuilder(VertexLayout.standard);
61 final double width = (columns - 1) * cellSize;
62 final double depth = (rows - 1) * cellSize;
63 final List<int> indices = <int>[];
64 for (var row = 0; row < rows; row++) {
65 for (var column = 0; column < columns; column++) {
66 indices.add(
67 builder.addVertex(
68 position: Vector3(
69 column * cellSize - width * 0.5,
70 heights[row * columns + column],
71 row * cellSize - depth * 0.5,
72 ),
73 normal: Vector3(0.0, 1.0, 0.0),
74 ),
75 );
76 }
77 }
78 for (var row = 0; row + 1 < rows; row++) {
79 for (var column = 0; column + 1 < columns; column++) {
80 final int tl = indices[row * columns + column];
81 final int tr = indices[row * columns + column + 1];
82 final int bl = indices[(row + 1) * columns + column];
83 final int br = indices[(row + 1) * columns + column + 1];
84 builder.addQuad(tl, bl, br, tr);
85 }
86 }
87 return builder.build();
88 }
90 MeshNode _node(
91 DemoContext context,
92 MeshData mesh,
93 RenderMaterial material,
94 Vector3 position,
95 ) => MeshNode(
96 DeviceMesh.upload(context.device, mesh),
97 material,
98 name: material.name,
99 )..setPositionFrom(position);
101 @override
102 Scene build(DemoContext context) {
103 _world = onRunPhysics(CollisionWorld());
104 final Collider box = _world.add(
105 Collider(
106 shape: CollisionBox(Vector3(0.5, 0.5, 0.5)),
107 position: Vector3(-6, 0, 0),
108 ),
109 );
110 _sphereCollider = _world.add(
111 Collider(shape: CollisionSphere(0.5), position: Vector3(-3, 0, 0)),
112 );
113 final Collider capsule = _world.add(
114 Collider(
115 shape: CollisionCapsule(radius: 0.35, halfHeight: 0.4),
116 position: Vector3(0, 0, 0),
117 ),
118 );
119 final Collider wedge = _world.add(
120 Collider(
121 shape: CollisionWedge(Vector3(0.6, 0.5, 0.5)),
122 position: Vector3(3, 0, 0),
123 ),
124 );
125 final Float32List heights = Float32List.fromList(<double>[
126 0.0,
127 0.1,
128 0.0,
129 -0.1,
130 0.1,
131 0.3,
132 0.2,
133 0.0,
134 0.0,
135 0.2,
136 0.4,
137 0.1,
138 -0.1,
139 0.0,
140 0.1,
141 0.0,
142 ]);
143 final Collider field = _world.add(
144 Collider(
145 shape: CollisionHeightfield(
146 columns: 4,
147 rows: 4,
148 cellSize: 0.5,
149 heights: heights,
150 ),
151 position: Vector3(6, 0, 0),
152 ),
153 );
155 final Scene scene = Scene()
156 ..add(
157 _node(
158 context,
159 CuboidShape(size: Vector3(1, 1, 1)).build(),
160 RenderMaterial(
161 name: 'box',
162 baseColor: LinearColor.fromSrgb(0.8, 0.4, 0.3, 1.0),
163 ),
164 box.position,
165 ),
166 )
167 ..add(
168 _node(
169 context,
170 SphereShape(segments: 24, rings: 12).build(),
171 RenderMaterial(
172 name: 'sphere',
173 baseColor: LinearColor.fromSrgb(0.3, 0.6, 0.8, 1.0),
174 ),
175 _sphereCollider.position,
176 ),
177 )
178 ..add(
179 _node(
180 context,
181 const CapsuleShape(radius: 0.35, height: 0.8).build(),
182 RenderMaterial(
183 name: 'capsule',
184 baseColor: LinearColor.fromSrgb(0.5, 0.8, 0.4, 1.0),
185 ),
186 capsule.position,
187 ),
188 )
189 ..add(
190 _node(
191 context,
192 _wedgeMesh(Vector3(0.6, 0.5, 0.5)),
193 RenderMaterial(
194 name: 'wedge',
195 baseColor: LinearColor.fromSrgb(0.8, 0.7, 0.3, 1.0),
196 ),
197 wedge.position,
198 ),
199 )
200 ..add(
201 _node(
202 context,
203 _terrainMesh(4, 4, 0.5, heights),
204 RenderMaterial(
205 name: 'field',
206 baseColor: LinearColor.fromSrgb(0.6, 0.55, 0.4, 1.0),
207 ),
208 field.position,
209 ),
210 )
211 ..add(
212 LightNode(name: 'sun', intensity: 2.5 * LightNode.legacyUnit)
213 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
214 );
215 return scene;
216 }
218 @override
219 void verify(Scene scene, FrameResult frame) {
220 // A probe placed exactly at the sphere's own centre should find the
221 // sphere, and nothing else: the shapes are spaced apart on purpose.
222 _world.overlap(
223 CollisionBox(Vector3(0.1, 0.1, 0.1)),
224 _sphereCollider.position,
225 _hits,
226 );
227 if (_hits.length != 1 || !identical(_hits.first, _sphereCollider)) {
228 throw StateError('the probe should have found only the sphere');
229 }
230 if (frame.drawCalls < 5) {
231 throw StateError('all five shapes should have drawn');
232 }
233 }