From cff4feea6b0ee0249b6955c0783a302df77b82c4 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 16 Nov 2008 22:07:46 +0000 Subject: fixed rounding errors in the character core causing it to favour certain directions --- src/base/math.hpp | 7 +++++++ src/game/gamecore.cpp | 21 +++++++++++---------- src/game/gamecore.hpp | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/base/math.hpp b/src/base/math.hpp index 5e3f7ede..302935d7 100644 --- a/src/base/math.hpp +++ b/src/base/math.hpp @@ -19,6 +19,13 @@ inline float sign(float f) return f<0.0f?-1.0f:1.0f; } +inline int round(float f) +{ + if(f > 0) + return (int)(f+0.5f); + return (int)(f-0.5f); +} + template inline T mix(const T a, const T b, TB amount) { diff --git a/src/game/gamecore.cpp b/src/game/gamecore.cpp index ccef8de8..c15f3adf 100644 --- a/src/game/gamecore.cpp +++ b/src/game/gamecore.cpp @@ -186,9 +186,9 @@ void CHARACTER_CORE::tick(bool use_input) // get ground state bool grounded = false; - if(col_check_point((int)(pos.x+phys_size/2), (int)(pos.y+phys_size/2+5))) + if(col_check_point(pos.x+phys_size/2, pos.y+phys_size/2+5)) grounded = true; - if(col_check_point((int)(pos.x-phys_size/2), (int)(pos.y+phys_size/2+5))) + if(col_check_point(pos.x-phys_size/2, pos.y+phys_size/2+5)) grounded = true; vec2 target_direction = normalize(vec2(input.target_x, input.target_y)); @@ -466,16 +466,17 @@ void CHARACTER_CORE::move() void CHARACTER_CORE::write(NETOBJ_CHARACTER_CORE *obj_core) { - obj_core->x = (int)pos.x; - obj_core->y = (int)pos.y; - obj_core->vx = (int)(vel.x*256.0f); - obj_core->vy = (int)(vel.y*256.0f); + obj_core->x = round(pos.x); + obj_core->y = round(pos.y); + + obj_core->vx = round(vel.x*256.0f); + obj_core->vy = round(vel.y*256.0f); obj_core->hook_state = hook_state; obj_core->hook_tick = hook_tick; - obj_core->hook_x = (int)hook_pos.x; - obj_core->hook_y = (int)hook_pos.y; - obj_core->hook_dx = (int)(hook_dir.x*256.0f); - obj_core->hook_dy = (int)(hook_dir.y*256.0f); + obj_core->hook_x = round(hook_pos.x); + obj_core->hook_y = round(hook_pos.y); + obj_core->hook_dx = round(hook_dir.x*256.0f); + obj_core->hook_dy = round(hook_dir.y*256.0f); obj_core->hooked_player = hooked_player; obj_core->jumped = jumped; obj_core->direction = direction; diff --git a/src/game/gamecore.hpp b/src/game/gamecore.hpp index 5db5207b..5ff68ca7 100644 --- a/src/game/gamecore.hpp +++ b/src/game/gamecore.hpp @@ -194,7 +194,7 @@ public: #define min(a, b) ( a > b ? b : a) #define max(a, b) ( a > b ? a : b) -inline bool col_check_point(float x, float y) { return col_is_solid((int)x, (int)y) != 0; } +inline bool col_check_point(float x, float y) { return col_is_solid(round(x), round(y)) != 0; } inline bool col_check_point(vec2 p) { return col_check_point(p.x, p.y); } #endif -- cgit 1.4.1