-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Hebert
committed
Apr 5, 2014
1 parent
d6aa717
commit 060bbe7
Showing
10 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,73 @@ | ||
#include "collision_tile.h" | ||
|
||
using namespace sides; | ||
using namespace tiles; | ||
|
||
namespace { | ||
inline float get_slope(const TileType& tile_type) { | ||
const TileType right_top = TileType().set(RIGHT_SLOPE).set(TOP_SLOPE); | ||
const TileType left_bottom = TileType().set(LEFT_SLOPE).set(BOTTOM_SLOPE); | ||
const bool is_positive = | ||
((right_top & tile_type) == right_top) || | ||
((left_bottom & tile_type) == left_bottom); | ||
return is_positive ? 0.5f : -0.5f; | ||
} | ||
|
||
inline units::Game get_offset(const TileType& tile_type) { | ||
{ | ||
const TileType left_top_tall = | ||
TileType().set(LEFT_SLOPE).set(TOP_SLOPE).set(TALL_SLOPE); | ||
const TileType right_bottom_short = | ||
TileType().set(RIGHT_SLOPE).set(BOTTOM_SLOPE).set(SHORT_SLOPE); | ||
if ((left_top_tall & tile_type) == left_top_tall || | ||
(right_bottom_short & tile_type) == right_bottom_short) { | ||
return units::tileToGame(1); | ||
} | ||
} | ||
|
||
const TileType left_bottom_tall = | ||
TileType().set(LEFT_SLOPE).set(BOTTOM_SLOPE).set(TALL_SLOPE); | ||
const TileType right_top_short = | ||
TileType().set(RIGHT_SLOPE).set(TOP_SLOPE).set(SHORT_SLOPE); | ||
if ((left_bottom_tall & tile_type) == left_bottom_tall || | ||
(right_top_short & tile_type) == right_top_short) { | ||
return 0.0f; | ||
} | ||
|
||
return units::kHalfTile; | ||
} | ||
} | ||
|
||
boost::optional<units::Game> CollisionTile::testCollision( | ||
sides::SideType side, | ||
units::Game /*position*/) const { | ||
if (tile_type_[tiles::WALL]) { | ||
units::Game perpendicular_position, | ||
units::Game leading_position, | ||
bool should_test_slopes) const { | ||
if (tile_type_[WALL]) { | ||
if (side == TOP_SIDE) | ||
return units::tileToGame(row_); | ||
if (side == BOTTOM_SIDE) | ||
return units::tileToGame(row_ + 1); | ||
if (side == LEFT_SIDE) | ||
return units::tileToGame(col_); | ||
return units::tileToGame(col_ + 1); | ||
} else if (should_test_slopes && | ||
tile_type_[SLOPE] && | ||
!tile_type_[slope_flag_from_side(side)]) { | ||
const units::Game row = units::tileToGame(row_); | ||
const units::Game col = units::tileToGame(col_); | ||
const float slope = get_slope(tile_type_); | ||
const units::Game offset = get_offset(tile_type_); | ||
const units::Game calculated_position = sides::vertical(side) ? | ||
slope * (perpendicular_position - col) + offset + row : | ||
(perpendicular_position - row - offset) / slope + col; | ||
|
||
const bool is_colliding = is_max(side) ? | ||
leading_position <= calculated_position : | ||
leading_position >= calculated_position; | ||
|
||
if (is_colliding) | ||
return calculated_position; | ||
} | ||
return boost::none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
Episode 66 - General Slopes | ||
=========================== | ||
|
||
Problem: | ||
- Implement General Formula for slopes | ||
|
||
Solution: | ||
- General Formulas (from Episode 61) | ||
y = slope * (x - col) + offset + row | ||
|- perpendicular_position | ||
x = (y - row - offset) / slope + col | ||
|- perpendicular_position | ||
|
||
row,col,tile_type: stored in CollisionTile | ||
offset,slope: calculated from tile_type | ||
|
||
- one of two checks for collision | ||
calculated_position=calculated position (y or x) | ||
leading_position=position of the leading edge of the colliding entity | ||
side=the side of the TILE being collided with | ||
is_max_side?=side is BOTTOM or RIGHT (further along in the axis) | ||
|
||
if is_max_side? side | ||
leading_position <= calculated_position | ||
else | ||
leading_position >= calculated_position | ||
|
||
example: | ||
Projectile is going LEFT | ||
Projectile's leading position is its LEFT side | ||
side we are checking is RIGHT | ||
calculated position represents the right side | ||
since RIGHT is the max of (LEFT|RIGHT) | ||
collision occurs if leading position is <= calculated position | ||
|
||
slope Tiles | ||
+0.5 RIGHT-TOP, LEFT-BOTTOM | ||
-0.5 LEFT-TOP , RIGHT-BOTTOM | ||
|
||
offset Tiles | ||
1T LEFT-TOP-TALL , RIGHT-BOTTOM-SHORT | ||
0T LEFT-BOTTOM-TALL, RIGHT-TOP-SHORT | ||
0.5T (Everything else) | ||
|
||
|\ | ||
| \ | ||
___\ | ||
LEFT-BOTTOM | ||
LEFT and BOTTOM Sides are invalid |