Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn setup(
commands.spawn((
RigidBody::Dynamic,
Collider::cuboid(1.0, 1.0, 1.0),
AngularVelocity(Vec3::new(2.5, 3.5, 1.5)),
Velocity::from_angular_xyz(2.5, 3.5, 1.5),
Mesh3d(meshes.add(Cuboid::from_length(1.0))),
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
Transform::from_xyz(0.0, 4.0, 0.0),
Expand Down
6 changes: 3 additions & 3 deletions crates/avian2d/examples/ccd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn setup(mut commands: Commands) {
},
Transform::from_xyz(-200.0, -200.0, 0.0),
RigidBody::Kinematic,
AngularVelocity(25.0),
Velocity::from_angular(25.0),
Collider::rectangle(1.0, 400.0),
// Enable swept CCD for this body. Considers both translational and rotational motion by default.
// This could also be on the ball projectiles.
Expand All @@ -72,7 +72,7 @@ fn setup(mut commands: Commands) {
},
Transform::from_xyz(200.0, -200.0, 0.0),
RigidBody::Kinematic,
AngularVelocity(-25.0),
Velocity::from_angular(-25.0),
Collider::rectangle(1.0, 400.0),
// Enable swept CCD for this body. Considers both translational and rotational motion by default.
// This could also be on the ball projectiles.
Expand Down Expand Up @@ -140,7 +140,7 @@ fn spawn_balls(
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.7, 0.9))),
Transform::from_xyz(0.0, 350.0, 0.0),
RigidBody::Dynamic,
LinearVelocity(2000.0 * direction),
Velocity::from_linear(2000.0 * direction),
Friction::ZERO.with_combine_rule(CoefficientCombine::Min),
Collider::from(circle),
));
Expand Down
14 changes: 7 additions & 7 deletions crates/avian2d/examples/custom_collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,29 @@ fn setup(
),
RigidBody::Dynamic,
CircleCollider::new(particle_radius.adjust_precision()),
LinearDamping(0.4),
Damping::from_linear(0.4),
));
}
}
}

