Skip to content

Commit

Permalink
wip make intersect return optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ligfx committed Jul 9, 2021
1 parent 54864b7 commit 83ba254
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ bool Map::collideLineWithRoomBoundaries(Point src, Point dest, std::shared_ptr<R
/*if (previousroom)
if (x[i].containsPoint(oldpoint)) continue; */

Point temppoint;
if (Line::intersect(x[i], movement, temppoint)) {
optional<Point> temppoint = Line::intersect(x[i], movement);
if (temppoint) {
//if (temppoint == src) return false; // for debug use: sneakily fail all movement between rooms

// see if this is nearer than any previous points we've found
double distx = temppoint.x - src.x;
double disty = temppoint.y - src.y;
double distx = temppoint->x - src.x;
double disty = temppoint->y - src.y;
double d = distx * distx + disty * disty;
if (d > distance)
continue;
Expand All @@ -234,15 +234,15 @@ bool Map::collideLineWithRoomBoundaries(Point src, Point dest, std::shared_ptr<R
float newx, newy;
/*if (temppoint == src) { // TODO: this is not an accurate check!! likely cause of falling through PERM
// we might be on a PERM line! check backwards.
newx = temppoint.x + (src.x <= dest.x ? (src.x == dest.x ? 0.0 : -0.5) : 0.5);
newy = temppoint.y + (src.y <= dest.y ? (src.y == dest.y ? 0.0 : -0.5) : 0.5);
newx = temppoint->x + (src.x <= dest.x ? (src.x == dest.x ? 0.0 : -0.5) : 0.5);
newy = temppoint->y + (src.y <= dest.y ? (src.y == dest.y ? 0.0 : -0.5) : 0.5);
} else {*/
newx = temppoint.x + (src.x <= dest.x ? (src.x == dest.x ? 0.0 : 0.5) : -0.5);
newy = temppoint.y + (src.y <= dest.y ? (src.y == dest.y ? 0.0 : 0.5) : -0.5);
newx = temppoint->x + (src.x <= dest.x ? (src.x == dest.x ? 0.0 : 0.5) : -0.5);
newy = temppoint->y + (src.y <= dest.y ? (src.y == dest.y ? 0.0 : 0.5) : -0.5);
//}

if (room->containsPoint(newx, newy)) { // if a little along our movement vector is still in our room, forget it
/*std::cout << "physics debug: next room is original room at (" << temppoint.x << ", " << temppoint.y << ")" << std::endl;
/*std::cout << "physics debug: next room is original room at (" << temppoint->x << ", " << temppoint->y << ")" << std::endl;
std::cout << "room line: (" << x[i].getStart().x << ", " << x[i].getStart().y << ") to ";
std::cout << "(" << x[i].getEnd().x << ", " << x[i].getEnd().y << ")" << std::endl;
std::cout << "movement line: (" << movement.getStart().x << ", " << movement.getStart().y << ") to ";
Expand Down Expand Up @@ -279,7 +279,7 @@ bool Map::collideLineWithRoomBoundaries(Point src, Point dest, std::shared_ptr<R
}
}*/

std::shared_ptr<Room> z = roomAt(temppoint.x, temppoint.y); // TODO: evil performance-killing debug check
std::shared_ptr<Room> z = roomAt(temppoint->x, temppoint->y); // TODO: evil performance-killing debug check
if (!z) {
// TODO: commented out this error message for sake of fuzzie's sanity, but it's still an issue
/*std::cout << "physics bug: fell out of room system at (" << where.x << ", " << where.y << ")" << std::endl;
Expand All @@ -293,7 +293,7 @@ bool Map::collideLineWithRoomBoundaries(Point src, Point dest, std::shared_ptr<R
// it is nearer and not the same room, so make it our priority
distance = d;
foundsomething = true;
where = temppoint;
where = temppoint.value();
wall = x[i];
walldir = i;
newroom = nextroom;
Expand Down
9 changes: 4 additions & 5 deletions src/physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ Line::Line(Point s, Point e) {
end = e;
}

bool Line::intersect(const Line& l1, const Line& l2, Point& where) {
optional<Point> Line::intersect(const Line& l1, const Line& l2) {
auto denominator = (l2.end.y - l2.start.y) * (l1.end.x - l1.start.x) - (l2.end.x - l2.start.x) * (l1.end.y - l1.start.y);
if (std::fabs(denominator) < 0.00001) {
// lines are parallel, e.g. they either don't intersect or they overlap
return false;
return {};
}

auto a = (l2.end.x - l2.start.x) * (l1.start.y - l2.start.y) - (l2.end.y - l2.start.y) * (l1.start.x - l2.start.x);
Expand All @@ -52,10 +52,9 @@ bool Line::intersect(const Line& l1, const Line& l2, Point& where) {
// intersected!
auto x = l1.start.x + ua * (l1.end.x - l1.start.x);
auto y = l1.start.y + ua * (l1.end.y - l1.start.y);
where = Point(x, y);
return true;
return Point(x, y);
}
return false;
return {};
}

/* vim: set noet: */
3 changes: 2 additions & 1 deletion src/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include "serfwd.h"
#include "utils/optional.h"

#include <cassert>
#include <cmath> // sqrt
Expand Down Expand Up @@ -71,7 +72,7 @@ class Line {
return *this;
}

static bool intersect(const Line& l1, const Line& l2, Point& where);
static optional<Point> intersect(const Line& l1, const Line& l2);

bool isHorizontal() const { return start.y == end.y; }
bool isVertical() const { return start.x == end.x; }
Expand Down

0 comments on commit 83ba254

Please sign in to comment.