Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of magic numbers in DynamicRaycastVehicleController #495

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/control/ray_cast_vehicle_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::dynamics::{RigidBody, RigidBodyHandle, RigidBodySet};
use crate::geometry::{ColliderHandle, ColliderSet, Ray};
use crate::math::{Point, Real, Rotation, Vector};
use crate::pipeline::{QueryFilter, QueryPipeline};
use crate::utils::{WCross, WDot};
use crate::utils::{Axis, WCross, WDot};

/// A character controller to simulate vehicles using ray-casting for the wheels.
pub struct DynamicRayCastVehicleController {
Expand All @@ -17,9 +17,9 @@ pub struct DynamicRayCastVehicleController {
/// Handle of the vehicle’s chassis.
pub chassis: RigidBodyHandle,
/// The chassis’ local _up_ direction (`0 = x, 1 = y, 2 = z`)
pub index_up_axis: usize,
pub index_up_axis: Axis,
/// The chassis’ local _forward_ direction (`0 = x, 1 = y, 2 = z`)
pub index_forward_axis: usize,
pub index_forward_axis: Axis,
}

#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -238,8 +238,8 @@ impl DynamicRayCastVehicleController {
axle: vec![],
current_vehicle_speed: 0.0,
chassis,
index_up_axis: 1,
index_forward_axis: 0,
index_up_axis: Axis::Y,
index_forward_axis: Axis::X,
}
}

Expand Down Expand Up @@ -407,7 +407,7 @@ impl DynamicRayCastVehicleController {

self.current_vehicle_speed = chassis.linvel().norm();

let forward_w = chassis.position() * Vector::ith(self.index_forward_axis, 1.0);
let forward_w = chassis.position() * Vector::ith(self.index_forward_axis as usize, 1.0);

if forward_w.dot(chassis.linvel()) < 0.0 {
self.current_vehicle_speed *= -1.0;
Expand Down Expand Up @@ -454,7 +454,8 @@ impl DynamicRayCastVehicleController {
let vel = chassis.velocity_at_point(&wheel.raycast_info.hard_point_ws);

if wheel.raycast_info.is_in_contact {
let mut fwd = chassis.position() * Vector::ith(self.index_forward_axis, 1.0);
let mut fwd =
chassis.position() * Vector::ith(self.index_forward_axis as usize, 1.0);
let proj = fwd.dot(&wheel.raycast_info.contact_normal_ws);
fwd -= wheel.raycast_info.contact_normal_ws * proj;

Expand Down Expand Up @@ -681,7 +682,7 @@ impl DynamicRayCastVehicleController {
let side_impulse = self.axle[wheel_id] * wheel.side_impulse;

let v_chassis_world_up =
chassis.position().rotation * Vector::ith(self.index_up_axis, 1.0);
chassis.position().rotation * Vector::ith(self.index_up_axis as usize, 1.0);
impulse_point -= v_chassis_world_up
* (v_chassis_world_up.dot(&(impulse_point - chassis.center_of_mass()))
* (1.0 - wheel.roll_influence));
Expand Down
9 changes: 9 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,12 @@ impl<T> IndexMut2<usize> for [T] {
}
}
}

#[repr(u8)]
#[derive(Clone, Copy)]
/// One of 3 spatial axes
pub enum Axis {
X = 0,
Y = 1,
Z = 2,
Copy link
Contributor

@Vrixyz Vrixyz May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of 2d dimension, the ZAxis is invalid though, this line should be gated by a

Suggested change
Z = 2,
#[cfg(feature = "dim3")]
Z = 2,

}