/// Pulls all particles towards the center.
fn center_gravity(
mut particles: Query<(&Transform, &mut LinearVelocity), Without<CenterBody>>,
mut particles: Query<(&Transform, &mut Velocity), Without<CenterBody>>,
time: Res<Time>,
) {
let delta_seconds = time.delta_secs_f64().adjust_precision();
for (transform, mut lin_vel) in &mut particles {
for (transform, mut velocity) in &mut particles {
let pos_delta = transform.translation.truncate().adjust_precision();
let dir = -pos_delta.normalize_or_zero();
lin_vel.0 += 800.0 * delta_seconds * dir;
velocity.linear += 800.0 * delta_seconds * dir;
}
}

/// Rotates the center body periodically clockwise and counterclockwise.
fn rotate(mut query: Query<&mut AngularVelocity, With<CenterBody>>, time: Res<Time>) {
fn rotate(mut query: Query<&mut Velocity, With<CenterBody>>, time: Res<Time>) {
let sin = 3.0 * time.elapsed_secs_f64().adjust_precision().sin();
for mut ang_vel in &mut query {
ang_vel.0 = sin;
for mut velocity in &mut query {
velocity.angular = sin;
}
}
18 changes: 8 additions & 10 deletions crates/avian2d/examples/dynamic_character_2d/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn movement(
mut controllers: Query<(
&MovementAcceleration,
&JumpImpulse,
&mut LinearVelocity,
&mut Velocity,
Has<Grounded>,
)>,
) {
Expand All @@ -201,16 +201,14 @@ fn movement(
let delta_time = time.delta_secs_f64().adjust_precision();

for event in movement_reader.read() {
for (movement_acceleration, jump_impulse, mut linear_velocity, is_grounded) in
&mut controllers
{
for (movement_acceleration, jump_impulse, mut velocity, is_grounded) in &mut controllers {
match event {
MovementAction::Move(direction) => {
linear_velocity.x += *direction * movement_acceleration.0 * delta_time;
velocity.linear.x += *direction * movement_acceleration.0 * delta_time;
}
MovementAction::Jump => {
if is_grounded {
linear_velocity.y = jump_impulse.0;
velocity.linear.y = jump_impulse.0;
}
}
}
Expand All @@ -221,14 +219,14 @@ fn movement(
/// Slows down movement in the X direction.
fn apply_movement_damping(
time: Res<Time>,
mut query: Query<(&MovementDampingFactor, &mut LinearVelocity)>,
mut query: Query<(&MovementDampingFactor, &mut Velocity)>,
) {
// Precision is adjusted so that the example works with
// both the `f32` and `f64` features. Otherwise you don't need this.
let delta_time = time.delta_secs_f64().adjust_precision();

for (damping_factor, mut linear_velocity) in &mut query {
// We could use `LinearDamping`, but we don't want to dampen movement along the Y axis
linear_velocity.x *= 1.0 / (1.0 + damping_factor.0 * delta_time);
for (damping_factor, mut velocity) in &mut query {
// We could use `Damping`, but we don't want to dampen movement along the Y axis
velocity.linear.x *= 1.0 / (1.0 + damping_factor.0 * delta_time);
}
}
2 changes: 1 addition & 1 deletion crates/avian2d/examples/fixed_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn setup(mut commands: Commands) {
.spawn((
square_sprite.clone(),
RigidBody::Kinematic,
AngularVelocity(1.5),
Velocity::from_angular(1.5),
))
.id();

Expand Down
46 changes: 21 additions & 25 deletions crates/avian2d/examples/kinematic_character_2d/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,19 @@ fn update_grounded(
fn movement(
time: Res<Time>,
mut movement_reader: MessageReader<MovementAction>,
mut controllers: Query<(
&CharacterMovementSettings,
&mut LinearVelocity,
Has<Grounded>,
)>,
mut controllers: Query<(&CharacterMovementSettings, &mut Velocity, Has<Grounded>)>,
) {
let delta_secs = time.delta_secs_f64().adjust_precision();

for event in movement_reader.read() {
for (movement, mut linear_velocity, is_grounded) in &mut controllers {
for (movement, mut velocity, is_grounded) in &mut controllers {
match event {
MovementAction::Move(direction) => {
linear_velocity.x += *direction * movement.acceleration * delta_secs;
velocity.linear.x += *direction * movement.acceleration * delta_secs;
}
MovementAction::Jump => {
if is_grounded {
linear_velocity.y = movement.jump_impulse;
velocity.linear.y = movement.jump_impulse;
}
}
}
Expand All @@ -228,43 +224,43 @@ fn movement(
/// Applies gravity to character controllers.
fn apply_gravity(
time: Res<Time>,
mut controllers: Query<(&CharacterMovementSettings, &mut LinearVelocity)>,
mut controllers: Query<(&CharacterMovementSettings, &mut Velocity)>,
) {
let delta_secs = time.delta_secs_f64().adjust_precision();

for (movement, mut linear_velocity) in &mut controllers {
for (movement, mut velocity) in &mut controllers {
let gravity_direction = movement.gravity.normalize_or_zero();

let velocity_along_gravity = linear_velocity.dot(gravity_direction);
let velocity_along_gravity = velocity.linear.dot(gravity_direction);
if velocity_along_gravity > movement.terminal_velocity {
// Don't apply more gravity if we're already at terminal velocity.
continue;
}

// Calculate the new velocity after applying gravity.
let new_velocity = linear_velocity.0 + movement.gravity * delta_secs;
let new_velocity = velocity.linear + movement.gravity * delta_secs;

// Don't exceed terminal velocity.
let new_velocity_along_gravity = new_velocity.dot(gravity_direction);
if new_velocity_along_gravity < movement.terminal_velocity {
linear_velocity.0 = new_velocity;
velocity.linear = new_velocity;
} else {
linear_velocity.0 = gravity_direction * movement.terminal_velocity;
velocity.linear = gravity_direction * movement.terminal_velocity;
}
}
}

/// Slows down movement in the X axis.
fn apply_movement_damping(
mut query: Query<(&CharacterMovementSettings, &mut LinearVelocity)>,
mut query: Query<(&CharacterMovementSettings, &mut Velocity)>,
time: Res<Time>,
) {
let delta_secs = time.delta_secs_f64().adjust_precision();

for (movement, mut linear_velocity) in &mut query {
// Approximate exponential decay. We could use `LinearDamping` for this,
for (movement, mut velocity) in &mut query {
// Approximate exponential decay. We could use `Damping` for this,
// but we don't want to dampen movement along the Y axis.
linear_velocity.x *= 1.0 / (1.0 + delta_secs * movement.damping);
velocity.linear.x *= 1.0 / (1.0 + delta_secs * movement.damping);
}
}

Expand All @@ -280,15 +276,15 @@ fn move_and_slide(
Option<&GroundDetection>,
Option<&mut CharacterCollisions>,
&mut Transform,
&mut LinearVelocity,
&mut Velocity,
&Collider,
),
With<CharacterController>,
>,
move_and_slide: MoveAndSlide,
time: Res<Time>,
) {
for (entity, ground_detection, mut collisions, mut transform, mut lin_vel, collider) in
for (entity, ground_detection, mut collisions, mut transform, mut velocity, collider) in
&mut query
{
let mut hit_ground_or_ceiling = false;
Expand All @@ -308,7 +304,7 @@ fn move_and_slide(
collider,
transform.translation.xy().adjust_precision(),
Rotation::from(transform.rotation).as_radians(),
lin_vel.0,
velocity.linear,
time.delta(),
&MoveAndSlideConfig::default(),
&SpatialQueryFilter::from_excluded_entities([entity]),
Expand All @@ -330,7 +326,7 @@ fn move_and_slide(
// Decompose the original input velocity into components relative to the hit normal and the up direction,
// to determine how much of the velocity is contributing to climbing, slipping, and unconstrained movement.
let [horizontal_component, vertical_component] =
split_into_components(lin_vel.0, up);
split_into_components(velocity.linear, up);

// Decompose the horizontal component and the current sliding velocity to determine
// whether the character is trying to climb or slip, and whether it is actually climbing or slipping.
Expand Down Expand Up @@ -394,9 +390,9 @@ fn move_and_slide(
// and to prevent sticking to ceilings when jumping.
if hit_ground_or_ceiling {
let up = transform.up().xy().adjust_precision();
let velocity_along_up = lin_vel.dot(up);
let velocity_along_up = velocity.linear.dot(up);
let new_velocity_along_up = projected_velocity.dot(up);
lin_vel.0 += (new_velocity_along_up - velocity_along_up) * up;
velocity.linear += (new_velocity_along_up - velocity_along_up) * up;
}
}
}
Expand Down Expand Up @@ -451,7 +447,7 @@ fn apply_forces_to_dynamic_bodies(
}

let touch_dir = -collision.normal.adjust_precision();
let relative_velocity = collision.character_velocity - forces.linear_velocity();
let relative_velocity = collision.character_velocity - forces.velocity().linear;
let touch_velocity = touch_dir.dot(relative_velocity) * touch_dir;
let impulse = touch_velocity * mass;

Expand Down
12 changes: 6 additions & 6 deletions crates/avian2d/examples/many_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,25 @@ fn setup(
fn movement(
time: Res<Time>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut marbles: Query<&mut LinearVelocity, With<Controllable>>,
mut marbles: Query<&mut Velocity, With<Controllable>>,
) {
// Precision is adjusted so that the example works with
// both the `f32` and `f64` features. Otherwise you don't need this.
let delta_time = time.delta_secs_f64().adjust_precision();

for mut linear_velocity in &mut marbles {
for mut velocity in &mut marbles {
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
// Use a higher acceleration for upwards movement to overcome gravity
linear_velocity.y += 2500.0 * delta_time;
velocity.linear.y += 2500.0 * delta_time;
}
if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) {
linear_velocity.y -= 500.0 * delta_time;
velocity.linear.y -= 500.0 * delta_time;
}
if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) {
linear_velocity.x -= 500.0 * delta_time;
velocity.linear.x -= 500.0 * delta_time;
}
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
linear_velocity.x += 500.0 * delta_time;
velocity.linear.x += 500.0 * delta_time;
}
}
}
26 changes: 13 additions & 13 deletions crates/avian2d/examples/move_and_slide_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ fn setup(
///
/// This only updates velocity. The actual movement is handled by the `run_move_and_slide` system.
fn player_movement(
mut query: Query<&mut LinearVelocity, With<Player>>,
mut query: Query<&mut Velocity, With<Player>>,
time: Res<Time>,
input: Res<ButtonInput<KeyCode>>,
) {
for mut lin_vel in &mut query {
for mut velocity in &mut query {
// Determine movement velocity from input
let mut movement_velocity = Vec2::ZERO;
if input.pressed(KeyCode::KeyW) {
Expand All @@ -215,12 +215,12 @@ fn player_movement(
}

// Add to current velocity
lin_vel.0 += movement_velocity.adjust_precision();
velocity.linear += movement_velocity.adjust_precision();

let current_speed = lin_vel.length();
let current_speed = velocity.linear.length();
if current_speed > 0.0 {
// Apply friction
lin_vel.0 = lin_vel.0 / current_speed
velocity.linear = velocity.linear / current_speed
* (current_speed - current_speed * 20.0 * time.delta_secs().adjust_precision())
.max(0.0)
}
Expand All @@ -236,7 +236,7 @@ fn run_move_and_slide(
(
Entity,
&mut Transform,
&mut LinearVelocity,
&mut Velocity,
&mut TouchedEntities,
&Collider,
),
Expand All @@ -246,7 +246,7 @@ fn run_move_and_slide(
time: Res<Time>,
mut gizmos: Gizmos,
) {
for (entity, mut transform, mut lin_vel, mut touched, collider) in &mut query {
for (entity, mut transform, mut velocity, mut touched, collider) in &mut query {
touched.clear();

// Perform move and slide
Expand All @@ -261,7 +261,7 @@ fn run_move_and_slide(
.to_euler(EulerRot::XYZ)
.2
.adjust_precision(),
lin_vel.0,
velocity.linear,
time.delta(),
&MoveAndSlideConfig::default(),
&SpatialQueryFilter::from_excluded_entities([entity]),
Expand All @@ -286,20 +286,20 @@ fn run_move_and_slide(

// Update transform and velocity
transform.translation = position.extend(0.0).f32();
lin_vel.0 = projected_velocity;
velocity.linear = projected_velocity;
}
}

fn update_debug_text(
mut text: Single<&mut Text, With<DebugText>>,
player: Single<(&LinearVelocity, &TouchedEntities, &CollidingEntities), With<Player>>,
player: Single<(&Velocity, &TouchedEntities, &CollidingEntities), With<Player>>,
names: Query<NameOrEntity>,
) {
let (lin_vel, touched, colliding_entities) = player.into_inner();
let (velocity, touched, colliding_entities) = player.into_inner();
***text = format!(
"velocity: [{:.3}, {:.3}]\n{} intersections (goal is 0): {:#?}\n{} touched: {:#?}",
lin_vel.x,
lin_vel.y,
velocity.linear.x,
velocity.linear.y,
colliding_entities.len(),
names
.iter_many(colliding_entities.iter())
Expand Down
Loading
Loading