add (not working) ground check
This commit is contained in:
parent
d96b684bfb
commit
8640e29b4e
4 changed files with 43 additions and 6 deletions
|
|
@ -15,6 +15,6 @@ opt-level = 3
|
||||||
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.13.1"
|
bevy = { version = "0.13.1"}
|
||||||
bevy_editor_pls = "0.8.1"
|
bevy_editor_pls = "0.8.1"
|
||||||
bevy_xpbd_3d = {git = "https://git.opencodebox.com/MikolajG/bevy_xpbd", branch = "reflect-serialize"}
|
bevy_xpbd_3d = {git = "https://git.opencodebox.com/MikolajG/bevy_xpbd", branch = "reflect-serialize"}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ mkShell rec {
|
||||||
pkg-config
|
pkg-config
|
||||||
];
|
];
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
|
mold clang # To compile using mold linker
|
||||||
udev alsa-lib vulkan-loader
|
udev alsa-lib vulkan-loader
|
||||||
xorg.libX11 xorg.libXcursor xorg.libXi xorg.libXrandr # To use the x11 feature
|
xorg.libX11 xorg.libXcursor xorg.libXi xorg.libXrandr # To use the x11 feature
|
||||||
libxkbcommon wayland # To use the wayland feature
|
libxkbcommon wayland # To use the wayland feature
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ pub struct CharacterController{
|
||||||
pub movement_acceleration: Scalar,
|
pub movement_acceleration: Scalar,
|
||||||
pub movement_dampening_factor: Scalar,
|
pub movement_dampening_factor: Scalar,
|
||||||
pub jump_impulse: Scalar,
|
pub jump_impulse: Scalar,
|
||||||
|
pub max_slope_angle: Scalar,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CharacterController {
|
impl Default for CharacterController {
|
||||||
|
|
@ -55,6 +56,7 @@ impl Default for CharacterController {
|
||||||
movement_acceleration: 30.0,
|
movement_acceleration: 30.0,
|
||||||
movement_dampening_factor: 0.95,
|
movement_dampening_factor: 0.95,
|
||||||
jump_impulse: 7.0,
|
jump_impulse: 7.0,
|
||||||
|
max_slope_angle: (30.0 as Scalar).to_radians(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,15 +66,26 @@ pub struct CharacterControllerBundle{
|
||||||
character_controller: CharacterController,
|
character_controller: CharacterController,
|
||||||
rigid_body: RigidBody,
|
rigid_body: RigidBody,
|
||||||
collider: Collider,
|
collider: Collider,
|
||||||
|
ground_caster: ShapeCaster,
|
||||||
locked_axis: LockedAxes,
|
locked_axis: LockedAxes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CharacterControllerBundle {
|
impl CharacterControllerBundle {
|
||||||
pub fn new(controller_collider: Collider, controller: CharacterController) -> Self {
|
pub fn new(controller_collider: Collider, controller: CharacterController) -> Self {
|
||||||
|
let mut caster_shape = controller_collider.clone();
|
||||||
|
caster_shape.set_scale(Vector::ONE * 0.99, 10);
|
||||||
|
|
||||||
|
|
||||||
Self{
|
Self{
|
||||||
character_controller: controller,
|
character_controller: controller,
|
||||||
rigid_body: RigidBody::Dynamic,
|
rigid_body: RigidBody::Dynamic,
|
||||||
collider: controller_collider,
|
collider: controller_collider,
|
||||||
|
ground_caster: ShapeCaster::new(
|
||||||
|
caster_shape,
|
||||||
|
Vector::ZERO,
|
||||||
|
Quaternion::default(),
|
||||||
|
Direction3d::NEG_Y,
|
||||||
|
),
|
||||||
locked_axis: LockedAxes::ROTATION_LOCKED,
|
locked_axis: LockedAxes::ROTATION_LOCKED,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -80,20 +93,41 @@ impl CharacterControllerBundle {
|
||||||
|
|
||||||
impl Default for CharacterControllerBundle {
|
impl Default for CharacterControllerBundle {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
let collider = Collider::capsule(1.0, 0.5);
|
||||||
|
let mut caster_shape = collider.clone();
|
||||||
|
caster_shape.set_scale(Vector::ONE * 0.99, 10);
|
||||||
CharacterControllerBundle{
|
CharacterControllerBundle{
|
||||||
character_controller: CharacterController::default(),
|
character_controller: CharacterController::default(),
|
||||||
rigid_body: RigidBody::Dynamic,
|
rigid_body: RigidBody::Dynamic,
|
||||||
collider: Collider::capsule(1.0, 0.5),
|
collider: collider,
|
||||||
|
ground_caster: ShapeCaster::new(
|
||||||
|
caster_shape,
|
||||||
|
Vector::ZERO,
|
||||||
|
Quaternion::default(),
|
||||||
|
Direction3d::NEG_Y,
|
||||||
|
),
|
||||||
locked_axis: LockedAxes::ROTATION_LOCKED,
|
locked_axis: LockedAxes::ROTATION_LOCKED,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_grounded(mut character_controller_query: Query<&CharacterController>){
|
pub fn update_grounded(mut commands: Commands, mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>){
|
||||||
|
|
||||||
for character_controller in character_controller_query.iter_mut() {
|
|
||||||
|
|
||||||
|
for (entity, character_controller, rotation, hits) in &mut query {
|
||||||
|
let is_grounded = hits.iter().any(|hit| {
|
||||||
|
rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs() <= character_controller.max_slope_angle
|
||||||
|
});
|
||||||
|
|
||||||
|
//println!("{}", is_grounded);
|
||||||
|
|
||||||
|
if is_grounded {
|
||||||
|
commands.entity(entity).insert(Grounded);
|
||||||
|
} else {
|
||||||
|
commands.entity(entity).remove::<Grounded>();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyboard_input(mut movement_event_writer: EventWriter<MovementAction>, keyboard_input: Res<ButtonInput<KeyCode>>){
|
pub fn keyboard_input(mut movement_event_writer: EventWriter<MovementAction>, keyboard_input: Res<ButtonInput<KeyCode>>){
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_xpbd_3d::prelude::*;
|
use bevy_xpbd_3d::prelude::*;
|
||||||
use bevy_editor_pls::prelude::*;
|
use bevy_editor_pls::prelude::*;
|
||||||
use charcacter_controller::*;
|
|
||||||
mod charcacter_controller;
|
mod charcacter_controller;
|
||||||
|
use charcacter_controller::*;
|
||||||
|
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins((DefaultPlugins, PhysicsPlugins::default(), EditorPlugin::default(), CharacterControllerPlugin))
|
.add_plugins((DefaultPlugins, PhysicsPlugins::default(), EditorPlugin::default(), CharacterControllerPlugin))
|
||||||
.add_systems(Startup, setup)
|
.add_systems(Startup, setup)
|
||||||
|
.add_plugins(FrameTimeDiagnosticsPlugin::default())
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue