add missing files after merging
[blender.git] / source / gameengine / Ketsji / KX_RayCast.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * KX_MouseFocusSensor determines mouse in/out/over events.
29  */
30
31 /** \file gameengine/Ketsji/KX_RayCast.cpp
32  *  \ingroup ketsji
33  */
34
35
36 #include <stdlib.h>
37 #include <stdio.h>
38
39 #include "KX_RayCast.h"
40
41 #include "MT_Point3.h"
42 #include "MT_Vector3.h"
43
44 #include "KX_IPhysicsController.h"
45 #include "PHY_IPhysicsEnvironment.h"
46 #include "PHY_IPhysicsController.h"
47
48 KX_RayCast::KX_RayCast(KX_IPhysicsController* ignoreController, bool faceNormal, bool faceUV)
49         :PHY_IRayCastFilterCallback(dynamic_cast<PHY_IPhysicsController*>(ignoreController), faceNormal, faceUV) 
50 {
51 }
52
53 void KX_RayCast::reportHit(PHY_RayCastResult* result)
54 {
55         m_hitFound = true;
56         m_hitPoint.setValue((const float*)result->m_hitPoint);
57         m_hitNormal.setValue((const float*)result->m_hitNormal);
58         m_hitUVOK = result->m_hitUVOK;
59         m_hitUV.setValue((const float*)result->m_hitUV);
60         m_hitMesh = result->m_meshObject;
61         m_hitPolygon = result->m_polygon;
62 }
63
64 bool KX_RayCast::RayTest(PHY_IPhysicsEnvironment* physics_environment, const MT_Point3& _frompoint, const MT_Point3& topoint, KX_RayCast& callback)
65 {
66         if(physics_environment==NULL) return false; /* prevents crashing in some cases */
67         
68         // Loops over all physics objects between frompoint and topoint,
69         // calling callback.RayHit for each one.
70         //
71         // callback.RayHit should return true to stop looking, or false to continue.
72         //
73         // returns true if an object was found, false if not.
74         
75         MT_Point3 frompoint(_frompoint);
76         const MT_Vector3 todir( (topoint - frompoint).safe_normalized() );
77         MT_Point3 prevpoint(_frompoint+todir*(-1.f));
78         
79         PHY_IPhysicsController* hit_controller;
80
81         while((hit_controller = physics_environment->rayTest(callback,
82                         frompoint.x(),frompoint.y(),frompoint.z(),
83                         topoint.x(),topoint.y(),topoint.z())) != NULL) 
84         {
85                 KX_ClientObjectInfo* info = static_cast<KX_ClientObjectInfo*>(hit_controller->getNewClientInfo());
86                 
87                 if (!info)
88                 {
89                         printf("no info!\n");
90                         MT_assert(info && "Physics controller with no client object info");
91                         break;
92                 }
93                 
94                 // The biggest danger to endless loop, prevent this by checking that the
95                 // hit point always progresses along the ray direction..
96                 prevpoint -= callback.m_hitPoint;
97                 if (prevpoint.length2() < MT_EPSILON)
98                         break;
99
100                 if (callback.RayHit(info))
101                         // caller may decide to stop the loop and still cancel the hit
102                         return callback.m_hitFound;
103
104                 // Skip past the object and keep tracing.
105                 // Note that retrieving in a single shot multiple hit points would be possible 
106                 // but it would require some change in Bullet.
107                 prevpoint = callback.m_hitPoint;
108                 /* We add 0.001 of fudge, so that if the margin && radius == 0., we don't endless loop. */
109                 MT_Scalar marg = 0.001 + hit_controller->GetMargin();
110                 marg *= 2.f;
111                 /* Calculate the other side of this object */
112                 MT_Scalar h = MT_abs(todir.dot(callback.m_hitNormal));
113                 if (h <= 0.01)
114                         // the normal is almost orthogonal to the ray direction, cannot compute the other side
115                         break;
116                 marg /= h; 
117                 frompoint = callback.m_hitPoint + marg * todir;
118                 // verify that we are not passed the to point
119                 if ((topoint - frompoint).dot(todir) < 0.f)
120                         break;
121         }
122         return false;
123 }
124