Skip to content

Commit

Permalink
Implement reduction of player collision boxes with ball.
Browse files Browse the repository at this point in the history
When the ball is going fast and the player is not a goalie.
  • Loading branch information
IanTayler committed Nov 18, 2020
1 parent 9baa0eb commit 58706af
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion assets/sprites/player.ron
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ Prefab(
push_strength: 96.0,
side: UpperSide,
),
robot: Robot(logic_module: EngineRunner("basic")),
human: Human,
// robot: Robot(logic_module: EngineRunner("basic")),
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion src/components/collision_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use amethyst::{
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Clone, Serialize, PrefabData)]
#[derive(Debug, Deserialize, Copy, Clone, Serialize, PrefabData)]
#[prefab(Component)]
pub struct CollisionBox {
/// Distance from the center to the upper left.
Expand Down
23 changes: 22 additions & 1 deletion src/components/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config, utils};
use crate::{components::CollisionBox, config, utils};
use amethyst::{
assets::PrefabData,
derive::PrefabData,
Expand Down Expand Up @@ -72,3 +72,24 @@ impl PlayerType {
impl Component for PlayerType {
type Storage = DenseVecStorage<Self>;
}

/// Makes balls that go fast collide less with players.
pub fn reduce_collision(
player_collision: &CollisionBox,
player_type: &PlayerType,
ball_speed: f32,
) -> CollisionBox {
match player_type {
PlayerType::Goalie => *player_collision,
_ => {
let max_speed = config::BALL_SPEED_FOR_MINIMUM_COLLISION;
let min_factor = config::BALL_MINIMUM_COLLISION_FACTOR;
let reduce_factor = ball_speed.min(max_speed) * (1.0 - min_factor) / max_speed;
let keep_factor = 1.0 - reduce_factor;
CollisionBox {
upper_left_distance: player_collision.upper_left_distance * keep_factor,
lower_right_distance: player_collision.lower_right_distance * keep_factor,
}
}
}
}
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// TODO: move this to configuration files?
// Screen constants
pub const SCREEN_WIDTH: f32 = 256.0;
pub const SCREEN_HEIGHT: f32 = 256.0;
// Some ~physics constants
pub const BALL_SPEED_FOR_MINIMUM_COLLISION: f32 = 256.0;
pub const BALL_MINIMUM_COLLISION_FACTOR: f32 = 0.3;
// Player position constants
pub const MAXIMUM_TEAM_SIZE: usize = 5;
pub const FORWARD_NUMBER: usize = 0;
pub const GOALIE_NUMBER: usize = 1;
Expand Down
16 changes: 12 additions & 4 deletions src/systems/collision.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
components::{collision_box, Ball, CollisionBox, MovementState, Net, Player},
components::{
collision_box, player, Ball, CollisionBox, MovementState, Net, Player, PlayerType,
},
resources::Score,
utils::Side,
};
Expand All @@ -18,6 +20,7 @@ impl<'s> System<'s> for Collisions {
WriteStorage<'s, MovementState>,
ReadStorage<'s, Ball>,
ReadStorage<'s, Player>,
ReadStorage<'s, PlayerType>,
ReadStorage<'s, Net>,
ReadStorage<'s, CollisionBox>,
ReadStorage<'s, Transform>,
Expand All @@ -32,6 +35,7 @@ impl<'s> System<'s> for Collisions {
mut movement_states,
balls,
players,
player_types,
nets,
collision_boxes,
transforms,
Expand All @@ -46,13 +50,17 @@ impl<'s> System<'s> for Collisions {
(&mut movement_states, &balls, &collision_boxes, &transforms).join()
{
// Handle collisions with players (i.e. kicks)
for (player, player_collision, player_transform) in
(&players, &collision_boxes, &transforms).join()
for (player, player_type, player_collision, player_transform) in
(&players, &player_types, &collision_boxes, &transforms).join()
{
if collision_box::are_colliding(
ball_collision,
ball_transform,
player_collision,
&player::reduce_collision(
player_collision,
player_type,
movement_state.velocity.norm(),
),
player_transform,
) {
let center_difference =
Expand Down

0 comments on commit 58706af

Please sign in to comment.