Panda3D
 All Classes Functions Variables Enumerations
bulletHingeConstraint.cxx
00001 // Filename: bulletHingeConstraint.cxx
00002 // Created by:  enn0x (01Mar10)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "bulletHingeConstraint.h"
00016 #include "bulletRigidBodyNode.h"
00017 
00018 #include "deg_2_rad.h"
00019 
00020 TypeHandle BulletHingeConstraint::_type_handle;
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: BulletHingeConstraint::Constructor
00024 //       Access: Published
00025 //  Description: Creates a hinge constraint in the same way as the
00026 //               other constructor, but uses the world as second
00027 //               body so that node_a is fixed to some point in
00028 //               mid-air for example.
00029 ////////////////////////////////////////////////////////////////////
00030 BulletHingeConstraint::
00031 BulletHingeConstraint(const BulletRigidBodyNode *node_a, 
00032                       const LPoint3 &pivot_a,
00033                       const LVector3 &axis_a,
00034                       bool use_frame_a) {
00035 
00036   btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
00037   btVector3 pos_a = LVecBase3_to_btVector3(pivot_a);
00038   btVector3 vec_a = LVecBase3_to_btVector3(axis_a);
00039 
00040   _constraint = new btHingeConstraint(*ptr_a, pos_a, vec_a, use_frame_a);
00041 }
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 //     Function: BulletHingeConstraint::Constructor
00045 //       Access: Published
00046 //  Description: Creates a hinge connecting node_a to node_b. The
00047 //               pivot point is the point at which the body is fixed
00048 //               to the constraint. In other words: It specifies
00049 //               where on each body the rotation axis should be. This
00050 //               axis is specified using axis_a and axis_b.
00051 //               Remember, everything is specified in the bodies own
00052 //               coordinate system!
00053 ////////////////////////////////////////////////////////////////////
00054 BulletHingeConstraint::
00055 BulletHingeConstraint(const BulletRigidBodyNode *node_a,
00056                       const BulletRigidBodyNode *node_b,
00057                       const LPoint3 &pivot_a,
00058                       const LPoint3 &pivot_b,
00059                       const LVector3 &axis_a,
00060                       const LVector3 &axis_b,
00061                       bool use_frame_a) {
00062 
00063   btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object());
00064   btVector3 pos_a = LVecBase3_to_btVector3(pivot_a);
00065   btVector3 vec_a = LVecBase3_to_btVector3(axis_a);
00066 
00067   btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object());
00068   btVector3 pos_b = LVecBase3_to_btVector3(pivot_b);
00069   btVector3 vec_b = LVecBase3_to_btVector3(axis_b);
00070 
00071   _constraint = new btHingeConstraint(*ptr_a, *ptr_b, pos_a, pos_b, vec_a, vec_b, use_frame_a);
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: BulletHingeConstraint::ptr
00076 //       Access: Public
00077 //  Description:
00078 ////////////////////////////////////////////////////////////////////
00079 btTypedConstraint *BulletHingeConstraint::
00080 ptr() const {
00081 
00082   return _constraint;
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: BulletHingeConstraint::set_angular_only
00087 //       Access: Published
00088 //  Description:
00089 ////////////////////////////////////////////////////////////////////
00090 void BulletHingeConstraint::
00091 set_angular_only(bool value) {
00092 
00093   return _constraint->setAngularOnly(value);
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: BulletHingeConstraint::get_angular_only
00098 //       Access: Published
00099 //  Description:
00100 ////////////////////////////////////////////////////////////////////
00101 bool BulletHingeConstraint::
00102 get_angular_only() const {
00103 
00104   return _constraint->getAngularOnly();
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////
00108 //     Function: BulletHingeConstraint::set_limit
00109 //       Access: Published
00110 //  Description: Sets the lower and upper rotational limits in
00111 //               degrees.
00112 ////////////////////////////////////////////////////////////////////
00113 void BulletHingeConstraint::
00114 set_limit(PN_stdfloat low, PN_stdfloat high, PN_stdfloat softness, PN_stdfloat bias, PN_stdfloat relaxation) {
00115 
00116   low  = deg_2_rad(low);
00117   high = deg_2_rad(high);
00118 
00119   _constraint->setLimit(low, high, softness, bias, relaxation);
00120 }
00121 
00122 ////////////////////////////////////////////////////////////////////
00123 //     Function: BulletHingeConstraint::set_axis
00124 //       Access: Published
00125 //  Description: Sets the hinge's rotation axis in world
00126 //               coordinates.
00127 ////////////////////////////////////////////////////////////////////
00128 void BulletHingeConstraint::
00129 set_axis(const LVector3 &axis) {
00130 
00131   nassertv(!axis.is_nan());
00132 
00133   btVector3 v = LVecBase3_to_btVector3(axis);
00134   _constraint->setAxis(v);
00135 }
00136 
00137 ////////////////////////////////////////////////////////////////////
00138 //     Function: BulletHingeConstraint::get_lower_limit
00139 //       Access: Published
00140 //  Description: Returns the lower angular limit in degrees.
00141 ////////////////////////////////////////////////////////////////////
00142 PN_stdfloat BulletHingeConstraint::
00143 get_lower_limit() const {
00144 
00145   return rad_2_deg(_constraint->getLowerLimit());
00146 }
00147 
00148 ////////////////////////////////////////////////////////////////////
00149 //     Function: BulletHingeConstraint::get_upper_limit
00150 //       Access: Published
00151 //  Description: Returns the upper angular limit in degrees.
00152 ////////////////////////////////////////////////////////////////////
00153 PN_stdfloat BulletHingeConstraint::
00154 get_upper_limit() const {
00155 
00156   return rad_2_deg(_constraint->getUpperLimit());
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: BulletHingeConstraint::get_hinge_angle
00161 //       Access: Published
00162 //  Description: Returns the angle between node_a and node_b in
00163 //               degrees.
00164 ////////////////////////////////////////////////////////////////////
00165 PN_stdfloat BulletHingeConstraint::
00166 get_hinge_angle() {
00167 
00168   return rad_2_deg(_constraint->getHingeAngle());
00169 }
00170 
00171 ////////////////////////////////////////////////////////////////////
00172 //     Function: BulletHingeConstraint::enable_angular_motor
00173 //       Access: Published
00174 //  Description: Applies an impulse to the constraint so that the
00175 //               angle changes at target_velocity (probably
00176 //               degrees/second) where max_impulse is the maximum
00177 //               impulse that is used for achieving the specified
00178 //               velocity.
00179 ////////////////////////////////////////////////////////////////////
00180 void BulletHingeConstraint::
00181 enable_angular_motor(bool enable, PN_stdfloat target_velocity, PN_stdfloat max_impulse) {
00182 
00183   _constraint->enableAngularMotor(enable, target_velocity, max_impulse);
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function: BulletHingeConstraint::enable_motor
00188 //       Access: Published
00189 //  Description:
00190 ////////////////////////////////////////////////////////////////////
00191 void BulletHingeConstraint::
00192 enable_motor(bool enable) {
00193 
00194   _constraint->enableMotor(enable);
00195 }
00196 
00197 ////////////////////////////////////////////////////////////////////
00198 //     Function: BulletHingeConstraint::set_max_motor_impulse
00199 //       Access: Published
00200 //  Description: Sets the maximum impulse used to achieve the
00201 //               velocity set in enable_angular_motor.
00202 ////////////////////////////////////////////////////////////////////
00203 void BulletHingeConstraint::
00204 set_max_motor_impulse(PN_stdfloat max_impulse) {
00205 
00206   _constraint->setMaxMotorImpulse(max_impulse);
00207 }
00208 
00209 ////////////////////////////////////////////////////////////////////
00210 //     Function: BulletHingeConstraint::set_motor_target
00211 //       Access: Published
00212 //  Description:
00213 ////////////////////////////////////////////////////////////////////
00214 void BulletHingeConstraint::
00215 set_motor_target(const LQuaternion &quat, PN_stdfloat dt) {
00216 
00217   _constraint->setMotorTarget(LQuaternion_to_btQuat(quat), dt);
00218 }
00219 
00220 ////////////////////////////////////////////////////////////////////
00221 //     Function: BulletHingeConstraint::set_motor_target
00222 //       Access: Published
00223 //  Description:
00224 ////////////////////////////////////////////////////////////////////
00225 void BulletHingeConstraint::
00226 set_motor_target(PN_stdfloat target_angle, PN_stdfloat dt) {
00227 
00228   _constraint->setMotorTarget(target_angle, dt);
00229 }
00230 
 All Classes Functions Variables Enumerations