ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Line.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This code is based on the CRAB engine
24  *
25  * Copyright (c) Arvind Raja Yadav
26  *
27  * Licensed under MIT
28  *
29  */
30 
31 #ifndef CRAB_LINE_H
32 #define CRAB_LINE_H
33 
34 #include "crab/vectors.h"
35 
36 namespace Crab {
37 
38 // Find if 2 lines intersect and store the point of intersection
39 template<typename T>
40 bool collideLine(const T &p0X, const T &p0Y, const T &p1X, const T &p1Y,
41  const T &p2X, const T &p2Y, const T &p3X, const T &p3Y,
42  T *x = nullptr, T *y = nullptr) {
43  Vector2D<T> s1, s2;
44  s1.x = p1X - p0X;
45  s1.y = p1Y - p0Y;
46  s2.x = p3X - p2X;
47  s2.y = p3Y - p2Y;
48 
49  float d = (-s2.x * s1.y + s1.x * s2.y);
50 
51  if (d != 0) {
52  float s, t;
53  s = (-s1.y * (p0X - p2X) + s1.x * (p0Y - p2Y)) / d;
54  t = (s2.x * (p0Y - p2Y) - s2.y * (p0X - p2X)) / d;
55 
56  if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
57  // Collision detected
58  if (x != nullptr)
59  *x = p0X + (t * s1.x);
60  if (y != nullptr)
61  *y = p0Y + (t * s1.y);
62 
63  return true;
64  }
65  }
66 
67  return false; // No collision
68 }
69 
70 } // End of namespace Crab
71 
72 #endif // CRAB_LINE_H
Definition: moveeffect.h:37