Skip to content

Instantly share code, notes, and snippets.

@gmorenz
Created May 8, 2016 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmorenz/8243b5995cee62eddd080456ca7041fb to your computer and use it in GitHub Desktop.
Save gmorenz/8243b5995cee62eddd080456ca7041fb to your computer and use it in GitHub Desktop.
extern crate nphysics3d as np;
extern crate ncollide as nc;
extern crate nalgebra as na;
use na::Vector3;
use nc::shape::Ball;
use nc::shape::Cuboid;
use np::world::World as WorldGeneric;
use np::object::{RigidBodyHandle as RigidBodyHandleGeneric, RigidBody as RigidBodyGeneric};
type World = WorldGeneric<f32>;
type RigidBodyHandle = RigidBodyHandleGeneric<f32>;
type RigidBody = RigidBodyGeneric<f32>;
const BALL_RADIUS: f32 = 20.;
const BALL_RESTITUTION: f32 = 1.0;
const BALL_FRICTION: f32 = 0.0;
const INITIAL_SPEED: f32 = 20.0;
fn ball(world: &mut World) -> RigidBodyHandle {
let col_ball = Ball::new(BALL_RADIUS);
let mut rb = RigidBody::new_dynamic(col_ball,
1.0f32 / np::volumetric::ball_volume(3, BALL_RADIUS),
BALL_RESTITUTION,
BALL_FRICTION);
rb.apply_central_impulse(Vector3::new(0.0, INITIAL_SPEED, 0.0));
world.add_rigid_body(rb)
}
const PADDLE_SIZE: Vector3<f32> = Vector3{ x: 10000.0, y: 10., z: 10000. };
const PADDLE_FRICTION: f32 = 0.0;
const PADDLE_RESTITUTION: f32 = 1.0;
const PADDLE_DISTANCE: f32 = 100.;
fn paddle(world: &mut World, side: f32) {
let s = Cuboid::new(PADDLE_SIZE);
let mut rb = RigidBody::new_static(s, PADDLE_RESTITUTION, PADDLE_FRICTION);
rb.set_translation(Vector3::new(0.0, PADDLE_DISTANCE * side, 0.0));
world.add_rigid_body(rb);
}
fn main() {
let mut world = World::new();
paddle(&mut world, 1.);
paddle(&mut world, -1.);
let ball = ball(&mut world);
let mut vels = vec!();
for _ in 0.. 1_000_000 {
world.step(0.01);
vels.push(((ball.borrow().lin_vel(), ball.borrow().ang_vel()), ball.borrow().position().translation));
}
// Dedup while keeping count of elements, start and end positions
let mut vels_dedup = vec!();
let mut vels_iter = vels.into_iter();
let (mut vel_cur, mut pos_start) = vels_iter.next().unwrap();
let mut i = 1;
for (vel, pos) in vels_iter {
if vel == vel_cur {
i+=1;
}
else {
vels_dedup.push((i, vel_cur, pos_start, pos));
vel_cur = vel;
pos_start = pos;
i = 1;
}
}
// We lose the last vel/pos set... which is fine since we don't have a sane end position anyways.
for (i, vel, start_pos, end_pos) in vels_dedup {
println!("Timesteps collapsed {}\n\
\tvelocity: {}\n\
\tangular velocity: {}\n\
\tstart pos: {}\n\
\tend pos: {}", i, vel.0, vel.1, start_pos, end_pos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment