1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
24 /**********************
25 * Collision callback *
26 **********************/
28 #define MAX_CONTACTS 3
29 void collision_callback(void *data
, dGeomID g1
, dGeomID g2
)
31 if (dGeomIsSpace (g1
) || dGeomIsSpace (g2
)) {
33 // colliding a space with something :
34 dSpaceCollide2 (g1
, g2
, data
,&collision_callback
);
36 // collide all geoms internal to the space(s)
37 if (dGeomIsSpace (g1
))
38 dSpaceCollide ((dSpaceID
)g1
, data
, &collision_callback
);
39 if (dGeomIsSpace (g2
))
40 dSpaceCollide ((dSpaceID
)g2
, data
, &collision_callback
);
45 dWorld
*world
= World::Instance()->getWorld();
46 dJointGroup
*jgroup
= World::Instance()->getJointGroup();
48 dContact contacts
[MAX_CONTACTS
];
50 Object
*o1
= (Object
*)dGeomGetData(g1
);
51 Object
*o2
= (Object
*)dGeomGetData(g2
);
53 int num
= dCollide(g1
,g2
,MAX_CONTACTS
,&contacts
[0].geom
,sizeof(dContact
));
57 if ( o1
->collide(o2
) && o2
->collide(o1
) )
68 dWorldID world_id
= world
->id();
69 dJointGroupID jgroup_id
= jgroup
->id();
71 for (int i
=0; i
<num
; i
++)
73 contacts
[i
].surface
.mode
= dContactBounce
;
74 contacts
[i
].surface
.bounce
= 0.0;//0.1;
75 contacts
[i
].surface
.bounce_vel
= 0.0;//0.01;
76 contacts
[i
].surface
.mu
= dInfinity
;
79 for (int i
=0; i
<num
; i
++)
81 dJointID c
= dJointCreateContact(world_id
,jgroup_id
,&contacts
[i
]);
82 dJointAttach(c
,dGeomGetBody(contacts
[i
].geom
.g1
),
83 dGeomGetBody(contacts
[i
].geom
.g2
));
91 World::World() : m_maxcontacts(512), m_stepcount(2), m_stepsize(0.1)
93 m_jointgroup
= new dJointGroup(m_maxcontacts
);
94 m_space
= new dHashSpace(0);
95 dReal center
[] = {0,0,0};
96 dReal extents
[] = {100,100,10};
97 m_space
= new dQuadTreeSpace(0,center
,extents
,6);
107 World
* World::Instance()
115 m_space
->collide(0,collision_callback
);
117 for (int i
=0; i
<m_stepcount
; i
++)
119 dWorldQuickStep(m_world
.id(),m_stepsize
/m_stepcount
);
122 m_jointgroup
->empty();
125 void ray_callback(void *data
, dGeomID g1
, dGeomID g2
)
127 dContactGeom
*res
= (dContactGeom
*)data
;
129 //Object *o1 = (Object*)dGeomGetData(g1);
130 //Object *o2 = (Object*)dGeomGetData(g2);
132 if (dGeomGetClass(g1
) != dRayClass
&&
133 dGeomGetClass(g2
) != dRayClass
) {
137 dCollide(g1
, g2
, 1, res
, sizeof(dContactGeom
));
140 printf("Got %d contacts\n", num);
143 printf("Found one\n");
144 if (geom.depth <= res->depth) {
152 dReal
World::trace(dRay
*ray
)
155 geom
.depth
= dInfinity
;
156 ray
->collide2((dGeomID
)m_space
->id(), &geom
, ray_callback
);