flutter3d
Showcase Changelog 62 packages API reference

Two-bone IK: the source

since 0.7.0 Animation

Regions the guide quotes: pose (line 42), solve (line 129).

1/// An arm bent to reach a point: `TwoBoneIk` solves on a `Pose` alone, with
2/// no scene behind it, and only afterwards is the result written onto real
3/// nodes so this page has something to draw.
4///
5/// Quoted by `two_bone_ik.md` and shown whole in the Source tab.
6library;
7
8import 'dart:typed_data';
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `two-bone-ik` page: an arm solved by `TwoBoneIk` towards a target down
14/// and out along one fixed direction. Its check fails if the wrist misses the
15/// target.
16final class TwoBoneIkDemo extends ShowcaseDemo {
17 /// How far out the target sits, as a fraction from 0 to 1 of 80% of the arm's
18 /// full length, so the elbow always has to bend. The "Reach" slider writes
19 /// it.
20 double reach = 0.5;
22 late final AnimationPose _pose;
23 late final SceneNode _shoulder;
24 late final SceneNode _elbow;
25 late final SceneNode _wrist;
26 late final MeshNode _targetMarker;
27 double _lastError = 0.0;
29 static const double _boneLength = 1.0;
30 static Vector3 get _pole => Vector3(1.0, 0.0, 0.0);
32 @override
33 void configureView(DemoContext context) {
34 context.orbit
35 ..distance = 5.0
36 ..pitch = 0.2
37 ..yaw = 0.3;
38 }
40 @override
41 Scene build(DemoContext context) {
42 _pose = AnimationPose(
43 parents: <int>[-1, 0, 1],
44 restTranslations: Float32List.fromList(<double>[
45 0.0, 0.0, 0.0, // shoulder
46 0.0, -_boneLength, 0.0, // elbow, relative to the shoulder
47 0.0, -_boneLength, 0.0, // wrist, relative to the elbow
48 ]),
49 restRotations: Float32List.fromList(<double>[
50 0.0,
51 0.0,
52 0.0,
53 1.0,
54 0.0,
55 0.0,
56 0.0,
57 1.0,
58 0.0,
59 0.0,
60 0.0,
61 1.0,
62 ]),
63 restScales: Float32List.fromList(<double>[
64 1.0,
65 1.0,
66 1.0,
67 1.0,
68 1.0,
69 1.0,
70 1.0,
71 1.0,
72 1.0,
73 ]),
74 );
76 final DeviceMesh joint = DeviceMesh.upload(
77 context.device,
78 SphereShape(radius: 0.12, segments: 16, rings: 8).build(),
79 );
80 _shoulder = MeshNode(
81 joint,
82 RenderMaterial(
83 name: 'shoulder',
84 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.65, 1.0),
85 ),
86 name: 'shoulder',
87 );
88 _elbow = MeshNode(
89 joint,
90 RenderMaterial(
91 name: 'elbow',
92 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.65, 1.0),
93 ),
94 name: 'elbow',
95 );
96 _wrist = MeshNode(
97 joint,
98 RenderMaterial(
99 name: 'wrist',
100 baseColor: LinearColor.fromSrgb(0.9, 0.5, 0.2, 1.0),
101 ),
102 name: 'wrist',
103 );
104 _shoulder.add(_elbow);
105 _elbow.add(_wrist);
106 _targetMarker = MeshNode(
107 DeviceMesh.upload(
108 context.device,
109 SphereShape(radius: 0.08, segments: 12, rings: 6).build(),
110 ),
111 RenderMaterial(
112 name: 'target',
113 baseColor: LinearColor.fromSrgb(0.9, 0.2, 0.2, 1.0),
114 ),
115 name: 'target',
116 );
118 return Scene()
119 ..add(_shoulder)
120 ..add(_targetMarker)
121 ..add(
122 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
123 ..setLocalForward(Vector3(-0.4, -0.7, -0.5)),
124 );
125 }
127 @override
128 void update(DemoContext context, double dt) {
129 final double maxReach = _boneLength * 2;
130 final Vector3 direction = Vector3(0.6, -0.7, 0.3)..normalize();
131 final Vector3 target = direction * (reach * maxReach * 0.8);
133 _pose.resetToRest();
134 _lastError = TwoBoneIk.solve(
135 _pose,
136 root: 0,
137 mid: 1,
138 tip: 2,
139 target: target,
140 pole: _pole,
141 );
142 _pose.writeTo(<SceneNode?>[_shoulder, _elbow, _wrist]);
143 _targetMarker.setPositionFrom(target);
144 }
146 @override
147 List<DemoControl> controls(DemoContext context) => <DemoControl>[
148 SliderControl(
149 'Reach',
150 min: 0,
151 max: 1,
152 value: () => reach,
153 onChanged: (double v) => reach = v,
154 ),
155 ];
157 @override
158 void verify(Scene scene, FrameResult frame) {
159 if (_lastError > 1e-3) {
160 throw StateError(
161 'the wrist did not reach the target; missed by $_lastError',
162 );
163 }
164 }