|
|
|
@ -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) {
|
|
|
|
|