Skip to content

Commit

Permalink
wip physicsutils
Browse files Browse the repository at this point in the history
  • Loading branch information
ligfx committed Jul 9, 2021
1 parent 56eaf3f commit f0b6e25
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 67 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ add_library(openc2e-core STATIC
src/parseException.cpp
src/partzorder.cpp
src/PathResolver.cpp
src/physics.cpp
src/PointerAgent.cpp
src/Port.cpp
src/prayManager.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "MetaRoom.h"
#include "Room.h"
#include "creaturesException.h"
#include "physicsUtils.h"

#include <cassert>
#include <iostream>
Expand Down Expand Up @@ -218,7 +219,7 @@ bool Map::collideLineWithRoomBoundaries(Point src, Point dest, std::shared_ptr<R
/*if (previousroom)
if (x[i].containsPoint(oldpoint)) continue; */

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

Expand Down
60 changes: 0 additions & 60 deletions src/physics.cpp

This file was deleted.

11 changes: 6 additions & 5 deletions src/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class Line {
Point start, end;

public:
void dump() const;

Line() {
start = Point(0, 0);
end = Point(1, 1);
Expand All @@ -64,16 +62,19 @@ class Line {
end = l.end;
}

Line(Point s, Point e);
Line(Point s, Point e) {
if (s.x > e.x)
std::swap(s, e);
start = s;
end = e;
}

Line& operator=(const Line& l) {
start = l.start;
end = l.end;
return *this;
}

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; }
float slope() const {
Expand Down
29 changes: 29 additions & 0 deletions src/physicsUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "physics.h"

namespace physicsUtils {

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

auto a = (l2.getEnd().x - l2.getStart().x) * (l1.getStart().y - l2.getStart().y) - (l2.getEnd().y - l2.getStart().y) * (l1.getStart().x - l2.getStart().x);
auto b = (l1.getEnd().x - l1.getStart().x) * (l1.getStart().y - l2.getStart().y) - (l1.getEnd().y - l1.getStart().y) * (l1.getStart().x - l2.getStart().x);

auto ua = a / denominator;
auto ub = b / denominator;

if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
// intersected!
auto x = l1.getStart().x + ua * (l1.getEnd().x - l1.getStart().x);
auto y = l1.getStart().y + ua * (l1.getEnd().y - l1.getStart().y);
return Point(x, y);
}
return {};
}

} // namespace physicsUtils

0 comments on commit f0b6e25

Please sign in to comment.