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

Drafting xpbd f64 support #47

Merged
merged 8 commits into from
Mar 18, 2024
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --doc --all-features
args: --doc
- name: Get package names
id: get-package-names
run: |-
Expand All @@ -135,7 +135,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: doc
args: --no-deps ${{ steps.get-package-names.outputs.PACKAGE_NAMES }} --all-features ${{ steps.get-features-for-docs.outputs.FEATURES_FOR_DOCS }}
args: --no-deps ${{ steps.get-package-names.outputs.PACKAGE_NAMES }}
docs-and-demos-ghpages:
name: Update Docs and Demos in GitHub Pages
runs-on: ubuntu-latest
Expand Down Expand Up @@ -176,7 +176,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |-
set -ex
cargo doc --verbose ${{ steps.get-package-names.outputs.PACKAGE_NAMES }} --all-features ${{ steps.get-features-for-docs.outputs.FEATURES_FOR_DOCS }}
cargo doc --verbose ${{ steps.get-package-names.outputs.PACKAGE_NAMES }}
echo "<meta http-equiv=refresh content=0;url=bevy_tnua/index.html>" > target/doc/index.html
for physics_backend in rapier xpbd; do
cargo build \
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ bevy = { version = "^0.13", default-features = false, features = [
"x11",
# "filesystem_watcher",
] }
bevy_xpbd_3d = { version = "^0.4" }
bevy_xpbd_3d = { version = "^0.4", features = ["3d","debug-plugin", "parallel", "async-collider", "parry-f32"] }
bevy-tnua-xpbd3d = { path = "xpbd3d" }

[package.metadata.docs.rs]
all-features = true

[features]
f64 = ["bevy-tnua-physics-integration-layer/f64"]
13 changes: 9 additions & 4 deletions demos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ exclude = [
default = [
# Comment this out when Bevy gets upgraded and bevy_egui lags behind
"egui",
"bevy_xpbd_3d?/parry-f32",
"bevy_xpbd_2d?/parry-f32",
]
egui = ["dep:bevy_egui", "dep:egui_plot"]
rapier = []
rapier2d = ["rapier", "dep:bevy_rapier2d", "dep:bevy-tnua-rapier2d"]
rapier3d = ["rapier", "dep:bevy_rapier3d", "dep:bevy-tnua-rapier3d"]
f64 = ["bevy_xpbd_3d?/parry-f64", "bevy_xpbd_2d?/parry-f64", "bevy-tnua-physics-integration-layer/f64", "bevy-tnua/f64"]

xpbd = []
xpbd2d = ["xpbd", "dep:bevy_xpbd_2d", "dep:bevy-tnua-xpbd2d"]
Expand Down Expand Up @@ -50,11 +53,13 @@ bevy-tnua-rapier2d = { path = "../rapier2d", optional = true }
bevy_rapier3d = { version = "^0.25", features = ["debug-render-3d"], optional = true }
bevy-tnua-rapier3d = { path = "../rapier3d", optional = true }

bevy_xpbd_2d = { version = "^0.4", optional = true }
bevy-tnua-xpbd2d = { path = "../xpbd2d", optional = true }
bevy_xpbd_2d = {version = "^0.4", default-features = false, features = ["2d","debug-plugin", "parallel"], optional = true}
bevy-tnua-xpbd2d = { path = "../xpbd2d", default-features = false, optional = true }

bevy_xpbd_3d = { version = "^0.4", optional = true }
bevy-tnua-xpbd3d = { path = "../xpbd3d", optional = true }
bevy_xpbd_3d = {version = "^0.4", default-features = false, features = ["3d","debug-plugin", "parallel", "async-collider"], optional = true }
bevy-tnua-xpbd3d = { path = "../xpbd3d", default-features = false, optional = true }

bevy_egui = { version = "0.25", optional = true }
egui_plot = { version = "0.26", optional = true }


6 changes: 6 additions & 0 deletions demos/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
#[cfg(all(feature = "bevy_xpbd_2d/parry-f32", feature = "f64"))]
compile_error!(
"Default Feature (f32) and f64 are mutually exclusive and cannot be enabled together"
);
}
3 changes: 2 additions & 1 deletion demos/src/bin/platformer_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy_tnua::control_helpers::{
TnuaCrouchEnforcer, TnuaCrouchEnforcerPlugin, TnuaSimpleAirActionsCounter,
TnuaSimpleFallThroughPlatformsHelper,
};
use bevy_tnua::math::{AsF32, Vector3};
use bevy_tnua::prelude::*;
use bevy_tnua::{TnuaGhostSensor, TnuaToggle};
#[cfg(feature = "rapier2d")]
Expand Down Expand Up @@ -264,7 +265,7 @@ fn setup_player(mut commands: Commands) {
});

