diff --git a/src/gamelib/collider.cpp b/src/gamelib/collider.cpp index 0f7e79e..4cfd314 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. @@ -34,11 +34,17 @@ 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); } + 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, @@ -61,41 +67,43 @@ namespace curry { const WorldGrid& parWorld ) { const vec2f direction = parTo - parFrom; - std::cout << "direction: " << direction << ' '; 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); - vec2us stop_at = pixel_to_world_tile(parWorld, parFrom); - vec2f tile_relative_offs = parTo - world_tile_to_pixel(parWorld, dest_tile); + const vec2f pix_side_offset = + make_pixel_side_offset(parWorld.tile_size(), side_offset, direction); + 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) { - for (auto curr_pos : vwr::increasing_sequence_range(tile, tile + parObjectSize)) { + 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)) { const bool walkable = parWorld.tile_property(parWorld.tile(curr_pos)).walkable; if (not walkable) { - tile_relative_offs = vec2f(0.0f); + collision_mask = direction_mask(direction); + tile_relative_offs *= (1.0f - collision_mask); return false; } } - stop_at = tile - side_offset; + stop_at = tile; 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, + for_each_cell_under_segment( + from, + to, parObjectSize, parWorld, forward_callback, false ); - 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 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/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/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. 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) { 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