Skip to content

Commit 56c5eb1

Browse files
author
fedor
committed
bouncing sphere
1 parent 2d8665e commit 56c5eb1

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

examples/sphere.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use cgmath::{Vector3, Vector4};
55

66
use bulletrs::*;
77

8-
98
fn main() {
109
let configuration = CollisionConfiguration::new_default();
1110

@@ -18,20 +17,32 @@ fn main() {
1817

1918
dynamics_world.set_gravity(Vector3::new(0.0, -10.0, 0.0));
2019

20+
let ground_shape = Shape::new_plane(Vector3::new(0.0, 1.0, 0.0), -2.0);
21+
let ground_rigid_body = RigidBody::new(
22+
0.0,
23+
Vector3::new(0.0, 0.0, 0.0),
24+
ground_shape,
25+
Vector3::new(0.0, 0.0, 0.0),
26+
Vector4::new(0.0, 0.0, 0.0, 1.0),
27+
);
28+
ground_rigid_body.set_restitution(0.95);
29+
30+
dynamics_world.add_rigid_body(&ground_rigid_body);
31+
2132
let fall_shape = Shape::new_sphere(2.0);
22-
let mass = 1.0;
23-
let fall_inertia = fall_shape.calculate_local_inertia(mass);
33+
let mass = 0.1;
2434
let fall_rigid_body = RigidBody::new(
2535
mass,
36+
fall_shape.calculate_local_inertia(mass),
2637
fall_shape,
27-
fall_inertia,
2838
Vector3::new(0.0, 5.0, 0.0),
2939
Vector4::new(0.0, 0.0, 0.0, 1.0),
3040
);
3141
dynamics_world.add_rigid_body(&fall_rigid_body);
42+
fall_rigid_body.set_restitution(0.9);
3243

3344
for _ in 0 .. 100 {
34-
dynamics_world.step(0.01, 0, 0.01);
45+
dynamics_world.step(0.1, 0, 0.0);
3546
println!("{:?}", fall_rigid_body.get_world_transform());
3647
}
3748
}

src/collision/collision_shapes/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
use sys;
22
use std::mem;
3-
3+
use bullet_vector3::BulletVector3;
44
use mint::Vector3;
55

66
pub enum Shape {
77
Sphere(sys::btSphereShape),
8+
Plane(sys::btStaticPlaneShape),
89
}
910

1011
impl Shape {
1112
pub fn new_sphere(radius: f64) -> Shape {
1213
Shape::Sphere(unsafe { sys::btSphereShape::new(radius) })
1314
}
1415

16+
pub fn new_plane<T : Into<Vector3<f64>>>(normal : T, plane_const: f64) -> Shape {
17+
let up: BulletVector3 = normal.into().into();
18+
Shape::Plane(unsafe { sys::btStaticPlaneShape::new(up.0.as_ptr() as *const _, plane_const) })
19+
}
20+
21+
pub fn as_ptr(&self) -> *mut sys::btCollisionShape {
22+
match self {
23+
&Shape::Sphere(ref sphere) => sphere as *const _ as *mut _,
24+
&Shape::Plane(ref plane) => plane as *const _ as *mut _,
25+
}
26+
}
27+
1528
pub fn calculate_local_inertia(&self, mass: f64) -> Vector3<f64> {
1629
let mut inertia: [f64; 4] = unsafe { mem::uninitialized() };
1730
match self {
@@ -24,6 +37,16 @@ impl Shape {
2437
);
2538
}
2639
Vector3::from_slice(&inertia[0..3])
40+
},
41+
&Shape::Plane(ref sphere) => {
42+
unsafe {
43+
sys::btStaticPlaneShape_calculateLocalInertia(
44+
sphere as *const _ as *mut _,
45+
mass,
46+
inertia.as_mut_ptr() as *mut _,
47+
);
48+
}
49+
Vector3::from_slice(&inertia[0..3])
2750
}
2851
}
2952
}

src/dynamics/rigid_body.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ pub struct RigidBody {
1414
impl RigidBody {
1515
pub fn new<T1: Into<Vector3<f64>>, T2: Into<Vector3<f64>>, T3: Into<Vector4<f64>>>(
1616
mass: f64,
17-
shape: Shape,
1817
inertia: T1,
18+
shape: Shape,
1919
translation: T2,
2020
orientation: T3,
2121
) -> RigidBody {
2222
let mut inertia: BulletVector3 = inertia.into().into();
23-
let mut shape_box = Box::new(shape);
23+
let shape_box = Box::new(shape);
2424
let translation: BulletVector3 = translation.into().into();
2525
let orientation: [f64; 4] = orientation.into().into();
2626
let transform = unsafe {
@@ -37,7 +37,7 @@ impl RigidBody {
3737
sys::btRigidBody_btRigidBodyConstructionInfo::new(
3838
mass,
3939
&mut *motion_state_box as *mut _ as *mut _,
40-
&mut *shape_box as *mut _ as *mut _,
40+
shape_box.as_ptr(),
4141
inertia.0.as_mut_ptr() as *mut _,
4242
)
4343
});
@@ -52,10 +52,13 @@ impl RigidBody {
5252
}
5353
}
5454

55-
pub fn as_ptr(&self) -> *mut sys::btRigidBody {
55+
pub(crate) unsafe fn as_ptr(&self) -> *mut sys::btRigidBody {
5656
&*self.rigid_body as *const _ as *mut _
5757
}
5858

59+
pub fn set_restitution(&self, restitution : f64) {
60+
unsafe { sys::btCollisionObject_setRestitution(self.as_ptr() as *mut _, restitution); }
61+
}
5962

6063
pub fn get_world_transform(&self) -> Vector3<f64> {
6164
unsafe {

0 commit comments

Comments
 (0)