checkpoint, kinda fucked up

This commit is contained in:
Joe Ardent 2023-02-04 14:00:56 -08:00
parent 936e3591d1
commit 37ce29fd71
6 changed files with 112 additions and 66 deletions

View file

@ -58,9 +58,9 @@ pub struct CatControllerSettings {
impl Default for CatControllerSettings {
fn default() -> Self {
Self {
kp: 20.0,
kd: 4.5,
ki: 0.07,
kp: 40.0,
kd: 5.0,
ki: 0.1,
}
}
}
@ -85,7 +85,7 @@ impl Default for CatControllerState {
pitch_prev: Default::default(),
decay_factor: 0.99,
roll_limit: 1.5,
pitch_limit: 1.0,
pitch_limit: 0.8,
}
}
}

View file

@ -1,17 +1,23 @@
use std::f32::consts::PI;
#[cfg(feature = "inspector")]
use std::time::Instant;
use bevy::prelude::{Commands, Entity, Query, Res, ResMut, Time, Transform, Vec3, With, Without};
use bevy_rapier3d::prelude::{
CollisionGroups, ExternalForce, Friction, Group, QueryFilter, RapierConfiguration,
use bevy::prelude::{
Commands, Entity, Quat, Query, Res, ResMut, Time, Transform, Vec3, With, Without,
};
use bevy_rapier3d::{
prelude::{
CollisionGroups, ExternalForce, Group, MultibodyJoint, QueryFilter, RapierConfiguration,
RapierContext, ReadMassProperties, Velocity,
},
rapier::prelude::JointAxis,
};
#[cfg(feature = "inspector")]
use super::ActionDebugInstant;
use super::{CatControllerSettings, CatControllerState, MovementSettings, Tunneling};
use crate::{
bike::{CyberBikeBody, CyberWheel, BIKE_WHEEL_COLLISION_GROUP},
bike::{CyberBikeBody, CyberSteering, CyberWheel, BIKE_WHEEL_COLLISION_GROUP},
input::InputState,
};
@ -61,7 +67,7 @@ pub(super) fn falling_cat(
let (derivative, integral) = control_vars.update_pitch(pitch_error, time.delta_seconds());
let mag = (settings.kp * pitch_error) + (settings.ki * integral) + (settings.kd * derivative);
if mag.is_finite() {
forces.torque += wright * mag;
forces.torque += wright * mag * 0.25;
}
#[cfg(feature = "inspector")]
@ -79,25 +85,32 @@ pub(super) fn falling_cat(
pub(super) fn input_forces(
settings: Res<MovementSettings>,
input: Res<InputState>,
mut cquery: Query<&mut Friction, With<CyberWheel>>,
mut bquery: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>,
mut braking_query: Query<&mut MultibodyJoint, With<CyberWheel>>,
mut body_query: Query<
(&Transform, &mut ExternalForce),
(With<CyberBikeBody>, Without<CyberSteering>),
>,
mut steering_query: Query<&mut Transform, With<CyberSteering>>,
) {
let (xform, mut forces) = bquery.single_mut();
let (xform, mut forces) = body_query.single_mut();
// thrust
let thrust = xform.forward() * input.throttle * settings.accel;
forces.force += thrust;
let point = xform.translation + xform.back();
let force = ExternalForce::at_point(thrust, point, xform.translation);
*forces += force;
// brake
for mut friction in cquery.iter_mut() {
friction.coefficient = if input.brake { 2.0 } else { 0.0 };
for mut motor in braking_query.iter_mut() {
let factor = if input.brake { settings.accel } else { 0.0 };
motor.data.set_motor_velocity(JointAxis::X, 0.0, factor);
}
// steering
let force = xform.right() * input.yaw * settings.sensitivity;
let point = xform.translation + xform.forward();
let force = ExternalForce::at_point(force, point, xform.translation);
*forces += force;
let angle = input.yaw * (PI / 2.0);
let mut steering = steering_query.single_mut();
steering.rotation = Quat::from_axis_angle(Vec3::Y, angle + 90.0f32.to_radians());
//dbg!(steering.rotation);
}
/// Don't let the wheels get stuck underneat the planet
@ -153,7 +166,7 @@ pub(super) fn tunnel_out(
}
tunneling.frames -= 1;
force.force = tunneling.dir * settings.gravity * 1.5 * mprops.0.mass;
#[cfg(feature = "inspector")]
//#[cfg(feature = "inspector")]
dbg!(&tunneling);
}
}

View file

@ -82,5 +82,5 @@ pub(super) fn spawn_cyberbike(
.insert(CatControllerState::default())
.id();
spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials);
spawn_tires(&mut commands, bike, xform, &wheel_conf, &mut meshterials);
}

View file

@ -6,6 +6,9 @@ use bevy::{
#[derive(Component)]
pub struct CyberBikeBody;
#[derive(Component)]
pub struct CyberSteering;
#[derive(Debug, Component)]
pub struct CyberWheel;

View file

@ -1,16 +1,16 @@
use bevy::prelude::{shape::UVSphere as Tire, *};
use bevy_rapier3d::prelude::{
Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction,
MultibodyJoint, PrismaticJointBuilder, Restitution, RigidBody, Sleeping,
TransformInterpolation,
Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping,
ExternalForce, Friction, MultibodyJoint, PrismaticJointBuilder, Restitution,
RevoluteJointBuilder, RigidBody, Sleeping, TransformInterpolation,
};
use super::{CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
pub fn spawn_tires(
commands: &mut Commands,
xform: &Transform,
bike: Entity,
xform: Transform,
conf: &WheelConfig,
meshterials: &mut Meshterial,
) {
@ -47,8 +47,13 @@ pub fn spawn_tires(
};
let friction = Friction {
coefficient: 0.0,
..Default::default()
coefficient: 1.0,
combine_rule: CoefficientCombineRule::Min,
};
let restitution = Restitution {
coefficient: 0.8,
combine_rule: CoefficientCombineRule::Average,
};
let mut wheel_poses = Vec::with_capacity(2);
@ -58,7 +63,7 @@ pub fn spawn_tires(
let wheel_x = 0.0;
let wheel_z = -conf.front_forward;
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
wheel_poses.push(offset);
wheel_poses.push((offset, Some(CyberSteering)));
}
// rear
@ -66,12 +71,10 @@ pub fn spawn_tires(
let wheel_x = 0.0;
let wheel_z = conf.rear_back;
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
wheel_poses.push(offset);
wheel_poses.push((offset, None));
}
for offset in wheel_poses {
let trans = xform.translation + offset;
let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans);
for (offset, steering) in wheel_poses {
let wheel_damping = Damping {
linear_damping: 0.8,
..Default::default()
@ -83,42 +86,69 @@ pub fn spawn_tires(
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
.local_anchor1(offset)
.limits(limits)
.motor_position(-0.1, stiffness, damping);
let joint = MultibodyJoint::new(bike, prismatic);
.motor_position(limits[0], stiffness, damping);
let spatial_bundle = SpatialBundle {
transform: wheel_pos_in_world,
let fork = commands
.spawn(SpatialBundle {
transform: Transform::from_translation(offset),
..Default::default()
};
})
.set_parent(bike)
.id();
let tire_spundle = SpatialBundle {
transform: Transform::IDENTITY,
..Default::default()
};
commands
let suspension = MultibodyJoint::new(fork, prismatic);
let sentity = commands
.spawn(RigidBody::Dynamic)
.insert(spatial_bundle)
.insert((
.insert(suspension)
.insert(SpatialBundle {
transform: Transform::from_translation(offset),
..Default::default()
})
.set_parent(bike)
.id();
if let Some(steering) = steering {
commands.entity(sentity).insert(steering);
}
let revolute = RevoluteJointBuilder::new(Vec3::X);
let axel = MultibodyJoint::new(sentity, revolute);
let wheel_bundle = (
wheel_collider,
mass_props,
wheel_damping,
ccd,
not_sleeping,
joint,
wheels_collision_group,
friction,
restitution,
CyberWheel,
ExternalForce::default(),
Restitution::new(0.2),
))
not_sleeping,
axel,
);
let _wentity = commands
.spawn(RigidBody::Dynamic)
.insert(SpatialBundle {
// transform: Transform::from_rotation(xform.rotation)
// .with_translation(xform.translation)
transform: Transform::from_rotation(Quat::from_axis_angle(
Vec3::Z,
90.0f32.to_radians(),
)),
..Default::default()
})
.insert(wheel_bundle)
.with_children(|wheel| {
wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert(
TransformInterpolation {
wheel
.spawn(SpatialBundle::default())
.insert(pbr_bundle.clone())
.insert(TransformInterpolation {
start: None,
end: None,
},
);
});
})
.set_parent(sentity)
.id();
}
}

View file

@ -32,7 +32,7 @@ fn spawn_planet(
let pcollide = (
shape,
Friction {
coefficient: 0.05,
coefficient: 0.7,
..Default::default()
},
Restitution::new(0.0),