From fe45a9f166a8bdf7ac351398eee2c67e10038155 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Tue, 9 Feb 2021 19:35:49 -0800 Subject: [PATCH] small amount of cleanup, nphysics also seems to be getting phased out for rapier --- Cargo.toml | 2 +- src/framework.rs | 51 -------------- src/main.rs | 179 +++++++++++++++++++++++++++++++---------------- 3 files changed, 118 insertions(+), 114 deletions(-) delete mode 100644 src/framework.rs diff --git a/Cargo.toml b/Cargo.toml index 6cf16d5..f27e9b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,4 @@ wgpu-subscriber = "0.1.0" tobj = "2.0.3" legion = "0.3.1" nalgebra = "0.24.1" -nphysics3d = "0.19.0" \ No newline at end of file +rapier3d = { version = "0.5.0", features = [ "simd-nightly", "parallel" ] } \ No newline at end of file diff --git a/src/framework.rs b/src/framework.rs deleted file mode 100644 index 8ec039f..0000000 --- a/src/framework.rs +++ /dev/null @@ -1,51 +0,0 @@ - - -/* -pub trait Runtime: 'static + Sized { - fn optional_features() -> wgpu::Features { - wgpu::Features::empty() - } - fn required_features() -> wgpu::Features { - wgpu::Features::empty() - } - fn required_limits() -> wgpu::Limits { - wgpu::Limits::default() - } - fn init( - sc_desc: &wgpu::SwapChainDescriptor, - device: &wgpu::Device, - queue: &wgpu::Queue, - ) -> Self; - fn resize( - &mut self, - sc_desc: &wgpu::SwapChainDescriptor, - device: &wgpu::Device, - queue: &wgpu::Queue, - ); - fn update(&mut self, event: WindowEvent); - fn render( - &mut self, - frame: &wgpu::SwapChainTexture, - device: &wgpu::Device, - queue: &wgpu::Queue, - spawner: &impl LocalSpawn, - ); -} -*/ - -/* -#[cfg(not(target_arch = "wasm32"))] -pub fn run(title: &str) { - let setup = futures::executor::block_on(setup::(title)); - start::(setup); -} - -#[cfg(target_arch = "wasm32")] -pub fn run(title: &str) { - let title = title.to_owned(); - wasm_bindgen_futures::spawn_local(async move { - let setup = setup::(&title).await; - start::(setup); - }); -} -*/ \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5b07314..f119523 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -extern crate nphysics3d; + extern crate tobj; extern crate winit; @@ -12,12 +12,6 @@ use bytemuck::__core::ops::Range; use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3, SquareMatrix}; use futures::task::LocalSpawn; use legion::*; -use nphysics3d::math::Inertia; -use nphysics3d::math::Velocity as VelocityN; -use nphysics3d::nalgebra::{Isometry3, Matrix3, Matrix4, Point3, Point4, Vector3, Vector4}; -use nphysics3d::ncollide3d::pipeline::CollisionGroups; -use nphysics3d::ncollide3d::shape::{Ball, ShapeHandle}; -use nphysics3d::object::{BodyStatus, ColliderDesc, RigidBodyDesc, DefaultBodySet, DefaultColliderSet, BodyPartHandle}; use wgpu::{BindGroup, Buffer, TextureView}; use wgpu_subscriber; use winit::{ @@ -26,13 +20,13 @@ use winit::{ }; use winit::event::DeviceEvent::MouseMotion; use winit::platform::unix::x11::ffi::Time; - +use rapier3d::math; use crate::render::Renderer; -use nphysics3d::world::{DefaultMechanicalWorld, DefaultGeometricalWorld}; -use nphysics3d::joint::DefaultJointConstraintSet; -use nphysics3d::force_generator::DefaultForceGeneratorSet; +use rapier3d::pipeline::PhysicsPipeline; +use rapier3d::dynamics::{IntegrationParameters, RigidBodySet, JointSet, RigidBodyBuilder}; +use rapier3d::geometry::{BroadPhase, NarrowPhase, ColliderSet, ColliderBuilder}; +use rapier3d::na::{Isometry, Isometry3, Vector, Vector3}; -mod framework; mod geometry; mod light; mod render; @@ -177,6 +171,7 @@ fn main() { window.request_redraw(); last_update_inst = Instant::now(); } + physics(); pool.run_until_stalled(); } event::Event::DeviceEvent { @@ -234,58 +229,118 @@ fn main() { pub fn physics() { - let mut mechanical_world = DefaultMechanicalWorld::new(Vector3::new(0.0, -9.81, 0.0)); - let mut geometrical_world = DefaultGeometricalWorld::new(); - - let mut bodies = DefaultBodySet::new(); - let mut colliders = DefaultColliderSet::new(); - let mut joint_constraints = DefaultJointConstraintSet::new(); - let mut force_generators = DefaultForceGeneratorSet::new(); - - // Run the simulation. - mechanical_world.step( - &mut geometrical_world, - &mut bodies, - &mut colliders, - &mut joint_constraints, - &mut force_generators - ); - - let rigid_body = - RigidBodyDesc::new() - // The rigid body position. Will override `.translation(...)` and `.rotation(...)`. - .position(Isometry3::new( - Vector3::new(1.0, 2.0, 3.0), - Vector3::y() * PI, - )) - .gravity_enabled(true) - .status(BodyStatus::Dynamic) - .velocity(VelocityN::linear(1.0, 2.0, 3.0)) - .linear_damping(10.0) - .angular_damping(5.0) - .max_linear_velocity(10.0) - .max_angular_velocity(1.7) - .mass(1.2) - // Arbitrary user-defined data associated to the rigid body to be built. - .user_data(10) - .build(); - - - // let parent_rigid_body = RigidBodyDesc::new() - // .build(); - let parent_handle = bodies.insert(rigid_body); - - let shape = ShapeHandle::new(Ball::new(1.5)); - let collider = ColliderDesc::new(shape) - .density(1.0) - .translation(Vector3::y() * 5.0) - .build(BodyPartHandle(parent_handle, 0)); - let collider_handle = colliders.insert(collider); - - // let ball = ShapeHandle::new(Ball::new(1.5)); +/* // let mut mechanical_world = DefaultMechanicalWorld::new(Vector3::new(0.0, -9.81, 0.0)); + // let mut geometrical_world = DefaultGeometricalWorld::new(); + // + // let mut bodies = DefaultBodySet::new(); + // let mut colliders = DefaultColliderSet::new(); + // let mut joint_constraints = DefaultJointConstraintSet::new(); + // let mut force_generators = DefaultForceGeneratorSet::new(); + // + // // Run the simulation. + // mechanical_world.step( + // &mut geometrical_world, + // &mut bodies, + // &mut colliders, + // &mut joint_constraints, + // &mut force_generators + // ); + // + // let ground_handle = bodies.insert(Ground::new()); + // let ground_shape = ShapeHandle::new(Cuboid::new(Vector3::new(3.0, 0.2, 3.0) + // )); // - // let collider = ColliderDesc::new(rigid_body) + // let main_floor = ColliderDesc::new(ground_shape) + // .translation(Vector3::y() * -0.2) + // .build(BodyPartHandle(ground_handle, 0)); + // colliders.insert(main_floor); + // + // // Build the rigid body. + // let rb = RigidBodyDesc::new() + // .translation(Vector3::new(1.0, 1.0, 1.0)) // .build(); + // let rb_handle = bodies.insert(rb); + // + // // Build the collider. + // let cuboid = ShapeHandle::new(Cuboid::new(Vector3::new(1.0, 1.0, 1.0))); + // let co = ColliderDesc::new(cuboid.clone()) + // .density(1.0) + // .build(BodyPartHandle(rb_handle, 0)); + // colliders.insert(co); + // + // //testbed.set_body_color(rb_handle, color); + // + // let rigid_body = + // RigidBodyDesc::new() + // // The rigid body position. Will override `.translation(...)` and `.rotation(...)`. + // .position(Isometry3::new( + // Vector3::new(1.0, 2.0, 3.0), + // Vector3::y() * PI, + // )) + // .gravity_enabled(true) + // .status(BodyStatus::Dynamic) + // .velocity(VelocityN::linear(1.0, 2.0, 3.0)) + // .linear_damping(10.0) + // .angular_damping(5.0) + // .max_linear_velocity(10.0) + // .max_angular_velocity(1.7) + // .mass(1.2) + // // Arbitrary user-defined data associated to the rigid body to be built. + // .user_data(10) + // .build(); + // + // let parent_handle = bodies.insert(rigid_body); + // + // let shape = ShapeHandle::new(Ball::new(1.5)); + // let collider = ColliderDesc::new(shape) + // .density(1.0) + // .translation(Vector3::y() * 5.0) + // .build(BodyPartHandle(parent_handle, 0)); + // let collider_handle = colliders.insert(collider);*/ + + // Here the gravity is -9.81 along the y axis. + let mut pipeline = PhysicsPipeline::new(); + let gravity = rapier3d::math::Vector::new(0.0, -9.81, 0.0); + let integration_parameters = IntegrationParameters::default(); + let mut broad_phase = BroadPhase::new(); + let mut narrow_phase = NarrowPhase::new(); + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let mut joints = JointSet::new(); + // We ignore contact events for now. + let event_handler = (); + + let mut dynamic_ball_body = RigidBodyBuilder::new_dynamic().build(); + let mut dynamic_ball_body_handle = bodies.insert(dynamic_ball_body); + + let mut static_floor_body = RigidBodyBuilder::new_static().position(Isometry3::new( + Vector3::new(1.0, 2.0, 3.0), + Vector::y() * PI, + )).build(); + let mut static_floor_body_handle = bodies.insert(static_floor_body); + + + let ball_collider = ColliderBuilder::ball(0.5).build(); + let floor_collider = ColliderBuilder::cuboid(0.5, 0.2, 0.1).build(); + + let ball_collider_handle = colliders.insert(ball_collider, dynamic_ball_body_handle, &mut bodies); + let floor_collider_handle = colliders.insert(floor_collider, dynamic_ball_body_handle, &mut bodies); + + // Run the simulation in the game loop. + loop { + pipeline.step( + &gravity, + &integration_parameters, + &mut broad_phase, + &mut narrow_phase, + &mut bodies, + &mut colliders, + &mut joints, + None, + None, + &event_handler + ); + } } pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {