Tizen 2.1 base
[platform/upstream/libbullet.git] / Demos / Gpu2dDemo / btGpuDemoPairCache.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16
17
18 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
19
20 #include "btGpuDemoPairCache.h"
21
22 #include "btGpuDemoDynamicsWorld.h"
23
24
25
26 void btGpuDemoPairCache::processAllOverlappingPairs(btOverlapCallback* callback, btDispatcher* dispatcher)
27 {
28         int sz = m_maxProxies * m_maxNeighbors;
29         int numContConstraints = 0;
30         int2* pIds = gpCudaDemoDynamicsWorld->getIdsPtr();
31         for(int idx = 0; idx < sz; idx++)
32         {
33                 int neigh  = m_hNeighbors[idx];
34                 if(neigh >= 0)
35                 {
36                         int i=idx / m_maxNeighbors;
37                         int j=idx % m_maxNeighbors;
38                         pIds[numContConstraints].x = i;
39                         pIds[numContConstraints].y = m_hNeighbors[i * m_maxNeighbors + j];
40                         numContConstraints++;
41                 }
42         }
43         gpCudaDemoDynamicsWorld->setTotalNumConstraints(numContConstraints);    
44
45
46
47
48 // this will be called for each overlapping pair if collision detection uses pairCache other than btGpuDemoPairCache
49 // IMPORTANT : m_numConstraints in gpCudaDemoDynamicsWorld is set to 0 at start of simulation step
50 // IMPORTANT : companionIds for all objects should be properly set at start of simulation step
51 void cudaDemoNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo)
52 {
53         btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
54         btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;
55         if (dispatcher.needsCollision(colObj0,colObj1))
56         {
57                 // int id0 = collisionPair.m_pProxy0->m_uniqueId - 2;
58                 // int id1 = collisionPair.m_pProxy1->m_uniqueId - 2;
59                 // cannot use m_uniqueId : it may be altered by broadphase code
60                 // so we'll use companionIds set on the initialization stage
61                 unsigned int id0 = colObj0->getCompanionId();
62                 unsigned int id1 = colObj1->getCompanionId();
63                 if(id0 > id1)
64                 {
65                         int tmp = id0; id0 = id1; id1 = tmp;
66                 }
67                 int totalNumConstraints = gpCudaDemoDynamicsWorld->getTotalNumConstraints();
68                 int2* pIds = gpCudaDemoDynamicsWorld->getIdsPtr();
69                 pIds += totalNumConstraints;
70                 pIds->x = id0;
71                 pIds->y = id1;
72                 totalNumConstraints++;
73                 gpCudaDemoDynamicsWorld->setTotalNumConstraints(totalNumConstraints);   
74         }
75 } // cudaDemoNearCallback()
76