Skip to content

Commit

Permalink
Refactoring and reformating
Browse files Browse the repository at this point in the history
  • Loading branch information
twitu committed Jun 22, 2020
1 parent d589d5f commit 355e742
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
23 changes: 10 additions & 13 deletions src/boid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use ncollide3d::nalgebra::clamp;
use ncollide3d::nalgebra::geometry::UnitQuaternion;
use ncollide3d::nalgebra::{Point3, Vector3, Vector4};
use ncollide3d::query::{Ray, RayCast};
use ncollide3d::shape::{Ball, Cylinder, Plane};
use rand::{thread_rng, Rng};
use rand_distr::{Distribution, UnitSphere};
use ncollide3d::shape::{Cylinder, Plane, Ball};

// Scaling factors
const TIME_SCALE: f32 = 1.0;
Expand Down Expand Up @@ -109,10 +109,7 @@ impl Boid {
// It is possible that there is no unobstructed direction
// but that is highly unlikely and possible only when the boid
// is entirely surrounded by obstacles.
match best_dir {
Some(dir) => Some(rot * dir),
None => None
}
best_dir.map(|dir: &Vector3<f32>| rot * dir)

This comment has been minimized.

Copy link
@pickfire

pickfire Jun 23, 2020

But this type is most likely not necessary, didn't test.

This comment has been minimized.

Copy link
@twitu

twitu Jun 23, 2020

Author Owner

Explicitly mentioning type is not necessary in most places. However since this more like a small toy project, I'm writing it down where I feel like. But this brings up an interesting question, is there any standard/convention that states where to and not to explicitly mention types? Where to balance between readability and excess information?

This comment has been minimized.

Copy link
@pickfire

pickfire Jun 23, 2020

I never seen any standards or convention regrading this but usually I never write types if it is not necessary since rust-analyzer can have inlay hints to show it, but still I disabled the inlay hints because it was blocking my view.

So my take is write it only if it's necessary like it won't compile or you definitely won't understand it without writing it down. This case looks simple enough to understand that.

}

/// calculate clamped acceleration in the direction of `vel`
Expand Down Expand Up @@ -260,30 +257,30 @@ pub fn create_obstacles() -> Vec<(Box<dyn RayCast<f32> + Sync>, Isometry<f32>)>
// create obstacles
let mut obstacles: Vec<(Box<dyn RayCast<f32> + Sync>, Isometry<f32>)> = Vec::new();

for (norm, (x, y, z)) in [
let cylinders = [
(Vector3::x_axis(), (-30.0f32, 0.0, 0.0)),
(-Vector3::x_axis(), (30.0, 0.0, 0.0)),
(Vector3::y_axis(), (0.0, -30.0, 0.0)),
(-Vector3::y_axis(), (0.0, 30.0, 0.0)),
(Vector3::z_axis(), (0.0, 0.0, -30.0)),
(-Vector3::z_axis(), (0.0, 0.0, 30.0)),
]
.iter()
{
];

for (norm, (x, y, z)) in cylinders.iter() {
obstacles.push((
Box::new(Plane::new(*norm)),
Isometry::translation(*x, *y, *z),
));
}

for (x, y, z) in [
let walls = [
(20.0f32, -5.0, 20.0),
(20.0, -5.0, -20.0),
(-20.0, -5.0, -20.0),
(-20.0, -5.0, 20.0),
]
.iter()
{
];

for (x, y, z) in walls.iter() {
obstacles.push((
Box::new(Cylinder::new(25.0, 4.0)),
Isometry::translation(*x, *y, *z),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod boid;
pub mod boid;
22 changes: 8 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ncollide3d::nalgebra::geometry::UnitQuaternion;
use ncollide3d::nalgebra::{Vector3, Vector4};
use rayon::prelude::*;
use three;
use three::{Mesh, Object};
use ncollide3d::nalgebra::geometry::UnitQuaternion;
use ncollide3d::nalgebra::{Vector3, Vector4};

mod boid;
use boid::Boid;
Expand All @@ -15,10 +15,10 @@ const ORIGIN: [f32; 3] = [0.0, 0.0, 0.0];
const SPAWN_CENTRE: [f32; 3] = [0.0, 0.0, 0.0];
const SPAWN_RADIUS: f32 = 6.0;
const SPAWN_NUMBER: usize = 500;
const SPAWN_COLOURS: [u32; 3] = [ 0xFFA500 , 0x8E44AD, 0x1C2833 ];
const SPAWN_COLOURS: [u32; 3] = [0xFFA500, 0x8E44AD, 0x1C2833];
const FPS: u32 = 60;
const DELTA: f32 = 1.0 / FPS as f32;
const CAMERA_ROTATION: f32 = DELTA * std::f32::consts::PI / 20.0; // 18 degrees per second
const CAMERA_ROTATION: f32 = DELTA * std::f32::consts::PI / 20.0; // 18 degrees per second

fn main() {
// add window
Expand Down Expand Up @@ -68,9 +68,7 @@ fn main() {
boids
.par_iter_mut()
.enumerate()
.for_each(|(i, b): (usize, &mut Boid)| {
b.frame_update(i, &copy, &obstacles, DELTA)
});
.for_each(|(i, b): (usize, &mut Boid)| b.frame_update(i, &copy, &obstacles, DELTA));
boids
.iter()
.zip(cones.iter())
Expand Down Expand Up @@ -110,7 +108,6 @@ fn add_lighting_to_scene(win: &mut three::Window) {
}

fn add_objects_to_scene(win: &mut three::Window) {

// glass box
let object = {
let geometry = three::Geometry::cuboid(60.0, 60.0, 60.0);
Expand All @@ -131,13 +128,10 @@ fn add_objects_to_scene(win: &mut three::Window) {
win.factory.mesh(geometry, material)
};
// set rotation -90 degrees about x-axis
let rot = *UnitQuaternion::from_axis_angle(&Vector3::x_axis(), -std::f32::consts::FRAC_PI_2).as_vector();
let rot = *UnitQuaternion::from_axis_angle(&Vector3::x_axis(), -std::f32::consts::FRAC_PI_2)
.as_vector();
let rot: [f32; 4] = rot.into();
object.set_transform(
[0.0, -30.0, 0.0],
rot,
1.0,
);
object.set_transform([0.0, -30.0, 0.0], rot, 1.0);
win.scene.add(&object);

// tree trunks
Expand Down

0 comments on commit 355e742

Please sign in to comment.