feat: 添加跨平台运行时、资产系统和UI适配功能 (#256)
* feat(platform-common): 添加WASM加载器和环境检测API * feat(rapier2d): 新增Rapier2D WASM绑定包 * feat(physics-rapier2d): 添加跨平台WASM加载器 * feat(asset-system): 添加运行时资产目录和bundle格式 * feat(asset-system-editor): 新增编辑器资产管理包 * feat(editor-core): 添加构建系统和模块管理 * feat(editor-app): 重构浏览器预览使用import maps * feat(platform-web): 添加BrowserRuntime和资产读取 * feat(engine): 添加材质系统和着色器管理 * feat(material): 新增材质系统和着色器编辑器 * feat(tilemap): 增强tilemap编辑器和动画系统 * feat(modules): 添加module.json配置 * feat(core): 添加module.json和类型定义更新 * chore: 更新依赖和构建配置 * refactor(plugins): 更新插件模板使用ModuleManifest * chore: 添加第三方依赖库 * chore: 移除BehaviourTree-ai和ecs-astar子模块 * docs: 更新README和文档主题样式 * fix: 修复Rust文档测试和添加rapier2d WASM绑定 * fix(tilemap-editor): 修复画布高DPI屏幕分辨率适配问题 * feat(ui): 添加UI屏幕适配系统(CanvasScaler/SafeArea) * fix(ecs-engine-bindgen): 添加缺失的ecs-framework-math依赖 * fix: 添加缺失的包依赖修复CI构建 * fix: 修复CodeQL检测到的代码问题 * fix: 修复构建错误和缺失依赖 * fix: 修复类型检查错误 * fix(material-system): 修复tsconfig配置支持TypeScript项目引用 * fix(editor-core): 修复Rollup构建配置添加tauri external * fix: 修复CodeQL检测到的代码问题 * fix: 修复CodeQL检测到的代码问题
This commit is contained in:
290
thirdparty/rapier.js/src/control/character_controller.rs
vendored
Normal file
290
thirdparty/rapier.js/src/control/character_controller.rs
vendored
Normal file
@@ -0,0 +1,290 @@
|
||||
use crate::dynamics::RawRigidBodySet;
|
||||
use crate::geometry::{RawBroadPhase, RawColliderSet, RawNarrowPhase};
|
||||
use crate::math::RawVector;
|
||||
use crate::utils::{self, FlatHandle};
|
||||
use na::{Isometry, Unit};
|
||||
use rapier::control::{
|
||||
CharacterAutostep, CharacterCollision, CharacterLength, EffectiveCharacterMovement,
|
||||
KinematicCharacterController,
|
||||
};
|
||||
use rapier::geometry::{ColliderHandle, ShapeCastHit};
|
||||
use rapier::math::{Point, Real, Vector};
|
||||
use rapier::parry::query::ShapeCastStatus;
|
||||
use rapier::pipeline::{QueryFilter, QueryFilterFlags};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct RawKinematicCharacterController {
|
||||
controller: KinematicCharacterController,
|
||||
result: EffectiveCharacterMovement,
|
||||
events: Vec<CharacterCollision>,
|
||||
}
|
||||
|
||||
fn length_value(length: CharacterLength) -> Real {
|
||||
match length {
|
||||
CharacterLength::Absolute(val) => val,
|
||||
CharacterLength::Relative(val) => val,
|
||||
}
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
impl RawKinematicCharacterController {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(offset: Real) -> Self {
|
||||
let controller = KinematicCharacterController {
|
||||
offset: CharacterLength::Absolute(offset),
|
||||
autostep: None,
|
||||
snap_to_ground: None,
|
||||
..KinematicCharacterController::default()
|
||||
};
|
||||
|
||||
Self {
|
||||
controller,
|
||||
result: EffectiveCharacterMovement {
|
||||
translation: Vector::zeros(),
|
||||
grounded: false,
|
||||
is_sliding_down_slope: false,
|
||||
},
|
||||
events: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn up(&self) -> RawVector {
|
||||
self.controller.up.into_inner().into()
|
||||
}
|
||||
|
||||
pub fn setUp(&mut self, vector: &RawVector) {
|
||||
self.controller.up = Unit::new_normalize(vector.0);
|
||||
}
|
||||
|
||||
pub fn normalNudgeFactor(&self) -> Real {
|
||||
self.controller.normal_nudge_factor
|
||||
}
|
||||
|
||||
pub fn setNormalNudgeFactor(&mut self, value: Real) {
|
||||
self.controller.normal_nudge_factor = value;
|
||||
}
|
||||
|
||||
pub fn offset(&self) -> Real {
|
||||
length_value(self.controller.offset)
|
||||
}
|
||||
|
||||
pub fn setOffset(&mut self, value: Real) {
|
||||
self.controller.offset = CharacterLength::Absolute(value);
|
||||
}
|
||||
|
||||
pub fn slideEnabled(&self) -> bool {
|
||||
self.controller.slide
|
||||
}
|
||||
|
||||
pub fn setSlideEnabled(&mut self, enabled: bool) {
|
||||
self.controller.slide = enabled
|
||||
}
|
||||
|
||||
pub fn autostepMaxHeight(&self) -> Option<Real> {
|
||||
self.controller.autostep.map(|e| length_value(e.max_height))
|
||||
}
|
||||
|
||||
pub fn autostepMinWidth(&self) -> Option<Real> {
|
||||
self.controller.autostep.map(|e| length_value(e.min_width))
|
||||
}
|
||||
|
||||
pub fn autostepIncludesDynamicBodies(&self) -> Option<bool> {
|
||||
self.controller.autostep.map(|e| e.include_dynamic_bodies)
|
||||
}
|
||||
|
||||
pub fn autostepEnabled(&self) -> bool {
|
||||
self.controller.autostep.is_some()
|
||||
}
|
||||
|
||||
pub fn enableAutostep(&mut self, maxHeight: Real, minWidth: Real, includeDynamicBodies: bool) {
|
||||
self.controller.autostep = Some(CharacterAutostep {
|
||||
min_width: CharacterLength::Absolute(minWidth),
|
||||
max_height: CharacterLength::Absolute(maxHeight),
|
||||
include_dynamic_bodies: includeDynamicBodies,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn disableAutostep(&mut self) {
|
||||
self.controller.autostep = None;
|
||||
}
|
||||
|
||||
pub fn maxSlopeClimbAngle(&self) -> Real {
|
||||
self.controller.max_slope_climb_angle
|
||||
}
|
||||
|
||||
pub fn setMaxSlopeClimbAngle(&mut self, angle: Real) {
|
||||
self.controller.max_slope_climb_angle = angle;
|
||||
}
|
||||
|
||||
pub fn minSlopeSlideAngle(&self) -> Real {
|
||||
self.controller.min_slope_slide_angle
|
||||
}
|
||||
|
||||
pub fn setMinSlopeSlideAngle(&mut self, angle: Real) {
|
||||
self.controller.min_slope_slide_angle = angle
|
||||
}
|
||||
|
||||
pub fn snapToGroundDistance(&self) -> Option<Real> {
|
||||
self.controller.snap_to_ground.map(length_value)
|
||||
}
|
||||
|
||||
pub fn enableSnapToGround(&mut self, distance: Real) {
|
||||
self.controller.snap_to_ground = Some(CharacterLength::Absolute(distance));
|
||||
}
|
||||
|
||||
pub fn disableSnapToGround(&mut self) {
|
||||
self.controller.snap_to_ground = None;
|
||||
}
|
||||
|
||||
pub fn snapToGroundEnabled(&self) -> bool {
|
||||
self.controller.snap_to_ground.is_some()
|
||||
}
|
||||
|
||||
pub fn computeColliderMovement(
|
||||
&mut self,
|
||||
dt: Real,
|
||||
broad_phase: &RawBroadPhase,
|
||||
narrow_phase: &RawNarrowPhase,
|
||||
bodies: &mut RawRigidBodySet,
|
||||
colliders: &mut RawColliderSet,
|
||||
collider_handle: FlatHandle,
|
||||
desired_translation_delta: &RawVector,
|
||||
apply_impulses_to_dynamic_bodies: bool,
|
||||
character_mass: Option<Real>,
|
||||
filter_flags: u32,
|
||||
filter_groups: Option<u32>,
|
||||
filter_predicate: &js_sys::Function,
|
||||
) {
|
||||
let handle = crate::utils::collider_handle(collider_handle);
|
||||
if let Some(collider) = colliders.0.get(handle) {
|
||||
let collider_pose = *collider.position();
|
||||
let collider_shape = collider.shared_shape().clone();
|
||||
let collider_parent = collider.parent();
|
||||
|
||||
crate::utils::with_filter(filter_predicate, |predicate| {
|
||||
let query_filter = QueryFilter {
|
||||
flags: QueryFilterFlags::from_bits(filter_flags)
|
||||
.unwrap_or(QueryFilterFlags::empty()),
|
||||
groups: filter_groups.map(crate::geometry::unpack_interaction_groups),
|
||||
exclude_collider: Some(handle),
|
||||
exclude_rigid_body: collider_parent,
|
||||
predicate,
|
||||
};
|
||||
|
||||
let character_mass = character_mass
|
||||
.or_else(|| {
|
||||
collider_parent
|
||||
.and_then(|h| bodies.0.get(h))
|
||||
.map(|b| b.mass())
|
||||
})
|
||||
.unwrap_or(0.0);
|
||||
|
||||
let mut query_pipeline = broad_phase.0.as_query_pipeline_mut(
|
||||
narrow_phase.0.query_dispatcher(),
|
||||
&mut bodies.0,
|
||||
&mut colliders.0,
|
||||
query_filter,
|
||||
);
|
||||
|
||||
self.events.clear();
|
||||
let events = &mut self.events;
|
||||
self.result = self.controller.move_shape(
|
||||
dt,
|
||||
&query_pipeline.as_ref(),
|
||||
&*collider_shape,
|
||||
&collider_pose,
|
||||
desired_translation_delta.0,
|
||||
|event| events.push(event),
|
||||
);
|
||||
|
||||
if apply_impulses_to_dynamic_bodies {
|
||||
self.controller.solve_character_collision_impulses(
|
||||
dt,
|
||||
&mut query_pipeline,
|
||||
&*collider_shape,
|
||||
character_mass,
|
||||
self.events.iter(),
|
||||
);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
self.result.translation.fill(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn computedMovement(&self) -> RawVector {
|
||||
self.result.translation.into()
|
||||
}
|
||||
|
||||
pub fn computedGrounded(&self) -> bool {
|
||||
self.result.grounded
|
||||
}
|
||||
|
||||
pub fn numComputedCollisions(&self) -> usize {
|
||||
self.events.len()
|
||||
}
|
||||
|
||||
pub fn computedCollision(&self, i: usize, collision: &mut RawCharacterCollision) -> bool {
|
||||
if let Some(coll) = self.events.get(i) {
|
||||
collision.0 = *coll;
|
||||
}
|
||||
|
||||
i < self.events.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct RawCharacterCollision(CharacterCollision);
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl RawCharacterCollision {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> Self {
|
||||
Self(CharacterCollision {
|
||||
handle: ColliderHandle::invalid(),
|
||||
character_pos: Isometry::identity(),
|
||||
translation_applied: Vector::zeros(),
|
||||
translation_remaining: Vector::zeros(),
|
||||
hit: ShapeCastHit {
|
||||
time_of_impact: 0.0,
|
||||
witness1: Point::origin(),
|
||||
witness2: Point::origin(),
|
||||
normal1: Vector::y_axis(),
|
||||
normal2: Vector::y_axis(),
|
||||
status: ShapeCastStatus::Failed,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn handle(&self) -> FlatHandle {
|
||||
utils::flat_handle(self.0.handle.0)
|
||||
}
|
||||
|
||||
pub fn translationDeltaApplied(&self) -> RawVector {
|
||||
self.0.translation_applied.into()
|
||||
}
|
||||
|
||||
pub fn translationDeltaRemaining(&self) -> RawVector {
|
||||
self.0.translation_remaining.into()
|
||||
}
|
||||
|
||||
pub fn toi(&self) -> Real {
|
||||
self.0.hit.time_of_impact
|
||||
}
|
||||
|
||||
pub fn worldWitness1(&self) -> RawVector {
|
||||
self.0.hit.witness1.coords.into() // Already in world-space.
|
||||
}
|
||||
|
||||
pub fn worldWitness2(&self) -> RawVector {
|
||||
(self.0.character_pos * self.0.hit.witness2).coords.into()
|
||||
}
|
||||
|
||||
pub fn worldNormal1(&self) -> RawVector {
|
||||
self.0.hit.normal1.into_inner().into() // Already in world-space.
|
||||
}
|
||||
|
||||
pub fn worldNormal2(&self) -> RawVector {
|
||||
(self.0.character_pos * self.0.hit.normal2.into_inner()).into()
|
||||
}
|
||||
}
|
||||
11
thirdparty/rapier.js/src/control/mod.rs
vendored
Normal file
11
thirdparty/rapier.js/src/control/mod.rs
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
pub use self::character_controller::RawKinematicCharacterController;
|
||||
pub use self::pid_controller::RawPidController;
|
||||
|
||||
#[cfg(feature = "dim3")]
|
||||
pub use self::ray_cast_vehicle_controller::RawDynamicRayCastVehicleController;
|
||||
|
||||
mod character_controller;
|
||||
mod pid_controller;
|
||||
|
||||
#[cfg(feature = "dim3")]
|
||||
mod ray_cast_vehicle_controller;
|
||||
264
thirdparty/rapier.js/src/control/pid_controller.rs
vendored
Normal file
264
thirdparty/rapier.js/src/control/pid_controller.rs
vendored
Normal file
@@ -0,0 +1,264 @@
|
||||
use crate::dynamics::RawRigidBodySet;
|
||||
use crate::math::RawVector;
|
||||
use crate::utils::{self, FlatHandle};
|
||||
use rapier::control::PidController;
|
||||
use rapier::dynamics::AxesMask;
|
||||
use rapier::math::Vector;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(feature = "dim3")]
|
||||
use crate::math::RawRotation;
|
||||
#[cfg(feature = "dim2")]
|
||||
use rapier::math::Rotation;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct RawPidController {
|
||||
controller: PidController,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl RawPidController {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(kp: f32, ki: f32, kd: f32, axes_mask: u8) -> Self {
|
||||
let controller = PidController::new(
|
||||
kp,
|
||||
ki,
|
||||
kd,
|
||||
AxesMask::from_bits(axes_mask).unwrap_or(AxesMask::all()),
|
||||
);
|
||||
Self { controller }
|
||||
}
|
||||
|
||||
pub fn set_kp(&mut self, kp: f32, axes: u8) {
|
||||
let axes = AxesMask::from_bits(axes).unwrap_or(AxesMask::all());
|
||||
if axes.contains(AxesMask::LIN_X) {
|
||||
self.controller.pd.lin_kp.x = kp;
|
||||
}
|
||||
if axes.contains(AxesMask::LIN_Y) {
|
||||
self.controller.pd.lin_kp.y = kp;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::LIN_Z) {
|
||||
self.controller.pd.lin_kp.z = kp;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::ANG_X) {
|
||||
self.controller.pd.ang_kp.x = kp;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::ANG_Y) {
|
||||
self.controller.pd.ang_kp.y = kp;
|
||||
}
|
||||
if axes.contains(AxesMask::ANG_Z) {
|
||||
#[cfg(feature = "dim2")]
|
||||
{
|
||||
self.controller.pd.ang_kp = kp;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
{
|
||||
self.controller.pd.ang_kp.z = kp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_ki(&mut self, ki: f32, axes: u8) {
|
||||
let axes = AxesMask::from_bits(axes).unwrap_or(AxesMask::all());
|
||||
if axes.contains(AxesMask::LIN_X) {
|
||||
self.controller.lin_ki.x = ki;
|
||||
}
|
||||
if axes.contains(AxesMask::LIN_Y) {
|
||||
self.controller.lin_ki.y = ki;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::LIN_Z) {
|
||||
self.controller.lin_ki.z = ki;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::ANG_X) {
|
||||
self.controller.ang_ki.x = ki;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::ANG_Y) {
|
||||
self.controller.ang_ki.y = ki;
|
||||
}
|
||||
if axes.contains(AxesMask::ANG_Z) {
|
||||
#[cfg(feature = "dim2")]
|
||||
{
|
||||
self.controller.ang_ki = ki;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
{
|
||||
self.controller.ang_ki.z = ki;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_kd(&mut self, kd: f32, axes: u8) {
|
||||
let axes = AxesMask::from_bits(axes).unwrap_or(AxesMask::all());
|
||||
if axes.contains(AxesMask::LIN_X) {
|
||||
self.controller.pd.lin_kd.x = kd;
|
||||
}
|
||||
if axes.contains(AxesMask::LIN_Y) {
|
||||
self.controller.pd.lin_kd.x = kd;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::LIN_Z) {
|
||||
self.controller.pd.lin_kd.x = kd;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::ANG_X) {
|
||||
self.controller.pd.ang_kd.x = kd;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
if axes.contains(AxesMask::ANG_Y) {
|
||||
self.controller.pd.ang_kd.y = kd;
|
||||
}
|
||||
if axes.contains(AxesMask::ANG_Z) {
|
||||
#[cfg(feature = "dim2")]
|
||||
{
|
||||
self.controller.pd.ang_kd = kd;
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
{
|
||||
self.controller.pd.ang_kd.z = kd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_axes_mask(&mut self, axes_mask: u8) {
|
||||
if let Some(mask) = AxesMask::from_bits(axes_mask) {
|
||||
self.controller.pd.axes = mask;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset_integrals(&mut self) {
|
||||
self.controller.reset_integrals();
|
||||
}
|
||||
|
||||
pub fn apply_linear_correction(
|
||||
&mut self,
|
||||
dt: f32,
|
||||
bodies: &mut RawRigidBodySet,
|
||||
rb_handle: FlatHandle,
|
||||
target_translation: &RawVector,
|
||||
target_linvel: &RawVector,
|
||||
) {
|
||||
let rb_handle = utils::body_handle(rb_handle);
|
||||
let Some(rb) = bodies.0.get_mut(rb_handle) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let correction = self.controller.linear_rigid_body_correction(
|
||||
dt,
|
||||
rb,
|
||||
target_translation.0.into(),
|
||||
target_linvel.0,
|
||||
);
|
||||
rb.set_linvel(*rb.linvel() + correction, true);
|
||||
}
|
||||
|
||||
#[cfg(feature = "dim2")]
|
||||
pub fn apply_angular_correction(
|
||||
&mut self,
|
||||
dt: f32,
|
||||
bodies: &mut RawRigidBodySet,
|
||||
rb_handle: FlatHandle,
|
||||
target_rotation: f32,
|
||||
target_angvel: f32,
|
||||
) {
|
||||
let rb_handle = crate::utils::body_handle(rb_handle);
|
||||
let Some(rb) = bodies.0.get_mut(rb_handle) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let correction = self.controller.angular_rigid_body_correction(
|
||||
dt,
|
||||
rb,
|
||||
Rotation::new(target_rotation),
|
||||
target_angvel,
|
||||
);
|
||||
rb.set_angvel(rb.angvel() + correction, true);
|
||||
}
|
||||
|
||||
#[cfg(feature = "dim3")]
|
||||
pub fn apply_angular_correction(
|
||||
&mut self,
|
||||
dt: f32,
|
||||
bodies: &mut RawRigidBodySet,
|
||||
rb_handle: FlatHandle,
|
||||
target_rotation: &RawRotation,
|
||||
target_angvel: &RawVector,
|
||||
) {
|
||||
let rb_handle = crate::utils::body_handle(rb_handle);
|
||||
let Some(rb) = bodies.0.get_mut(rb_handle) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let correction = self.controller.angular_rigid_body_correction(
|
||||
dt,
|
||||
rb,
|
||||
target_rotation.0,
|
||||
target_angvel.0,
|
||||
);
|
||||
rb.set_angvel(rb.angvel() + correction, true);
|
||||
}
|
||||
|
||||
pub fn linear_correction(
|
||||
&mut self,
|
||||
dt: f32,
|
||||
bodies: &RawRigidBodySet,
|
||||
rb_handle: FlatHandle,
|
||||
target_translation: &RawVector,
|
||||
target_linvel: &RawVector,
|
||||
) -> RawVector {
|
||||
let rb_handle = crate::utils::body_handle(rb_handle);
|
||||
let Some(rb) = bodies.0.get(rb_handle) else {
|
||||
return RawVector(Vector::zeros());
|
||||
};
|
||||
|
||||
self.controller
|
||||
.linear_rigid_body_correction(dt, rb, target_translation.0.into(), target_linvel.0)
|
||||
.into()
|
||||
}
|
||||
|
||||
#[cfg(feature = "dim2")]
|
||||
pub fn angular_correction(
|
||||
&mut self,
|
||||
dt: f32,
|
||||
bodies: &RawRigidBodySet,
|
||||
rb_handle: FlatHandle,
|
||||
target_rotation: f32,
|
||||
target_angvel: f32,
|
||||
) -> f32 {
|
||||
let rb_handle = crate::utils::body_handle(rb_handle);
|
||||
let Some(rb) = bodies.0.get(rb_handle) else {
|
||||
return 0.0;
|
||||
};
|
||||
|
||||
self.controller.angular_rigid_body_correction(
|
||||
dt,
|
||||
rb,
|
||||
Rotation::new(target_rotation),
|
||||
target_angvel,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "dim3")]
|
||||
pub fn angular_correction(
|
||||
&mut self,
|
||||
dt: f32,
|
||||
bodies: &RawRigidBodySet,
|
||||
rb_handle: FlatHandle,
|
||||
target_rotation: &RawRotation,
|
||||
target_angvel: &RawVector,
|
||||
) -> RawVector {
|
||||
let rb_handle = crate::utils::body_handle(rb_handle);
|
||||
let Some(rb) = bodies.0.get(rb_handle) else {
|
||||
return RawVector(Vector::zeros());
|
||||
};
|
||||
|
||||
self.controller
|
||||
.angular_rigid_body_correction(dt, rb, target_rotation.0, target_angvel.0)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
336
thirdparty/rapier.js/src/control/ray_cast_vehicle_controller.rs
vendored
Normal file
336
thirdparty/rapier.js/src/control/ray_cast_vehicle_controller.rs
vendored
Normal file
@@ -0,0 +1,336 @@
|
||||
use crate::dynamics::RawRigidBodySet;
|
||||
use crate::geometry::{RawBroadPhase, RawColliderSet, RawNarrowPhase};
|
||||
use crate::math::RawVector;
|
||||
use crate::utils::{self, FlatHandle};
|
||||
use rapier::control::{DynamicRayCastVehicleController, WheelTuning};
|
||||
use rapier::math::Real;
|
||||
use rapier::pipeline::{QueryFilter, QueryFilterFlags};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct RawDynamicRayCastVehicleController {
|
||||
controller: DynamicRayCastVehicleController,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl RawDynamicRayCastVehicleController {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(chassis: FlatHandle) -> Self {
|
||||
Self {
|
||||
controller: DynamicRayCastVehicleController::new(utils::body_handle(chassis)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_vehicle_speed(&self) -> Real {
|
||||
self.controller.current_vehicle_speed
|
||||
}
|
||||
|
||||
pub fn chassis(&self) -> FlatHandle {
|
||||
utils::flat_handle(self.controller.chassis.0)
|
||||
}
|
||||
|
||||
pub fn index_up_axis(&self) -> usize {
|
||||
self.controller.index_up_axis
|
||||
}
|
||||
pub fn set_index_up_axis(&mut self, axis: usize) {
|
||||
self.controller.index_up_axis = axis;
|
||||
}
|
||||
|
||||
pub fn index_forward_axis(&self) -> usize {
|
||||
self.controller.index_forward_axis
|
||||
}
|
||||
pub fn set_index_forward_axis(&mut self, axis: usize) {
|
||||
self.controller.index_forward_axis = axis;
|
||||
}
|
||||
|
||||
pub fn add_wheel(
|
||||
&mut self,
|
||||
chassis_connection_cs: &RawVector,
|
||||
direction_cs: &RawVector,
|
||||
axle_cs: &RawVector,
|
||||
suspension_rest_length: Real,
|
||||
radius: Real,
|
||||
) {
|
||||
self.controller.add_wheel(
|
||||
chassis_connection_cs.0.into(),
|
||||
direction_cs.0,
|
||||
axle_cs.0,
|
||||
suspension_rest_length,
|
||||
radius,
|
||||
&WheelTuning::default(),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn num_wheels(&self) -> usize {
|
||||
self.controller.wheels().len()
|
||||
}
|
||||
|
||||
pub fn update_vehicle(
|
||||
&mut self,
|
||||
dt: Real,
|
||||
broad_phase: &RawBroadPhase,
|
||||
narrow_phase: &RawNarrowPhase,
|
||||
bodies: &mut RawRigidBodySet,
|
||||
colliders: &mut RawColliderSet,
|
||||
filter_flags: u32,
|
||||
filter_groups: Option<u32>,
|
||||
filter_predicate: &js_sys::Function,
|
||||
) {
|
||||
crate::utils::with_filter(filter_predicate, |predicate| {
|
||||
let query_filter = QueryFilter {
|
||||
flags: QueryFilterFlags::from_bits(filter_flags)
|
||||
.unwrap_or(QueryFilterFlags::empty()),
|
||||
groups: filter_groups.map(crate::geometry::unpack_interaction_groups),
|
||||
predicate,
|
||||
exclude_rigid_body: Some(self.controller.chassis),
|
||||
exclude_collider: None,
|
||||
};
|
||||
|
||||
let query_pipeline = broad_phase.0.as_query_pipeline_mut(
|
||||
narrow_phase.0.query_dispatcher(),
|
||||
&mut bodies.0,
|
||||
&mut colliders.0,
|
||||
query_filter,
|
||||
);
|
||||
|
||||
self.controller.update_vehicle(dt, query_pipeline);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Access to wheel properties.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* Getters + setters
|
||||
*/
|
||||
pub fn wheel_chassis_connection_point_cs(&self, i: usize) -> Option<RawVector> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.chassis_connection_point_cs.into())
|
||||
}
|
||||
pub fn set_wheel_chassis_connection_point_cs(&mut self, i: usize, value: &RawVector) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.chassis_connection_point_cs = value.0.into();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_suspension_rest_length(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.suspension_rest_length)
|
||||
}
|
||||
pub fn set_wheel_suspension_rest_length(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.suspension_rest_length = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_max_suspension_travel(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.max_suspension_travel)
|
||||
}
|
||||
pub fn set_wheel_max_suspension_travel(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.max_suspension_travel = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_radius(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.radius)
|
||||
}
|
||||
pub fn set_wheel_radius(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.radius = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_suspension_stiffness(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.suspension_stiffness)
|
||||
}
|
||||
pub fn set_wheel_suspension_stiffness(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.suspension_stiffness = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_suspension_compression(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.damping_compression)
|
||||
}
|
||||
pub fn set_wheel_suspension_compression(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.damping_compression = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_suspension_relaxation(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.damping_relaxation)
|
||||
}
|
||||
pub fn set_wheel_suspension_relaxation(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.damping_relaxation = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_max_suspension_force(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.max_suspension_force)
|
||||
}
|
||||
pub fn set_wheel_max_suspension_force(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.max_suspension_force = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_brake(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.brake)
|
||||
}
|
||||
pub fn set_wheel_brake(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.brake = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_steering(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.steering)
|
||||
}
|
||||
pub fn set_wheel_steering(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.steering = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_engine_force(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.engine_force)
|
||||
}
|
||||
pub fn set_wheel_engine_force(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.engine_force = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_direction_cs(&self, i: usize) -> Option<RawVector> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.direction_cs.into())
|
||||
}
|
||||
pub fn set_wheel_direction_cs(&mut self, i: usize, value: &RawVector) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.direction_cs = value.0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_axle_cs(&self, i: usize) -> Option<RawVector> {
|
||||
self.controller.wheels().get(i).map(|w| w.axle_cs.into())
|
||||
}
|
||||
pub fn set_wheel_axle_cs(&mut self, i: usize, value: &RawVector) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.axle_cs = value.0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_friction_slip(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.friction_slip)
|
||||
}
|
||||
pub fn set_wheel_friction_slip(&mut self, i: usize, value: Real) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.friction_slip = value;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wheel_side_friction_stiffness(&self, i: usize) -> Option<f32> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.side_friction_stiffness)
|
||||
}
|
||||
|
||||
pub fn set_wheel_side_friction_stiffness(&mut self, i: usize, stiffness: f32) {
|
||||
if let Some(wheel) = self.controller.wheels_mut().get_mut(i) {
|
||||
wheel.side_friction_stiffness = stiffness;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Getters only.
|
||||
*/
|
||||
pub fn wheel_rotation(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.rotation)
|
||||
}
|
||||
|
||||
pub fn wheel_forward_impulse(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.forward_impulse)
|
||||
}
|
||||
|
||||
pub fn wheel_side_impulse(&self, i: usize) -> Option<Real> {
|
||||
self.controller.wheels().get(i).map(|w| w.side_impulse)
|
||||
}
|
||||
|
||||
pub fn wheel_suspension_force(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.wheel_suspension_force)
|
||||
}
|
||||
|
||||
pub fn wheel_contact_normal_ws(&self, i: usize) -> Option<RawVector> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.raycast_info().contact_normal_ws.into())
|
||||
}
|
||||
|
||||
pub fn wheel_contact_point_ws(&self, i: usize) -> Option<RawVector> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.raycast_info().contact_point_ws.into())
|
||||
}
|
||||
|
||||
pub fn wheel_suspension_length(&self, i: usize) -> Option<Real> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.raycast_info().suspension_length)
|
||||
}
|
||||
|
||||
pub fn wheel_hard_point_ws(&self, i: usize) -> Option<RawVector> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.raycast_info().hard_point_ws.into())
|
||||
}
|
||||
|
||||
pub fn wheel_is_in_contact(&self, i: usize) -> bool {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.map(|w| w.raycast_info().is_in_contact)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn wheel_ground_object(&self, i: usize) -> Option<FlatHandle> {
|
||||
self.controller
|
||||
.wheels()
|
||||
.get(i)
|
||||
.and_then(|w| w.raycast_info().ground_object)
|
||||
.map(|h| utils::flat_handle(h.0))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user