Debian Codegraph @

src/game/offtrackdetector.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "offtrackdetector.hpp"
17 #include "car.hpp"
18 #include "track.hpp"
19 #include "tracktile.hpp"
20
21 #include <MCMathUtil>
22
23 OffTrackDetector::OffTrackDetector(Car & car)
24 : m_car(car)
25 , m_track(nullptr)
26 , m_tileWLimit(TrackTile::width() / 2 - TrackTile::width() / 10)
27 , m_tileHLimit(TrackTile::height() / 2 - TrackTile::height() / 10)
28 {
29 }
30
31 void OffTrackDetector::setTrack(std::shared_ptr<Track> track)
32 {
33 m_track = track;
34 }
35
36 void OffTrackDetector::update()
37 {
38 {
39 const auto leftFrontTirePos(m_car.leftFrontTireLocation());
40 const auto tile = m_track->trackTileAtLocation(leftFrontTirePos.i(), leftFrontTirePos.j());
41
42 m_car.setLeftSideOffTrack(false);
43
44 if (isOffTrack(leftFrontTirePos, *tile))
45 {
46 m_car.setLeftSideOffTrack(true);
47 }
48 }
49
50 {
51 const auto rightFrontTirePos(m_car.rightFrontTireLocation());
52 const auto tile = m_track->trackTileAtLocation(rightFrontTirePos.i(), rightFrontTirePos.j());
53
54 m_car.setRightSideOffTrack(false);
55
56 if (isOffTrack(rightFrontTirePos, *tile))
57 {
58 m_car.setRightSideOffTrack(true);
59 }
60 }
61 }
62
63 bool OffTrackDetector::isOffTrack(MCVector2dF tire, const TrackTile & tile) const
64 {
65 if (!tile.hasAsphalt())
66 {
67 return true;
68 }
69 else if (tile.tileTypeEnum() == TrackTile::TileType::Straight || tile.tileTypeEnum() == TrackTile::TileType::Finish)
70 {
71 if ((tile.rotation() + 90) % 180 == 0)
72 {
73 if (const float y = tire.j(); y > tile.location().y() + m_tileHLimit || y < tile.location().y() - m_tileHLimit)
74 {
75 return true;
76 }
77 }
78 else if (tile.rotation() % 180 == 0)
79 {
80 if (const float x = tire.i(); x > tile.location().x() + m_tileWLimit || x < tile.location().x() - m_tileWLimit)
81 {
82 return true;
83 }
84 }
85 }
86 else if (tile.tileTypeEnum() == TrackTile::TileType::Straight45Male)
87 {
88 const auto diff = tire - MCVector2dF(tile.location().x(), tile.location().y());
89 const auto rotatedDiff = MCMathUtil::rotatedVector(diff, tile.rotation() - 45);
90
91 if (rotatedDiff.j() > m_tileHLimit || rotatedDiff.j() < -m_tileHLimit)
92 {
93 return true;
94 }
95 }
96 else if (tile.tileTypeEnum() == TrackTile::TileType::Straight45Female)
97 {
98 const auto diff = tire - MCVector2dF(tile.location().x(), tile.location().y());
99 const auto rotatedDiff = MCMathUtil::rotatedVector(diff, 360 - tile.rotation() - 45);
100
101 if (rotatedDiff.j() < m_tileHLimit)
102 {
103 return true;
104 }
105 }
106 else if (tile.tileTypeEnum() == TrackTile::TileType::Corner90)
107 {
108 // Let's pretend that this is a good enough approximation
109 const auto rotatedCorner = MCVector2dF(tile.location().x(), tile.location().y()) + //
110 MCMathUtil::rotatedVector(MCVector2dF(TrackTileBase::width() / 2, TrackTileBase::height() / 2), tile.rotation() + 270);
111 const auto diff = tire - rotatedCorner;
112 const auto r1 = TrackTileBase::width() / 10;
113 const auto r2 = TrackTileBase::width() - TrackTileBase::width() / 20;
114 if (diff.lengthSquared() < r1 * r1 || diff.lengthSquared() > r2 * r2)
115 {
116 return true;
117 }
118 }
119
120 return false;
121 }