Panda3D
 All Classes Functions Variables Enumerations
bulletHingeConstraint.cxx
1 // Filename: bulletHingeConstraint.cxx
2 // Created by: enn0x (01Mar10)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "bulletHingeConstraint.h"
16 #include "bulletRigidBodyNode.h"
17 
18 #include "deg_2_rad.h"
19 
20 TypeHandle BulletHingeConstraint::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: BulletHingeConstraint::Constructor
24 // Access: Published
25 // Description: Creates a hinge constraint which connects one
26 // rigid body with some fixe dpoint in the world.
27 ////////////////////////////////////////////////////////////////////
30  const TransformState *ts_a,
31  bool use_frame_a) {
32 
33  btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
34  btTransform frame_a = TransformState_to_btTrans(ts_a);
35 
36  _constraint = new btHingeConstraint(*ptr_a, frame_a, use_frame_a);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: BulletHingeConstraint::Constructor
41 // Access: Published
42 // Description: Constructs a hinge constraint which connects two
43 // rigid bodies.
44 ////////////////////////////////////////////////////////////////////
47  const BulletRigidBodyNode *node_b,
48  const TransformState *ts_a,
49  const TransformState *ts_b,
50  bool use_frame_a) {
51 
52  btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
53  btTransform frame_a = TransformState_to_btTrans(ts_a);
54 
55  btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object());
56  btTransform frame_b = TransformState_to_btTrans(ts_b);
57 
58  _constraint = new btHingeConstraint(*ptr_a, *ptr_b, frame_a, frame_b, use_frame_a);
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: BulletHingeConstraint::Constructor
63 // Access: Published
64 // Description: Creates a hinge constraint in the same way as the
65 // other constructor, but uses the world as second
66 // body so that node_a is fixed to some point in
67 // mid-air for example.
68 ////////////////////////////////////////////////////////////////////
71  const LPoint3 &pivot_a,
72  const LVector3 &axis_a,
73  bool use_frame_a) {
74 
75  btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
76  btVector3 pos_a = LVecBase3_to_btVector3(pivot_a);
77  btVector3 vec_a = LVecBase3_to_btVector3(axis_a);
78 
79  _constraint = new btHingeConstraint(*ptr_a, pos_a, vec_a, use_frame_a);
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: BulletHingeConstraint::Constructor
84 // Access: Published
85 // Description: Creates a hinge connecting node_a to node_b. The
86 // pivot point is the point at which the body is fixed
87 // to the constraint. In other words: It specifies
88 // where on each body the rotation axis should be. This
89 // axis is specified using axis_a and axis_b.
90 // Remember, everything is specified in the bodies own
91 // coordinate system!
92 ////////////////////////////////////////////////////////////////////
95  const BulletRigidBodyNode *node_b,
96  const LPoint3 &pivot_a,
97  const LPoint3 &pivot_b,
98  const LVector3 &axis_a,
99  const LVector3 &axis_b,
100  bool use_frame_a) {
101 
102  btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
103  btVector3 pos_a = LVecBase3_to_btVector3(pivot_a);
104  btVector3 vec_a = LVecBase3_to_btVector3(axis_a);
105 
106  btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object());
107  btVector3 pos_b = LVecBase3_to_btVector3(pivot_b);
108  btVector3 vec_b = LVecBase3_to_btVector3(axis_b);
109 
110  _constraint = new btHingeConstraint(*ptr_a, *ptr_b, pos_a, pos_b, vec_a, vec_b, use_frame_a);
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: BulletHingeConstraint::ptr
115 // Access: Public
116 // Description:
117 ////////////////////////////////////////////////////////////////////
118 btTypedConstraint *BulletHingeConstraint::
119 ptr() const {
120 
121  return _constraint;
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: BulletHingeConstraint::set_angular_only
126 // Access: Published
127 // Description:
128 ////////////////////////////////////////////////////////////////////
129 void BulletHingeConstraint::
130 set_angular_only(bool value) {
131 
132  return _constraint->setAngularOnly(value);
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: BulletHingeConstraint::get_angular_only
137 // Access: Published
138 // Description:
139 ////////////////////////////////////////////////////////////////////
140 bool BulletHingeConstraint::
141 get_angular_only() const {
142 
143  return _constraint->getAngularOnly();
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: BulletHingeConstraint::set_limit
148 // Access: Published
149 // Description: Sets the lower and upper rotational limits in
150 // degrees.
151 ////////////////////////////////////////////////////////////////////
153 set_limit(PN_stdfloat low, PN_stdfloat high, PN_stdfloat softness, PN_stdfloat bias, PN_stdfloat relaxation) {
154 
155  low = deg_2_rad(low);
156  high = deg_2_rad(high);
157 
158  _constraint->setLimit(low, high, softness, bias, relaxation);
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function: BulletHingeConstraint::set_axis
163 // Access: Published
164 // Description: Sets the hinge's rotation axis in world
165 // coordinates.
166 ////////////////////////////////////////////////////////////////////
168 set_axis(const LVector3 &axis) {
169 
170  nassertv(!axis.is_nan());
171 
172  btVector3 v = LVecBase3_to_btVector3(axis);
173  _constraint->setAxis(v);
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: BulletHingeConstraint::get_lower_limit
178 // Access: Published
179 // Description: Returns the lower angular limit in degrees.
180 ////////////////////////////////////////////////////////////////////
181 PN_stdfloat BulletHingeConstraint::
183 
184  return rad_2_deg(_constraint->getLowerLimit());
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: BulletHingeConstraint::get_upper_limit
189 // Access: Published
190 // Description: Returns the upper angular limit in degrees.
191 ////////////////////////////////////////////////////////////////////
192 PN_stdfloat BulletHingeConstraint::
194 
195  return rad_2_deg(_constraint->getUpperLimit());
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: BulletHingeConstraint::get_hinge_angle
200 // Access: Published
201 // Description: Returns the angle between node_a and node_b in
202 // degrees.
203 ////////////////////////////////////////////////////////////////////
204 PN_stdfloat BulletHingeConstraint::
206 
207  return rad_2_deg(_constraint->getHingeAngle());
208 }
209 
210 ////////////////////////////////////////////////////////////////////
211 // Function: BulletHingeConstraint::enable_angular_motor
212 // Access: Published
213 // Description: Applies an impulse to the constraint so that the
214 // angle changes at target_velocity where max_impulse
215 // is the maximum impulse that is used for achieving
216 // the specified velocity.
217 //
218 // Note that the target_velocity is in radians/second,
219 // not degrees.
220 ////////////////////////////////////////////////////////////////////
222 enable_angular_motor(bool enable, PN_stdfloat target_velocity, PN_stdfloat max_impulse) {
223 
224  _constraint->enableAngularMotor(enable, target_velocity, max_impulse);
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: BulletHingeConstraint::enable_motor
229 // Access: Published
230 // Description:
231 ////////////////////////////////////////////////////////////////////
232 void BulletHingeConstraint::
233 enable_motor(bool enable) {
234 
235  _constraint->enableMotor(enable);
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: BulletHingeConstraint::set_max_motor_impulse
240 // Access: Published
241 // Description: Sets the maximum impulse used to achieve the
242 // velocity set in enable_angular_motor.
243 ////////////////////////////////////////////////////////////////////
245 set_max_motor_impulse(PN_stdfloat max_impulse) {
246 
247  _constraint->setMaxMotorImpulse(max_impulse);
248 }
249 
250 ////////////////////////////////////////////////////////////////////
251 // Function: BulletHingeConstraint::set_motor_target
252 // Access: Published
253 // Description:
254 ////////////////////////////////////////////////////////////////////
255 void BulletHingeConstraint::
256 set_motor_target(const LQuaternion &quat, PN_stdfloat dt) {
257 
258  _constraint->setMotorTarget(LQuaternion_to_btQuat(quat), dt);
259 }
260 
261 ////////////////////////////////////////////////////////////////////
262 // Function: BulletHingeConstraint::set_motor_target
263 // Access: Published
264 // Description:
265 ////////////////////////////////////////////////////////////////////
266 void BulletHingeConstraint::
267 set_motor_target(PN_stdfloat target_angle, PN_stdfloat dt) {
268 
269  _constraint->setMotorTarget(target_angle, dt);
270 }
271 
272 ////////////////////////////////////////////////////////////////////
273 // Function: BulletHingeConstraint::set_frames
274 // Access: Published
275 // Description:
276 ////////////////////////////////////////////////////////////////////
277 void BulletHingeConstraint::
278 set_frames(const TransformState *ts_a, const TransformState *ts_b) {
279 
280  btTransform frame_a = TransformState_to_btTrans(ts_a);
281  btTransform frame_b = TransformState_to_btTrans(ts_b);
282 
283  _constraint->setFrames(frame_a, frame_b);
284 }
285 
void set_axis(const LVector3 &axis)
Sets the hinge's rotation axis in world coordinates.
PN_stdfloat get_hinge_angle()
Returns the angle between node_a and node_b in degrees.
PN_stdfloat get_upper_limit() const
Returns the upper angular limit in degrees.
void enable_angular_motor(bool enable, PN_stdfloat target_velocity, PN_stdfloat max_impulse)
Applies an impulse to the constraint so that the angle changes at target_velocity where max_impulse i...
PN_stdfloat get_lower_limit() const
Returns the lower angular limit in degrees.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
bool is_nan() const
Returns true if any component of the vector is not-a-number, false otherwise.
Definition: lvecBase3.h:463
void set_max_motor_impulse(PN_stdfloat max_impulse)
Sets the maximum impulse used to achieve the velocity set in enable_angular_motor.
void set_limit(PN_stdfloat low, PN_stdfloat high, PN_stdfloat softness=0.9f, PN_stdfloat bias=0.3f, PN_stdfloat relaxation=1.0f)
Sets the lower and upper rotational limits in degrees.
This is the base quaternion class.
Definition: lquaternion.h:96
BulletHingeConstraint(const BulletRigidBodyNode *node_a, const LPoint3 &pivot_a, const LVector3 &axis_a, bool use_frame_a=false)
Creates a hinge constraint in the same way as the other constructor, but uses the world as second bod...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85