Skip to content

Commit d72d4e5

Browse files
authored
Merge pull request #8 from jice-nospam/master
add RigidBodyHandle.reset_base_velocity
2 parents 25672ca + 75e2b97 commit d72d4e5

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target/
22
Cargo.lock
3+
.vscode/

src/command.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub enum Command {
2828
GetBasePositionAndOrientation(RigidBodyHandle),
2929
ResetBasePositionAndOrientation(RigidBodyHandle, Point3<f64>, Vector4<f64>),
3030

31+
ResetBaseVelocity(RigidBodyHandle, Option<Vector3<f64>>, Option<Vector3<f64>>),
32+
3133
SetAngularFactor(RigidBodyHandle, Vector3<f64>),
3234

3335
ApplyCentralImpulse(RigidBodyHandle, Vector3<f64>),
@@ -118,7 +120,7 @@ impl Command {
118120
);
119121
}
120122

121-
// with USE_MAXIMAL_COORDINATES physics client creates btRigidBody instead of btMultiBody
123+
// with USE_MAXIMAL_COORDINATES physics client creates btRigidBody instead of btMultiBody
122124
unsafe {
123125
::sys::b3CreateMultiBodyUseMaximalCoordinates(command);
124126
}
@@ -253,6 +255,23 @@ impl Command {
253255
CommandHandle { handle: command }
254256
}
255257

258+
&Command::ResetBaseVelocity(ref body, lin_vel_opt, ang_vel_opt) => {
259+
let command = unsafe {::sys::b3CreatePoseCommandInit(client.handle, body.unique_id)};
260+
if let Some(lin_vel) = lin_vel_opt {
261+
let mut lin_vel_arr: [f64; 3] = lin_vel.into();
262+
unsafe {
263+
::sys::b3CreatePoseCommandSetBaseLinearVelocity(command, lin_vel_arr.as_mut_ptr());
264+
}
265+
}
266+
if let Some(ang_vel) = ang_vel_opt {
267+
let mut ang_vel_arr: [f64; 3] = ang_vel.into();
268+
unsafe {
269+
::sys::b3CreatePoseCommandSetBaseAngularVelocity(command, ang_vel_arr.as_mut_ptr());
270+
}
271+
}
272+
CommandHandle { handle: command }
273+
}
274+
256275
&Command::SetAngularFactor(ref body, factor) => {
257276
let mut factor: [f64; 3] = factor.into();
258277
let command = unsafe {

src/rigidbody.rs

+14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ impl RigidBodyHandle {
4848
)
4949
}
5050

51+
/// You can reset the linear and/or angular velocity of the base of a body using reset_base_velocity.
52+
pub fn reset_base_velocity(&self, lin_vel: Option<Vector3<f64>>, ang_vel: Option<Vector3<f64>>) -> Result<(), Error> {
53+
self.is_removed()?;
54+
55+
self.client_handle.submit_client_command_and_wait_status(
56+
&Command::ResetBaseVelocity(
57+
self.clone(),
58+
lin_vel,
59+
ang_vel,
60+
),
61+
);
62+
Ok(())
63+
}
64+
5165
pub fn set_angular_factor(&self, factor: Vector3<f64>) -> Result<(), Error> {
5266
self.is_removed()?;
5367

tests/rigid_body.rs

+28
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,31 @@ fn get_actual_state() {
2929
assert_eq!(velocity, Vector3::from([0.0, 0.0, 0.0]));
3030
}
3131

32+
#[test]
33+
fn reset_base_velocity() {
34+
let bullet = Bullet::connect(ConnectMethod::Direct).unwrap();
35+
let client = bullet.physics_client_handle();
36+
37+
client.reset_simulation();
38+
39+
let sphere_shape = client
40+
.create_collision_shape(ShapeType::Sphere { radius: 0.1 })
41+
.unwrap();
42+
43+
let body = client
44+
.create_rigid_body(
45+
sphere_shape.clone(),
46+
0.1,
47+
Vector3::from([1.0, 2.0, 3.0]),
48+
Vector4::from([0.0, 0.0, 0.0, 1.0]),
49+
)
50+
.unwrap();
51+
52+
let velocity = body.get_linear_velocity().unwrap();
53+
assert_eq!(velocity, Vector3::from([0.0, 0.0, 0.0]));
54+
55+
body.reset_base_velocity(Some(Vector3::from([1.0,2.0,3.0])),None).unwrap();
56+
let new_velocity = body.get_linear_velocity().unwrap();
57+
assert_eq!(new_velocity, Vector3::from([1.0, 2.0, 3.0]));
58+
59+
}

0 commit comments

Comments
 (0)