Compare commits

..

6 commits

Author SHA1 Message Date
6b3cb59bc2 Fix collision right-/downwards.
It still glitches when walking diagonally.
2018-03-05 10:35:48 +00:00
9da16ca82d Assert that tile calculation is correct. 2018-03-05 10:35:15 +00:00
6861329ab9 Rename for_each_voxel to for_each_cell. 2018-03-05 10:34:41 +00:00
5bccef1f4f Add print statements to hopefully help with debugging. 2018-03-01 23:19:42 +00:00
89f7d5c948 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.
2018-03-01 23:19:27 +00:00
ea5bbb8673 Update copyright to 2018 2018-02-28 10:38:37 +00:00
8 changed files with 36 additions and 26 deletions

View file

@ -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<vec2f>(parWorld.tile_size()));
auto forward_callback = [=,&parWorld,&stop_at,&tile_relative_offs](vec2us tile) {
for (auto curr_pos : vwr::increasing_sequence_range<vec2us>(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<vec2us>(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

View file

@ -1,5 +1,5 @@
/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
Copyright 2016-2018 Michele "King_DuckZ" Santullo
This file is part of MyCurry.

View file

@ -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,

View file

@ -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,

View file

@ -1,5 +1,5 @@
/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
Copyright 2016-2018 Michele "King_DuckZ" Santullo
This file is part of MyCurry.

View file

@ -1,5 +1,5 @@
/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
Copyright 2016-2018 Michele "King_DuckZ" Santullo
This file is part of MyCurry.

View file

@ -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<vec2us>(
const auto retval = vector_cast<vec2us>(
parPos / vector_cast<vec2f>(parWorld.tile_size())
);
assert(world_tile_to_pixel(parWorld, retval) <= parPos);
return retval;
}
vec2f world_tile_to_pixel (const WorldGrid& parWorld, const vec2us& parTile) {

View file

@ -25,7 +25,7 @@
#include <vector>
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<vec2us> 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