Raycast, sweep and overlap: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: world (line 37), raycast (line 56), sweep (line 58), overlap (line 65), check (line 114).
1/// A raycast, a sweep and an overlap against the same small level, reported
2/// as text: three questions a `CollisionWorld` answers exactly rather than
3/// a picture asking you to take its word for it.
4///
5/// Quoted by `collision_queries.md` and shown whole in the Source tab.
6library;
8import 'package:flutter/widgets.dart';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_physics/backend.dart';
11import 'package:flutter3d_physics/flutter3d_physics.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
13import 'package:flutter3d_showcase/src/demo/run_physics.dart';
15/// The `collision-queries` page: a ray, a sweep and an overlap asked of one
16/// small level and answered as text. Its check is that the ray meets the
17/// wall 4.8 m out, the sweep stops on it, and the overlap finds only the
18/// pickup.
19final class CollisionQueriesDemo extends ShowcaseDemo {
20 late final CollisionWorld _world;
21 late final Collider _pickup;
23 final RayHit _ray = RayHit();
24 final SweepHit _sweep = SweepHit();
25 final List<Collider> _overlapping = <Collider>[];
28 void configureView(DemoContext context) {
29 context.orbit
30 ..distance = 8.0
31 ..pitch = 0.25
32 ..yaw = 0.6;
33 }
36 Scene build(DemoContext context) {
37 _world = onRunPhysics(CollisionWorld());
38 final Collider wall = _world.add(
39 Collider(
40 shape: CollisionBox(Vector3(2.0, 1.0, 0.2)),
41 position: Vector3(0.0, 1.0, 0.0),
42 ),
43 );
44 _pickup = _world.add(
45 Collider(
46 shape: CollisionSphere(0.4),
47 position: Vector3(2.0, 0.5, 2.0),
48 kind: ColliderKind.trigger,
49 ),
50 );
51 // A trigger is not level geometry: `add` files anything that is not
52 // `ColliderKind.static` as a mover, and a mover's grid cell is only
53 // filled in when the world is asked to index it.
54 _world.reindex();
56 _world.rayCast(Vector3(0.0, 1.0, 5.0), Vector3(0.0, 0.0, -1.0), 10.0, _ray);
58 _world.sweep(
59 CollisionBox(Vector3(0.3, 0.5, 0.3)),
60 Vector3(0.0, 0.5, 3.0),
61 Vector3(0.0, 0.0, -4.0),
62 _sweep,
63 );
65 _world.overlap(
66 CollisionBox(Vector3(0.1, 0.1, 0.1)),
67 _pickup.position,
68 _overlapping,
69 );
71 return Scene()
72 ..add(
73 MeshNode(
74 DeviceMesh.upload(
75 context.device,
76 CuboidShape(size: Vector3(4.0, 2.0, 0.4)).build(),
77 ),
78 RenderMaterial(
79 name: 'wall',
80 baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.6, 1.0),
81 ),
82 name: 'wall',
83 )..setPositionFrom(wall.position),
84 )
85 ..add(
86 LightNode(name: 'sun', intensity: 2.5 * LightNode.legacyUnit)
87 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
88 );
89 }
92 Widget? customBody(BuildContext buildContext, DemoContext context) {
93 String vec3(Vector3 v) =>
94 '(${v.x.toStringAsFixed(2)}, ${v.y.toStringAsFixed(2)}, '
95 '${v.z.toStringAsFixed(2)})';
96 return Container(
97 color: const Color(0xFF14161A),
98 padding: const EdgeInsets.all(24),
99 alignment: Alignment.topLeft,
100 child: DefaultTextStyle(
101 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 15),
102 child: Text(
103 'raycast: ${_ray.didHit ? 'hit at ${vec3(_ray.point)}' : 'miss'}\n'
104 'sweep: ${_sweep.didHit ? 'stopped at fraction ${_sweep.fraction.toStringAsFixed(3)}, '
105 'normal ${vec3(_sweep.normal)}' : 'clear'}\n'
106 'overlap: ${_overlapping.length} collider(s) at the pickup',
107 ),
108 ),
109 );
110 }
113 void verify(Scene scene, FrameResult frame) {
114 if (!_ray.didHit || (_ray.distance - 4.8).abs() > 0.01) {
115 throw StateError('the ray should meet the wall 4.8m out');
116 }
117 if (!_sweep.didHit || _sweep.normal.z < 0.9) {
118 throw StateError('the sweep should stop on the wall\'s near face');
119 }
120 if (_overlapping.length != 1 || !identical(_overlapping.first, _pickup)) {
121 throw StateError('the overlap should find only the pickup');
122 }
123 if (frame.drawCalls < 1) {
124 throw StateError('the wall did not reach the frame');
125 }
126 }
127}