Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
6b3cb59bc2 | |||
9da16ca82d | |||
6861329ab9 | |||
5bccef1f4f | |||
89f7d5c948 | |||
ea5bbb8673 |
8 changed files with 36 additions and 26 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
|
Copyright 2016-2018 Michele "King_DuckZ" Santullo
|
||||||
|
|
||||||
This file is part of MyCurry.
|
This file is part of MyCurry.
|
||||||
|
|
||||||
|
@ -34,11 +34,17 @@ namespace curry {
|
||||||
|
|
||||||
vec2us check_tiles_count (const vec2us& parObjSize, const vec2f& parDirection) {
|
vec2us check_tiles_count (const vec2us& parObjSize, const vec2f& parDirection) {
|
||||||
assert(parObjSize > 0);
|
assert(parObjSize > 0);
|
||||||
const uint16_t horz = (parDirection.y() != 0.0f ? parObjSize.x() : 0);
|
const uint16_t horz = (parDirection.y() != 0.0f ? parObjSize.x() : 1);
|
||||||
const uint16_t vert = (parDirection.x() != 0.0f ? parObjSize.y() : 0);
|
const uint16_t vert = (parDirection.x() != 0.0f ? parObjSize.y() : 1);
|
||||||
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,
|
||||||
|
@ -61,41 +67,43 @@ namespace curry {
|
||||||
const WorldGrid& parWorld
|
const WorldGrid& parWorld
|
||||||
) {
|
) {
|
||||||
const vec2f direction = parTo - parFrom;
|
const vec2f direction = parTo - parFrom;
|
||||||
std::cout << "direction: " << direction << ' ';
|
|
||||||
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 vec2f pix_side_offset =
|
||||||
const vec2us dest_tile = pixel_to_world_tile(parWorld, parTo);
|
make_pixel_side_offset(parWorld.tile_size(), side_offset, direction);
|
||||||
vec2us stop_at = pixel_to_world_tile(parWorld, parFrom);
|
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);
|
||||||
for (auto curr_pos : vwr::increasing_sequence_range<vec2us>(tile, tile + parObjectSize)) {
|
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;
|
const bool walkable = parWorld.tile_property(parWorld.tile(curr_pos)).walkable;
|
||||||
if (not walkable) {
|
if (not walkable) {
|
||||||
tile_relative_offs = vec2f(0.0f);
|
collision_mask = direction_mask(direction);
|
||||||
|
tile_relative_offs *= (1.0f - collision_mask);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stop_at = tile - side_offset;
|
stop_at = tile;
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const vec2f pix_side_offset =
|
for_each_cell_under_segment(
|
||||||
make_pixel_side_offset(parWorld.tile_size(), side_offset, direction);
|
from,
|
||||||
for_each_voxel_under_segment(
|
to,
|
||||||
parFrom + pix_side_offset,
|
|
||||||
parTo + pix_side_offset,
|
|
||||||
parObjectSize,
|
parObjectSize,
|
||||||
parWorld,
|
parWorld,
|
||||||
forward_callback,
|
forward_callback,
|
||||||
false
|
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
|
} //namespace curry
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
|
Copyright 2016-2018 Michele "King_DuckZ" Santullo
|
||||||
|
|
||||||
This file is part of MyCurry.
|
This file is part of MyCurry.
|
||||||
|
|
||||||
|
|
|
@ -93,7 +93,7 @@ namespace curry {
|
||||||
|
|
||||||
//see:
|
//see:
|
||||||
//http://stackoverflow.com/questions/24679963/precise-subpixel-line-drawing-algorithm-rasterization-algorithm
|
//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& parFrom,
|
||||||
const vec2f& parTo,
|
const vec2f& parTo,
|
||||||
vec2us parObjSize,
|
vec2us parObjSize,
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace curry {
|
||||||
float segment_intersection (const vec2f& parA, const vec2f& parDirA, const vec2f& parB, const vec2f& parDirB);
|
float segment_intersection (const vec2f& parA, const vec2f& parDirA, const vec2f& parB, const vec2f& parDirB);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void for_each_voxel_under_segment (
|
void for_each_cell_under_segment (
|
||||||
const vec2f& parFrom,
|
const vec2f& parFrom,
|
||||||
const vec2f& parTo,
|
const vec2f& parTo,
|
||||||
vec2us parObjSize,
|
vec2us parObjSize,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
|
Copyright 2016-2018 Michele "King_DuckZ" Santullo
|
||||||
|
|
||||||
This file is part of MyCurry.
|
This file is part of MyCurry.
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
|
Copyright 2016-2018 Michele "King_DuckZ" Santullo
|
||||||
|
|
||||||
This file is part of MyCurry.
|
This file is part of MyCurry.
|
||||||
|
|
||||||
|
|
|
@ -135,9 +135,11 @@ namespace curry {
|
||||||
|
|
||||||
vec2us pixel_to_world_tile (const WorldGrid& parWorld, const vec2f& parPos) {
|
vec2us pixel_to_world_tile (const WorldGrid& parWorld, const vec2f& parPos) {
|
||||||
assert(position_is_on_map(parWorld, 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())
|
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) {
|
vec2f world_tile_to_pixel (const WorldGrid& parWorld, const vec2us& parTile) {
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
TEST_CASE ("Check that 2D raytracing works", "[raytracing][geometry]") {
|
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::vec2us;
|
||||||
using curry::vec2f;
|
using curry::vec2f;
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ TEST_CASE ("Check that 2D raytracing works", "[raytracing][geometry]") {
|
||||||
|
|
||||||
{
|
{
|
||||||
std::vector<vec2us> diagonal;
|
std::vector<vec2us> diagonal;
|
||||||
for_each_voxel_under_segment (
|
for_each_cell_under_segment (
|
||||||
vec2f(0.0f, 0.0f), //from
|
vec2f(0.0f, 0.0f), //from
|
||||||
vec2f(639.0f, 639.0f), //to
|
vec2f(639.0f, 639.0f), //to
|
||||||
vec2us(1, 1), //objsize
|
vec2us(1, 1), //objsize
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue