Fix collision right-/downwards.

It still glitches when walking diagonally.
This commit is contained in:
King_DuckZ 2018-03-05 10:35:48 +00:00
parent 9da16ca82d
commit 6b3cb59bc2

View file

@ -39,6 +39,12 @@ namespace curry {
return vec2us(horz, vert); 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 ( vec2f make_pixel_side_offset (
const vec2us& parTileSize, const vec2us& parTileSize,
const vec2us& parSideOffset, const vec2us& parSideOffset,
@ -60,53 +66,44 @@ namespace curry {
const vec2f& parTo, const vec2f& parTo,
const WorldGrid& parWorld const WorldGrid& parWorld
) { ) {
std::cout << "---------------- collision test start --------------------\n";
const vec2f direction = parTo - parFrom; const vec2f direction = parTo - parFrom;
std::cout << "direction: " << direction << ' ';
std::cout << "src_tile=" << pixel_to_world_tile(parWorld, parFrom) << '\n';
if (direction == 0.0f) if (direction == 0.0f)
return parTo; return parTo;
const vec2us side_offset = collision_borders(parObjectSize, direction); const vec2us side_offset = collision_borders(parObjectSize, direction);
const vec2us length = check_tiles_count(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 = const vec2f pix_side_offset =
make_pixel_side_offset(parWorld.tile_size(), side_offset, direction); make_pixel_side_offset(parWorld.tile_size(), side_offset, direction);
vec2us stop_at = pixel_to_world_tile(parWorld, parFrom + pix_side_offset); const vec2f to = parTo + pix_side_offset;
vec2f tile_relative_offs = parTo - world_tile_to_pixel(parWorld, dest_tile); 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<vec2f>(parWorld.tile_size())); assert(tile_relative_offs < vector_cast<vec2f>(parWorld.tile_size()));
auto forward_callback = [=,&parWorld,&stop_at,&tile_relative_offs](vec2us tile) { vec2f collision_mask(0.0f);
std::cout << "\t------------------- callback invoked ---------------\n"; auto forward_callback = [=,&parWorld,&stop_at,&tile_relative_offs,&collision_mask](vec2us tile) {
for (auto curr_pos : vwr::increasing_sequence_range<vec2us>(tile, tile + length)) { for (auto curr_pos : vwr::increasing_sequence_range<vec2us>(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; const bool walkable = parWorld.tile_property(parWorld.tile(curr_pos)).walkable;
if (not walkable) { if (not walkable) {
std::cout << "\t*not* walkable!\n"; collision_mask = direction_mask(direction);
tile_relative_offs = vec2f(0.0f); tile_relative_offs *= (1.0f - collision_mask);
return false; return false;
} }
} }
stop_at = tile; stop_at = tile;
std::cout << "\tstop_at set to " << stop_at << " and returning from callback\n";
return true; return true;
}; };
for_each_cell_under_segment( for_each_cell_under_segment(
parFrom + pix_side_offset, from,
parTo + pix_side_offset, to,
parObjectSize, parObjectSize,
parWorld, parWorld,
forward_callback, forward_callback,
false false
); );
std::cout << "loop done\n";
std::cout << "world_tile_to_pixel() = " << world_tile_to_pixel(parWorld, stop_at) << return world_tile_to_pixel(parWorld, stop_at) + tile_relative_offs - pix_side_offset + collision_mask * direction_mask(pix_side_offset) * 31.0f;
" 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 } //namespace curry