Panda3D
bulletGenericConstraint.cxx
1 // Filename: bulletGenericConstraint.cxx
2 // Created by: enn0x (02Mar10)
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 "bulletGenericConstraint.h"
16 #include "bulletRigidBodyNode.h"
17 
18 TypeHandle BulletGenericConstraint::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: BulletGenericConstraint::Constructor
22 // Access: Published
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 BulletGenericConstraint::
26 BulletGenericConstraint(const BulletRigidBodyNode *node_a,
27  const TransformState *frame_a,
28  bool use_frame_a) {
29 
30  btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
31  btTransform trans_a = TransformState_to_btTrans(frame_a);
32 
33  _constraint = new btGeneric6DofConstraint(*ptr_a, trans_a, use_frame_a);
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: BulletGenericConstraint::Constructor
38 // Access: Published
39 // Description:
40 ////////////////////////////////////////////////////////////////////
41 BulletGenericConstraint::
42 BulletGenericConstraint(const BulletRigidBodyNode *node_a,
43  const BulletRigidBodyNode *node_b,
44  const TransformState *frame_a,
45  const TransformState *frame_b,
46  bool use_frame_a) {
47 
48  btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
49  btTransform trans_a = TransformState_to_btTrans(frame_a);
50 
51  btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object());
52  btTransform trans_b = TransformState_to_btTrans(frame_b);
53 
54  _constraint = new btGeneric6DofConstraint(*ptr_a, *ptr_b, trans_a, trans_b, use_frame_a);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: BulletGenericConstraint::ptr
59 // Access: Public
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 btTypedConstraint *BulletGenericConstraint::
63 ptr() const {
64 
65  return _constraint;
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: BulletGenericConstraint::get_axis
70 // Access: Published
71 // Description:
72 ////////////////////////////////////////////////////////////////////
73 LVector3 BulletGenericConstraint::
74 get_axis(int axis) const {
75 
76  nassertr(axis >= 0, LVector3::zero());
77  nassertr(axis <= 3, LVector3::zero());
78 
79  _constraint->buildJacobian();
80  return btVector3_to_LVector3(_constraint->getAxis(axis));
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: BulletGenericConstraint::get_pivot
85 // Access: Published
86 // Description:
87 ////////////////////////////////////////////////////////////////////
88 PN_stdfloat BulletGenericConstraint::
89 get_pivot(int axis) const {
90 
91  nassertr(axis >= 0, 0.0f);
92  nassertr(axis <= 3, 0.0f);
93 
94  _constraint->buildJacobian();
95  return _constraint->getRelativePivotPosition(axis);
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: BulletGenericConstraint::get_angle
100 // Access: Published
101 // Description:
102 ////////////////////////////////////////////////////////////////////
103 PN_stdfloat BulletGenericConstraint::
104 get_angle(int axis) const {
105 
106  nassertr(axis >= 0, 0.0f);
107  nassertr(axis <= 3, 0.0f);
108 
109  _constraint->buildJacobian();
110  return _constraint->getAngle(axis);
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: BulletGenericConstraint::set_linear_limit
115 // Access: Published
116 // Description:
117 ////////////////////////////////////////////////////////////////////
118 void BulletGenericConstraint::
119 set_linear_limit(int axis, PN_stdfloat low, PN_stdfloat high) {
120 
121  nassertv(axis >= 0);
122  nassertv(axis <= 3);
123 
124  _constraint->buildJacobian();
125  _constraint->setLimit(axis, low, high);
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: BulletGenericConstraint::set_angular_limit
130 // Access: Published
131 // Description:
132 ////////////////////////////////////////////////////////////////////
133 void BulletGenericConstraint::
134 set_angular_limit(int axis, PN_stdfloat low, PN_stdfloat high) {
135 
136  nassertv(axis >= 0);
137  nassertv(axis <= 3);
138 
139  low = deg_2_rad(low);
140  high = deg_2_rad(high);
141 
142  _constraint->buildJacobian();
143  _constraint->setLimit(axis + 3, low, high);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: BulletGenericConstraint::get_rotational_limit_motor
148 // Access: Published
149 // Description:
150 ////////////////////////////////////////////////////////////////////
151 BulletRotationalLimitMotor BulletGenericConstraint::
152 get_rotational_limit_motor(int axis) {
153 
154  return BulletRotationalLimitMotor(*_constraint->getRotationalLimitMotor(axis));
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: BulletGenericConstraint::get_translational_limit_motor
159 // Access: Published
160 // Description:
161 ////////////////////////////////////////////////////////////////////
162 BulletTranslationalLimitMotor BulletGenericConstraint::
163 get_translational_limit_motor() {
164 
165  return BulletTranslationalLimitMotor(*_constraint->getTranslationalLimitMotor());
166 }
167 
168 ////////////////////////////////////////////////////////////////////
169 // Function: BulletGenericConstraint::set_frames
170 // Access: Published
171 // Description:
172 ////////////////////////////////////////////////////////////////////
173 void BulletGenericConstraint::
174 set_frames(const TransformState *ts_a, const TransformState *ts_b) {
175 
176  btTransform frame_a = TransformState_to_btTrans(ts_a);
177  btTransform frame_b = TransformState_to_btTrans(ts_b);
178 
179  _constraint->setFrames(frame_a, frame_b);
180 }
181 
Rotation Limit structure for generic joints.
static const LVector3f & zero()
Returns a zero-length vector.
Definition: lvector3.h:270
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
Rotation Limit structure for generic joints.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85