Project

General

Profile

SO3Engine
OgreNewt_ConvexCast.cpp
Go to the documentation of this file.
8
9
10
11namespace OgreNewt
12{
14 mReturnInfoList(NULL),
15 mReturnInfoListLength(0),
16 mReturnInfoListSize(0)
17 {
18 }
19
21 {
22 if( mReturnInfoList )
23 {
24 delete[] mReturnInfoList;
25 }
26 }
27
28
29 void Convexcast::go(const OgreNewt::World* world, const OgreNewt::ConvexCollisionPtr& col, const Ogre::Vector3& startpt, const Ogre::Quaternion &colori, const Ogre::Vector3& endpt, int maxcontactscount, int threadIndex)
30 {
31
32 if( world->getDebugger().isRaycastRecording() )
33 {
34 world->getDebugger().addConvexRay(col, startpt, colori, endpt);
35 }
36 // reserve memory
37 if( mReturnInfoListSize < maxcontactscount )
38 {
40 if( mReturnInfoList )
41 delete[] mReturnInfoList;
42 mReturnInfoList = new NewtonWorldConvexCastReturnInfo[maxcontactscount];
43 mReturnInfoListSize = maxcontactscount;
44 }
45
47 // perform the cast
48 dFloat matrix[16];
49 OgreNewt::Converters::QuatPosToMatrix(colori, startpt, &matrix[0]);
51
52
54 NewtonWorldConvexCast( world->getNewtonWorld(), &matrix[0], (float*)&endpt, col->getNewtonCollision(),
55 &mFirstContactDistance, this, OgreNewt::Convexcast::newtonConvexcastPreFilter,
57
58
60 {
61 Body* body;
62 for(int i = 0; i < mReturnInfoListLength; i++)
63 {
64 body = (OgreNewt::Body*) NewtonBodyGetUserData(mReturnInfoList[i].m_hitBody);
65 world->getDebugger().addHitBody(body);
66 }
67 }
68 }
69
70 unsigned _CDECL Convexcast::newtonConvexcastPreFilter(const NewtonBody *body, const NewtonCollision *collision, void* userData)
71 {
72 // get our object!
73 Convexcast* me = (Convexcast*)userData;
74
75 Body* bod = (Body*)NewtonBodyGetUserData( body );
76 const World* world = bod->getWorld();
77
78 if (me->userPreFilterCallback( bod ))
79 return 1;
80 else
81 {
82
84 {
85 world->getDebugger().addDiscardedBody(bod);
86 }
87
88 return 0;
89 }
90 }
91
92
93
94 //-------------------------------------------------------------------------------------
99
101
105
106 BasicConvexcast::BasicConvexcast(const OgreNewt::World* world, const OgreNewt::ConvexCollisionPtr& col, const Ogre::Vector3& startpt, const Ogre::Quaternion &colori, const Ogre::Vector3& endpt, int maxcontactscount, int threadIndex, OgreNewt::Body* ignoreBody)
107 {
108 mIgnoreBody = ignoreBody;
109
110 go( world, col, startpt, colori, endpt, maxcontactscount, threadIndex);
111 }
112
114
116 {
117 if (mIgnoreBody == NULL || mIgnoreBody != body)
118 {
119 return true;
120 }
121 else
122 {
123 return false;
124 }
125 }
126
128 {
129 int count = 0;
130 for( int i = 0; i < mReturnInfoListLength; i++ )
131 {
132 int j;
133 for( j = 0; j < i; j++ )
134 if( mReturnInfoList[i].m_hitBody == mReturnInfoList[j].m_hitBody )
135 break;
136
137 if( j == i )
138 count++;
139 }
140
141 return count;
142 }
143
144
146 {
148 }
149
151 {
153
154 if( hitnum < 0 || hitnum >= mReturnInfoListLength )
155 return info;
156
157 info.mBody = (OgreNewt::Body*) NewtonBodyGetUserData(mReturnInfoList[hitnum].m_hitBody);
158 info.mCollisionID = mReturnInfoList[hitnum].m_contactID;
159 info.mContactNormal.x = Ogre::Real(mReturnInfoList[hitnum].m_normal[0]);
160 info.mContactNormal.y = Ogre::Real(mReturnInfoList[hitnum].m_normal[1]);
161 info.mContactNormal.z = Ogre::Real(mReturnInfoList[hitnum].m_normal[2]);
162 info.mContactNormalOnHitPoint.x = Ogre::Real(0.0);
163 info.mContactNormalOnHitPoint.y = Ogre::Real(0.0);
164 info.mContactNormalOnHitPoint.z = Ogre::Real(0.0);
165 info.mContactPoint.x = Ogre::Real(mReturnInfoList[hitnum].m_point[0]);
166 info.mContactPoint.y = Ogre::Real(mReturnInfoList[hitnum].m_point[1]);
167 info.mContactPoint.z = Ogre::Real(mReturnInfoList[hitnum].m_point[2]);
168 info.mContactPenetration = mReturnInfoList[hitnum].m_penetration;
169
170 return info;
171 }
172
174 {
176 }
177
178} // end NAMESPACE OgreNewt
179
simple class that represents a single convexcast contact
Ogre::Real mContactPenetration
contact penetration at collision point
dLong mCollisionID
collision ID of the primitive hit by the ray (for compound collision bodies)
Ogre::Vector3 mContactPoint
point of the contact in global space
OgreNewt::Body * mBody
pointer to body intersected with
Ogre::Vector3 mContactNormalOnHitPoint
surface normal at the surface of the hit body
Ogre::Vector3 mContactNormal
normal of intersection.
int getContactsCount() const
how many contacts do we have
bool userPreFilterCallback(OgreNewt::Body *body)
prefilter filter, ignores body set in constructor or always returns true if it's not set
ConvexcastContactInfo getInfoAt(int hitnum) const
retrieve the raycast info for a specific hit.
int calculateBodyHitCount() const
how many bodies did we hit? if maxcontactscount is to small, this value will be smaller too!
Ogre::Real getDistanceToFirstHit() const
retrieve the distance to the first contact (in range [0,1] from startpt to endpt)
OgreNewt::Body * mIgnoreBody
Body to ignore in filter.
main class for all Rigid Bodies in the system.
OgreNewt::World *const getWorld() const
get a pointer to the OgreNewt::World this body belongs to.
NewtonCollision *const getNewtonCollision() const
retrieve the Newton pointer
represents a collision shape that is explicitly convex.
Ogre::Real mFirstContactDistance
distance in [0,1] to first contact
virtual ~Convexcast()
destuctor.
void go(const OgreNewt::World *world, const OgreNewt::ConvexCollisionPtr &col, const Ogre::Vector3 &startpt, const Ogre::Quaternion &colori, const Ogre::Vector3 &endpt, int maxcontactscount, int threadIndex)
performs the convexcast.
virtual bool userPreFilterCallback(OgreNewt::Body *body)
user callback pre-filter function.
NewtonWorldConvexCastReturnInfo * mReturnInfoList
list that stores the results of the convex-cast
int mReturnInfoListLength
the real length of the list
int mReturnInfoListSize
the actual maximum length of the list (number of elements memory has been reserved for)
bool isRaycastRecording()
returns true, if currently recording raycasts
bool isRaycastRecordingHitBodies()
returns true, if hit bodies are currently recording
void addHitBody(const OgreNewt::Body *body)
this function is used internally
void addDiscardedBody(const OgreNewt::Body *body)
this function is used internally
void addConvexRay(const OgreNewt::ConvexCollisionPtr &col, const Ogre::Vector3 &startpt, const Ogre::Quaternion &colori, const Ogre::Vector3 &endpt)
this function is used internally
represents a physics world.
Debugger & getDebugger() const
get the debugger for this world
NewtonWorld * getNewtonWorld() const
retrieves a pointer to the NewtonWorld
_OgreNewtExport void QuatPosToMatrix(const Ogre::Quaternion &quat, const Ogre::Vector3 &pos, dFloat *matrix)
Take a Quaternion and Position Matrix and create a Newton-happy float matrix!