Panda3D
bulletVehicle.cxx
1 // Filename: bulletVehicle.cxx
2 // Created by: enn0x (16Feb10)
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 "bulletVehicle.h"
16 #include "bulletWorld.h"
17 #include "bulletRigidBodyNode.h"
18 #include "bulletWheel.h"
19 
20 TypeHandle BulletVehicle::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: BulletVehicle::Constructor
24 // Access: Published
25 // Description: Creates a new BulletVehicle instance in the given
26 // world and with a chassis node.
27 ////////////////////////////////////////////////////////////////////
30 
31  btRigidBody *body = btRigidBody::upcast(chassis->get_object());
32 
33  _raycaster = new btDefaultVehicleRaycaster(world->get_world());
34  _vehicle = new btRaycastVehicle(_tuning._, body, _raycaster);
35 
36  set_coordinate_system(get_default_up_axis());
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: BulletVehicle::set_coordinate_system
41 // Access: Published
42 // Description: Specifies which axis is "up". Nessecary for the
43 // vehicle's suspension to work properly!
44 ////////////////////////////////////////////////////////////////////
45 void BulletVehicle::
46 set_coordinate_system(BulletUpAxis up) {
47 
48  switch (up) {
49  case X_up:
50  _vehicle->setCoordinateSystem(1, 0, 2);
51  break;
52  case Y_up:
53  _vehicle->setCoordinateSystem(0, 1, 2);
54  break;
55  case Z_up:
56  _vehicle->setCoordinateSystem(0, 2, 1);
57  break;
58  default:
59  bullet_cat.error() << "invalid up axis:" << up << endl;
60  break;
61  }
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: BulletVehicle::get_forward_vector
66 // Access: Published
67 // Description: Returns the forward vector representing the car's
68 // actual direction of movement. The forward vetcor
69 // is given in global coordinates.
70 ////////////////////////////////////////////////////////////////////
73 
74  return btVector3_to_LVector3(_vehicle->getForwardVector());
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function: BulletVehicle::get_chassis
79 // Access: Published
80 // Description: Returns the chassis of this vehicle. The chassis
81 // is a rigid body node.
82 ////////////////////////////////////////////////////////////////////
85 
86  btRigidBody *bodyPtr = _vehicle->getRigidBody();
87  return (bodyPtr) ? (BulletRigidBodyNode *)bodyPtr->getUserPointer() : NULL;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: BulletVehicle::get_current_speed_km_hour
92 // Access: Published
93 // Description: Returns the current speed in kilometers per hour.
94 // Convert to miles using: km/h * 0.62 = mph
95 ////////////////////////////////////////////////////////////////////
96 PN_stdfloat BulletVehicle::
98 
99  return (PN_stdfloat)_vehicle->getCurrentSpeedKmHour();
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: BulletVehicle::reset_suspension
104 // Access: Published
105 // Description: Resets the vehicle's suspension.
106 ////////////////////////////////////////////////////////////////////
107 void BulletVehicle::
109 
110  _vehicle->resetSuspension();
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: BulletVehicle::get_steering_value
115 // Access: Published
116 // Description: Returns the steering angle of the wheel with index
117 // idx in degrees.
118 ////////////////////////////////////////////////////////////////////
119 PN_stdfloat BulletVehicle::
120 get_steering_value(int idx) const {
121 
122  nassertr(idx < get_num_wheels(), 0.0f);
123  return rad_2_deg(_vehicle->getSteeringValue(idx));
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: BulletVehicle::set_steering_value
128 // Access: Published
129 // Description: Sets the steering value (in degrees) of the wheel
130 // with index idx.
131 ////////////////////////////////////////////////////////////////////
132 void BulletVehicle::
133 set_steering_value(PN_stdfloat steering, int idx) {
134 
135  nassertv(idx < get_num_wheels());
136  _vehicle->setSteeringValue(deg_2_rad(steering), idx);
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: BulletVehicle::apply_engine_force
141 // Access: Published
142 // Description: Applies force at the wheel with index idx for
143 // acceleration.
144 ////////////////////////////////////////////////////////////////////
145 void BulletVehicle::
146 apply_engine_force(PN_stdfloat force, int idx) {
147 
148  nassertv(idx < get_num_wheels());
149  _vehicle->applyEngineForce(force, idx);
150 }
151 
152 ////////////////////////////////////////////////////////////////////
153 // Function: BulletVehicle::set_brake
154 // Access: Published
155 // Description: Applies braking force to the wheel with index idx.
156 ////////////////////////////////////////////////////////////////////
157 void BulletVehicle::
158 set_brake(PN_stdfloat brake, int idx) {
159 
160  nassertv(idx < get_num_wheels());
161  _vehicle->setBrake(brake, idx);
162 }
163 
164 ////////////////////////////////////////////////////////////////////
165 // Function: BulletVehicle::set_pitch_control
166 // Access: Published
167 // Description:
168 ////////////////////////////////////////////////////////////////////
169 void BulletVehicle::
170 set_pitch_control(PN_stdfloat pitch) {
171 
172  _vehicle->setPitchControl(pitch);
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: BulletVehicle::create_wheel
177 // Access: Published
178 // Description: Factory method for creating wheels for this
179 // vehicle instance.
180 ////////////////////////////////////////////////////////////////////
183 
184  btVector3 pos(0.0, 0.0, 0.0);
185  btVector3 direction = get_axis(_vehicle->getUpAxis());
186  btVector3 axle = get_axis(_vehicle->getRightAxis());
187 
188  btScalar suspension(0.4);
189  btScalar radius(0.3);
190 
191  btWheelInfo &info = _vehicle->addWheel(pos, direction, axle, suspension, radius, _tuning._, false);
192 
193  info.m_clientInfo = NULL;
194 
195  return BulletWheel(info);
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: BulletVehicle::get_axis
200 // Access: Private
201 // Description:
202 ////////////////////////////////////////////////////////////////////
203 btVector3 BulletVehicle::
204 get_axis(int idx) {
205 
206  switch (idx) {
207  case 0:
208  return btVector3(1.0, 0.0, 0.0);
209  case 1:
210  return btVector3(0.0, 1.0, 0.0);
211  case 2:
212  return btVector3(0.0, 0.0, 1.0);
213  default:
214  return btVector3(0.0, 0.0, 0.0);
215  }
216 }
217 
218 ////////////////////////////////////////////////////////////////////
219 // Function: BulletVehicle::get_wheel
220 // Access: Published
221 // Description: Returns the BulletWheel with index idx. Causes an
222 // AssertionError if idx is equal or larger than the
223 // number of wheels.
224 ////////////////////////////////////////////////////////////////////
226 get_wheel(int idx) const {
227 
228  nassertr(idx < get_num_wheels(), BulletWheel::empty());
229  return BulletWheel(_vehicle->getWheelInfo(idx));
230 }
231 
232 ////////////////////////////////////////////////////////////////////
233 // Function: BulletVehicle::sync_b2p
234 // Access: Public
235 // Description:
236 ////////////////////////////////////////////////////////////////////
237 void BulletVehicle::
238 sync_b2p() {
239 
240  for (int i=0; i < get_num_wheels(); i++) {
241  btWheelInfo info = _vehicle->getWheelInfo(i);
242 
243  PandaNode *node = (PandaNode *)info.m_clientInfo;
244  if (node) {
245 
246  CPT(TransformState) ts = btTrans_to_TransformState(info.m_worldTransform);
247 
248  // <A> Transform relative to wheel node's parent
249  //node->set_transform(ts);
250 
251  // <B> Transform absolute
252  NodePath np = NodePath::any_path(node);
253  np.set_transform(np.get_top(), ts);
254  }
255  }
256 }
257 
void reset_suspension()
Resets the vehicle&#39;s suspension.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
BulletWheel get_wheel(int idx) const
Returns the BulletWheel with index idx.
void set_coordinate_system(BulletUpAxis up)
Specifies which axis is "up".
BulletWheel create_wheel()
Factory method for creating wheels for this vehicle instance.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
BulletVehicle(BulletWorld *world, BulletRigidBodyNode *chassis)
Creates a new BulletVehicle instance in the given world and with a chassis node.
static BulletWheel empty()
Named constructor intended to be used for asserts with have to return a concrete value.
Definition: bulletWheel.I:42
LVector3 get_forward_vector() const
Returns the forward vector representing the car&#39;s actual direction of movement.
void set_transform(const TransformState *transform, Thread *current_thread=Thread::get_current_thread())
Changes the complete transform object on this node.
Definition: nodePath.I:718
static NodePath any_path(PandaNode *node, Thread *current_thread=Thread::get_current_thread())
Returns a new NodePath that represents any arbitrary path from the root to the indicated node...
Definition: nodePath.I:77
int get_num_wheels() const
Returns the number of wheels this vehicle has.
Definition: bulletVehicle.I:58
PN_stdfloat get_current_speed_km_hour() const
Returns the current speed in kilometers per hour.
void set_brake(PN_stdfloat brake, int idx)
Applies braking force to the wheel with index idx.
PN_stdfloat get_steering_value(int idx) const
Returns the steering angle of the wheel with index idx in degrees.
void set_steering_value(PN_stdfloat steering, int idx)
Sets the steering value (in degrees) of the wheel with index idx.
NodePath get_top(Thread *current_thread=Thread::get_current_thread()) const
Returns a singleton NodePath that represents the top of the path, or empty NodePath if this path is e...
Definition: nodePath.cxx:317
BulletRigidBodyNode * get_chassis()
Returns the chassis of this vehicle.
void apply_engine_force(PN_stdfloat force, int idx)
Applies force at the wheel with index idx for acceleration.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
One wheel of a BulletVehicle.
Definition: bulletWheel.h:57
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165