~ubuntu-branches/debian/sid/astromenace/sid

« back to all changes in this revision

Viewing changes to AstroMenaceSource/Core/CollisionDetection/OBB.cpp

  • Committer: Package Import Robot
  • Author(s): Boris Pek
  • Date: 2013-04-09 02:04:25 UTC
  • Revision ID: package-import@ubuntu.com-20130409020425-a7fl9xk4diamw6di
Tags: upstream-1.3.1+repack
Import upstream version 1.3.1+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
 
 
3
        AstroMenace (Hardcore 3D space shooter with spaceship upgrade possibilities)
 
4
        Copyright © 2006-2012 Michael Kurinnoy, Viewizard
 
5
 
 
6
 
 
7
        AstroMenace 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
        AstroMenace 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 AstroMenace. If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
 
 
21
        Web Site: http://www.viewizard.com/
 
22
        Project: http://sourceforge.net/projects/openastromenace/
 
23
        E-mail: viewizard@viewizard.com
 
24
 
 
25
*************************************************************************************/
 
26
 
 
27
 
 
28
#include "CollisionDetection.h"
 
29
 
 
30
 
 
31
 
 
32
 
 
33
//-----------------------------------------------------------------------------
 
34
// Проверка столкновений OBB-OBB
 
35
//-----------------------------------------------------------------------------
 
36
bool vw_OBBOBBCollision(VECTOR3D Object1OBB[8], VECTOR3D Object1OBBLocation, VECTOR3D Object1Location, float Object1RotationMatrix[9],
 
37
                                                VECTOR3D Object2OBB[8], VECTOR3D Object2OBBLocation, VECTOR3D Object2Location, float Object2RotationMatrix[9])
 
