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
//! Wrapping of Rapiers rigid bodies to be used with Let Engine and Glam.
use glam::Vec2;
use rapier2d::prelude::*;
#[derive(Clone, Default, Debug)]
pub struct RigidBody(pub(crate) rapier2d::dynamics::RigidBody);
impl RigidBody {
/// The activation status of this rigid-body.
pub fn activation(&self) -> &RigidBodyActivation {
self.0.activation()
}
/// Mutable reference to the activation status of this rigid-body.
pub fn activation_mut(&mut self) -> &mut RigidBodyActivation {
self.0.activation_mut()
}
/// Is this rigid-body enabled?
pub fn is_enabled(&self) -> bool {
self.0.is_enabled()
}
/// Sets whether this rigid-body is enabled or not.
pub fn set_enabled(&mut self, enabled: bool) {
self.0.set_enabled(enabled)
}
/// The linear damping coefficient of this rigid-body.
#[inline]
pub fn linear_damping(&self) -> Real {
self.0.linear_damping()
}
/// Sets the linear damping coefficient of this rigid-body.
#[inline]
pub fn set_linear_damping(&mut self, damping: Real) {
self.0.set_linear_damping(damping)
}
/// The angular damping coefficient of this rigid-body.
#[inline]
pub fn angular_damping(&self) -> Real {
self.0.angular_damping()
}
/// Sets the angular damping coefficient of this rigid-body.
#[inline]
pub fn set_angular_damping(&mut self, damping: Real) {
self.0.set_angular_damping(damping)
}
/// The type of this rigid-body.
pub fn body_type(&self) -> RigidBodyType {
self.0.body_type()
}
/// Sets the type of this rigid-body.
pub fn set_body_type(&mut self, status: RigidBodyType, wake_up: bool) {
self.0.set_body_type(status, wake_up)
}
/// The world-space center-of-mass of this rigid-body.
#[inline]
pub fn center_of_mass(&self) -> Vec2 {
(*self.0.center_of_mass()).into()
}
/// The mass-properties of this rigid-body.
#[inline]
pub fn mass_properties(&self) -> &RigidBodyMassProps {
self.0.mass_properties()
}
/// 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.0.effective_dominance_group()
}
/// The axes along which this rigid-body cannot translate or rotate.
#[inline]
pub fn locked_axes(&self) -> LockedAxes {
self.0.locked_axes()
}
/// 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) {
self.0.set_locked_axes(locked_axes, wake_up)
}
#[inline]
/// Locks or unlocks all the rotations of this rigid-body.
pub fn lock_rotations(&mut self, locked: bool, wake_up: bool) {
self.0.lock_rotations(locked, wake_up)
}
#[inline]
/// Locks or unlocks all the rotations of this rigid-body.
pub fn lock_translations(&mut self, locked: bool, wake_up: bool) {
self.0.lock_translations(locked, wake_up)
}
#[inline]
/// Locks or unlocks translation of this rigid-body along each cartesian axes.
pub fn set_enabled_translations(
&mut self,
allow_translation_x: bool,
allow_translation_y: bool,
wake_up: bool,
) {
self.0
.set_enabled_translations(allow_translation_x, allow_translation_y, wake_up)
}
/// Are the translations of this rigid-body locked?
pub fn is_translation_locked(&self) -> bool {
self.0.is_translation_locked()
}
/// Is the rotation of this rigid-body locked?
pub fn is_rotation_locked(&self) -> bool {
self.0.is_rotation_locked()
}
/// Enables of disable CCD (continuous collision-detection) for this rigid-body.
///
/// CCD prevents tunneling, but may still allow limited interpenetration of colliders.
pub fn enable_ccd(&mut self, enabled: bool) {
self.0.enable_ccd(enabled)
}
/// Is CCD (continous collision-detection) enabled for this rigid-body?
pub fn is_ccd_enabled(&self) -> bool {
self.0.is_ccd_enabled()
}
// 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.0.is_ccd_active()
}
/// Is this rigid body dynamic?
///
/// A dynamic body can move freely and is affected by forces.
pub fn is_dynamic(&self) -> bool {
self.0.is_dynamic()
}
/// Is this rigid body kinematic?
///
/// A kinematic body can move freely but is not affected by forces.
pub fn is_kinematic(&self) -> bool {
self.0.is_kinematic()
}
/// Is this rigid body fixed?
///
/// A fixed body cannot move and is not affected by forces.
pub fn is_fixed(&self) -> bool {
self.0.is_fixed()
}
/// The mass of this rigid body.
///
/// Returns zero if this rigid body has an infinite mass.
pub fn mass(&self) -> Real {
self.0.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) -> (Vec2, f32) {
(*self.0.next_position()).into()
}
/// The scale factor applied to the gravity affecting this rigid-body.
pub fn gravity_scale(&self) -> Real {
self.0.gravity_scale()
}
/// Sets the gravity scale facter for this rigid-body.
pub fn set_gravity_scale(&mut self, scale: Real, wake_up: bool) {
self.0.set_gravity_scale(scale, wake_up)
}
/// The dominance group of this rigid-body.
pub fn dominance_group(&self) -> i8 {
self.0.dominance_group()
}
/// The dominance group of this rigid-body.
pub fn set_dominance_group(&mut self, dominance: i8) {
self.0.set_dominance_group(dominance)
}
/// Put this rigid body to sleep.
///
/// A sleeping body no longer moves and is no longer simulated by the physics engine unless
/// it is waken up. It can be woken manually with `self.wake_up` or automatically due to
/// external forces like contacts.
pub fn sleep(&mut self) {
self.0.sleep()
}
/// Wakes up this rigid body if it is sleeping.
///
/// If `strong` is `true` then it is assured that the rigid-body will
/// remain awake during multiple subsequent timesteps.
pub fn wake_up(&mut self, strong: bool) {
self.0.wake_up(strong)
}
/// Is this rigid body sleeping?
pub fn is_sleeping(&self) -> bool {
self.0.is_sleeping()
}
/// Is the velocity of this body not zero?
pub fn is_moving(&self) -> bool {
self.0.is_moving()
}
/// The linear velocity of this rigid-body.
pub fn linvel(&self) -> Vec2 {
(*self.0.linvel()).into()
}
/// The angular velocity of this rigid-body.
pub fn angvel(&self) -> Real {
self.0.angvel()
}
pub fn set_linvel(&mut self, linvel: Vec2, wake_up: bool) {
self.0.set_linvel(linvel.into(), wake_up)
}
/// 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.
pub fn set_angvel(&mut self, angvel: Real, wake_up: bool) {
self.0.set_angvel(angvel, wake_up)
}
/// If this rigid body is kinematic, sets its future translation after the next timestep integration.
pub fn set_next_kinematic_rotation(&mut self, rotation: Real) {
if self.is_kinematic() {
self.0
.set_next_kinematic_rotation(Rotation::from_angle(rotation));
}
}
/// If this rigid body is kinematic, sets its future translation after the next timestep integration.
pub fn set_next_kinematic_translation(&mut self, translation: Vec2) {
self.0.set_next_kinematic_translation(translation.into())
}
/// If this rigid body is kinematic, sets its future position after the next timestep integration.
pub fn set_next_kinematic_position(&mut self, pos: (Vec2, f32)) {
self.0.set_next_kinematic_position(pos.into())
}
/// Predicts the next position of this rigid-body, by integrating its velocity and forces
/// by a time of `dt`.
pub fn predict_position_using_velocity_and_forces(&self, dt: Real) -> (Vec2, f32) {
self.0.predict_position_using_velocity_and_forces(dt).into()
}
}
impl RigidBody {
/// The velocity of the given world-space point on this rigid-body.
pub fn velocity_at_point(&self, point: Vec2) -> Vec2 {
let pos = *self.0.velocity_at_point(&point.into());
(pos.x, pos.y).into()
}
/// The kinetic energy of this body.
pub fn kinetic_energy(&self) -> Real {
self.0.kinetic_energy()
}
/// The potential energy of this body in a gravity field.
pub fn gravitational_potential_energy(&self, dt: Real, gravity: Vec2) -> Real {
self.0.gravitational_potential_energy(dt, gravity.into())
}
}
/// ## Applying forces and torques
impl RigidBody {
/// Resets to zero all the constant (linear) forces manually applied to this rigid-body.
pub fn reset_forces(&mut self, wake_up: bool) {
self.0.reset_forces(wake_up)
}
/// Resets to zero all the constant torques manually applied to this rigid-body.
pub fn reset_torques(&mut self, wake_up: bool) {
self.0.reset_torques(wake_up)
}
/// Adds to this rigid-body a constant force applied at its center-of-mass.ç
///
/// This does nothing on non-dynamic bodies.
pub fn add_force(&mut self, force: Vec2, wake_up: bool) {
self.0.add_force(force.into(), wake_up)
}
/// Adds to this rigid-body a constant torque at its center-of-mass.
///
/// This does nothing on non-dynamic bodies.
pub fn add_torque(&mut self, torque: Real, wake_up: bool) {
self.0.add_torque(torque, wake_up)
}
/// Adds to this rigid-body a constant force at the given world-space point of this rigid-body.
///
/// This does nothing on non-dynamic bodies.
pub fn add_force_at_point(&mut self, force: Vec2, point: Vec2, wake_up: bool) {
self.0
.add_force_at_point(force.into(), point.into(), wake_up)
}
}
/// ## Applying impulses and angular impulses
impl RigidBody {
/// Applies an impulse at the center-of-mass of this rigid-body.
/// The impulse is applied right away, changing the linear velocity.
/// This does nothing on non-dynamic bodies.
pub fn apply_impulse(&mut self, impulse: Vec2, wake_up: bool) {
self.0.apply_impulse(impulse.into(), wake_up)
}
/// 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.
pub fn apply_torque_impulse(&mut self, torque_impulse: Real, wake_up: bool) {
self.0.apply_torque_impulse(torque_impulse, wake_up)
}
/// Applies an impulse at the given world-space point of this rigid-body.
/// The impulse is applied right away, changing the linear and/or angular velocities.
/// This does nothing on non-dynamic bodies.
pub fn apply_impulse_at_point(&mut self, impulse: Vec2, point: Vec2, wake_up: bool) {
self.0
.apply_impulse_at_point(impulse.into(), point.into(), wake_up)
}
/// Retrieves the constant force(s) that the user has added to the body.
///
/// Returns zero if the rigid-body isn’t dynamic.
pub fn user_force(&self) -> Vec2 {
self.0.user_force().into()
}
}
/// A builder for rigid-bodies.
#[derive(Clone, Debug, PartialEq)]
#[must_use = "Builder functions return the updated builder"]
pub struct RigidBodyBuilder {
/// The linear velocity of the rigid-body to be built.
pub linvel: Vec2,
/// The angular velocity of the rigid-body to be built.
pub angvel: Real,
/// 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,
body_type: RigidBodyType,
mprops_flags: LockedAxes,
/// Whether or not the rigid-body to be created can sleep if it reaches a dynamic equilibrium.
pub can_sleep: bool,
/// Whether or not 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 dominance group of the rigid-body to be built.
pub dominance_group: i8,
/// Will the rigid-body being built be enabled?
pub enabled: bool,
}
impl RigidBodyBuilder {
/// Initialize a new builder for a rigid body which is either fixed, dynamic, or kinematic.
pub fn new(body_type: RigidBodyType) -> Self {
Self {
linvel: Vec2::default(),
angvel: 0.0,
gravity_scale: 1.0,
linear_damping: 0.0,
angular_damping: 0.0,
body_type,
mprops_flags: LockedAxes::empty(),
can_sleep: true,
sleeping: false,
ccd_enabled: false,
dominance_group: 0,
enabled: true,
}
}
/// Initializes the builder of a new fixed rigid body.
pub fn fixed() -> Self {
Self::new(RigidBodyType::Fixed)
}
/// Initializes the builder of a new velocity-based kinematic rigid body.
pub fn kinematic_velocity_based() -> Self {
Self::new(RigidBodyType::KinematicVelocityBased)
}
/// Initializes the builder of a new position-based kinematic rigid body.
pub fn kinematic_position_based() -> Self {
Self::new(RigidBodyType::KinematicPositionBased)
}
/// Initializes the builder of a new dynamic rigid body.
pub fn dynamic() -> Self {
Self::new(RigidBodyType::Dynamic)
}
/// 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 of this rigid-body.
pub fn dominance_group(mut self, group: i8) -> Self {
self.dominance_group = group;
self
}
/// Sets the axes along which this rigid-body cannot translate or rotate.
pub fn locked_axes(mut self, locked_axes: LockedAxes) -> Self {
self.mprops_flags = locked_axes;
self
}
/// Prevents this rigid-body from translating because of forces.
pub fn lock_translations(mut self) -> Self {
self.mprops_flags.set(LockedAxes::TRANSLATION_LOCKED, true);
self
}
/// Only allow translations of this rigid-body around specific coordinate axes.
pub fn enabled_translations(
mut self,
allow_translations_x: bool,
allow_translations_y: bool,
) -> Self {
self.mprops_flags
.set(LockedAxes::TRANSLATION_LOCKED_X, !allow_translations_x);
self.mprops_flags
.set(LockedAxes::TRANSLATION_LOCKED_Y, !allow_translations_y);
self
}
/// Prevents this rigid-body from rotating because of forces.
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
}
/// Sets the damping factor for the linear part of the rigid-body motion.
///
/// The higher the linear damping factor is, the more quickly the rigid-body
/// will slow-down its translational movement.
pub fn linear_damping(mut self, factor: Real) -> Self {
self.linear_damping = factor;
self
}
/// Sets the damping factor for the angular part of the rigid-body motion.
///
/// The higher the angular damping factor is, the more quickly the rigid-body
/// will slow-down its rotational movement.
pub fn angular_damping(mut self, factor: Real) -> Self {
self.angular_damping = factor;
self
}
/// Sets the initial linear velocity of the rigid-body to be created.
pub fn linvel(mut self, linvel: Vec2) -> Self {
self.linvel = linvel;
self
}
/// Sets the initial angular velocity of the rigid-body to be created.
pub fn angvel(mut self, angvel: Real) -> Self {
self.angvel = angvel;
self
}
/// Sets whether or not the rigid-body to be created can sleep if it reaches a dynamic equilibrium.
pub fn can_sleep(mut self, can_sleep: bool) -> Self {
self.can_sleep = can_sleep;
self
}
/// Sets whether or not continuous collision-detection is enabled for this rigid-body.
///
/// CCD prevents tunneling, but may still allow limited interpenetration of colliders.
pub fn ccd_enabled(mut self, enabled: bool) -> Self {
self.ccd_enabled = enabled;
self
}
/// Sets whether or not the rigid-body is to be created asleep.
pub fn sleeping(mut self, sleeping: bool) -> Self {
self.sleeping = sleeping;
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 builder = rapier2d::dynamics::RigidBodyBuilder::new(self.body_type)
.locked_axes(self.mprops_flags)
.linvel(self.linvel.into())
.angvel(self.angvel)
.gravity_scale(self.gravity_scale)
.linear_damping(self.linear_damping)
.angular_damping(self.angular_damping)
.can_sleep(self.can_sleep)
.sleeping(self.sleeping)
.ccd_enabled(self.ccd_enabled)
.dominance_group(self.dominance_group)
.enabled(self.enabled);
RigidBody(builder.into())
}
}
impl From<RigidBodyBuilder> for RigidBody {
fn from(val: RigidBodyBuilder) -> Self {
val.build()
}
}