From 6b3cb59bc2c157a3388481ec416ea31cae7d6da2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 5 Mar 2018 10:35:48 +0000 Subject: [PATCH] Fix collision right-/downwards. It still glitches when walking diagonally. --- src/gamelib/collider.cpp | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/gamelib/collider.cpp b/src/gamelib/collider.cpp index 7491461..4cfd314 100644 --- a/src/gamelib/collider.cpp +++ b/src/gamelib/collider.cpp @@ -39,6 +39,12 @@ namespace curry { return vec2us(horz, vert); } + vec2f direction_mask (const vec2f& parDirection) { + const auto horz = (parDirection.x() == 0.0f ? 0.0f : 1.0f); + const auto vert = (parDirection.y() == 0.0f ? 0.0f : 1.0f); + return vec2f(horz, vert); + } + vec2f make_pixel_side_offset ( const vec2us& parTileSize, const vec2us& parSideOffset, @@ -60,53 +66,44 @@ namespace curry { const vec2f& parTo, const WorldGrid& parWorld ) { - std::cout << "---------------- collision test start --------------------\n"; const vec2f direction = parTo - parFrom; - std::cout << "direction: " << direction << ' '; - std::cout << "src_tile=" << pixel_to_world_tile(parWorld, parFrom) << '\n'; if (direction == 0.0f) return parTo; const vec2us side_offset = collision_borders(parObjectSize, direction); const vec2us length = check_tiles_count(parObjectSize, direction); - std::cout << "side_offset: " << side_offset << " length: " << length << '\n'; - const vec2us dest_tile = pixel_to_world_tile(parWorld, parTo); const vec2f pix_side_offset = make_pixel_side_offset(parWorld.tile_size(), side_offset, direction); - vec2us stop_at = pixel_to_world_tile(parWorld, parFrom + pix_side_offset); - vec2f tile_relative_offs = parTo - world_tile_to_pixel(parWorld, dest_tile); + const vec2f to = parTo + pix_side_offset; + const vec2f from = parFrom + pix_side_offset; + vec2us stop_at = pixel_to_world_tile(parWorld, from); + const vec2us dest_tile = pixel_to_world_tile(parWorld, to); + vec2f tile_relative_offs = to - world_tile_to_pixel(parWorld, dest_tile); assert(tile_relative_offs < vector_cast(parWorld.tile_size())); - auto forward_callback = [=,&parWorld,&stop_at,&tile_relative_offs](vec2us tile) { - std::cout << "\t------------------- callback invoked ---------------\n"; + vec2f collision_mask(0.0f); + auto forward_callback = [=,&parWorld,&stop_at,&tile_relative_offs,&collision_mask](vec2us tile) { for (auto curr_pos : vwr::increasing_sequence_range(tile, tile + length)) { - std::cout << "\ttesting " << curr_pos << " (stop_at=" << stop_at << ", tile=" << tile << ")\n"; const bool walkable = parWorld.tile_property(parWorld.tile(curr_pos)).walkable; if (not walkable) { - std::cout << "\t*not* walkable!\n"; - tile_relative_offs = vec2f(0.0f); + collision_mask = direction_mask(direction); + tile_relative_offs *= (1.0f - collision_mask); return false; } } stop_at = tile; - std::cout << "\tstop_at set to " << stop_at << " and returning from callback\n"; return true; }; for_each_cell_under_segment( - parFrom + pix_side_offset, - parTo + pix_side_offset, + from, + to, parObjectSize, parWorld, forward_callback, false ); - std::cout << "loop done\n"; - std::cout << "world_tile_to_pixel() = " << world_tile_to_pixel(parWorld, stop_at) << - " tile_relative_offs: " << tile_relative_offs << - " pix_side_offset: " << pix_side_offset << '\n'; - std::cout << "returning " << world_tile_to_pixel(parWorld, stop_at) + tile_relative_offs - pix_side_offset << '\n'; - return world_tile_to_pixel(parWorld, stop_at) + tile_relative_offs - pix_side_offset; + return world_tile_to_pixel(parWorld, stop_at) + tile_relative_offs - pix_side_offset + collision_mask * direction_mask(pix_side_offset) * 31.0f; } } //namespace curry