38
{
 
39
        // строим матрицу, чтобы развернуть точки
 
40
        float TMPInvObject1RotationMatrix[9];
 
41
        memcpy(TMPInvObject1RotationMatrix, Object1RotationMatrix, 9*sizeof(float));
 
42
        Matrix33InverseRotate(TMPInvObject1RotationMatrix);
 
43
        // размер первого бокса
 
44
        VECTOR3D a = (Object1OBB[0] - Object1OBB[6])^0.5f;
 
45
        Matrix33CalcPoint(&a, TMPInvObject1RotationMatrix);
 
46
 
 
47
        // строим инверсную матрицу для 2-го объекта
 
48
        float TMPInvObject2RotationMatrix[9];
 
49
        memcpy(TMPInvObject2RotationMatrix, Object2RotationMatrix, 9*sizeof(float));
 
50
        Matrix33InverseRotate(TMPInvObject2RotationMatrix);
 
51
        // размер второго бокса
 
52
        VECTOR3D b = (Object2OBB[0] - Object2OBB[6])^0.5f;
 
53
        Matrix33CalcPoint(&b, TMPInvObject2RotationMatrix);
 
54
 
 
55
 
 
56
        // получаем смещение в мировой системе координат
 
57
        VECTOR3D T = (Object2Location + Object2OBBLocation)
 
58
                                         - (Object1Location + Object1OBBLocation);
 
59
        Matrix33CalcPoint(&T, TMPInvObject1RotationMatrix);
 
60
 
 
61
        // строем матрицу для переноса точек 2-го объекта в систему координат 1-го
 
62
        Matrix33Mult(TMPInvObject1RotationMatrix, Object2RotationMatrix);
 
63
        float R[3][3];
 
64
        R[0][0] = TMPInvObject1RotationMatrix[0];
 
65
        R[0][1] = TMPInvObject1RotationMatrix[3];
 
66
        R[0][2] = TMPInvObject1RotationMatrix[6];
 
67
        R[1][0] = TMPInvObject1RotationMatrix[1];
 
68
        R[1][1] = TMPInvObject1RotationMatrix[4];
 
69
        R[1][2] = TMPInvObject1RotationMatrix[7];
 
70
        R[2][0] = TMPInvObject1RotationMatrix[2];
 
71
        R[2][1] = TMPInvObject1RotationMatrix[5];
 
72
        R[2][2] = TMPInvObject1RotationMatrix[8];
 
73
 
 
74
 
 
75
        //делаем 15 проверок, т.к. у нас 15 разделяющих осей
 
76
 
 
77
        //1 (Ra)x
 
78
        if(fabsf(T.x) > a.x + b.x * fabsf(R[0][0]) + b.y * fabsf(R[0][1]) + b.z * fabsf(R[0][2]))
 
79
                return false;
 
80
        //2 (Ra)y
 
81
        if(fabsf(T.y) > a.y + b.x * fabsf(R[1][0]) + b.y * fabsf(R[1][1]) + b.z * fabsf(R[1][2]))
 
82
                return false;
 
83
        //3 (Ra)z
 
84
        if(fabsf(T.z) > a.z + b.x * fabsf(R[2][0]) + b.y * fabsf(R[2][1]) + b.z * fabsf(R[2][2]))
 
85
                return false;
 
86
 
 
87
        //4 (Rb)x
 
88
        if(fabsf(T.x*R[0][0] + T.y*R[1][0] + T.z*R[2][0]) >
 
89
                (b.x + a.x*fabsf(R[0][0]) + a.y * fabsf(R[1][0]) + a.z*fabsf(R[2][0])))
 
90
                return false;
 
91
        //5 (Rb)y
 
92
        if(fabsf(T.x*R[0][1] + T.y*R[1][1] + T.z*R[2][1]) >
 
93
                (b.y + a.x*fabsf(R[0][1]) + a.y * fabsf(R[1][1]) + a.z*fabsf(R[2][1])))
 
94
                return false;
 
95
        //6 (Rb)z
 
96
        if(fabsf(T.x*R[0][2] + T.y*R[1][2] + T.z*R[2][2]) >
 
97
                (b.z + a.x*fabsf(R[0][2]) + a.y * fabsf(R[1][2]) + a.z*fabsf(R[2][2])))
 
98
                return false;
 
99
 
 
100
        //7 (Ra)x X (Rb)x
 
101
        if(fabsf(T.z*R[1][0] - T.y*R[2][0]) > a.y*fabsf(R[2][0]) +
 
102
                a.z*fabsf(R[1][0]) + b.y*fabsf(R[0][2]) + b.z*fabsf(R[0][1]))
 
103
                return false;
 
104
        //8 (Ra)x X (Rb)y
 
105
        if(fabsf(T.z*R[1][1] - T.y*R[2][1]) > a.y*fabsf(R[2][1]) +
 
106
                a.z*fabsf(R[1][1]) + b.x*fabsf(R[0][2]) + b.z*fabsf(R[0][0]))
 
107
                return false;
 
108
        //9 (Ra)x X (Rb)z
 
109
        if(fabsf(T.z*R[1][2]-T.y*R[2][2]) > a.y*fabsf(R[2][2]) +
 
110
                a.z*fabsf(R[1][2]) + b.x*fabsf(R[0][1]) + b.y*fabsf(R[0][0]))
 
111
                return false;
 
112
        //10 (Ra)y X (Rb)x
 
113
        if(fabsf(T.x*R[2][0]-T.z*R[0][0]) > a.x*fabsf(R[2][0]) +
 
114
                a.z*fabsf(R[0][0]) + b.y*fabsf(R[1][2]) + b.z*fabsf(R[1][1]))
 
115
                return false;
 
116
        //11 (Ra)y X (Rb)y
 
117
        if(fabsf(T.x*R[2][1]-T.z*R[0][1]) > a.x*fabsf(R[2][1]) +
 
118
                a.z*fabsf(R[0][1]) + b.x*fabsf(R[1][2]) + b.z*fabsf(R[1][0]))
 
119
                return false;
 
120
        //12 (Ra)y X (Rb)z
 
121
        if(fabsf(T.x*R[2][2]-T.z*R[0][2]) > a.x*fabsf(R[2][2]) +
 
122
                a.z*fabsf(R[0][2]) + b.x*fabsf(R[1][1]) + b.y*fabsf(R[1][0]))
 
123
                return false;
 
124
        //13 (Ra)z X (Rb)x
 
125
        if(fabsf(T.y*R[0][0]-T.x*R[1][0]) > a.x*fabsf(R[1][0]) +
 
126
                a.y*fabsf(R[0][0]) + b.y*fabsf(R[2][2]) + b.z*fabsf(R[2][1]))
 
127
                return false;
 
128
        //14 (Ra)z X (Rb)y
 
129
        if(fabsf(T.y*R[0][1]-T.x*R[1][1]) > a.x*fabsf(R[1][1]) +
 
130
                a.y*fabsf(R[0][1]) + b.x*fabsf(R[2][2]) + b.z*fabsf(R[2][0]))
 
131
                return false;
 
132
        //15 (Ra)z X (Rb)z
 
133
        if(fabsf(T.y*R[0][2]-T.x*R[1][2]) > a.x*fabsf(R[1][2]) +
 
134
                a.y*fabsf(R[0][2]) + b.x*fabsf(R[2][1]) + b.y*fabsf(R[2][0]))
 
135
                return false;
 
136
 
 
137
        return true;
 
138
}