Added a ton of stuff, including the beginnings of a new UI system
[ne.git] / src / phys / world.cpp
blob1b71976c4c7afd6e01b323a01bca98713a56efa3
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 ************************************************************************/
18 #include "world.h"
20 #include "object.h"
21 #include <GL/gl.h>
23 /**********************
24 * Collision callback *
25 **********************/
27 #define MAX_CONTACTS 3
28 void collision_callback(void *data, dGeomID g1, dGeomID g2)
30 dWorld *world = World::Instance()->getWorld();
31 dJointGroup *jgroup = World::Instance()->getJointGroup();
33 dContact contacts[MAX_CONTACTS];
35 Object *o1 = (Object*)dGeomGetData(g1);
36 Object *o2 = (Object*)dGeomGetData(g2);
38 int num = dCollide(g1,g2,MAX_CONTACTS,&contacts[0].geom,sizeof(dContact));
40 if (num && o1 && o2)
42 if ( o1->collide(o2) && o2->collide(o1) )
44 // they collide
46 else
48 // they don't
49 return;
53 dWorldID world_id = world->id();
54 dJointGroupID jgroup_id = jgroup->id();
56 for (int i=0; i<num; i++)
58 contacts[i].surface.mode = dContactBounce;
59 contacts[i].surface.bounce = 0.1;
60 contacts[i].surface.bounce_vel = 0.01;
61 contacts[i].surface.mu = 5000;
62 dJointID c = dJointCreateContact(world_id,jgroup_id,&contacts[i]);
63 dJointAttach(c,dGeomGetBody(contacts[i].geom.g1),
64 dGeomGetBody(contacts[i].geom.g2));
68 /***************
69 * World class *
70 ***************/
72 World::World() : m_maxcontacts(512), m_stepcount(2), m_stepsize(0.1)
74 m_jointgroup = new dJointGroup(m_maxcontacts);
75 m_space = new dHashSpace(0);
76 dReal center[] = {0,0,0};
77 dReal extents[] = {100,100,10};
78 m_space = new dQuadTreeSpace(0,center,extents,6);
79 dInitODE();
82 World::~World()
84 delete m_jointgroup;
85 delete m_space;
88 World* World::Instance()
90 static World w;
91 return &w;
94 void World::step()
96 m_space->collide(0,collision_callback);
98 for (int i=0; i<m_stepcount; i++)
100 dWorldQuickStep(m_world.id(),m_stepsize/m_stepcount);
103 m_jointgroup->empty();