svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22205:22290
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <stdlib.h>
32 #include <stdio.h>
33
34 #include "KX_RayCast.h"
35
36 #include "MT_Point3.h"
37 #include "MT_Vector3.h"
38
39 #include "KX_IPhysicsController.h"
40 #include "PHY_IPhysicsEnvironment.h"
41 #include "PHY_IPhysicsController.h"
42
43 KX_RayCast::KX_RayCast(KX_IPhysicsController* ignoreController, bool faceNormal)
44         :PHY_IRayCastFilterCallback(dynamic_cast<PHY_IPhysicsController*>(ignoreController), faceNormal) 
45 {
46 }
47
48 void KX_RayCast::reportHit(PHY_RayCastResult* result)
49 {
50         m_hitFound = true;
51         m_hitPoint.setValue((const float*)result->m_hitPoint);
52         m_hitNormal.setValue((const float*)result->m_hitNormal);
53         m_hitMesh = result->m_meshObject;
54         m_hitPolygon = result->m_polygon;
55 }
56
57 bool KX_RayCast::RayTest(PHY_IPhysicsEnvironment* physics_environment, const MT_Point3& _frompoint, const MT_Point3& topoint, KX_RayCast& callback)
58 {
59         if(physics_environment==NULL) return false; /* prevents crashing in some cases */
60         
61         // Loops over all physics objects between frompoint and topoint,
62         // calling callback.RayHit for each one.
63         //
64         // callback.RayHit should return true to stop looking, or false to continue.
65         //
66         // returns true if an object was found, false if not.
67         
68         MT_Point3 frompoint(_frompoint);
69         const MT_Vector3 todir( (topoint - frompoint).safe_normalized() );
70         MT_Point3 prevpoint(_frompoint+todir*(-1.f));
71         
72         PHY_IPhysicsController* hit_controller;
73
74         while((hit_controller = physics_environment->rayTest(callback,
75                         frompoint.x(),frompoint.y(),frompoint.z(),
76                         topoint.x(),topoint.y(),topoint.z())) != NULL) 
77         {
78                 KX_ClientObjectInfo* info = static_cast<KX_ClientObjectInfo*>(hit_controller->getNewClientInfo());
79                 
80                 if (!info)
81                 {
82                         printf("no info!\n");
83                         MT_assert(info && "Physics controller with no client object info");
84                         break;
85                 }
86                 
87                 // The biggest danger to to endless loop, prevent this by checking that the
88                 // hit point always progresses along the ray direction..
89                 prevpoint -= callback.m_hitPoint;
90                 if (prevpoint.length2() < MT_EPSILON)
91                         break;
92
93                 if (callback.RayHit(info))
94                         // caller may decide to stop the loop and still cancel the hit
95                         return callback.m_hitFound;
96
97                 // Skip past the object and keep tracing.
98                 // Note that retrieving in a single shot multiple hit points would be possible 
99                 // but it would require some change in Bullet.
100                 prevpoint = callback.m_hitPoint;
101                 /* We add 0.001 of fudge, so that if the margin && radius == 0., we don't endless loop. */
102                 MT_Scalar marg = 0.001 + hit_controller->GetMargin();
103                 marg *= 2.f;
104                 /* Calculate the other side of this object */
105                 MT_Scalar h = MT_abs(todir.dot(callback.m_hitNormal));
106                 if (h <= 0.01)
107                         // the normal is almost orthogonal to the ray direction, cannot compute the other side
108                         break;
109                 marg /= h; 
110                 frompoint = callback.m_hitPoint + marg * todir;
111                 // verify that we are not passed the to point
112                 if ((topoint - frompoint).dot(todir) < 0.f)
113                         break;
114         }
115         return false;
116 }
117