From de9aa009862b724996b9412f1e322e25555831d7 Mon Sep 17 00:00:00 2001 From: rsninja722 Date: Sun, 3 Apr 2022 17:11:32 -0400 Subject: [PATCH 1/3] collisions (not colliding) --- .../src/scenes/player_interaction.rs | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index b5275dcf..4f8e550c 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -8,7 +8,10 @@ use crate::{ asset_manager::{load_music_from_internal_data, load_sound_from_internal_data}, discord::{DiscordChannel, DiscordRpcSignal}, global_resource_package::GlobalResources, - model::{player::Player, world_object::WorldSpaceObjectCollider}, + model::{ + player::Player, + world_object::{ObjectCollider, WorldSpaceObjectCollider}, + }, project_constants::ProjectConstants, rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer}, }; @@ -160,7 +163,6 @@ impl PlayableScene { let current_temperature = self.world_map.sample_temperature_at(player.position); let map_size = self.world_map.get_map_size(); // TODO: You can access the colission list with: self.world_colliders - // like this: self.world_colliders[0].size.x; // Get input direction components let h_axis = raylib.is_key_down(KeyboardKey::KEY_D) as i8 @@ -196,7 +198,42 @@ impl PlayableScene { let velocity_modifier = &player.velocity * delta_time; - player.position += velocity_modifier; + player.position.x += velocity_modifier.x; + + for i in &self.world_colliders { + if player.position.x <= i.position.x + i.size.x + && player.position.x + player.size >= i.position.x + && player.position.y <= i.position.y + i.size.y + && player.position.y + player.size >= i.position.y + { + player.position.x -= velocity_modifier.x; + player.velocity.x = 0; + break; + } + } + + if player.position.x < 0.0 || next_player_position.x > self.world_map.get_map_size().x * 128.0 { + player.position.x -= velocity_modifier.x; + player_velocity.x = 0.0; + } + + player.position.y += velocity_modifier.y; + + for i in &self.world_colliders { + if player.position.x <= i.position.x + i.size.x + && player.position.x + player.size >= i.position.x + && player.position.y <= i.position.y + i.size.y + && player.position.y + player.size >= i.position.y + { + player.position.y -= velocity_modifier.y; + break; + } + } + + if player.position.y < 0.0 || next_player_position.y > self.world_map.get_map_size().y * 128.0 { + player.position.y -= velocity_modifier.y; + player_velocity.y = 0.0; + } self.update_camera(raylib); } From ba2343b09f985abc02b7a2c6b57b90c70d103113 Mon Sep 17 00:00:00 2001 From: rsninja722 Date: Sun, 3 Apr 2022 17:56:58 -0400 Subject: [PATCH 2/3] collisions (colliding) --- game/dist/project-constants.json | 4 +- .../src/scenes/player_interaction.rs | 62 ++++++++++++++----- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/game/dist/project-constants.json b/game/dist/project-constants.json index 46f8c02e..537edb67 100644 --- a/game/dist/project-constants.json +++ b/game/dist/project-constants.json @@ -18,8 +18,8 @@ } }, "player": { - "max_velocity": 3, - "acceleration": 2, + "max_velocity": 30, + "acceleration": 20, "deceleration": 1, "start_size": 0.8 } diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index 777533a7..8ddd0cf3 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -161,6 +161,13 @@ impl PlayableScene { 20, Color::GREEN, ); + draw.draw_text( + format!("player: ({}, {}) size: {} map: ({}, {})", self.player.position.x,self.player.position.y,self.player.size,self.world_map.get_map_size().x,self.world_map.get_map_size().y).as_str(), + 10, + 50, + 20, + Color::GREEN, + ); } draw.draw_rectangle(draw.get_screen_width() / 2 - 225, 0, 450, 40, Color::WHITE); @@ -229,41 +236,62 @@ impl PlayableScene { let velocity_modifier = &player.velocity * delta_time; + let player_size = + (constants.tile_size as f32 * constants.player.start_size * player.size / 2.0) as f32; + player.position.x += velocity_modifier.x; for i in &self.world_colliders { - if player.position.x <= i.position.x + i.size.x - && player.position.x + player.size >= i.position.x - && player.position.y <= i.position.y + i.size.y - && player.position.y + player.size >= i.position.y + if player.position.x - player_size <= i.position.x + i.size.x / 2.0 + && player.position.x + player_size >= i.position.x + i.size.x / 2.0 + && player.position.y - player_size<= i.position.y + i.size.y / 2.0 + && player.position.y + player_size >= i.position.y + i.size.x / 2.0 { - player.position.x -= velocity_modifier.x; - player.velocity.x = 0; + if player.velocity.x < 0.0 { + player.position.x = i.position.x + i.size.x / 2.0 + player_size; + } else if player.velocity.x > 0.0 { + player.position.x = i.position.x - i.size.x / 2.0 - player_size; + } + + player.velocity.x = 0.0; break; } } - if player.position.x < 0.0 || next_player_position.x > self.world_map.get_map_size().x * 128.0 { - player.position.x -= velocity_modifier.x; - player_velocity.x = 0.0; + if player.position.x - player_size < 0.0 || player.position.x + player_size > self.world_map.get_map_size().x { + if player.velocity.x < 0.0 { + player.position.x = player_size; + } else if player.velocity.x > 0.0 { + player.position.x = self.world_map.get_map_size().x - player_size; + } + player.velocity.x = 0.0; } player.position.y += velocity_modifier.y; for i in &self.world_colliders { - if player.position.x <= i.position.x + i.size.x - && player.position.x + player.size >= i.position.x - && player.position.y <= i.position.y + i.size.y - && player.position.y + player.size >= i.position.y + if player.position.x - player_size <= i.position.x + i.size.x / 2.0 + && player.position.x + player_size >= i.position.x + i.size.x / 2.0 + && player.position.y - player_size<= i.position.y + i.size.y / 2.0 + && player.position.y + player_size >= i.position.y + i.size.x / 2.0 { - player.position.y -= velocity_modifier.y; + if player.velocity.y < 0.0 { + player.position.y = i.position.y + i.size.y / 2.0 + player_size; + } else if player.velocity.x > 0.0 { + player.position.y = i.position.y - i.size.y / 2.0 - player_size; + } + break; } } - if player.position.y < 0.0 || next_player_position.y > self.world_map.get_map_size().y * 128.0 { - player.position.y -= velocity_modifier.y; - player_velocity.y = 0.0; + if player.position.y + player_size > 0.0 || player.position.y - player_size < -self.world_map.get_map_size().y { + if player.velocity.y > 0.0 { + player.position.y = -player_size; + } else if player.velocity.y < 0.0 { + player.position.y = -self.world_map.get_map_size().y + player_size; + } + player.velocity.y = 0.0; } self.update_camera(raylib); From d6db37ca939b0badae7f5fe155dc768b72efe459 Mon Sep 17 00:00:00 2001 From: rsninja722 Date: Sun, 3 Apr 2022 18:05:44 -0400 Subject: [PATCH 3/3] collisions with objects --- game/dist/project-constants.json | 4 +- .../src/scenes/player_interaction.rs | 39 +++++++++++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/game/dist/project-constants.json b/game/dist/project-constants.json index 537edb67..46f8c02e 100644 --- a/game/dist/project-constants.json +++ b/game/dist/project-constants.json @@ -18,8 +18,8 @@ } }, "player": { - "max_velocity": 30, - "acceleration": 20, + "max_velocity": 3, + "acceleration": 2, "deceleration": 1, "start_size": 0.8 } diff --git a/game/game_logic/src/scenes/player_interaction.rs b/game/game_logic/src/scenes/player_interaction.rs index 24e5659d..b9374575 100644 --- a/game/game_logic/src/scenes/player_interaction.rs +++ b/game/game_logic/src/scenes/player_interaction.rs @@ -181,7 +181,15 @@ impl PlayableScene { Color::GREEN, ); draw.draw_text( - format!("player: ({}, {}) size: {} map: ({}, {})", self.player.position.x,self.player.position.y,self.player.size,self.world_map.get_map_size().x,self.world_map.get_map_size().y).as_str(), + format!( + "player: ({}, {}) size: {} map: ({}, {})", + self.player.position.x, + self.player.position.y, + self.player.size, + self.world_map.get_map_size().x, + self.world_map.get_map_size().y + ) + .as_str(), 10, 50, 20, @@ -263,21 +271,24 @@ impl PlayableScene { for i in &self.world_colliders { if player.position.x - player_size <= i.position.x + i.size.x / 2.0 && player.position.x + player_size >= i.position.x + i.size.x / 2.0 - && player.position.y - player_size<= i.position.y + i.size.y / 2.0 + && player.position.y - player_size <= i.position.y + i.size.y / 2.0 && player.position.y + player_size >= i.position.y + i.size.x / 2.0 { - if player.velocity.x < 0.0 { - player.position.x = i.position.x + i.size.x / 2.0 + player_size; - } else if player.velocity.x > 0.0 { - player.position.x = i.position.x - i.size.x / 2.0 - player_size; - } - + // if player.velocity.x < 0.0 { + // player.position.x = i.position.x + i.size.x / 2.0 + player_size; + // } else if player.velocity.x > 0.0 { + // player.position.x = i.position.x - i.size.x / 2.0 - player_size; + // } + + player.position.x -= velocity_modifier.x; player.velocity.x = 0.0; break; } } - if player.position.x - player_size < 0.0 || player.position.x + player_size > self.world_map.get_map_size().x { + if player.position.x - player_size < 0.0 + || player.position.x + player_size > self.world_map.get_map_size().x + { if player.velocity.x < 0.0 { player.position.x = player_size; } else if player.velocity.x > 0.0 { @@ -291,20 +302,24 @@ impl PlayableScene { for i in &self.world_colliders { if player.position.x - player_size <= i.position.x + i.size.x / 2.0 && player.position.x + player_size >= i.position.x + i.size.x / 2.0 - && player.position.y - player_size<= i.position.y + i.size.y / 2.0 + && player.position.y - player_size <= i.position.y + i.size.y / 2.0 && player.position.y + player_size >= i.position.y + i.size.x / 2.0 { if player.velocity.y < 0.0 { player.position.y = i.position.y + i.size.y / 2.0 + player_size; - } else if player.velocity.x > 0.0 { + } else if player.velocity.y > 0.0 { player.position.y = i.position.y - i.size.y / 2.0 - player_size; } + player.position.y -= velocity_modifier.y; + player.velocity.y = 0.0; break; } } - if player.position.y + player_size > 0.0 || player.position.y - player_size < -self.world_map.get_map_size().y { + if player.position.y + player_size > 0.0 + || player.position.y - player_size < -self.world_map.get_map_size().y + { if player.velocity.y > 0.0 { player.position.y = -player_size; } else if player.velocity.y < 0.0 {