From 41f6ae77e730890e43a3d2ec372d27b0fa30f197 Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Mon, 2 May 2022 13:28:55 +0200 Subject: [PATCH] working player bound collision --- pong/src/game_field.rs | 8 ++++---- pong/src/pong.rs | 6 +++--- src/lib.rs | 8 ++++---- www/index.js | 1 + 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pong/src/game_field.rs b/pong/src/game_field.rs index 2d6e2f1..30c1c09 100644 --- a/pong/src/game_field.rs +++ b/pong/src/game_field.rs @@ -3,7 +3,7 @@ use crate::game_object::components::{DefaultGeomComp, DefaultPhysicsComp}; use crate::game_object::game_object::{DefaultGameObject, GameObject}; use crate::geom::geom::Vector; use crate::geom::shape::{Shape, ShapeType}; -use crate::pong::pong_collisions::{handle_ball_bounds_collision, handle_player_ball_collision}; +use crate::pong::pong_collisions::{handle_ball_bounds_collision, handle_player_ball_collision, handle_player_bound_collision}; use crate::utils::utils::{DefaultLoggerFactory, Logger, LoggerFactory, NoopLogger}; use std::borrow::{Borrow, BorrowMut}; use std::cell::{Cell, Ref, RefCell, RefMut}; @@ -69,7 +69,7 @@ impl Field { field.collision_handler.register( (String::from("player"), String::from("bound")), - handle_ball_bounds_collision, + handle_player_bound_collision, ); field.collision_detector.set_groups( @@ -135,11 +135,11 @@ impl Field { let input = input_opt.unwrap(); match input.input { InputType::UP => { - let updated_vel_y = (obj_mut.vel().y - 1.).max(-1.); + let updated_vel_y = (obj_mut.vel().y - 1.).max(-5.); obj_mut.vel_mut().y = updated_vel_y; } InputType::DOWN => { - let updated_vel_y = (obj_mut.vel().y + 1.).min(1.); + let updated_vel_y = (obj_mut.vel().y + 1.).min(5.); obj_mut.vel_mut().y = updated_vel_y; } }; diff --git a/pong/src/pong.rs b/pong/src/pong.rs index 88e8d96..4ca8472 100644 --- a/pong/src/pong.rs +++ b/pong/src/pong.rs @@ -45,11 +45,11 @@ pub mod pong_collisions { let shape = player.shape().clone(); let player_orientation = player.orientation().clone(); let height = match shape { - ShapeType::Rect(_, _, height) => height.clone(), - ShapeType::Circle(_, radius) => radius * 2., + ShapeType::Rect(_, _, height) => height.clone() / 2., + ShapeType::Circle(_, radius) => radius, }; let mut perpendicular = player_orientation.get_opposing_orthogonal(bound.orientation()); - perpendicular.y *= height; + perpendicular.y *= (height + 1.); let mut new_pos = bound.pos().clone(); new_pos.add(&perpendicular); let player_pos = player.pos_mut(); diff --git a/src/lib.rs b/src/lib.rs index b1d9ed5..8a87d42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,8 +33,8 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; #[derive(Clone, Copy, Debug, PartialEq, Serialize)] pub struct GameObjectDTO { pub id: u16, - pub x: u16, - pub y: u16, + pub x: f64, + pub y: f64, pub orientation_x: f64, pub orientation_y: f64, pub vel_x: f64, @@ -53,8 +53,8 @@ impl GameObjectDTO { let shape = obj.shape(); return GameObjectDTO { id: obj.id(), - x: pos.x as u16, - y: pos.y as u16, + x: pos.x, + y: pos.y, orientation_x: orientation.x, orientation_y: orientation.y, vel_x: vel.x, diff --git a/www/index.js b/www/index.js index e870258..d6f9ef4 100644 --- a/www/index.js +++ b/www/index.js @@ -94,6 +94,7 @@ const drawObjects = () => { drawLine(ctx, obj.x, obj.y, obj.x + obj.vel_x * 20, obj.y + obj.vel_y * 20, 'red') // orientation drawLine(ctx, obj.x, obj.y, obj.x + obj.orientation_x * 20, obj.y + obj.orientation_y * 20, 'blue') + ctx.fillText(`[x: ${obj.x}, y: ${obj.y}]`, obj.x + 10, obj.y) } }) }