1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
#[cfg(doc)]
use super::IntegrationParameters;
use crate::dynamics::{
LockedAxes, MassProperties, RigidBodyActivation, RigidBodyAdditionalMassProps, RigidBodyCcd,
RigidBodyChanges, RigidBodyColliders, RigidBodyDamping, RigidBodyDominance, RigidBodyForces,
RigidBodyIds, RigidBodyMassProps, RigidBodyPosition, RigidBodyType, RigidBodyVelocity,
};
use crate::geometry::{
ColliderHandle, ColliderMassProps, ColliderParent, ColliderPosition, ColliderSet, ColliderShape,
};
use crate::math::{AngVector, Pose, Real, Rotation, Vector, rotation_from_angle};
use crate::utils::CrossProduct;
#[cfg(feature = "dim2")]
use crate::num::Zero;
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
/// A physical object that can move, rotate, and collide with other objects in your simulation.
///
/// Rigid bodies are the fundamental moving objects in physics simulations. Think of them as
/// the "physical representation" of your game objects - a character, a crate, a vehicle, etc.
///
/// ## Body types
///
/// - **Dynamic**: Affected by forces, gravity, and collisions. Use for objects that should move realistically (falling boxes, projectiles, etc.)
/// - **Fixed**: Never moves. Use for static geometry like walls, floors, and terrain
/// - **Kinematic**: Moved by setting velocity or position directly, not by forces. Use for moving platforms, doors, or player-controlled characters
///
/// ## Creating bodies
///
/// Always use [`RigidBodyBuilder`] to create new rigid bodies:
///
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// let body = RigidBodyBuilder::dynamic()
/// .translation(Vector::new(0.0, 10.0, 0.0))
/// .build();
/// let handle = bodies.insert(body);
/// ```
#[derive(Debug, Clone)]
// #[repr(C)]
// #[repr(align(64))]
pub struct RigidBody {
pub(crate) ids: RigidBodyIds,
pub(crate) pos: RigidBodyPosition,
pub(crate) damping: RigidBodyDamping<Real>,
pub(crate) vels: RigidBodyVelocity<Real>,
pub(crate) forces: RigidBodyForces,
pub(crate) mprops: RigidBodyMassProps,
pub(crate) ccd_vels: RigidBodyVelocity<Real>,
pub(crate) ccd: RigidBodyCcd,
pub(crate) colliders: RigidBodyColliders,
/// Whether or not this rigid-body is sleeping.
pub(crate) activation: RigidBodyActivation,
pub(crate) changes: RigidBodyChanges,
/// The status of the body, governing how it is affected by external forces.
pub(crate) body_type: RigidBodyType,
/// The dominance group this rigid-body is part of.
pub(crate) dominance: RigidBodyDominance,
pub(crate) enabled: bool,
pub(crate) additional_solver_iterations: usize,
/// User-defined data associated to this rigid-body.
pub user_data: u128,
}
impl Default for RigidBody {
fn default() -> Self {
Self::new()
}
}
impl RigidBody {
fn new() -> Self {
Self {
pos: RigidBodyPosition::default(),
mprops: RigidBodyMassProps::default(),
ccd_vels: RigidBodyVelocity::default(),
vels: RigidBodyVelocity::default(),
damping: RigidBodyDamping::default(),
forces: RigidBodyForces::default(),
ccd: RigidBodyCcd::default(),
ids: RigidBodyIds::default(),
colliders: RigidBodyColliders::default(),
activation: RigidBodyActivation::active(),
changes: RigidBodyChanges::all(),
body_type: RigidBodyType::Dynamic,
dominance: RigidBodyDominance::default(),
enabled: true,
user_data: 0,
additional_solver_iterations: 0,
}
}
pub(crate) fn reset_internal_references(&mut self) {
self.colliders.0 = Vec::new();
self.ids = Default::default();
}
/// Copy all the characteristics from `other` to `self`.
///
/// If you have a mutable reference to a rigid-body `rigid_body: &mut RigidBody`, attempting to
/// assign it a whole new rigid-body instance, e.g., `*rigid_body = RigidBodyBuilder::dynamic().build()`,
/// will crash due to some internal indices being overwritten. Instead, use
/// `rigid_body.copy_from(&RigidBodyBuilder::dynamic().build())`.
///
/// This method will allow you to set most characteristics of this rigid-body from another
/// rigid-body instance without causing any breakage.
///
/// This method **cannot** be used for editing the list of colliders attached to this rigid-body.
/// Therefore, the list of colliders attached to `self` won’t be replaced by the one attached
/// to `other`.
///
/// The pose of `other` will only copied into `self` if `self` doesn’t have a parent (if it has
/// a parent, its position is directly controlled by the parent rigid-body).
pub fn copy_from(&mut self, other: &RigidBody) {
// NOTE: we deconstruct the rigid-body struct to be sure we don’t forget to
// add some copies here if we add more field to RigidBody in the future.
let RigidBody {
pos,
mprops,
ccd_vels: integrated_vels,
vels,
damping,
forces,
ccd,
ids: _ids, // Internal ids must not be overwritten.
colliders: _colliders, // This function cannot be used to edit collider sets.
activation,
changes: _changes, // Will be set to ALL.
body_type,
dominance,
enabled,
additional_solver_iterations,
user_data,
} = other;
self.pos = *pos;
self.mprops = mprops.clone();
self.ccd_vels = *integrated_vels;
self.vels = *vels;
self.damping = *damping;
self.forces = *forces;
self.ccd = *ccd;
self.activation = *activation;
self.body_type = *body_type;
self.dominance = *dominance;
self.enabled = *enabled;
self.additional_solver_iterations = *additional_solver_iterations;
self.user_data = *user_data;
self.changes = RigidBodyChanges::all();
}
/// Set the additional number of solver iterations run for this rigid-body and
/// everything interacting with it.
///
/// See [`Self::set_additional_solver_iterations`] for additional information.
pub fn additional_solver_iterations(&self) -> usize {
self.additional_solver_iterations
}
/// Set the additional number of solver iterations run for this rigid-body and
/// everything interacting with it.
///
/// Increasing this number will help improve simulation accuracy on this rigid-body
/// and every rigid-body interacting directly or indirectly with it (through joints
/// or contacts). This implies a performance hit.
///
/// The default value is 0, meaning exactly [`IntegrationParameters::num_solver_iterations`] will
/// be used as number of solver iterations for this body.
pub fn set_additional_solver_iterations(&mut self, additional_iterations: usize) {
self.additional_solver_iterations = additional_iterations;
}
/// The activation status of this rigid-body.
pub fn activation(&self) -> &RigidBodyActivation {
&self.activation
}
/// Mutable reference to the activation status of this rigid-body.
pub fn activation_mut(&mut self) -> &mut RigidBodyActivation {
self.changes |= RigidBodyChanges::SLEEP;
&mut self.activation
}
/// Is this rigid-body enabled?
pub fn is_enabled(&self) -> bool {
self.enabled
}
/// Sets whether this rigid-body is enabled or not.
pub fn set_enabled(&mut self, enabled: bool) {
if enabled != self.enabled {
if enabled {
// NOTE: this is probably overkill, but it makes sure we don’t
// forget anything that needs to be updated because the rigid-body
// was basically interpreted as if it was removed while it was
// disabled.
self.changes = RigidBodyChanges::all();
} else {
self.changes |= RigidBodyChanges::ENABLED_OR_DISABLED;
}
self.enabled = enabled;
}
}
/// The linear damping coefficient (velocity reduction over time).
///
/// Damping gradually slows down moving objects. `0.0` = no damping (infinite momentum),
/// higher values = faster slowdown. Use for air resistance, friction, etc.
#[inline]
pub fn linear_damping(&self) -> Real {
self.damping.linear_damping
}
/// Sets how quickly linear velocity decreases over time.
///
/// - `0.0` = no slowdown (space/frictionless)
/// - `0.1` = gradual slowdown (air resistance)
/// - `1.0+` = rapid slowdown (thick fluid)
#[inline]
pub fn set_linear_damping(&mut self, damping: Real) {
self.damping.linear_damping = damping;
}
/// The angular damping coefficient (rotation slowdown over time).
///
/// Like linear damping but for rotation. Higher values make spinning objects stop faster.
#[inline]
pub fn angular_damping(&self) -> Real {
self.damping.angular_damping
}
/// Sets how quickly angular velocity decreases over time.
///
/// Controls how fast spinning objects slow down.
#[inline]
pub fn set_angular_damping(&mut self, damping: Real) {
self.damping.angular_damping = damping
}
/// The type of this rigid-body.
pub fn body_type(&self) -> RigidBodyType {
self.body_type
}
/// Sets the type of this rigid-body.
pub fn set_body_type(&mut self, status: RigidBodyType, wake_up: bool) {
if status != self.body_type {
self.changes.insert(RigidBodyChanges::TYPE);
self.body_type = status;
if status == RigidBodyType::Fixed {
self.vels = RigidBodyVelocity::zero();
}
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
}
}
/// The center of mass position in world coordinates.
///
/// This is the "balance point" where the body's mass is centered. Forces applied here
/// produce no rotation, only translation.
#[inline]
pub fn center_of_mass(&self) -> Vector {
self.mprops.world_com
}
/// The center of mass in the body's local coordinate system.
///
/// This is relative to the body's position, computed from attached colliders.
#[inline]
pub fn local_center_of_mass(&self) -> Vector {
self.mprops.local_mprops.local_com
}
/// The mass-properties of this rigid-body.
#[inline]
pub fn mass_properties(&self) -> &RigidBodyMassProps {
&self.mprops
}
/// The dominance group of this rigid-body.
///
/// This method always returns `i8::MAX + 1` for non-dynamic
/// rigid-bodies.
#[inline]
pub fn effective_dominance_group(&self) -> i16 {
self.dominance.effective_group(&self.body_type)
}
/// Sets the axes along which this rigid-body cannot translate or rotate.
#[inline]
pub fn set_locked_axes(&mut self, locked_axes: LockedAxes, wake_up: bool) {
if locked_axes != self.mprops.flags {
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
self.mprops.flags = locked_axes;
self.update_world_mass_properties();
}
}
/// The axes along which this rigid-body cannot translate or rotate.
#[inline]
pub fn locked_axes(&self) -> LockedAxes {
self.mprops.flags
}
/// Locks or unlocks all rotational movement for this body.
///
/// When locked, the body cannot rotate at all (useful for keeping objects upright).
/// Use for characters that shouldn't tip over, or objects that should only slide.
#[inline]
pub fn lock_rotations(&mut self, locked: bool, wake_up: bool) {
if locked != self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED) {
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
self.mprops.flags.set(LockedAxes::ROTATION_LOCKED_X, locked);
self.mprops.flags.set(LockedAxes::ROTATION_LOCKED_Y, locked);
self.mprops.flags.set(LockedAxes::ROTATION_LOCKED_Z, locked);
self.update_world_mass_properties();
}
}
#[inline]
/// Locks or unlocks rotations of this rigid-body along each cartesian axes.
pub fn set_enabled_rotations(
&mut self,
allow_rotations_x: bool,
allow_rotations_y: bool,
allow_rotations_z: bool,
wake_up: bool,
) {
if self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_X) == allow_rotations_x
|| self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_Y) == allow_rotations_y
|| self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_Z) == allow_rotations_z
{
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
self.mprops
.flags
.set(LockedAxes::ROTATION_LOCKED_X, !allow_rotations_x);
self.mprops
.flags
.set(LockedAxes::ROTATION_LOCKED_Y, !allow_rotations_y);
self.mprops
.flags
.set(LockedAxes::ROTATION_LOCKED_Z, !allow_rotations_z);
self.update_world_mass_properties();
}
}
/// Locks or unlocks rotations of this rigid-body along each cartesian axes.
#[deprecated(note = "Use `set_enabled_rotations` instead")]
pub fn restrict_rotations(
&mut self,
allow_rotations_x: bool,
allow_rotations_y: bool,
allow_rotations_z: bool,
wake_up: bool,
) {
self.set_enabled_rotations(
allow_rotations_x,
allow_rotations_y,
allow_rotations_z,
wake_up,
);
}
/// Locks or unlocks all translational movement for this body.
///
/// When locked, the body cannot move from its position (but can still rotate).
/// Use for rotating platforms, turrets, or objects fixed in space.
#[inline]
pub fn lock_translations(&mut self, locked: bool, wake_up: bool) {
if locked != self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED) {
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
self.mprops
.flags
.set(LockedAxes::TRANSLATION_LOCKED, locked);
self.update_world_mass_properties();
}
}
#[inline]
/// Locks or unlocks rotations of this rigid-body along each cartesian axes.
pub fn set_enabled_translations(
&mut self,
allow_translation_x: bool,
allow_translation_y: bool,
#[cfg(feature = "dim3")] allow_translation_z: bool,
wake_up: bool,
) {
#[cfg(feature = "dim2")]
if self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED_X) != allow_translation_x
&& self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED_Y) != allow_translation_y
{
// Nothing to change.
return;
}
#[cfg(feature = "dim3")]
if self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED_X) != allow_translation_x
&& self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED_Y) != allow_translation_y
&& self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED_Z) != allow_translation_z
{
// Nothing to change.
return;
}
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
self.mprops
.flags
.set(LockedAxes::TRANSLATION_LOCKED_X, !allow_translation_x);
self.mprops
.flags
.set(LockedAxes::TRANSLATION_LOCKED_Y, !allow_translation_y);
#[cfg(feature = "dim3")]
self.mprops
.flags
.set(LockedAxes::TRANSLATION_LOCKED_Z, !allow_translation_z);
self.update_world_mass_properties();
}
#[inline]
#[deprecated(note = "Use `set_enabled_translations` instead")]
/// Locks or unlocks rotations of this rigid-body along each cartesian axes.
pub fn restrict_translations(
&mut self,
allow_translation_x: bool,
allow_translation_y: bool,
#[cfg(feature = "dim3")] allow_translation_z: bool,
wake_up: bool,
) {
self.set_enabled_translations(
allow_translation_x,
allow_translation_y,
#[cfg(feature = "dim3")]
allow_translation_z,
wake_up,
)
}
/// Are the translations of this rigid-body locked?
#[cfg(feature = "dim2")]
pub fn is_translation_locked(&self) -> bool {
self.mprops
.flags
.contains(LockedAxes::TRANSLATION_LOCKED_X | LockedAxes::TRANSLATION_LOCKED_Y)
}
/// Are the translations of this rigid-body locked?
#[cfg(feature = "dim3")]
pub fn is_translation_locked(&self) -> bool {
self.mprops.flags.contains(LockedAxes::TRANSLATION_LOCKED)
}
/// Are the rotations of this rigid-body locked?
#[cfg(feature = "dim2")]
pub fn is_rotation_locked(&self) -> bool {
self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_Z)
}
/// Returns `true` for each rotational degrees of freedom locked on this rigid-body.
#[cfg(feature = "dim3")]
pub fn is_rotation_locked(&self) -> [bool; 3] {
[
self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_X),
self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_Y),
self.mprops.flags.contains(LockedAxes::ROTATION_LOCKED_Z),
]
}
/// Enables or disables Continuous Collision Detection for this body.
///
/// CCD prevents fast-moving objects from tunneling through thin walls, but costs more CPU.
/// Enable for bullets, fast projectiles, or any object that must never pass through geometry.
pub fn enable_ccd(&mut self, enabled: bool) {
self.ccd.ccd_enabled = enabled;
}
/// Checks if CCD is enabled for this body.
///
/// Returns `true` if CCD is turned on (not whether it's currently active this frame).
pub fn is_ccd_enabled(&self) -> bool {
self.ccd.ccd_enabled
}
/// Sets the maximum prediction distance Soft Continuous Collision-Detection.
///
/// When set to 0, soft-CCD is disabled. Soft-CCD helps prevent tunneling especially of
/// slow-but-thin to moderately fast objects. The soft CCD prediction distance indicates how
/// far in the object’s path the CCD algorithm is allowed to inspect. Large values can impact
/// performance badly by increasing the work needed from the broad-phase.
///
/// It is a generally cheaper variant of regular CCD (that can be enabled with
/// [`RigidBody::enable_ccd`] since it relies on predictive constraints instead of
/// shape-cast and substeps.
pub fn set_soft_ccd_prediction(&mut self, prediction_distance: Real) {
self.ccd.soft_ccd_prediction = prediction_distance;
}
/// The soft-CCD prediction distance for this rigid-body.
///
/// See the documentation of [`RigidBody::set_soft_ccd_prediction`] for additional details on
/// soft-CCD.
pub fn soft_ccd_prediction(&self) -> Real {
self.ccd.soft_ccd_prediction
}
// This is different from `is_ccd_enabled`. This checks that CCD
// is active for this rigid-body, i.e., if it was seen to move fast
// enough to justify a CCD run.
/// Is CCD active for this rigid-body?
///
/// The CCD is considered active if the rigid-body is moving at
/// a velocity greater than an automatically-computed threshold.
///
/// This is not the same as `self.is_ccd_enabled` which only
/// checks if CCD is enabled to run for this rigid-body or if
/// it is completely disabled (independently from its velocity).
pub fn is_ccd_active(&self) -> bool {
self.ccd.ccd_active
}
/// Recalculates mass, center of mass, and inertia from attached colliders.
///
/// Normally automatic, but call this if you modify collider shapes/masses at runtime.
/// Only needed after directly modifying colliders without going through the builder.
pub fn recompute_mass_properties_from_colliders(&mut self, colliders: &ColliderSet) {
self.mprops.recompute_mass_properties_from_colliders(
colliders,
&self.colliders,
self.body_type,
&self.pos.position,
);
}
/// Adds extra mass on top of collider-computed mass.
///
/// Total mass = collider masses + this additional mass. Use when you want to make
/// a body heavier without changing collider densities.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// // Add 50kg to make this body heavier
/// bodies[body].set_additional_mass(50.0, true);
/// ```
///
/// Angular inertia is automatically scaled to match the mass increase.
/// Updated automatically at next physics step or call `recompute_mass_properties_from_colliders()`.
#[inline]
pub fn set_additional_mass(&mut self, additional_mass: Real, wake_up: bool) {
self.do_set_additional_mass_properties(
RigidBodyAdditionalMassProps::Mass(additional_mass),
wake_up,
)
}
/// Sets the rigid-body's additional mass-properties.
///
/// This is only the "additional" mass-properties because the total mass-properties of the
/// rigid-body is equal to the sum of this additional mass-properties and the mass computed from
/// the colliders (with non-zero densities) attached to this rigid-body.
///
/// That total mass-properties (which include the attached colliders’ contributions)
/// will be updated at the name physics step, or can be updated manually with
/// [`Self::recompute_mass_properties_from_colliders`].
///
/// This will override any previous mass-properties set by [`Self::set_additional_mass`],
/// [`Self::set_additional_mass_properties`], [`RigidBodyBuilder::additional_mass`], or
/// [`RigidBodyBuilder::additional_mass_properties`] for this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[inline]
pub fn set_additional_mass_properties(&mut self, props: MassProperties, wake_up: bool) {
self.do_set_additional_mass_properties(
RigidBodyAdditionalMassProps::MassProps(props),
wake_up,
)
}
fn do_set_additional_mass_properties(
&mut self,
props: RigidBodyAdditionalMassProps,
wake_up: bool,
) {
let new_mprops = Some(Box::new(props));
if self.mprops.additional_local_mprops != new_mprops {
self.changes.insert(RigidBodyChanges::LOCAL_MASS_PROPERTIES);
self.mprops.additional_local_mprops = new_mprops;
if self.is_dynamic_or_kinematic() && wake_up {
self.wake_up(true);
}
}
}
/// Returns handles of all colliders attached to this body.
///
/// Use to iterate over a body's collision shapes or to modify them.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let mut colliders = ColliderSet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// # colliders.insert_with_parent(ColliderBuilder::ball(0.5), body, &mut bodies);
/// for collider_handle in bodies[body].colliders() {
/// if let Some(collider) = colliders.get_mut(*collider_handle) {
/// collider.set_friction(0.5);
/// }
/// }
/// ```
pub fn colliders(&self) -> &[ColliderHandle] {
&self.colliders.0[..]
}
/// Checks if this is a dynamic body (moves via forces and collisions).
///
/// Dynamic bodies are fully simulated and respond to gravity, forces, and collisions.
pub fn is_dynamic(&self) -> bool {
self.body_type == RigidBodyType::Dynamic
}
/// Checks if this is a kinematic body (moves via direct velocity/position control).
///
/// Kinematic bodies move by setting velocity directly, not by applying forces.
pub fn is_kinematic(&self) -> bool {
self.body_type.is_kinematic()
}
/// Is this rigid-body a dynamic rigid-body or a kinematic rigid-body?
///
/// This method is mostly convenient internally where kinematic and dynamic rigid-body
/// are subject to the same behavior.
pub fn is_dynamic_or_kinematic(&self) -> bool {
self.body_type.is_dynamic_or_kinematic()
}
/// The offset index in the solver’s active set, or `u32::MAX` if
/// the rigid-body isn’t dynamic or kinematic.
// TODO: is this really necessary? Could we just always assign u32::MAX
// to all the fixed bodies active set offsets?
pub fn effective_active_set_offset(&self) -> u32 {
if self.is_dynamic_or_kinematic() {
self.ids.active_set_id as u32
} else {
u32::MAX
}
}
/// Checks if this is a fixed body (never moves, infinite mass).
///
/// Fixed bodies are static geometry: walls, floors, terrain. They never move
/// and are not affected by any forces or collisions.
pub fn is_fixed(&self) -> bool {
self.body_type == RigidBodyType::Fixed
}
/// The mass of this rigid body in kilograms.
///
/// Returns zero for fixed bodies (which technically have infinite mass).
/// Mass is computed from attached colliders' shapes and densities.
pub fn mass(&self) -> Real {
self.mprops.local_mprops.mass()
}
/// The predicted position of this rigid-body.
///
/// If this rigid-body is kinematic this value is set by the `set_next_kinematic_position`
/// method and is used for estimating the kinematic body velocity at the next timestep.
/// For non-kinematic bodies, this value is currently unspecified.
pub fn next_position(&self) -> &Pose {
&self.pos.next_position
}
/// The gravity scale multiplier for this body.
///
/// - `1.0` (default) = normal gravity
/// - `0.0` = no gravity (floating)
/// - `2.0` = double gravity (heavy/fast falling)
/// - Negative values = reverse gravity (objects fall upward!)
pub fn gravity_scale(&self) -> Real {
self.forces.gravity_scale
}
/// Sets how much gravity affects this body (multiplier).
///
/// # Examples
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// bodies[body].set_gravity_scale(0.0, true); // Zero-G (space)
/// bodies[body].set_gravity_scale(0.1, true); // Moon gravity
/// bodies[body].set_gravity_scale(2.0, true); // Extra heavy
/// ```
pub fn set_gravity_scale(&mut self, scale: Real, wake_up: bool) {
if self.forces.gravity_scale != scale {
if wake_up && self.activation.sleeping {
self.changes.insert(RigidBodyChanges::SLEEP);
self.activation.sleeping = false;
}
self.forces.gravity_scale = scale;
}
}
/// The dominance group of this rigid-body.
pub fn dominance_group(&self) -> i8 {
self.dominance.0
}
/// The dominance group of this rigid-body.
pub fn set_dominance_group(&mut self, dominance: i8) {
if self.dominance.0 != dominance {
self.changes.insert(RigidBodyChanges::DOMINANCE);
self.dominance.0 = dominance
}
}
/// Adds a collider to this rigid-body.
pub(crate) fn add_collider_internal(
&mut self,
co_handle: ColliderHandle,
co_parent: &ColliderParent,
co_pos: &mut ColliderPosition,
co_shape: &ColliderShape,
co_mprops: &ColliderMassProps,
) {
self.colliders.attach_collider(
self.body_type,
&mut self.changes,
&mut self.ccd,
&mut self.mprops,
&self.pos,
co_handle,
co_pos,
co_parent,
co_shape,
co_mprops,
)
}
/// Removes a collider from this rigid-body.
pub(crate) fn remove_collider_internal(&mut self, handle: ColliderHandle) {
if let Some(i) = self.colliders.0.iter().position(|e| *e == handle) {
self.changes.set(RigidBodyChanges::COLLIDERS, true);
self.colliders.0.swap_remove(i);
}
}
/// Forces this body to sleep immediately (stop simulating it).
///
/// Sleeping bodies are excluded from physics simulation until disturbed. Use to manually
/// deactivate bodies you know won't move for a while.
///
/// The body will auto-wake if:
/// - Hit by a moving object
/// - Connected via joint to a moving body
/// - Manually woken with `wake_up()`
pub fn sleep(&mut self) {
self.activation.sleep();
self.vels = RigidBodyVelocity::zero();
}
/// Wakes up this body if it's sleeping, making it active in the simulation.
///
/// # Parameters
/// * `strong` - If `true`, guarantees the body stays awake for multiple frames.
/// If `false`, it might sleep again immediately if conditions are met.
///
/// Use after manually moving a sleeping body or to keep it active temporarily.
pub fn wake_up(&mut self, strong: bool) {
if self.activation.sleeping {
self.changes.insert(RigidBodyChanges::SLEEP);
}
self.activation.wake_up(strong);
}
/// Is this rigid body sleeping?
pub fn is_sleeping(&self) -> bool {
// TODO: should we:
// - return false for fixed bodies.
// - return true for non-sleeping dynamic bodies.
// - return true only for kinematic bodies with non-zero velocity?
self.activation.sleeping
}
/// Returns `true` if the body has non-zero linear or angular velocity.
///
/// Useful for checking if an object is actually moving vs sitting still.
pub fn is_moving(&self) -> bool {
#[cfg(feature = "dim2")]
let angvel_is_nonzero = self.vels.angvel != 0.0;
#[cfg(feature = "dim3")]
let angvel_is_nonzero = self.vels.angvel != Default::default();
self.vels.linvel != Default::default() || angvel_is_nonzero
}
/// Returns both linear and angular velocity as a combined structure.
///
/// Most users should use `linvel()` and `angvel()` separately instead.
pub fn vels(&self) -> &RigidBodyVelocity<Real> {
&self.vels
}
/// The current linear velocity (speed and direction of movement).
///
/// This is how fast the body is moving in units per second. Use with [`set_linvel()`](Self::set_linvel)
/// to directly control the body's movement speed.
pub fn linvel(&self) -> Vector {
self.vels.linvel
}
/// The current angular velocity (rotation speed) in 2D.
///
/// Returns radians per second. Positive = counter-clockwise, negative = clockwise.
#[cfg(feature = "dim2")]
pub fn angvel(&self) -> Real {
self.vels.angvel
}
/// The current angular velocity (rotation speed) in 3D.
///
/// Returns a vector in radians per second around each axis (X, Y, Z).
#[cfg(feature = "dim3")]
pub fn angvel(&self) -> AngVector {
self.vels.angvel
}
/// Set both the angular and linear velocity of this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
pub fn set_vels(&mut self, vels: RigidBodyVelocity<Real>, wake_up: bool) {
self.set_linvel(vels.linvel, wake_up);
#[cfg(feature = "dim2")]
self.set_angvel(vels.angvel, wake_up);
#[cfg(feature = "dim3")]
self.set_angvel(vels.angvel, wake_up);
}
/// Sets how fast this body is moving (linear velocity).
///
/// This directly sets the body's velocity without applying forces. Use for:
/// - Player character movement
/// - Kinematic object control
/// - Instantly changing an object's speed
///
/// For physics-based movement, consider using [`apply_impulse()`](Self::apply_impulse) or
/// [`add_force()`](Self::add_force) instead for more realistic behavior.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// // Make the body move to the right at 5 units/second
/// bodies[body].set_linvel(Vector::new(5.0, 0.0, 0.0), true);
/// ```
pub fn set_linvel(&mut self, linvel: Vector, wake_up: bool) {
if self.vels.linvel != linvel {
match self.body_type {
RigidBodyType::Dynamic | RigidBodyType::KinematicVelocityBased => {
self.vels.linvel = linvel;
if wake_up {
self.wake_up(true)
}
}
RigidBodyType::Fixed | RigidBodyType::KinematicPositionBased => {}
}
}
}
/// The angular velocity of this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[cfg(feature = "dim2")]
pub fn set_angvel(&mut self, angvel: Real, wake_up: bool) {
if self.vels.angvel != angvel {
match self.body_type {
RigidBodyType::Dynamic | RigidBodyType::KinematicVelocityBased => {
self.vels.angvel = angvel;
if wake_up {
self.wake_up(true)
}
}
RigidBodyType::Fixed | RigidBodyType::KinematicPositionBased => {}
}
}
}
/// The angular velocity of this rigid-body.
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[cfg(feature = "dim3")]
pub fn set_angvel(&mut self, angvel: AngVector, wake_up: bool) {
if self.vels.angvel != angvel {
match self.body_type {
RigidBodyType::Dynamic | RigidBodyType::KinematicVelocityBased => {
self.vels.angvel = angvel;
if wake_up {
self.wake_up(true)
}
}
RigidBodyType::Fixed | RigidBodyType::KinematicPositionBased => {}
}
}
}
/// The current position (translation + rotation) of this rigid body in world space.
///
/// Returns an `SimdPose` which combines both translation and rotation.
/// For just the position vector, use [`translation()`](Self::translation) instead.
#[inline]
pub fn position(&self) -> &Pose {
&self.pos.position
}
/// The current position vector of this rigid body (world coordinates).
///
/// This is just the XYZ location, without rotation. For the full pose (position + rotation),
/// use [`position()`](Self::position).
#[inline]
pub fn translation(&self) -> Vector {
self.pos.position.translation
}
/// Teleports this rigid body to a new position (world coordinates).
///
/// ⚠️ **Warning**: This instantly moves the body, ignoring physics! The body will "teleport"
/// without checking for collisions in between. Use this for:
/// - Respawning objects
/// - Level transitions
/// - Resetting positions
///
/// For smooth physics-based movement, use velocities or forces instead.
///
/// # Parameters
/// * `wake_up` - If `true`, prevents the body from immediately going back to sleep
#[inline]
pub fn set_translation(&mut self, translation: Vector, wake_up: bool) {
if self.pos.position.translation != translation
|| self.pos.next_position.translation != translation
{
self.changes.insert(RigidBodyChanges::POSITION);
self.pos.position.translation = translation;
self.pos.next_position.translation = translation;
// Update the world mass-properties so torque application remains valid.
self.update_world_mass_properties();
// TODO: Do we really need to check that the body isn't dynamic?
if wake_up && self.is_dynamic_or_kinematic() {
self.wake_up(true)
}
}
}
/// The current rotation/orientation of this rigid body.
#[inline]
pub fn rotation(&self) -> &Rotation {
&self.pos.position.rotation
}
/// Instantly rotates this rigid body to a new orientation.
///
/// ⚠️ **Warning**: This teleports the rotation, ignoring physics! See [`set_translation()`](Self::set_translation) for details.
#[inline]
pub fn set_rotation(&mut self, rotation: Rotation, wake_up: bool) {
if self.pos.position.rotation != rotation || self.pos.next_position.rotation != rotation {
self.changes.insert(RigidBodyChanges::POSITION);
self.pos.position.rotation = rotation;
self.pos.next_position.rotation = rotation;
// Update the world mass-properties so torque application remains valid.
self.update_world_mass_properties();
// TODO: Do we really need to check that the body isn't dynamic?
if wake_up && self.is_dynamic_or_kinematic() {
self.wake_up(true)
}
}
}
/// Teleports this body to a new position and rotation (ignoring physics).
///
/// ⚠️ **Warning**: Instantly moves the body without checking for collisions!
/// For position-based kinematic bodies, this also resets their interpolated velocity to zero.
///
/// Use for respawning, level transitions, or resetting positions.
pub fn set_position(&mut self, pos: Pose, wake_up: bool) {
if self.pos.position != pos || self.pos.next_position != pos {
self.changes.insert(RigidBodyChanges::POSITION);
self.pos.position = pos;
self.pos.next_position = pos;
// Update the world mass-properties so torque application remains valid.
self.update_world_mass_properties();
// TODO: Do we really need to check that the body isn't dynamic?
if wake_up && self.is_dynamic_or_kinematic() {
self.wake_up(true)
}
}
}
/// For position-based kinematic bodies: sets where the body should rotate to by next frame.
///
/// Only works for `KinematicPositionBased` bodies. Rapier computes the angular velocity
/// needed to reach this rotation smoothly.
pub fn set_next_kinematic_rotation(&mut self, rotation: Rotation) {
if self.is_kinematic() {
self.pos.next_position.rotation = rotation;
if self.pos.position.rotation != rotation {
self.wake_up(true);
}
}
}
/// For position-based kinematic bodies: sets where the body should move to by next frame.
///
/// Only works for `KinematicPositionBased` bodies. Rapier computes the velocity
/// needed to reach this position smoothly.
pub fn set_next_kinematic_translation(&mut self, translation: Vector) {
if self.is_kinematic() {
self.pos.next_position.translation = translation;
if self.pos.position.translation != translation {
self.wake_up(true);
}
}
}
/// For position-based kinematic bodies: sets the target pose (position + rotation) for next frame.
///
/// Only works for `KinematicPositionBased` bodies. Combines translation and rotation control.
pub fn set_next_kinematic_position(&mut self, pos: Pose) {
if self.is_kinematic() {
self.pos.next_position = pos;
if self.pos.position != pos {
self.wake_up(true);
}
}
}
/// Predicts the next position of this rigid-body, by integrating its velocity and forces
/// by a time of `dt`.
pub(crate) fn predict_position_using_velocity_and_forces_with_max_dist(
&self,
dt: Real,
max_dist: Real,
) -> Pose {
let new_vels = self.forces.integrate(dt, &self.vels, &self.mprops);
// Compute the clamped dt such that the body doesn't travel more than `max_dist`.
let linvel_norm = new_vels.linvel.length();
let clamped_linvel = linvel_norm.min(max_dist * crate::utils::inv(dt));
let clamped_dt = dt * clamped_linvel * crate::utils::inv(linvel_norm);
new_vels.integrate(
clamped_dt,
&self.pos.position,
&self.mprops.local_mprops.local_com,
)
}
/// Calculates where this body will be after `dt` seconds, considering current velocity AND forces.
///
/// Useful for predicting future positions or implementing custom integration.
/// Accounts for gravity and applied forces.
pub fn predict_position_using_velocity_and_forces(&self, dt: Real) -> Pose {
self.pos
.integrate_forces_and_velocities(dt, &self.forces, &self.vels, &self.mprops)
}
/// Calculates where this body will be after `dt` seconds, considering only current velocity (not forces).
///
/// Like `predict_position_using_velocity_and_forces()` but ignores applied forces.
/// Useful when you only care about inertial motion without acceleration.
pub fn predict_position_using_velocity(&self, dt: Real) -> Pose {
self.vels
.integrate(dt, &self.pos.position, &self.mprops.local_mprops.local_com)
}
pub(crate) fn update_world_mass_properties(&mut self) {
self.mprops
.update_world_mass_properties(self.body_type, &self.pos.position);
}
}
/// ## Applying forces and torques
impl RigidBody {
/// Clears all forces that were added with `add_force()`.
///
/// Forces are automatically cleared each physics step, so you rarely need this.
/// Useful if you want to cancel forces mid-frame.
pub fn reset_forces(&mut self, wake_up: bool) {
if self.forces.user_force != Vector::ZERO {
self.forces.user_force = Vector::ZERO;
if wake_up {
self.wake_up(true);
}
}
}
/// Clears all torques that were added with `add_torque()`.
///
/// Torques are automatically cleared each physics step. Rarely needed.
#[cfg(feature = "dim2")]
pub fn reset_torques(&mut self, wake_up: bool) {
if self.forces.user_torque != 0.0 {
self.forces.user_torque = 0.0;
if wake_up {
self.wake_up(true);
}
}
}
/// Clears all torques that were added with `add_torque()`.
///
/// Torques are automatically cleared each physics step. Rarely needed.
#[cfg(feature = "dim3")]
pub fn reset_torques(&mut self, wake_up: bool) {
if self.forces.user_torque != AngVector::ZERO {
self.forces.user_torque = AngVector::ZERO;
if wake_up {
self.wake_up(true);
}
}
}
/// Applies a continuous force to this body (like thrust, wind, or magnets).
///
/// Unlike [`apply_impulse()`](Self::apply_impulse) which is instant, forces are applied
/// continuously over time and accumulate until the next physics step. Use for:
/// - Rocket/jet thrust
/// - Wind or water currents
/// - Magnetic/gravity fields
/// - Continuous pushing/pulling
///
/// Forces are automatically cleared after each physics step, so you typically call this
/// every frame if you want continuous force application.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// // Apply thrust every frame
/// bodies[body].add_force(Vector::new(0.0, 100.0, 0.0), true);
/// ```
///
/// Only affects dynamic bodies (does nothing for kinematic/fixed bodies).
pub fn add_force(&mut self, force: Vector, wake_up: bool) {
if force != Vector::ZERO && self.body_type == RigidBodyType::Dynamic {
self.forces.user_force += force;
if wake_up {
self.wake_up(true);
}
}
}
/// Applies a continuous rotational force (torque) to spin this body.
///
/// Like `add_force()` but for rotation. Accumulates until next physics step.
/// In 2D: positive = counter-clockwise, negative = clockwise.
///
/// Only affects dynamic bodies.
#[cfg(feature = "dim2")]
pub fn add_torque(&mut self, torque: Real, wake_up: bool) {
if !torque.is_zero() && self.body_type == RigidBodyType::Dynamic {
self.forces.user_torque += torque;
if wake_up {
self.wake_up(true);
}
}
}
/// Applies a continuous rotational force (torque) to spin this body.
///
/// Like `add_force()` but for rotation. In 3D, the torque vector direction
/// determines the rotation axis (right-hand rule).
///
/// Only affects dynamic bodies.
#[cfg(feature = "dim3")]
pub fn add_torque(&mut self, torque: Vector, wake_up: bool) {
if torque != Vector::ZERO && self.body_type == RigidBodyType::Dynamic {
self.forces.user_torque += torque;
if wake_up {
self.wake_up(true);
}
}
}
/// Applies force at a specific point on the body (creates both force and torque).
///
/// When you push an object off-center, it both moves AND spins. This method handles both effects.
/// The force creates linear acceleration, and the offset from center-of-mass creates torque.
///
/// Use for: Forces applied at contact points, explosions at specific locations, pushing objects.
///
/// # Parameters
/// * `force` - The force vector to apply
/// * `point` - Where to apply the force (world coordinates)
///
/// Only affects dynamic bodies.
pub fn add_force_at_point(&mut self, force: Vector, point: Vector, wake_up: bool) {
if force != Vector::ZERO && self.body_type == RigidBodyType::Dynamic {
self.forces.user_force += force;
self.forces.user_torque += (point - self.mprops.world_com).gcross(force);
if wake_up {
self.wake_up(true);
}
}
}
}
/// ## Applying impulses and angular impulses
impl RigidBody {
/// Instantly changes the velocity by applying an impulse (like a kick or explosion).
///
/// An impulse is an instant change in momentum. Think of it as a "one-time push" that
/// immediately affects velocity. Use for:
/// - Jumping (apply upward impulse)
/// - Explosions pushing objects away
/// - Getting hit by something
/// - Launching projectiles
///
/// The effect depends on the body's mass - heavier objects will be affected less by the same impulse.
///
/// **For continuous forces** (like rocket thrust or wind), use [`add_force()`](Self::add_force) instead.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// // Make a character jump
/// bodies[body].apply_impulse(Vector::new(0.0, 300.0, 0.0), true);
/// ```
///
/// Only affects dynamic bodies (does nothing for kinematic/fixed bodies).
#[profiling::function]
pub fn apply_impulse(&mut self, impulse: Vector, wake_up: bool) {
if impulse != Vector::ZERO && self.body_type == RigidBodyType::Dynamic {
self.vels.linvel += impulse * self.mprops.effective_inv_mass;
if wake_up {
self.wake_up(true);
}
}
}
/// Applies an angular impulse at the center-of-mass of this rigid-body.
/// The impulse is applied right away, changing the angular velocity.
/// This does nothing on non-dynamic bodies.
#[cfg(feature = "dim2")]
#[profiling::function]
pub fn apply_torque_impulse(&mut self, torque_impulse: Real, wake_up: bool) {
if !torque_impulse.is_zero() && self.body_type == RigidBodyType::Dynamic {
self.vels.angvel += self.mprops.effective_world_inv_inertia * torque_impulse;
if wake_up {
self.wake_up(true);
}
}
}
/// Instantly changes rotation speed by applying angular impulse (like a sudden spin).
///
/// In 3D, the impulse vector direction determines the spin axis (right-hand rule).
/// Like `apply_impulse()` but for rotation. Only affects dynamic bodies.
#[cfg(feature = "dim3")]
#[profiling::function]
pub fn apply_torque_impulse(&mut self, torque_impulse: Vector, wake_up: bool) {
if torque_impulse != Vector::ZERO && self.body_type == RigidBodyType::Dynamic {
self.vels.angvel += self.mprops.effective_world_inv_inertia * torque_impulse;
if wake_up {
self.wake_up(true);
}
}
}
/// Applies impulse at a specific point on the body (creates both linear and angular effects).
///
/// Like `add_force_at_point()` but instant instead of continuous. When you hit an object
/// off-center, it both flies away AND spins - this method handles both.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// # let mut bodies = RigidBodySet::new();
/// # let body = bodies.insert(RigidBodyBuilder::dynamic());
/// // Hit the top-left corner of a box
/// bodies[body].apply_impulse_at_point(
/// Vector::new(100.0, 0.0, 0.0),
/// Vector::new(-0.5, 0.5, 0.0), // Top-left of a 1x1 box
/// true
/// );
/// // Box will move right AND spin
/// ```
///
/// Only affects dynamic bodies.
pub fn apply_impulse_at_point(&mut self, impulse: Vector, point: Vector, wake_up: bool) {
let torque_impulse = (point - self.mprops.world_com).gcross(impulse);
self.apply_impulse(impulse, wake_up);
self.apply_torque_impulse(torque_impulse, wake_up);
}
/// Returns the total force currently queued to be applied this frame.
///
/// This is the sum of all `add_force()` calls since the last physics step.
/// Returns zero for non-dynamic bodies.
pub fn user_force(&self) -> Vector {
if self.body_type == RigidBodyType::Dynamic {
self.forces.user_force
} else {
Vector::ZERO
}
}
/// Returns the total torque currently queued to be applied this frame.
///
/// This is the sum of all `add_torque()` calls since the last physics step.
/// Returns zero for non-dynamic bodies.
pub fn user_torque(&self) -> AngVector {
if self.body_type == RigidBodyType::Dynamic {
self.forces.user_torque
} else {
#[cfg(feature = "dim2")]
{
0.0
}
#[cfg(feature = "dim3")]
{
AngVector::ZERO
}
}
}
/// Checks if gyroscopic forces are enabled (3D only).
///
/// Gyroscopic forces cause spinning objects to resist changes in rotation axis
/// (like how spinning tops stay upright). Adds slight CPU cost.
#[cfg(feature = "dim3")]
pub fn gyroscopic_forces_enabled(&self) -> bool {
self.forces.gyroscopic_forces_enabled
}
/// Enables/disables gyroscopic forces for more realistic spinning behavior.
///
/// When enabled, rapidly spinning objects resist rotation axis changes (like gyroscopes).
/// Examples: spinning tops, flywheels, rotating spacecraft.
///
/// **Default**: Disabled (costs performance, rarely needed in games).
#[cfg(feature = "dim3")]
pub fn enable_gyroscopic_forces(&mut self, enabled: bool) {
self.forces.gyroscopic_forces_enabled = enabled;
}
}
impl RigidBody {
/// Calculates the velocity at a specific point on this body.
///
/// Due to rotation, different points on a rigid body move at different speeds.
/// This computes the linear velocity at any world-space point.
///
/// Useful for: impact calculations, particle effects, sound volume based on impact speed.
pub fn velocity_at_point(&self, point: Vector) -> Vector {
self.vels.velocity_at_point(point, self.mprops.world_com)
}
/// Calculates the kinetic energy of this body (energy from motion).
///
/// Returns `0.5 * mass * velocity² + 0.5 * inertia * angular_velocity²`
/// Useful for physics-based gameplay (energy tracking, damage based on impact energy).
pub fn kinetic_energy(&self) -> Real {
self.vels.kinetic_energy(&self.mprops)
}
/// Calculates the gravitational potential energy of this body.
///
/// Returns `mass * gravity * height`. Useful for energy conservation checks.
pub fn gravitational_potential_energy(&self, dt: Real, gravity: Vector) -> Real {
let world_com = self.mprops.local_mprops.world_com(&self.pos.position);
// Project position back along velocity vector one half-step (leap-frog)
// to sync up the potential energy with the kinetic energy:
let world_com = world_com - self.vels.linvel * (dt / 2.0);
-self.mass() * self.forces.gravity_scale * gravity.dot(world_com)
}
/// Computes the angular velocity of this rigid-body after application of gyroscopic forces.
#[cfg(feature = "dim3")]
pub fn angvel_with_gyroscopic_forces(&self, dt: Real) -> AngVector {
// NOTE: integrating the gyroscopic forces implicitly are both slower and
// very dissipative. Instead, we only keep the explicit term and
// ensure angular momentum is preserved (similar to Jolt).
let w = self.pos.position.rotation.inverse() * self.angvel();
let i = self.mprops.local_mprops.principal_inertia();
let ii = self.mprops.local_mprops.inv_principal_inertia;
let curr_momentum = i * w;
let explicit_gyro_momentum = -w.cross(curr_momentum) * dt;
let total_momentum = curr_momentum + explicit_gyro_momentum;
let total_momentum_sqnorm = total_momentum.length_squared();
if total_momentum_sqnorm != 0.0 {
let capped_momentum =
total_momentum * (curr_momentum.length_squared() / total_momentum_sqnorm).sqrt();
self.pos.position.rotation * (ii * capped_momentum)
} else {
self.angvel()
}
}
}
/// A builder for creating rigid bodies with custom properties.
///
/// This builder lets you configure all properties of a rigid body before adding it to your world.
/// Start with one of the type constructors ([`dynamic()`](Self::dynamic), [`fixed()`](Self::fixed),
/// [`kinematic_position_based()`](Self::kinematic_position_based), or
/// [`kinematic_velocity_based()`](Self::kinematic_velocity_based)), then chain property setters,
/// and finally call [`build()`](Self::build).
///
/// # Example
///
/// ```
/// # use rapier3d::prelude::*;
/// let body = RigidBodyBuilder::dynamic()
/// .translation(Vector::new(0.0, 5.0, 0.0)) // Start 5 units above ground
/// .linvel(Vector::new(1.0, 0.0, 0.0)) // Initial velocity to the right
/// .can_sleep(false) // Keep always active
/// .build();
/// ```
#[derive(Clone, Debug, PartialEq)]
#[must_use = "Builder functions return the updated builder"]
pub struct RigidBodyBuilder {
/// The initial position of the rigid-body to be built.
pub position: Pose,
/// The linear velocity of the rigid-body to be built.
pub linvel: Vector,
/// The angular velocity of the rigid-body to be built.
pub angvel: AngVector,
/// The scale factor applied to the gravity affecting the rigid-body to be built, `1.0` by default.
pub gravity_scale: Real,
/// Damping factor for gradually slowing down the translational motion of the rigid-body, `0.0` by default.
pub linear_damping: Real,
/// Damping factor for gradually slowing down the angular motion of the rigid-body, `0.0` by default.
pub angular_damping: Real,
/// The type of rigid-body being constructed.
pub body_type: RigidBodyType,
mprops_flags: LockedAxes,
/// The additional mass-properties of the rigid-body being built. See [`RigidBodyBuilder::additional_mass_properties`] for more information.
additional_mass_properties: RigidBodyAdditionalMassProps,
/// Whether the rigid-body to be created can sleep if it reaches a dynamic equilibrium.
pub can_sleep: bool,
/// Whether the rigid-body is to be created asleep.
pub sleeping: bool,
/// Whether Continuous Collision-Detection is enabled for the rigid-body to be built.
///
/// CCD prevents tunneling, but may still allow limited interpenetration of colliders.
pub ccd_enabled: bool,
/// The maximum prediction distance Soft Continuous Collision-Detection.
///
/// When set to 0, soft CCD is disabled. Soft-CCD helps prevent tunneling especially of
/// slow-but-thin to moderately fast objects. The soft CCD prediction distance indicates how
/// far in the object’s path the CCD algorithm is allowed to inspect. Large values can impact
/// performance badly by increasing the work needed from the broad-phase.
///
/// It is a generally cheaper variant of regular CCD (that can be enabled with
/// [`RigidBodyBuilder::ccd_enabled`] since it relies on predictive constraints instead of
/// shape-cast and substeps.
pub soft_ccd_prediction: Real,
/// The dominance group of the rigid-body to be built.
pub dominance_group: i8,
/// Will the rigid-body being built be enabled?
pub enabled: bool,
/// An arbitrary user-defined 128-bit integer associated to the rigid-bodies built by this builder.
pub user_data: u128,
/// The additional number of solver iterations run for this rigid-body and
/// everything interacting with it.
///
/// See [`RigidBody::set_additional_solver_iterations`] for additional information.
pub additional_solver_iterations: usize,
/// Are gyroscopic forces enabled for this rigid-body?
pub gyroscopic_forces_enabled: bool,
}
impl Default for RigidBodyBuilder {
fn default() -> Self {
Self::dynamic()
}
}
impl RigidBodyBuilder {
/// Initialize a new builder for a rigid body which is either fixed, dynamic, or kinematic.
pub fn new(body_type: RigidBodyType) -> Self {
#[cfg(feature = "dim2")]
let angvel = 0.0;
#[cfg(feature = "dim3")]
let angvel = AngVector::ZERO;
Self {
position: Pose::IDENTITY,
linvel: Vector::ZERO,
angvel,
gravity_scale: 1.0,
linear_damping: 0.0,
angular_damping: 0.0,
body_type,
mprops_flags: LockedAxes::empty(),
additional_mass_properties: RigidBodyAdditionalMassProps::default(),
can_sleep: true,
sleeping: false,
ccd_enabled: false,
soft_ccd_prediction: 0.0,
dominance_group: 0,
enabled: true,
user_data: 0,
additional_solver_iterations: 0,
gyroscopic_forces_enabled: false,
}
}
/// Initializes the builder of a new fixed rigid body.
#[deprecated(note = "use `RigidBodyBuilder::fixed()` instead")]
pub fn new_static() -> Self {
Self::fixed()
}
/// Initializes the builder of a new velocity-based kinematic rigid body.
#[deprecated(note = "use `RigidBodyBuilder::kinematic_velocity_based()` instead")]
pub fn new_kinematic_velocity_based() -> Self {
Self::kinematic_velocity_based()
}
/// Initializes the builder of a new position-based kinematic rigid body.
#[deprecated(note = "use `RigidBodyBuilder::kinematic_position_based()` instead")]
pub fn new_kinematic_position_based() -> Self {
Self::kinematic_position_based()
}
/// Creates a builder for a **fixed** (static) rigid body.
///
/// Fixed bodies never move and are not affected by any forces. Use them for:
/// - Walls, floors, and ceilings
/// - Static terrain and level geometry
/// - Any object that should never move in your simulation
///
/// Fixed bodies have infinite mass and never sleep.
pub fn fixed() -> Self {
Self::new(RigidBodyType::Fixed)
}
/// Creates a builder for a **velocity-based kinematic** rigid body.
///
/// Kinematic bodies are moved by directly setting their velocity (not by applying forces).
/// They can push dynamic bodies but are not affected by them. Use for:
/// - Moving platforms and elevators
/// - Doors and sliding panels
/// - Any object you want to control directly while still affecting other physics objects
///
/// Set velocity with [`RigidBody::set_linvel`] and [`RigidBody::set_angvel`].
pub fn kinematic_velocity_based() -> Self {
Self::new(RigidBodyType::KinematicVelocityBased)
}
/// Creates a builder for a **position-based kinematic** rigid body.
///
/// Similar to velocity-based kinematic, but you control it by setting its next position
/// directly rather than setting velocity. Rapier will automatically compute the velocity
/// needed to reach that position. Use for objects animated by external systems.
pub fn kinematic_position_based() -> Self {
Self::new(RigidBodyType::KinematicPositionBased)
}
/// Creates a builder for a **dynamic** rigid body.
///
/// Dynamic bodies are fully simulated - they respond to gravity, forces, collisions, and
/// constraints. This is the most common type for interactive objects. Use for:
/// - Physics objects that should fall and bounce (boxes, spheres, ragdolls)
/// - Projectiles and debris
/// - Vehicles and moving characters (when not using kinematic control)
/// - Any object that should behave realistically under physics
///
/// Dynamic bodies can sleep (become inactive) when at rest to save performance.
pub fn dynamic() -> Self {
Self::new(RigidBodyType::Dynamic)
}
/// Sets the additional number of solver iterations run for this rigid-body and
/// everything interacting with it.
///
/// See [`RigidBody::set_additional_solver_iterations`] for additional information.
pub fn additional_solver_iterations(mut self, additional_iterations: usize) -> Self {
self.additional_solver_iterations = additional_iterations;
self
}
/// Sets the scale applied to the gravity force affecting the rigid-body to be created.
pub fn gravity_scale(mut self, scale_factor: Real) -> Self {
self.gravity_scale = scale_factor;
self
}
/// Sets the dominance group (advanced collision priority system).
///
/// Higher dominance groups can push lower ones but not vice versa.
/// Rarely needed - most games don't use this. Default is 0 (all equal priority).
///
/// Use case: Heavy objects that should always push lighter ones in contacts.
pub fn dominance_group(mut self, group: i8) -> Self {
self.dominance_group = group;
self
}
/// Sets the initial position (XYZ coordinates) where this body will be created.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// let body = RigidBodyBuilder::dynamic()
/// .translation(Vector::new(10.0, 5.0, -3.0))
/// .build();
/// ```
pub fn translation(mut self, translation: Vector) -> Self {
self.position.translation = translation;
self
}
/// Sets the initial rotation/orientation of the body to be created.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// // Rotate 45 degrees around Y axis (in 3D)
/// let body = RigidBodyBuilder::dynamic()
/// .rotation(Vector::new(0.0, std::f32::consts::PI / 4.0, 0.0))
/// .build();
/// ```
pub fn rotation(mut self, angle: AngVector) -> Self {
self.position.rotation = rotation_from_angle(angle);
self
}
/// Sets the initial position (translation and orientation) of the rigid-body to be created.
#[deprecated = "renamed to `RigidBodyBuilder::pose`"]
pub fn position(mut self, pos: Pose) -> Self {
self.position = pos;
self
}
/// Sets the initial pose (translation and orientation) of the rigid-body to be created.
pub fn pose(mut self, pos: Pose) -> Self {
self.position = pos;
self
}
/// An arbitrary user-defined 128-bit integer associated to the rigid-bodies built by this builder.
pub fn user_data(mut self, data: u128) -> Self {
self.user_data = data;
self
}
/// Sets the additional mass-properties of the rigid-body being built.
///
/// This will be overridden by a call to [`Self::additional_mass`] so it only makes sense to call
/// either [`Self::additional_mass`] or [`Self::additional_mass_properties`].
///
/// Note that "additional" means that the final mass-properties of the rigid-bodies depends
/// on the initial mass-properties of the rigid-body (set by this method)
/// to which is added the contributions of all the colliders with non-zero density
/// attached to this rigid-body.
///
/// Therefore, if you want your provided mass-properties to be the final
/// mass-properties of your rigid-body, don't attach colliders to it, or
/// only attach colliders with densities equal to zero.
pub fn additional_mass_properties(mut self, mprops: MassProperties) -> Self {
self.additional_mass_properties = RigidBodyAdditionalMassProps::MassProps(mprops);
self
}
/// Sets the additional mass of the rigid-body being built.
///
/// This will be overridden by a call to [`Self::additional_mass_properties`] so it only makes
/// sense to call either [`Self::additional_mass`] or [`Self::additional_mass_properties`].
///
/// This is only the "additional" mass because the total mass of the rigid-body is
/// equal to the sum of this additional mass and the mass computed from the colliders
/// (with non-zero densities) attached to this rigid-body.
///
/// The total angular inertia of the rigid-body will be scaled automatically based on this
/// additional mass. If this scaling effect isn’t desired, use [`Self::additional_mass_properties`]
/// instead of this method.
///
/// # Parameters
/// * `mass`- The mass that will be added to the created rigid-body.
pub fn additional_mass(mut self, mass: Real) -> Self {
self.additional_mass_properties = RigidBodyAdditionalMassProps::Mass(mass);
self
}
/// Sets which movement axes are locked (cannot move/rotate).
///
/// See [`LockedAxes`] for examples of constraining movement to specific directions.
pub fn locked_axes(mut self, locked_axes: LockedAxes) -> Self {
self.mprops_flags = locked_axes;
self
}
/// Prevents all translational movement (body can still rotate).
///
/// Use for turrets, spinning objects fixed in place, etc.
pub fn lock_translations(mut self) -> Self {
self.mprops_flags.set(LockedAxes::TRANSLATION_LOCKED, true);
self
}
/// Locks translation along specific axes.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// // 2D game in 3D: lock Z movement
/// let body = RigidBodyBuilder::dynamic()
/// .enabled_translations(true, true, false) // X, Y free; Z locked
/// .build();
/// ```
pub fn enabled_translations(
mut self,
allow_translations_x: bool,
allow_translations_y: bool,
#[cfg(feature = "dim3")] allow_translations_z: bool,
) -> Self {
self.mprops_flags
.set(LockedAxes::TRANSLATION_LOCKED_X, !allow_translations_x);
self.mprops_flags
.set(LockedAxes::TRANSLATION_LOCKED_Y, !allow_translations_y);
#[cfg(feature = "dim3")]
self.mprops_flags
.set(LockedAxes::TRANSLATION_LOCKED_Z, !allow_translations_z);
self
}
#[deprecated(note = "Use `enabled_translations` instead")]
/// Only allow translations of this rigid-body around specific coordinate axes.
pub fn restrict_translations(
self,
allow_translations_x: bool,
allow_translations_y: bool,
#[cfg(feature = "dim3")] allow_translations_z: bool,
) -> Self {
self.enabled_translations(
allow_translations_x,
allow_translations_y,
#[cfg(feature = "dim3")]
allow_translations_z,
)
}
/// Prevents all rotational movement (body can still translate).
///
/// Use for characters that shouldn't tip over, objects that should only slide, etc.
pub fn lock_rotations(mut self) -> Self {
self.mprops_flags.set(LockedAxes::ROTATION_LOCKED_X, true);
self.mprops_flags.set(LockedAxes::ROTATION_LOCKED_Y, true);
self.mprops_flags.set(LockedAxes::ROTATION_LOCKED_Z, true);
self
}
/// Only allow rotations of this rigid-body around specific coordinate axes.
#[cfg(feature = "dim3")]
pub fn enabled_rotations(
mut self,
allow_rotations_x: bool,
allow_rotations_y: bool,
allow_rotations_z: bool,
) -> Self {
self.mprops_flags
.set(LockedAxes::ROTATION_LOCKED_X, !allow_rotations_x);
self.mprops_flags
.set(LockedAxes::ROTATION_LOCKED_Y, !allow_rotations_y);
self.mprops_flags
.set(LockedAxes::ROTATION_LOCKED_Z, !allow_rotations_z);
self
}
/// Locks or unlocks rotations of this rigid-body along each cartesian axes.
#[deprecated(note = "Use `enabled_rotations` instead")]
#[cfg(feature = "dim3")]
pub fn restrict_rotations(
self,
allow_rotations_x: bool,
allow_rotations_y: bool,
allow_rotations_z: bool,
) -> Self {
self.enabled_rotations(allow_rotations_x, allow_rotations_y, allow_rotations_z)
}
/// Sets linear damping (how quickly linear velocity decreases over time).
///
/// Models air resistance, drag, etc. Higher values = faster slowdown.
/// - `0.0` = no drag (space)
/// - `0.1` = light drag (air)
/// - `1.0+` = heavy drag (underwater)
pub fn linear_damping(mut self, factor: Real) -> Self {
self.linear_damping = factor;
self
}
/// Sets angular damping (how quickly rotation speed decreases over time).
///
/// Models rotational drag. Higher values = spinning stops faster.
pub fn angular_damping(mut self, factor: Real) -> Self {
self.angular_damping = factor;
self
}
/// Sets the initial linear velocity (movement speed and direction).
///
/// The body will start moving at this velocity when created.
pub fn linvel(mut self, linvel: Vector) -> Self {
self.linvel = linvel;
self
}
/// Sets the initial angular velocity (rotation speed).
///
/// The body will start rotating at this speed when created.
pub fn angvel(mut self, angvel: AngVector) -> Self {
self.angvel = angvel;
self
}
/// Sets whether this body can go to sleep when at rest (default: `true`).
///
/// Sleeping bodies are excluded from simulation until disturbed, saving CPU.
/// Set to `false` if you need the body always active (e.g., for continuous queries).
pub fn can_sleep(mut self, can_sleep: bool) -> Self {
self.can_sleep = can_sleep;
self
}
/// Enables Continuous Collision Detection to prevent fast objects from tunneling.
///
/// CCD prevents "tunneling" where fast-moving objects pass through thin walls.
/// Enable this for:
/// - Bullets and fast projectiles
/// - Small objects moving at high speed
/// - Objects that must never pass through walls
///
/// **Trade-off**: More accurate but more expensive. Most objects don't need CCD.
///
/// # Example
/// ```
/// # use rapier3d::prelude::*;
/// // Bullet that should never tunnel through walls
/// let bullet = RigidBodyBuilder::dynamic()
/// .ccd_enabled(true)
/// .build();
/// ```
pub fn ccd_enabled(mut self, enabled: bool) -> Self {
self.ccd_enabled = enabled;
self
}
/// Sets the maximum prediction distance Soft Continuous Collision-Detection.
///
/// When set to 0, soft-CCD is disabled. Soft-CCD helps prevent tunneling especially of
/// slow-but-thin to moderately fast objects. The soft CCD prediction distance indicates how
/// far in the object’s path the CCD algorithm is allowed to inspect. Large values can impact
/// performance badly by increasing the work needed from the broad-phase.
///
/// It is a generally cheaper variant of regular CCD (that can be enabled with
/// [`RigidBodyBuilder::ccd_enabled`] since it relies on predictive constraints instead of
/// shape-cast and substeps.
pub fn soft_ccd_prediction(mut self, prediction_distance: Real) -> Self {
self.soft_ccd_prediction = prediction_distance;
self
}
/// Sets whether the rigid-body is to be created asleep.
pub fn sleeping(mut self, sleeping: bool) -> Self {
self.sleeping = sleeping;
self
}
/// Are gyroscopic forces enabled for this rigid-body?
///
/// Enabling gyroscopic forces allows more realistic behaviors like gyroscopic precession,
/// but result in a slight performance overhead.
///
/// Disabled by default.
#[cfg(feature = "dim3")]
pub fn gyroscopic_forces_enabled(mut self, enabled: bool) -> Self {
self.gyroscopic_forces_enabled = enabled;
self
}
/// Enable or disable the rigid-body after its creation.
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
/// Build a new rigid-body with the parameters configured with this builder.
pub fn build(&self) -> RigidBody {
let mut rb = RigidBody::new();
rb.pos.next_position = self.position;
rb.pos.position = self.position;
rb.vels.linvel = self.linvel;
rb.vels.angvel = self.angvel;
rb.body_type = self.body_type;
rb.user_data = self.user_data;
rb.additional_solver_iterations = self.additional_solver_iterations;
if self.additional_mass_properties
!= RigidBodyAdditionalMassProps::MassProps(MassProperties::default())
&& self.additional_mass_properties != RigidBodyAdditionalMassProps::Mass(0.0)
{
rb.mprops.additional_local_mprops = Some(Box::new(self.additional_mass_properties));
}
rb.mprops.flags = self.mprops_flags;
rb.damping.linear_damping = self.linear_damping;
rb.damping.angular_damping = self.angular_damping;
rb.forces.gravity_scale = self.gravity_scale;
#[cfg(feature = "dim3")]
{
rb.forces.gyroscopic_forces_enabled = self.gyroscopic_forces_enabled;
}
rb.dominance = RigidBodyDominance(self.dominance_group);
rb.enabled = self.enabled;
rb.enable_ccd(self.ccd_enabled);
rb.set_soft_ccd_prediction(self.soft_ccd_prediction);
if self.can_sleep && self.sleeping {
rb.sleep();
}
if !self.can_sleep {
rb.activation.normalized_linear_threshold = -1.0;
rb.activation.angular_threshold = -1.0;
}
rb
}
}
impl From<RigidBodyBuilder> for RigidBody {
fn from(val: RigidBodyBuilder) -> RigidBody {
val.build()
}
}