Commit 986c2bb
authored
Support custom integration and using
# Objective
Currently, for KCC movement, users must keep `LinearVelocity` at zero and manage their own externally stored velocity. Otherwise, you will end up applying the velocity twice, once by the move-and-slide algorithm, and once by the built-in position integration.
This is inconvenient, and also causes potentially incorrect collision response for dynamic bodies that bump into the kinematic body, as the contact won't consider relative velocity properly. Not good!
It would be good if users could use `LinearVelocity` even for KCCs, and simply change the way it is applied.
## Solution
Add `CustomVelocityIntegration` and `CustomPositionIntegration` components for disabling Avian's built-in integration logic. This allows users to implement their own movement driven by `LinearVelocity` and move-and-slide.
## Testing
Ran `move_and_slide_2d` and `move_and_slide_3d`.
---
## Showcase
Add the `CustomPositionIntegration` component to disable Avian's own position updates based on velocity:
```rust
commands.spawn((
Character,
RigidBody::Kinematic,
Collider::from(shape),
// We want to control position updates manually using move and slide.
CustomPositionIntegration,
));
```
Movement systems can then simply modify `LinearVelocity`. In a way, this also makes many systems generic over dynamic and kinematic character controllers.
```rust
fn character_movement(
mut query: Query<&mut LinearVelocity, With<Character>>,
input: Res<ButtonInput<KeyCode>>,
) {
for mut lin_vel in &mut query {
// Determine movement velocity from input
let mut delta_vel = Vec2::ZERO;
if input.pressed(KeyCode::KeyW) {
delta_vel += Vec2::Y
}
if input.pressed(KeyCode::KeyS) {
delta_vel += Vec2::NEG_Y
}
if input.pressed(KeyCode::KeyA) {
delta_vel += Vec2::NEG_X
}
if input.pressed(KeyCode::KeyD) {
delta_vel += Vec2::X
}
delta_vel = delta_vel.normalize_or_zero();
delta_vel *= 100.0;
if input.pressed(KeyCode::ShiftLeft) {
delta_vel *= 2.0;
}
lin_vel.0 += delta_vel.adjust_precision();
}
}
```
You can then just run `move_and_slide` in some system, using `LinearVelocity` as the input velocity, and write the output velocity to `LinearVelocity`.LinearVelocity for KCCs (#899)1 parent d72c48b commit 986c2bb
File tree
6 files changed
+279
-200
lines changed- crates
- avian2d/examples
- avian3d/examples
- src
- character_controller
- dynamics
- integrator
- rigid_body/forces
6 files changed
+279
-200
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
38 | 34 | | |
39 | 35 | | |
40 | 36 | | |
| |||
47 | 43 | | |
48 | 44 | | |
49 | 45 | | |
| 46 | + | |
50 | 47 | | |
51 | 48 | | |
52 | 49 | | |
53 | 50 | | |
54 | | - | |
55 | 51 | | |
56 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
| |||
186 | 185 | | |
187 | 186 | | |
188 | 187 | | |
189 | | - | |
190 | | - | |
191 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
192 | 193 | | |
193 | 194 | | |
194 | | - | |
195 | 195 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
215 | 216 | | |
216 | | - | |
217 | | - | |
| 217 | + | |
| 218 | + | |
218 | 219 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
224 | 227 | | |
| 228 | + | |
225 | 229 | | |
226 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
227 | 251 | | |
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 | | - | |
| 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 | + | |
266 | 286 | | |
267 | | - | |
268 | | - | |
269 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
270 | 291 | | |
271 | 292 | | |
272 | 293 | | |
273 | 294 | | |
274 | | - | |
| 295 | + | |
275 | 296 | | |
276 | 297 | | |
277 | | - | |
| 298 | + | |
278 | 299 | | |
279 | 300 | | |
280 | | - | |
281 | | - | |
| 301 | + | |
| 302 | + | |
282 | 303 | | |
283 | 304 | | |
284 | 305 | | |
| |||
287 | 308 | | |
288 | 309 | | |
289 | 310 | | |
290 | | - | |
| 311 | + | |
291 | 312 | | |
292 | | - | |
| 313 | + | |
293 | 314 | | |
294 | 315 | | |
295 | 316 | | |
| |||
0 commit comments