summaryrefslogtreecommitdiffstats
path: root/src/Collision.cpp
blob: 81bc89ddf69c056a41d03020cf4bf3d7ef7227af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Collision.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Collision.h"
#include <cmath>
#include <limits>

namespace Zoom {

bool Collision::test(const Triangle &t, const MathUtil::Ray &ray, float *distance) {
  vmml::vec3f edge1 = t.getVertex(1) - t.getVertex(0);
  vmml::vec3f edge2 = t.getVertex(2) - t.getVertex(0);

  vmml::vec3f pvec = ray.getDirection().cross(edge2);

  float det = vmml::dot(edge1, pvec);
  if(det < MathUtil::EPSILON)
    return false;

  vmml::vec3f tvec = ray.getVertex() - t.getVertex(0);
  float u = vmml::dot(pvec, tvec);

  if(u < 0 || u > det)
    return false;

  vmml::vec3f qvec = tvec.cross(edge1);
  float v = vmml::dot(ray.getDirection(), qvec);
  if(v < 0 || u+v > det)
    return false;

  if(distance)
    *distance = vmml::dot(edge2, qvec) / det;

  return true;
}

vmml::vec3f Collision::projectToEdge(const vmml::vec3f& p, const vmml::vec3f& v1, const vmml::vec3f& v2) {
  vmml::vec3f pVec = p - v1;
  vmml::vec3f edge = v2 - v1;

  float lengthSq = edge.squared_length();
  float edgeProj = vmml::dot(edge, pVec);

  if(edgeProj < 0) return v1;
  if(edgeProj > lengthSq) return v2;

  return v1 + (edgeProj/lengthSq)*edge;
}


bool Collision::testEdge(const vmml::vec3f &v1, const vmml::vec3f &v2, const vmml::vec3f &m, float r, const vmml::vec3f &move, float *distance) {
  vmml::vec3f edge = v2 - v1;

  vmml::vec3f avec = vmml::dot(edge, move)*edge / edge.squared_length() - move;
  vmml::vec3f cvec = v1 + vmml::dot(edge, m-v1)*edge / edge.squared_length() - m;

  float a = avec.squared_length();
  float b_2 = vmml::dot(avec, cvec);
  float c = cvec.squared_length() - r*r;

  float rootSq = b_2*b_2 - a*c;
  if(rootSq < 0)
    return false;

  float minRoot = -a - b_2;
  float maxRoot = a*MathUtil::EPSILON - b_2;

  if(minRoot < 0)
    minRoot = 0;
  if(maxRoot < 0)
    return false;

  if(rootSq < minRoot*minRoot || rootSq >= maxRoot*maxRoot)
    return false;

  minRoot = a*(edge.dot(m - v1) - edge.squared_length())/edge.dot(move) - b_2;
  maxRoot = a*edge.dot(m - v1)/edge.dot(move) - b_2;

  if(minRoot < 0)
    minRoot = 0;
  if(maxRoot < 0)
    return false;

  if(rootSq < minRoot*minRoot || rootSq > maxRoot*maxRoot)
    return false;

  *distance = -(b_2 + std::sqrt(rootSq))/a;

  return true;
}

bool Collision::testVertex(const vmml::vec3f &v, const vmml::vec3f &m, float r, const vmml::vec3f &move, float *distance) {
  float a = move.squared_length();
  float b_2 = vmml::dot(m-v, move);
  float c = (m-v).squared_length() - r*r;

  float rootSq = b_2*b_2 - a*c;
  if(rootSq < 0)
    return false;

  float minRoot = -a - b_2;
  float maxRoot = a*MathUtil::EPSILON - b_2;

  if(minRoot < 0)
    minRoot = 0;
  if(maxRoot < 0)
    return false;

  if(rootSq <= minRoot*minRoot || rootSq >= maxRoot*maxRoot)
    return false;

  *distance = -(b_2 + std::sqrt(rootSq))/a;

  return true;
}

bool Collision::test(const Triangle &t, const vmml::vec3f &m, float r, const vmml::vec3f &move, float *distance, vmml::vec3f *normal) {
  if(move.squared_length() == 0)
    return false;

  float d;

  vmml::vec3f triangleNormal = t.getNormal();
  if(test(t, MathUtil::Ray(m - r*triangleNormal, move), &d) && d > -MathUtil::EPSILON) {
    if(d < 1) {
      if(distance)
        *distance = d;
      if(normal)
        *normal = triangleNormal;

      return true;
    }
    else {
      return false;
    }
  }

  bool collision = false;
  float minDistance;

  for(int i = 0; i < 3; ++i) {
    if(testEdge(t.getVertex(i), t.getVertex((i+1)%3), m, r, move, &d)) {
      if(!collision || d < minDistance) {
        collision = true;
        minDistance = d;

        if(normal) {
          vmml::vec3f p = m + move*d;

          *normal = (p - projectToEdge(p, t.getVertex(i), t.getVertex((i+1)%3)))/r;
        }
      }
    }
  }

  if(collision) {
    if(distance)
      *distance = minDistance;

    return true;
  }

  collision = false;
  for(int i = 0; i < 3; ++i) {
    if(testVertex(t.getVertex(i), m, r, move, &d)) {
      if(!collision || d < minDistance) {
        collision = true;
        minDistance = d;
      }

      if(normal) {
        vmml::vec3f p = m + move*d;

        *normal = (p - t.getVertex(i))/r;
      }
    }
  }

  if(collision) {
    if(distance)
      *distance = minDistance;

    return true;
  }
  else {
    return false;
  }
}

}