Proper colliders

This commit is contained in:
Evan Pratten 2021-04-24 15:07:04 -04:00
parent ab0fe7b693
commit 8b260be5b7
5 changed files with 55 additions and 17 deletions

View File

@ -4,8 +4,8 @@
"y": 10000.0
},
"player_spawn": {
"x": 200.0,
"y": 100.0
"x": 220.0,
"y": 50.0
},
"fish": [
{

View File

@ -69,13 +69,8 @@ impl InGameScreen {
// Render every collider
for collider in game_core.world.colliders.iter() {
context_2d.draw_rectangle_lines_ex(
Rectangle {
x: collider.x - (collider.width / 2.0),
y: collider.y - (collider.height / 2.0),
width: collider.width,
height: collider.height,
},
2,
collider,
1,
Color::RED,
);
}
@ -119,7 +114,9 @@ impl Screen for InGameScreen {
// Render the world
self.render_world(&mut context_2d, game_core);
if game_core.show_simple_debug_info{
self.render_colliders(&mut context_2d, game_core);
}
// Render entities
let fish_clone = game_core.world.fish.clone();

View File

@ -130,18 +130,18 @@ pub fn update_player_movement(
let player_real_movement = game_core.player.direction * speed_multiplier;
if raw_movement_direction.distance_to(Vector2::zero()) > game_core.player.size.y / 2.0 {
game_core.player.is_moving = true;
game_core.player.position += player_real_movement;
// Check for any collisions
for collider in game_core.world.colliders.iter() {
if collider.check_collision_point_rec(game_core.player.position + player_real_movement)
{
// game_core.player.is_moving = false;
if game_core.player.collides_with_rec(collider) {
game_core.player.is_moving = false;
break;
}
}
if game_core.player.is_moving {
game_core.player.position += player_real_movement;
if !game_core.player.is_moving {
game_core.player.position -= player_real_movement;
}
} else {
game_core.player.is_moving = false;

View File

@ -1,4 +1,6 @@
use raylib::math::Vector2;
use raylib::math::{Rectangle, Vector2};
use crate::lib::utils::triangles::rotate_vector;
#[derive(Debug, Default)]
pub struct Player {
@ -23,4 +25,35 @@ impl Player {
..Default::default()
}
}
pub fn collides_with_rec(&self, rectangle: &Rectangle) -> bool {
// // Build a bounding box of the player by their corners
// let top_left_corner = self.position - (self.size / 2.0);
// let bottom_right_corner = self.position + (self.size / 2.0);
// let top_right_corner = Vector2 {
// x: bottom_right_corner.x,
// y: top_left_corner.y,
// };
// let bottom_left_corner = Vector2 {
// x: top_left_corner.x,
// y: bottom_right_corner.y,
// };
// // Get the rotation
// let rotation = Vector2::zero().angle_to(self.direction);
// // Rotate the bounds
// let top_left_corner = rotate_vector(top_left_corner, rotation);
// let bottom_right_corner = rotate_vector(bottom_right_corner, rotation);
// let top_right_corner = rotate_vector(top_right_corner, rotation);
// let bottom_left_corner = rotate_vector(bottom_left_corner, rotation);
// // Check for collisions
// return rectangle.check_collision_point_rec(top_left_corner)
// || rectangle.check_collision_point_rec(bottom_right_corner)
// || rectangle.check_collision_point_rec(top_right_corner)
// || rectangle.check_collision_point_rec(bottom_left_corner);
return rectangle.check_collision_circle_rec(self.position, (self.size.y * 0.5) / 2.0);
}
}

View File

@ -35,7 +35,15 @@ impl World {
result.fish = FishEntity::new_from_positions(&result.fish_positions);
// Init colliders
result.colliders = colliders;
result.colliders = Vec::new();
for collider in colliders.iter(){
result.colliders.push(Rectangle {
x: collider.x - (collider.width / 2.0),
y: collider.y - (collider.height / 2.0),
width: collider.width,
height: collider.height,
});
}
Ok(result)
}