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