From ea5bbb8673424f7cb138f2a7b07ec01e29d32a4b Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 28 Feb 2018 10:38:37 +0000 Subject: [PATCH 1/6] Update copyright to 2018 --- src/gamelib/collider.cpp | 2 +- src/gamelib/collider.hpp | 2 +- src/gamelib/world_moveable.cpp | 2 +- src/gamelib/world_moveable.hpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gamelib/collider.cpp b/src/gamelib/collider.cpp index 0f7e79e..7ae37f7 100644 --- a/src/gamelib/collider.cpp +++ b/src/gamelib/collider.cpp @@ -1,5 +1,5 @@ /* - Copyright 2016, 2017 Michele "King_DuckZ" Santullo + Copyright 2016-2018 Michele "King_DuckZ" Santullo This file is part of MyCurry. diff --git a/src/gamelib/collider.hpp b/src/gamelib/collider.hpp index 2450c72..d7d5814 100644 --- a/src/gamelib/collider.hpp +++ b/src/gamelib/collider.hpp @@ -1,5 +1,5 @@ /* - Copyright 2016, 2017 Michele "King_DuckZ" Santullo + Copyright 2016-2018 Michele "King_DuckZ" Santullo This file is part of MyCurry. diff --git a/src/gamelib/world_moveable.cpp b/src/gamelib/world_moveable.cpp index c9ac2be..2e20957 100644 --- a/src/gamelib/world_moveable.cpp +++ b/src/gamelib/world_moveable.cpp @@ -1,5 +1,5 @@ /* - Copyright 2016, 2017 Michele "King_DuckZ" Santullo + Copyright 2016-2018 Michele "King_DuckZ" Santullo This file is part of MyCurry. diff --git a/src/gamelib/world_moveable.hpp b/src/gamelib/world_moveable.hpp index cac42de..ec6492f 100644 --- a/src/gamelib/world_moveable.hpp +++ b/src/gamelib/world_moveable.hpp @@ -1,5 +1,5 @@ /* - Copyright 2016, 2017 Michele "King_DuckZ" Santullo + Copyright 2016-2018 Michele "King_DuckZ" Santullo This file is part of MyCurry. From 89f7d5c94801077c6cfcdf40ca0ff04a9be01487 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 1 Mar 2018 23:19:27 +0000 Subject: [PATCH 2/6] I think this should return 1, not 0. Like this, I can loop over the column/row facing the collision side, ie for z = tile; z < tile + len. --- src/gamelib/collider.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gamelib/collider.cpp b/src/gamelib/collider.cpp index 7ae37f7..14613bb 100644 --- a/src/gamelib/collider.cpp +++ b/src/gamelib/collider.cpp @@ -34,8 +34,8 @@ namespace curry { vec2us check_tiles_count (const vec2us& parObjSize, const vec2f& parDirection) { assert(parObjSize > 0); - const uint16_t horz = (parDirection.y() != 0.0f ? parObjSize.x() : 0); - const uint16_t vert = (parDirection.x() != 0.0f ? parObjSize.y() : 0); + const uint16_t horz = (parDirection.y() != 0.0f ? parObjSize.x() : 1); + const uint16_t vert = (parDirection.x() != 0.0f ? parObjSize.y() : 1); return vec2us(horz, vert); } From 5bccef1f4f2c7199fbf455eacc494b3e11e2ec69 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 1 Mar 2018 23:19:42 +0000 Subject: [PATCH 3/6] Add print statements to hopefully help with debugging. --- src/gamelib/collider.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/gamelib/collider.cpp b/src/gamelib/collider.cpp index 14613bb..e58b1de 100644 --- a/src/gamelib/collider.cpp +++ b/src/gamelib/collider.cpp @@ -60,8 +60,10 @@ 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; @@ -69,24 +71,28 @@ namespace curry { 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); - vec2us stop_at = pixel_to_world_tile(parWorld, parFrom); + 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); assert(tile_relative_offs < vector_cast(parWorld.tile_size())); auto forward_callback = [=,&parWorld,&stop_at,&tile_relative_offs](vec2us tile) { - for (auto curr_pos : vwr::increasing_sequence_range(tile, tile + parObjectSize)) { + std::cout << "\t------------------- callback invoked ---------------\n"; + 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); return false; } } - stop_at = tile - side_offset; + stop_at = tile; + std::cout << "\tstop_at set to " << stop_at << " and returning from callback\n"; return true; }; - const vec2f pix_side_offset = - make_pixel_side_offset(parWorld.tile_size(), side_offset, direction); for_each_voxel_under_segment( parFrom + pix_side_offset, parTo + pix_side_offset, @@ -95,7 +101,12 @@ namespace curry { 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; } } //namespace curry From 6861329ab94aaca07a3a06bc6aa469829ea89b7d Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 5 Mar 2018 10:34:41 +0000 Subject: [PATCH 4/6] Rename for_each_voxel to for_each_cell. --- src/gamelib/collider.cpp | 2 +- src/gamelib/grid_raytrace.cpp | 2 +- src/gamelib/grid_raytrace.hpp | 2 +- test/unit/grid_raytrace.cpp | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gamelib/collider.cpp b/src/gamelib/collider.cpp index e58b1de..7491461 100644 --- a/src/gamelib/collider.cpp +++ b/src/gamelib/collider.cpp @@ -93,7 +93,7 @@ namespace curry { return true; }; - for_each_voxel_under_segment( + for_each_cell_under_segment( parFrom + pix_side_offset, parTo + pix_side_offset, parObjectSize, diff --git a/src/gamelib/grid_raytrace.cpp b/src/gamelib/grid_raytrace.cpp index a5b472a..0fbee0e 100644 --- a/src/gamelib/grid_raytrace.cpp +++ b/src/gamelib/grid_raytrace.cpp @@ -93,7 +93,7 @@ namespace curry { //see: //http://stackoverflow.com/questions/24679963/precise-subpixel-line-drawing-algorithm-rasterization-algorithm - void for_each_voxel_under_segment ( + void for_each_cell_under_segment ( const vec2f& parFrom, const vec2f& parTo, vec2us parObjSize, diff --git a/src/gamelib/grid_raytrace.hpp b/src/gamelib/grid_raytrace.hpp index 92809b1..c264efa 100644 --- a/src/gamelib/grid_raytrace.hpp +++ b/src/gamelib/grid_raytrace.hpp @@ -30,7 +30,7 @@ namespace curry { float segment_intersection (const vec2f& parA, const vec2f& parDirA, const vec2f& parB, const vec2f& parDirB); #endif - void for_each_voxel_under_segment ( + void for_each_cell_under_segment ( const vec2f& parFrom, const vec2f& parTo, vec2us parObjSize, diff --git a/test/unit/grid_raytrace.cpp b/test/unit/grid_raytrace.cpp index 438a738..cae8b5c 100644 --- a/test/unit/grid_raytrace.cpp +++ b/test/unit/grid_raytrace.cpp @@ -25,7 +25,7 @@ #include TEST_CASE ("Check that 2D raytracing works", "[raytracing][geometry]") { - using curry::for_each_voxel_under_segment; + using curry::for_each_cell_under_segment; using curry::vec2us; using curry::vec2f; @@ -49,7 +49,7 @@ TEST_CASE ("Check that 2D raytracing works", "[raytracing][geometry]") { { std::vector diagonal; - for_each_voxel_under_segment ( + for_each_cell_under_segment ( vec2f(0.0f, 0.0f), //from vec2f(639.0f, 639.0f), //to vec2us(1, 1), //objsize From 9da16ca82d5007811aae83119bb1c9aca10b9d55 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 5 Mar 2018 10:35:15 +0000 Subject: [PATCH 5/6] Assert that tile calculation is correct. --- src/gamelib/worldgrid.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gamelib/worldgrid.cpp b/src/gamelib/worldgrid.cpp index 72cce67..83f7362 100644 --- a/src/gamelib/worldgrid.cpp +++ b/src/gamelib/worldgrid.cpp @@ -135,9 +135,11 @@ namespace curry { vec2us pixel_to_world_tile (const WorldGrid& parWorld, const vec2f& parPos) { assert(position_is_on_map(parWorld, parPos)); - return vector_cast( + const auto retval = vector_cast( parPos / vector_cast(parWorld.tile_size()) ); + assert(world_tile_to_pixel(parWorld, retval) <= parPos); + return retval; } vec2f world_tile_to_pixel (const WorldGrid& parWorld, const vec2us& parTile) { From 6b3cb59bc2c157a3388481ec416ea31cae7d6da2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 5 Mar 2018 10:35:48 +0000 Subject: [PATCH 6/6] 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