Panda3D
odeWorld.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file odeWorld.cxx
10  * @author joswilso
11  * @date 2006-12-27
12  */
13 
14 #include "config_ode.h"
15 #include "odeWorld.h"
16 #include "odeBody.h"
17 
18 TypeHandle OdeWorld::_type_handle;
19 
20 OdeWorld::
21 OdeWorld() :
22  _id(dWorldCreate()) {
23  odeworld_cat.debug() << get_type() << "(" << _id << ")" << "\n";
24  _num_surfaces = 0;
25 
26 }
27 
28 OdeWorld::
29 OdeWorld(const OdeWorld &copy) :
30  _id(copy._id) {
31  _num_surfaces = 0;
32 
33 }
34 
35 OdeWorld::
36 ~OdeWorld() {
37  odeworld_cat.debug() << "~" << get_type() << "(" << _id << ")" << "\n";
38 }
39 
40 void OdeWorld::
41 destroy() {
42  if(_num_surfaces > 0) {
43  delete _surface_table;
44  }
45  nassertv(_id);
46  dWorldDestroy(_id);
47 }
48 
49 /*
50 void OdeWorld::
51 assign_surface_body(OdeBody& body, int surface) {
52  // odeworld_cat.debug() << "assign_surface_body body.Id =" << body.get_id()
53  // << " surface=" << surface << "\n";
54  _body_dampen_map[body.get_id()].surfaceType = surface;
55  _body_dampen_map[body.get_id()].dampen = 0.0f;
56 }
57 */
58 
59 void OdeWorld::
60 add_body_dampening(OdeBody& body, int surface) {
61  _body_dampen_map[body.get_id()].dampen = 0.0f;
62 }
63 
64 
65 void OdeWorld::
66 init_surface_table(uint8_t num_surfaces) {
67  _surface_table = new sSurfaceParams[num_surfaces * num_surfaces];
68  // _dampen_table = new sSurfaceParams[num_surfaces * num_surfaces];
69  _num_surfaces = num_surfaces;
70 }
71 
72 void OdeWorld::
73 set_surface(int pos1, int pos2, sSurfaceParams& entry) {
74  odeworld_cat.debug() << " pos1 " << pos1 << " pos2 " << pos2 << " num surfaces " << (int)_num_surfaces << " endline\n";
75  if((_num_surfaces <= pos1) || (_num_surfaces <= pos2)) {
76  odeworld_cat.error() << "surface position exceeds size of surface table, set num_surface in initSurfaceTable higher." << "\n";
77  return;
78  }
79  int true_pos = (pos1 * _num_surfaces) + pos2;
80  _surface_table[true_pos].colparams.mode = entry.colparams.mode;
81  _surface_table[true_pos].colparams.mu = entry.colparams.mu;
82  _surface_table[true_pos].colparams.mu2 = entry.colparams.mu2;
83  _surface_table[true_pos].colparams.bounce = entry.colparams.bounce;
84  _surface_table[true_pos].colparams.bounce_vel = entry.colparams.bounce_vel;
85  _surface_table[true_pos].colparams.soft_cfm = entry.colparams.soft_cfm;
86  _surface_table[true_pos].colparams.motion1 = entry.colparams.motion1;
87  _surface_table[true_pos].colparams.motion2 = entry.colparams.motion2;
88  _surface_table[true_pos].colparams.slip1 = entry.colparams.slip1;
89  _surface_table[true_pos].colparams.slip2 = entry.colparams.slip2;
90  _surface_table[true_pos].dampen = entry.dampen;
91 }
92 
93 
94 sSurfaceParams& OdeWorld::
95 get_surface(uint8_t surface1, uint8_t surface2) {
96  int true_pos = 0;
97  if(surface1 >= surface2) {
98  true_pos = (surface1 * _num_surfaces) + surface2;
99  } else {
100  true_pos = (surface2 * _num_surfaces) + surface1;
101  }
102  if((_num_surfaces <= surface1) || (_num_surfaces <= surface2)) {
103  odeworld_cat.error() << "surface position exceeds size of surface table, set num_surface in initSurfaceTable higher." << "\n";
104  // nassertr_always((_num_surfaces > surface1 && _num_surfaces > surface2),
105  // _surface_table[true_pos]);
106  }
107  return _surface_table[true_pos];
108 }
109 
110 void OdeWorld::
111 set_surface_entry(uint8_t pos1, uint8_t pos2,
112  dReal mu,
113  dReal bounce,
114  dReal bounce_vel,
115  dReal soft_erp,
116  dReal soft_cfm,
117  dReal slip,
118  dReal dampen) {
119  // todo: add mode
120  sSurfaceParams new_params;
121  int someMode = 0;
122  if (bounce > 0.0001) {
123  someMode |= dContactBounce;
124  }
125  if (soft_erp > 0.0001) {
126  someMode |= dContactSoftERP;
127  }
128  if (soft_cfm > 0.0001) {
129  someMode |= dContactSoftCFM;
130  }
131  if (slip > 0.0001) {
132  someMode = someMode | dContactSlip1 | dContactSlip2;
133  }
134  new_params.colparams.mode = dContactBounce | dContactSoftCFM | dContactApprox1;// | dContactSoftERP;
135  new_params.colparams.mu = mu;
136  new_params.colparams.mu2 = mu;
137  new_params.colparams.bounce = bounce;
138  new_params.colparams.bounce_vel = bounce_vel;
139  new_params.colparams.soft_erp = soft_erp;
140  new_params.colparams.soft_cfm = soft_cfm;
141  new_params.colparams.slip1 = slip;
142  new_params.colparams.slip2 = slip;
143  new_params.colparams.motion1 = 0.0;
144  new_params.colparams.motion2 = 0.0;
145  new_params.dampen = dampen;
146  // todo: a bit of wasted space here
147  set_surface(pos1, pos2, new_params);
148 
149  if(pos1 >= pos2) {
150  set_surface(pos1, pos2, new_params);
151  } else {
152  set_surface(pos2, pos1, new_params);
153  }
154 }
155 
156 
157 
158 void OdeWorld::
159 set_dampen_on_bodies(dBodyID id1, dBodyID id2,dReal damp) {
160  if(_body_dampen_map[id1].dampen < damp) {
161  _body_dampen_map[id1].dampen = damp;
162  }
163  if(_body_dampen_map[id2].dampen < damp) {
164  _body_dampen_map[id2].dampen = damp;
165  }
166 }
167 
168 float OdeWorld::
169 apply_dampening(float dt, OdeBody& body) {
170  dBodyID bodyId = body.get_id();
171  dReal damp = _body_dampen_map[bodyId].dampen;
172  float dampening = 1.00 - (damp * dt);
173  body.set_angular_vel(body.get_angular_vel() * dampening);
174  body.set_linear_vel(body.get_linear_vel() * dampening);
175  _body_dampen_map[bodyId].dampen = 0.0;
176  return dampening;
177 }
178 
179 OdeWorld::
180 operator bool () const {
181  return (_id != nullptr);
182 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dBodyID get_id() const
Returns the underlying dBodyID.
Definition: odeBody.I:28
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81