root / branches / cpp-ode / src / libmmpong / netgame_ode.cc @ 542

Revision 542, 1.8 kB (checked in by gaul@…, 6 months ago)
  • move code from .h into .cc files
  • prepare ODEObj for enable()/disable() in order to retrieve corresponding ODEObjs in collision detection
Line 
1#include "ode_box.h"
2#include "netgame_ode.h"
3
4using namespace mmp;
5
6
7boost::shared_ptr<NetGameODE> NetGameODE::assureODE(pointer game) {
8        boost::shared_ptr<NetGameODE> ptr = boost::dynamic_pointer_cast<NetGameODE>(game);
9        if (!ptr)
10                throw std::runtime_error("Given NetGame is not derived from NetGameODE");
11        return ptr;
12};
13
14#define STEPSIZE 0.001f
15void NetGameODE::advance(const boost::posix_time::ptime endtime) {
16        lock();
17        if (endtime < world_time) {
18                LOGWARN("endtime < world_time");
19                unlock();
20                return;
21        }
22       
23        boost::posix_time::time_duration elapsed = endtime - world_time;
24
25        //TODO (if necessary): improve (naive) time stepping by measuring computation times
26       
27        double seconds= ((double)elapsed.total_microseconds())/1.e6;
28        unsigned int step_count= seconds/STEPSIZE;
29        LOGDBG("step world "<<seconds<<" seconds with "<<step_count << " iterations");
30        for (unsigned int i=0; i<step_count; i++) {
31                collide();
32                quickStep(STEPSIZE);
33        }
34        world_time=endtime;
35       
36        unlock();
37}
38
39NetEnt::pointer NetGameODE::cln_ent_new(NetMsg::pointer msg) {
40        NetEnt::pointer ent=msg_ent_new_parse(msg);
41        return ent;
42}
43
44
45#define MAX_CONTACTS 4 //maximal number of contacts between two particular objects
46
47void NetGameODE::collideCallback(void *data, dGeomID o1, dGeomID o2) {
48        dContact contact[MAX_CONTACTS];
49        unsigned int n_collide = dCollide(o1, o2, MAX_CONTACTS, &contact[0].geom, sizeof(dContact));
50        for (unsigned int i=0; i< n_collide; i++){
51                contact[i].surface.mode = dContactBounce;// | dContactSoftCFM;
52                contact[i].surface.mu=dInfinity;
53                contact[i].surface.bounce=1.0; // \in [0,1]
54                contact[i].surface.bounce_vel=0.01;
55                //contact[i].surface.soft_cfm = 0.1;
56                dBodyID b1 = dGeomGetBody(o1), b2 = dGeomGetBody(o2);
57                dJointID c = dJointCreateContact(id(), 0, contact+i);
58                dJointAttach(c, b1, b2);
59                LOGDBG("Collision detected (BAM!)");
60        }
61}
62
63#undef MAX_CONTACTS
Note: See TracBrowser for help on using the browser.