Skip to content

Commit

Permalink
use vsync by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzque committed Dec 6, 2023
1 parent 70b758c commit 1c6fed3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 54 deletions.
7 changes: 0 additions & 7 deletions src/game/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub fn render_settings_gui(mut settings: ResMut<SettingsResource>, mut contexts:

ui.add(egui::Slider::new(&mut settings.max_speed, 0.0..=2.0).text("Max Speed"));
ui.add(egui::Slider::new(&mut settings.max_force, 0.0..=2.0).text("Max Force"));

ui.add(
egui::Slider::new(&mut settings.avoidance_force_bounds, 0.0..=100.0)
.text("Avoidance Force: Bounds"),
Expand All @@ -31,7 +30,6 @@ pub fn render_settings_gui(mut settings: ResMut<SettingsResource>, mut contexts:
egui::Slider::new(&mut settings.avoidance_distance_dropzone, 0.0..=500.0)
.text("Avoidance Distance: Dropzone "),
);

ui.add(
egui::Slider::new(&mut settings.velocity_time_scale, 0.0..=4000.0)
.text("Velocity Time Scale"),
Expand All @@ -52,7 +50,6 @@ pub fn render_settings_gui(mut settings: ResMut<SettingsResource>, mut contexts:
egui::Slider::new(&mut settings.cohesion_distance, 5.0..=150.0)
.text("Cohesion Distance (px)"),
);

ui.add(
egui::Slider::new(&mut settings.separation_weight, 0.0..=10.0)
.text("Separation Weight"),
Expand All @@ -69,22 +66,18 @@ pub fn render_settings_gui(mut settings: ResMut<SettingsResource>, mut contexts:
ui.add(
egui::Slider::new(&mut settings.cohesion_weight, 0.0..=10.0).text("Cohesion Weight"),
);

ui.add(
egui::Slider::new(&mut settings.controller_turn_speed, 0.0..=10.0)
.text("Controller Turn Speed"),
);

ui.add(
egui::Slider::new(&mut settings.controller_acceleration, 0.0..=1000.0)
.text("Controller Acceleration"),
);

ui.add(
egui::Slider::new(&mut settings.controller_dampening, 0.0..=1000.0)
.text("Controller Dampening"),
);

ui.add(
egui::Slider::new(&mut settings.controller_max_speed, 0.0..=1000.0)
.text("Controller Max Speed"),
Expand Down
28 changes: 2 additions & 26 deletions src/game/icons/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ fn update_key_input(
) {
let dt = time.delta_seconds();
if let Ok((_entity, mut transform, mut velocity_)) = query.get_single_mut() {
let turn = if keys.any_pressed([KeyCode::Left, KeyCode::Q]) {
let turn = if keys.any_pressed([KeyCode::Left, KeyCode::A]) {
// turn left
Some(1.0)
} else if keys.any_pressed([KeyCode::Right, KeyCode::E]) {
} else if keys.any_pressed([KeyCode::Right, KeyCode::D]) {
// turn right
Some(-1.0)
} else {
Expand All @@ -50,26 +50,11 @@ fn update_key_input(
transform.rotation = normalize_angle(transform.rotation);
}

// get forward and strafe vectors from camera rotation
let rotation = transform.rotation;
// // vector pointing forwards relative to the camera rotation, ignoring the y axis
// let forward_vector = {
// let f = rotation.mul_vec3(Vec3::Z).normalize();
// Vec3::new(f.x, 0.0, f.z).normalize()
// };
// // vector pointing left/right horizontally relative to the camera rotation
// let strafe_vector = Quat::from_rotation_y(90.0f32.to_radians())
// .mul_vec3(forward_vector)
// .normalize();

let r = std::f32::consts::PI / 2.0;
// Vector pointing forwards relative to the camera rotation
let forward_vector = Vec2::new((rotation - r).cos(), (rotation - r).sin());

// Vector pointing left/right horizontally relative to the camera rotation
// In 2D, this is a 90 degrees (or π/2 radians) rotation of the forward vector
let strafe_vector = Vec2::new(-forward_vector.y, forward_vector.x);

let mut accel = Vec2::ZERO;
if keys.any_pressed([KeyCode::Up, KeyCode::W]) {
// forward
Expand All @@ -79,14 +64,6 @@ fn update_key_input(
// backward
accel += forward_vector * 1.0;
}
if keys.pressed(KeyCode::A) {
// strafe left
accel += strafe_vector * -1.0;
}
if keys.pressed(KeyCode::D) {
// strafe right
accel += strafe_vector * 1.0;
}

// normalized and scaled by acceleration setting
let accel: Vec2 = if accel.length() > 0.0 {
Expand Down Expand Up @@ -122,6 +99,5 @@ fn update_key_input(
};

velocity_.0 = velocity;
// println!("updated velocity: {:?}", velocity);
}
}
35 changes: 19 additions & 16 deletions src/game/icons/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl<T> SpatialIndex<T>
where
T: Hash + Eq + Clone + Debug,
{
// iterator: impl Iterator<Item = (Vec2, Entity)>
pub fn new(min: Vec2, max: Vec2, cell_size: f32) -> Self {
Self {
min,
Expand Down Expand Up @@ -53,7 +52,6 @@ where
self.by_entity.insert(entity, (key, position, velocity));
}

/// Returns (T, position, velocity, distance)
pub fn query(
&self,
position: Vec2,
Expand Down Expand Up @@ -111,27 +109,32 @@ mod tests {
#[test]
fn test_spatial() {
let mut index = SpatialIndex::new(Vec2::new(-100.0, 100.0), Vec2::new(-100.0, 100.0), 10.0);
index.insert(1, Vec2::new(0.0, 0.0), Vec2::splat(0.0));
index.insert(2, Vec2::new(-20.0, -20.0), Vec2::splat(0.0));
index.insert(3, Vec2::new(20.0, 20.0), Vec2::splat(0.0));
assert_eq!(
index
.query(Vec2::new(0.5, 0.5), 10.0)
.map(|r| r.key)
.collect::<Vec<_>>(),
&[1]
);
index.insert(1, Vec2::new(-20.0, 0.0), Vec2::splat(0.0));
index.insert(2, Vec2::new(-10.0, 0.0), Vec2::splat(0.0));
index.insert(3, Vec2::new(0.0, 0.0), Vec2::splat(0.0));
index.insert(4, Vec2::new(10.0, 0.0), Vec2::splat(0.0));
index.insert(5, Vec2::new(20.0, 0.0), Vec2::splat(0.0));
index.insert(6, Vec2::new(30.0, 0.0), Vec2::splat(0.0));
index.insert(7, Vec2::new(40.0, 0.0), Vec2::splat(0.0));

assert!(!index
.query(Vec2::new(0.5, 0.5), 5.0)
.map(|r| r.key)
.collect::<Vec<_>>()
.is_empty());

let mut results = index
.query(Vec2::new(0.5, 0.5), 50.0)
.query(Vec2::new(0.0, 0.0), 20.0)
.map(|r| r.key)
.collect::<Vec<_>>();
results.sort();
assert_eq!(results, &[1, 2, 3]);
assert_eq!(results, &[1, 2, 4, 5]);

let mut results = index
.query(Vec2::new(20.0, 20.0), 30.0)
.query(Vec2::new(20.0, 0.0), 10.0)
.map(|r| r.key)
.collect::<Vec<_>>();
results.sort();
assert_eq!(results, &[1, 3]);
assert_eq!(results, &[4, 6]);
}
}
6 changes: 3 additions & 3 deletions src/game/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ impl Default for SettingsResource {
seek_weight: 0.1,

controller_turn_speed: 3.85,
controller_acceleration: 210.0,
controller_dampening: 330.0,
controller_max_speed: 480.0,
controller_acceleration: 35.0,
controller_dampening: 210.0,
controller_max_speed: 100.0,
}
}
}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ fn main() {
.set(WindowPlugin {
primary_window: Some(Window {
title: "Icon Wars".to_string(),
// present_mode: PresentMode::AutoVsync,
present_mode: PresentMode::Immediate,
present_mode: PresentMode::AutoVsync,
fit_canvas_to_parent: true,
resizable: true,
..Default::default()
Expand Down

0 comments on commit 1c6fed3

Please sign in to comment.