// `TnuaCrouchEnforcer` can be used to prevent the character from standing up when obstructed.
cmd.insert(TnuaCrouchEnforcer::new(0.5 * Vec3::Y, |cmd| {
cmd.insert(TnuaCrouchEnforcer::new(0.5 * Vector3::Y, |cmd| {
// It needs a sensor shape because it needs to do a shapecast upwards. Without a sensor shape
// it'd do a raycast.
#[cfg(feature = "rapier2d")]
Expand Down
3 changes: 2 additions & 1 deletion demos/src/bin/platformer_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy_tnua::control_helpers::{
TnuaCrouchEnforcer, TnuaCrouchEnforcerPlugin, TnuaSimpleAirActionsCounter,
TnuaSimpleFallThroughPlatformsHelper,
};
use bevy_tnua::math::{AsF32, Vector3};
use bevy_tnua::prelude::*;
use bevy_tnua::{TnuaAnimatingState, TnuaGhostSensor, TnuaToggle};
#[cfg(feature = "rapier3d")]
Expand Down Expand Up @@ -290,7 +291,7 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
});

// `TnuaCrouchEnforcer` can be used to prevent the character from standing up when obstructed.
cmd.insert(TnuaCrouchEnforcer::new(0.5 * Vec3::Y, |cmd| {
cmd.insert(TnuaCrouchEnforcer::new(0.5 * Vector3::Y, |cmd| {
#[cfg(feature = "rapier3d")]
cmd.insert(TnuaRapier3dSensorShape(rapier::Collider::cylinder(
0.0, 0.5,
Expand Down
14 changes: 8 additions & 6 deletions demos/src/bin/shooter_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bevy_tnua::control_helpers::{
TnuaCrouchEnforcer, TnuaCrouchEnforcerPlugin, TnuaSimpleAirActionsCounter,
TnuaSimpleFallThroughPlatformsHelper,
};
use bevy_tnua::math::{AdjustPrecision, AsF32, Float, Quaternion, Vector3};
use bevy_tnua::prelude::*;
use bevy_tnua::{TnuaAnimatingState, TnuaGhostSensor, TnuaToggle};
#[cfg(feature = "rapier3d")]
Expand Down Expand Up @@ -144,7 +145,7 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
speed: 20.0,
walk: TnuaBuiltinWalk {
float_height: 2.0,
turning_angvel: f32::INFINITY,
turning_angvel: Float::INFINITY,
..Default::default()
},
actions_in_air: 1,
Expand Down Expand Up @@ -297,7 +298,7 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
});

// `TnuaCrouchEnforcer` can be used to prevent the character from standing up when obstructed.
cmd.insert(TnuaCrouchEnforcer::new(0.5 * Vec3::Y, |cmd| {
cmd.insert(TnuaCrouchEnforcer::new(0.5 * Vector3::Y, |cmd| {
#[cfg(feature = "rapier3d")]
cmd.insert(TnuaRapier3dSensorShape(rapier::Collider::cylinder(
0.0, 0.5,
Expand Down Expand Up @@ -365,16 +366,17 @@ fn apply_camera_controls(
mouse_motion.clear();
Vec2::ZERO
};
let turning = Quat::from_rotation_y(-0.01 * total_delta.x);
let turning = Quaternion::from_rotation_y(-0.01 * total_delta.x.adjust_precision());
let Ok((player_transform, mut forward_from_camera)) = player_character_query.get_single_mut()
else {
return;
};
forward_from_camera.forward = turning.mul_vec3(forward_from_camera.forward);

for mut camera in camera_query.iter_mut() {
camera.translation =
player_transform.translation() + -5.0 * forward_from_camera.forward + 1.0 * Vec3::Y;
camera.look_to(forward_from_camera.forward, Vec3::Y);
camera.translation = player_transform.translation()
+ -5.0 * forward_from_camera.forward.f32()
+ 1.0 * Vec3::Y;
camera.look_to(forward_from_camera.forward.f32(), Vec3::Y);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use bevy_tnua::builtins::{TnuaBuiltinCrouch, TnuaBuiltinDash, TnuaBuiltinJumpState};
use bevy_tnua::math::Float;
use bevy_tnua::prelude::*;
use bevy_tnua::{TnuaAnimatingState, TnuaAnimatingStateDirective};

Expand All @@ -8,14 +9,15 @@ use crate::util::animating::AnimationsHandler;
#[derive(Debug)]
pub enum AnimationState {
Standing,
Running(f32),
Running(Float),
Jumping,
Falling,
Crouching,
Crawling(f32),
Crawling(Float),
Dashing,
}

#[allow(clippy::unnecessary_cast)]
pub fn animate_platformer_character(
mut animations_handlers_query: Query<(
// `TnuaAnimatingState` is a helper for controlling the animations. The user system is
Expand Down Expand Up @@ -112,7 +114,7 @@ pub fn animate_platformer_character(
// animation (without necessarily replacing it). In this case - control the speed
// of the animation based on the speed of the movement.
AnimationState::Running(speed) | AnimationState::Crawling(speed) => {
player.set_speed(*speed);
player.set_speed(*speed as f32);
}
// Jumping and dashing can be chained, we want to start a new jump/dash animation
// when one jump/dash is chained to another.
Expand Down Expand Up @@ -142,7 +144,7 @@ pub fn animate_platformer_character(
AnimationState::Running(speed) => {
player
.start(handler.animations["Running"].clone_weak())
.set_speed(*speed)
.set_speed(*speed as f32)
.repeat();
}
AnimationState::Jumping => {
Expand All @@ -164,7 +166,7 @@ pub fn animate_platformer_character(
AnimationState::Crawling(speed) => {
player
.start(handler.animations["Crawling"].clone_weak())
.set_speed(*speed)
.set_speed(*speed as f32)
.repeat();
}
AnimationState::Dashing => {
Expand Down
31 changes: 17 additions & 14 deletions demos/src/character_control_systems/platformer_control_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy_tnua::builtins::{TnuaBuiltinCrouch, TnuaBuiltinCrouchState, TnuaBuiltin
use bevy_tnua::control_helpers::{
TnuaCrouchEnforcer, TnuaSimpleAirActionsCounter, TnuaSimpleFallThroughPlatformsHelper,
};
use bevy_tnua::math::{AdjustPrecision, AsF32, Float, Vector3};
use bevy_tnua::prelude::*;
use bevy_tnua::{TnuaGhostSensor, TnuaProximitySensor};

Expand All @@ -13,6 +14,7 @@ use crate::ui::tuning::UiTunable;
use super::Dimensionality;

#[allow(clippy::type_complexity)]
#[allow(clippy::useless_conversion)]
pub fn apply_platformer_controls(
#[cfg(feature = "egui")] mut egui_context: EguiContexts,
keyboard: Res<ButtonInput<KeyCode>>,
Expand Down Expand Up @@ -72,29 +74,30 @@ pub fn apply_platformer_controls(
{
// This part is just keyboard input processing. In a real game this would probably be done
// with a third party plugin.
let mut direction = Vec3::ZERO;
let mut direction = Vector3::ZERO;

if config.dimensionality == Dimensionality::Dim3 {
if keyboard.any_pressed([KeyCode::ArrowUp, KeyCode::KeyW]) {
direction -= Vec3::Z;
direction -= Vector3::Z;
}
if keyboard.any_pressed([KeyCode::ArrowDown, KeyCode::KeyS]) {
direction += Vec3::Z;
direction += Vector3::Z;
}
}
if keyboard.any_pressed([KeyCode::ArrowLeft, KeyCode::KeyA]) {
direction -= Vec3::X;
direction -= Vector3::X;
}
if keyboard.any_pressed([KeyCode::ArrowRight, KeyCode::KeyD]) {
direction += Vec3::X;
direction += Vector3::X;
}

direction = direction.clamp_length_max(1.0);

if let Some(forward_from_camera) = forward_from_camera {
direction = Transform::default()
.looking_to(forward_from_camera.forward, Vec3::Y)
.transform_point(direction);
.looking_to(forward_from_camera.forward.f32(), Vec3::Y)
.transform_point(direction.f32())
.adjust_precision();
}

let jump = match config.dimensionality {
Expand Down Expand Up @@ -291,7 +294,7 @@ pub fn apply_platformer_controls(
// frame's input.
controller.basis(TnuaBuiltinWalk {
desired_velocity: if turn_in_place {
Vec3::ZERO
Vector3::ZERO
} else {
direction * speed_factor * config.speed
},
Expand Down Expand Up @@ -356,7 +359,7 @@ pub fn apply_platformer_controls(
} else {
// For shooters, we want to allow rotating mid-dash if the player moves the
// mouse.
Vec3::ZERO
Vector3::ZERO
},
allow_in_air: air_actions_counter.air_count_for(TnuaBuiltinDash::NAME)
<= config.actions_in_air,
Expand All @@ -369,14 +372,14 @@ pub fn apply_platformer_controls(
#[derive(Component)]
pub struct CharacterMotionConfigForPlatformerDemo {
pub dimensionality: Dimensionality,
pub speed: f32,
pub speed: Float,
pub walk: TnuaBuiltinWalk,
pub actions_in_air: usize,
pub jump: TnuaBuiltinJump,
pub crouch: TnuaBuiltinCrouch,
pub dash_distance: f32,
pub dash_distance: Float,
pub dash: TnuaBuiltinDash,
pub one_way_platforms_min_proximity: f32,
pub one_way_platforms_min_proximity: Float,
pub falling_through: FallingThroughControlScheme,
}

Expand Down Expand Up @@ -442,13 +445,13 @@ impl UiTunable for FallingThroughControlScheme {

#[derive(Component)]
pub struct ForwardFromCamera {
pub forward: Vec3,
pub forward: Vector3,
}

impl Default for ForwardFromCamera {
fn default() -> Self {
Self {
forward: Vec3::NEG_Z,
forward: Vector3::NEG_Z,
}
}
}
20 changes: 13 additions & 7 deletions demos/src/levels_setup/for_2d_platformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#[cfg(feature = "rapier2d")]
use bevy_rapier2d::{prelude as rapier, prelude::*};
use bevy_tnua::TnuaGhostPlatform;
use bevy_tnua::{
math::{AdjustPrecision, Vector2, Vector3},

Check warning on line 6 in demos/src/levels_setup/for_2d_platformer.rs

View workflow job for this annotation

GitHub Actions / Docs

unused imports: `AdjustPrecision`, `Vector2`

Check warning on line 6 in demos/src/levels_setup/for_2d_platformer.rs

View workflow job for this annotation

GitHub Actions / Docs

unused imports: `AdjustPrecision`, `Vector2`
TnuaGhostPlatform,
};
#[cfg(feature = "xpbd2d")]
use bevy_xpbd_2d::{prelude as xpbd, prelude::*};

Expand Down Expand Up @@ -31,7 +34,7 @@
#[cfg(feature = "xpbd2d")]
{
cmd.insert(xpbd::RigidBody::Static);
cmd.insert(xpbd::Collider::halfspace(Vec2::Y));
cmd.insert(xpbd::Collider::halfspace(Vector2::Y));
}

for ([width, height], transform) in [
Expand All @@ -58,7 +61,10 @@
#[cfg(feature = "xpbd2d")]
{
cmd.insert(xpbd::RigidBody::Static);
cmd.insert(xpbd::Collider::rectangle(width, height));
cmd.insert(xpbd::Collider::rectangle(
width.adjust_precision(),
height.adjust_precision(),
));
}
}

Expand Down Expand Up @@ -201,10 +207,10 @@
cmd.insert(MovingPlatform::new(
4.0,
&[
Vec3::new(-4.0, 6.0, 0.0),
Vec3::new(-8.0, 6.0, 0.0),
Vec3::new(-8.0, 10.0, 0.0),
Vec3::new(-4.0, 10.0, 0.0),
Vector3::new(-4.0, 6.0, 0.0),
Vector3::new(-8.0, 6.0, 0.0),
Vector3::new(-8.0, 10.0, 0.0),
Vector3::new(-4.0, 10.0, 0.0),
],
));
}
Expand Down
14 changes: 7 additions & 7 deletions demos/src/levels_setup/for_3d_platformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

#[cfg(feature = "rapier3d")]
use bevy_rapier3d::{prelude as rapier, prelude::*};
use bevy_tnua::TnuaGhostPlatform;
use bevy_tnua::{math::Vector3, TnuaGhostPlatform};
#[cfg(feature = "xpbd3d")]
use bevy_xpbd_3d::{prelude as xpbd, prelude::*};

Expand Down Expand Up @@ -172,12 +172,12 @@ pub fn setup_level(
cmd.insert(MovingPlatform::new(
4.0,
&[
Vec3::new(-4.0, 6.0, 0.0),
Vec3::new(-8.0, 6.0, 0.0),
Vec3::new(-8.0, 10.0, 0.0),
Vec3::new(-8.0, 10.0, -4.0),
Vec3::new(-4.0, 10.0, -4.0),
Vec3::new(-4.0, 10.0, 0.0),
Vector3::new(-4.0, 6.0, 0.0),
Vector3::new(-8.0, 6.0, 0.0),
Vector3::new(-8.0, 10.0, 0.0),
Vector3::new(-8.0, 10.0, -4.0),
Vector3::new(-4.0, 10.0, -4.0),
Vector3::new(-4.0, 10.0, 0.0),
],
));
}
Expand Down
Loading
Loading