summary refs log tree commit diff stats
path: root/src/collision.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-04-29 16:45:55 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-04-29 16:45:55 -0400
commitc00668c58b26325203cb6815bc3dedf1e7d7ac5e (patch)
treef03d9b420779f365a51285c9a2b675a5e7e965c5 /src/collision.cpp
parent36cceabfc5ddd22d9ae0d6c4dee9d4041bf2e348 (diff)
downloadtherapy-c00668c58b26325203cb6815bc3dedf1e7d7ac5e.tar.gz
therapy-c00668c58b26325203cb6815bc3dedf1e7d7ac5e.tar.bz2
therapy-c00668c58b26325203cb6815bc3dedf1e7d7ac5e.zip
Added map object collision
Collision checking in PonderingSystem was rewritten to work as follows: horizontal movement is step first, then vertical. In each step, the closest environmental boundary to the body is found on the axis of movement in the space traversed by the body. Then, if any map objects fall in the region between the body's old position and the environmental boundary (or body new position if no boundary was found), process collision with those bodies in increasing distance order, stopping if a collision stops movement short of where the next collision would take place. After this, process collision with all of the environmental boundaries at the axis distance found earlier, as long as movement hasn't stopped short.

This is not the most optimal implementation, and there is a lot of code repetition, but it is a start and it works.

All map objects currently function as walls.

This fixes the bug where you could, with pixel-perfect precision, jump into the corner of a wall tile.

The top of the hitbox for the spike tile was lowered by one pixel. This fixes a problem where if the player is halfway on a floor tile and halfway over a spike tile, the floor tile would not stop the spike tile from being processed, and the player would die.
Diffstat (limited to 'src/collision.cpp')
-rw-r--r--src/collision.cpp97
1 files changed, 0 insertions, 97 deletions
diff --git a/src/collision.cpp b/src/collision.cpp deleted file mode 100644 index b747a90..0000000 --- a/src/collision.cpp +++ /dev/null
@@ -1,97 +0,0 @@
1#include "collision.h"
2
3bool Collision::operator<(const Collision& other) const
4{
5 // Most important is the type of collision
6 if (type_ != other.type_)
7 {
8 return (static_cast<int>(type_) > static_cast<int>(other.type_));
9 }
10
11 // Next, categorize the collisions arbitrarily based on direction
12 if (dir_ != other.dir_)
13 {
14 return (static_cast<int>(dir_) < static_cast<int>(other.dir_));
15 }
16
17 // We want to process closer collisions first
18 if (axis_ != other.axis_)
19 {
20 switch (dir_)
21 {
22 case Direction::left:
23 case Direction::up:
24 {
25 return (axis_ < other.axis_);
26 }
27
28 case Direction::right:
29 case Direction::down:
30 {
31 return (axis_ > other.axis_);
32 }
33 }
34 }
35
36 // Order the remaining attributes arbitrarily
37 return std::tie(collider_, lower_, upper_) <
38 std::tie(other.collider_, other.lower_, other.upper_);
39}
40
41bool Collision::isColliding(
42 double x,
43 double y,
44 int w,
45 int h) const
46{
47 int right = x + w;
48 int bottom = y + h;
49
50 switch (dir_)
51 {
52 case Direction::left:
53 case Direction::right:
54 {
55 if (!((bottom > lower_) && (y < upper_)))
56 {
57 return false;
58 }
59
60 break;
61 }
62
63 case Direction::up:
64 case Direction::down:
65 {
66 if (!((right > lower_) && (x < upper_)))
67 {
68 return false;
69 }
70
71 break;
72 }
73 }
74
75 switch (dir_)
76 {
77 case Direction::left:
78 {
79 return (axis_ >= x);
80 }
81
82 case Direction::right:
83 {
84 return (axis_ <= right);
85 }
86
87 case Direction::up:
88 {
89 return (axis_ >= y);
90 }
91
92 case Direction::down:
93 {
94 return (axis_ <= bottom);
95 }
96 }
97}