Skip to content

Commit

Permalink
Wire up holding code
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Aug 15, 2024
1 parent 1dceaad commit a14335d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 54 deletions.
14 changes: 10 additions & 4 deletions src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use bevy::{
prelude::*,
};

use crate::prelude::Cooldown;
use crate::{
interaction::{GrabParams, ShadowParams},
prelude::Cooldown,
};

pub(super) mod prelude {
pub use super::{AvianPickupActor, AvianPickupActorState};
Expand Down Expand Up @@ -118,9 +121,12 @@ impl Component for AvianPickupActor {
fn register_component_hooks(hooks: &mut ComponentHooks) {
hooks.on_add(|mut world, targeted_entity, _component_id| {
let mut commands = world.commands();
commands
.entity(targeted_entity)
.insert((AvianPickupActorState::default(), Cooldown::default()));
commands.entity(targeted_entity).insert((
AvianPickupActorState::default(),
Cooldown::default(),
GrabParams::default(),
ShadowParams::default(),
));
});
}
}
2 changes: 1 addition & 1 deletion src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod hold;
mod pull;
mod throw;

pub(crate) use self::{drop::DropObject, pull::PullObject, throw::ThrowObject};
pub(crate) use self::{drop::DropObject, hold::prelude::*, pull::PullObject, throw::ThrowObject};

pub(super) fn plugin(app: &mut App) {
app.add_plugins((hold::plugin, pull::plugin, drop::plugin, throw::plugin));
Expand Down
127 changes: 78 additions & 49 deletions src/interaction/hold.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::TAU;

use crate::prelude::*;

pub(super) fn plugin(app: &mut App) {
Expand All @@ -6,52 +8,39 @@ pub(super) fn plugin(app: &mut App) {
.add_systems(hold.in_set(AvianPickupSystem::HoldObject));
}

fn hold(q_actor: Query<(&AvianPickupActorState, &GlobalTransform)>) {
for (&state, transform) in q_actor.iter() {
let AvianPickupActorState::Holding(_entity) = state else {
continue;
};
let _transform = transform.compute_transform();
info!("Hold!")
}
pub(super) mod prelude {
pub(crate) use super::{GrabParams, ShadowParams};
}

#[derive(Debug, Copy, Clone, Component)]
struct ShadowParams {
target_position: Vec3,
target_rotation: Quat,
max_angular: f32,
max_damp_angular: f32,
max_speed: f32,
max_damp_speed: f32,
// damp_factor = 1
// teleport_distance = 0
}

#[derive(Debug, Copy, Clone, Component)]
struct GrabParams {
contact_amount: f32,
time_to_arrive: f32,
/// Todo: this is never read
error_time: f32,
}

fn grabcontroller_simulate(
/// Basically GrabController::Simulate
fn hold(
time: Res<Time>,
mut q_object: Query<(
&ShadowParams,
&Mass,
&mut LinearVelocity,
&mut AngularVelocity,
&Position,
&Rotation,
&GlobalTransform,
)>,
mut q_actor: Query<(
&AvianPickupActorState,
&GlobalTransform,
&mut GrabParams,
&ShadowParams,
)>,
) {
let dt = time.delta_seconds();
for (shadow, mass, mut velocity, mut angvel, position, rotation, mut grab) in
q_object.iter_mut()
{
for (&state, transform, mut grab, shadow) in q_actor.iter_mut() {
let AvianPickupActorState::Holding(entity) = state else {
continue;
};
let _transform = transform.compute_transform();
info!("Hold!");
let dt = time.delta_seconds();
//
// Unwrap cannot fail: rigid bodies are guarateed to have a
// `Mass`, `LinearVelocity`, `AngularVelocity`, and `GlobalTransform`
let (mass, mut velocity, mut angvel, object_transform) = q_object.get_mut(entity).unwrap();
let object_transform = object_transform.compute_transform();

// imo InContactWithHeavyObject will always be false,
// as we are effectively asking "is the current object heavier than the
// current object?", so I removed that branch
Expand All @@ -64,8 +53,7 @@ fn grabcontroller_simulate(
// Skipping `ComputeShadowControl` as we use SI units directly
grab.time_to_arrive = compute_shadow_controller(
&mut shadow,
*position,
*rotation,
object_transform,
&mut velocity,
&mut angvel,
grab.time_to_arrive,
Expand All @@ -78,10 +66,52 @@ fn grabcontroller_simulate(
}
}

#[derive(Debug, Copy, Clone, Component)]
pub(crate) struct ShadowParams {
target_position: Vec3,
target_rotation: Quat,
max_angular: f32,
max_damp_angular: f32,
max_speed: f32,
max_damp_speed: f32,
// damp_factor = 1
// teleport_distance = 0
}

impl Default for ShadowParams {
fn default() -> Self {
Self {
target_position: Vec3::ZERO,
target_rotation: Quat::IDENTITY,
max_angular: TAU * 10.0,
max_damp_angular: 0.0,
max_speed: 25.4,
max_damp_speed: 25.4 * 2.,
}
}
}

#[derive(Debug, Copy, Clone, Component)]
pub(crate) struct GrabParams {
contact_amount: f32,
time_to_arrive: f32,
/// Todo: this is never read
error_time: f32,
}

impl Default for GrabParams {
fn default() -> Self {
Self {
contact_amount: 0.0,
time_to_arrive: 0.0,
error_time: 0.0,
}
}
}

fn compute_shadow_controller(
params: &mut ShadowParams,
position: Position,
rotation: Rotation,
transform: Transform,
linear_velocity: &mut LinearVelocity,
angular_velocity: &mut AngularVelocity,
seconds_to_arrival: f32,
Expand All @@ -98,7 +128,7 @@ fn compute_shadow_controller(
return seconds_to_arrival;
}

let delta_position = params.target_position - position.0;
let delta_position = params.target_position - transform.translation;
// Teleport distance is always 0, so we don't care about that branch of the
// code. That would be the only place where position and rotation are
// mutated, so that means we get to use them immutably here!
Expand All @@ -116,9 +146,9 @@ fn compute_shadow_controller(
.into();

// Don't think this is used? It at least doesn't appear in 2013's shadow params
let _last_position = position.0 + linear_velocity.0 * dt;
let _last_position = transform.translation + linear_velocity.0 * dt;

let delta_rotation = params.target_rotation * rotation.0.inverse();
let delta_rotation = params.target_rotation * transform.rotation.inverse();

let delta_angles = delta_rotation.to_scaled_axis();
*angular_velocity = compute_controller(
Expand Down Expand Up @@ -172,9 +202,8 @@ fn compute_controller(
velocity += acceleration_damping; // vel = (4, 0, 0)
}

let mut acceleration = Vec3::ZERO;
if max_speed > 0.0 {
acceleration = delta * scale_delta; // accel = (8, 0, 0)
let mut acceleration = delta * scale_delta; // accel = (8, 0, 0)
let speed = delta.length() * scale_delta; // speed = 8
if speed > max_speed {
let some_factor_idk = max_speed / speed; // some_fac = 4 / 8 = 0.5
Expand All @@ -185,7 +214,7 @@ fn compute_controller(
velocity
}

fn compute_controller_trimmed(
fn _compute_controller_trimmed(
mut velocity: Vec3,
delta: Vec3,
max_speed: f32,
Expand Down Expand Up @@ -213,11 +242,11 @@ fn compute_controller_trimmed(
velocity
}

fn compute_collider_no_damp(
velocity: Vec3,
fn _compute_collider_no_damp(
_velocity: Vec3,
delta: Vec3,
max_speed: f32,
max_damp_speed: f32,
_max_damp_speed: f32,
scale_delta: f32,
) -> Vec3 {
if max_speed > 0.0 {
Expand Down
1 change: 1 addition & 0 deletions src/interaction/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn find_object(
let can_hold = prop.toi <= config.trace_length;
if can_hold {
cooldown.hold();
info!("Start Holding");
*state = AvianPickupActorState::Holding(prop.entity);
} else {
let object_transform = object_transform.compute_transform();
Expand Down

0 comments on commit a14335d

Please sign in to comment.