config works great, mesh colliders v mesh colliders are very sticky...

master
mitchellhansen 4 years ago
parent f4c6391e37
commit 2b77153f10

@ -9,16 +9,25 @@ type = "PhysicsEntity"
mesh = "ball.obj" mesh = "ball.obj"
[entities.position] [entities.position]
x = 0.0 x = 15.0
y = 0.0 y = 3.0
z = 0.0 z = 30.0
[[entities]] [[entities]]
name = "camera.1" name = "camera.1"
type = "Camera" type = "Camera"
[entities.position]
x = 15.0
y = 15.0
z = 15.0
[[entities]] [[entities]]
name = "light.1" name = "light.1"
type = "Light" type = "Light"
mesh = "light.obj" mesh = "light.obj"
[entities.position]
x = 20.0
y = 20.0
z = 20.0

@ -2,8 +2,8 @@
# Material Count: 1 # Material Count: 1
newmtl None newmtl None
Ns 0 Ns 500
Ka 0.000000 0.000000 0.000000 Ka 0.8 0.8 0.8
Kd 0.8 0.8 0.8 Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8 Ks 0.8 0.8 0.8
d 1 d 1

File diff suppressed because it is too large Load Diff

@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 500
Ka 0.8 0.8 0.8
Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8
d 1
illum 2

File diff suppressed because it is too large Load Diff

@ -194,7 +194,7 @@ fn main() {
// Physics // Physics
let (physics_state, physics_pipeline) = let (physics_state, physics_pipeline) =
PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.05)); PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.0));
resources.insert(physics_state); resources.insert(physics_state);
resources.insert(physics_pipeline); resources.insert(physics_pipeline);

@ -117,7 +117,7 @@ pub fn render_test(
if renderer.lights_are_dirty { if renderer.lights_are_dirty {
renderer.lights_are_dirty = false; renderer.lights_are_dirty = false;
let mut query = <(&mut DirectionalLight, &mut Point3<f32>)>::query(); let mut query = <(&mut DirectionalLight, &mut Position)>::query();
for (i, (light, pos)) in query.iter_mut(world).enumerate() { for (i, (light, pos)) in query.iter_mut(world).enumerate() {
renderer.queue.write_buffer( renderer.queue.write_buffer(
&renderer.light_uniform_buf, &renderer.light_uniform_buf,
@ -129,7 +129,7 @@ pub fn render_test(
push_debug_group_checked("shadow passes", &mut encoder); push_debug_group_checked("shadow passes", &mut encoder);
let mut query = <(&mut DirectionalLight, &mut Point3<f32>)>::query(); let mut query = <(&mut DirectionalLight, &mut Position)>::query();
for (i, (light, pos)) in query.iter_mut(world).enumerate() { for (i, (light, pos)) in query.iter_mut(world).enumerate() {
insert_debug_marker_checked(&format!("shadow pass {} (light at position {:?})", i, pos), &mut encoder); insert_debug_marker_checked(&format!("shadow pass {} (light at position {:?})", i, pos), &mut encoder);

@ -1,14 +1,15 @@
use std::f32::consts::PI;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
use cgmath::{Euler, Quaternion, Deg, Rad, Point3}; use cgmath::{Deg, Euler, Point3, Quaternion, Rad};
use imgui::FontSource; use imgui::FontSource;
use legion::*;
use legion::IntoQuery;
use legion::systems::CommandBuffer; use legion::systems::CommandBuffer;
use legion::world::SubWorld; use legion::world::SubWorld;
use legion::IntoQuery;
use legion::*;
use nalgebra::Quaternion as naQuaternion; use nalgebra::Quaternion as naQuaternion;
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodyBuilder, RigidBodySet}; use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodyBuilder, RigidBodySet, MassProperties};
use rapier3d::geometry::{BroadPhase, ColliderBuilder, ColliderSet, NarrowPhase}; use rapier3d::geometry::{BroadPhase, ColliderBuilder, ColliderSet, NarrowPhase};
use rapier3d::na::{Isometry3, Vector, Vector3}; use rapier3d::na::{Isometry3, Vector, Vector3};
use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline}; use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline};
@ -19,14 +20,10 @@ use crate::geometry::RawMesh;
use crate::physics::state::PhysicsState; use crate::physics::state::PhysicsState;
use crate::render::state::RenderState; use crate::render::state::RenderState;
use crate::runtime::state::RuntimeState; use crate::runtime::state::RuntimeState;
use std::f32::consts::PI;
#[system] #[system]
#[write_component(Mesh)] #[write_component(Mesh)]
pub fn runtime_load( pub fn runtime_load(world: &mut SubWorld, #[resource] runtime_state: &mut RuntimeState) {
world: &mut SubWorld,
#[resource] runtime_state: &mut RuntimeState,
) {
runtime_state.preload_meshes(PathBuf::from("./resources")); runtime_state.preload_meshes(PathBuf::from("./resources"));
} }
@ -38,8 +35,7 @@ pub fn runtime_spawn(
#[resource] runtime_state: &mut RuntimeState, #[resource] runtime_state: &mut RuntimeState,
#[resource] renderer: &mut RenderState, #[resource] renderer: &mut RenderState,
) { ) {
for entity in runtime_state.get_configured_entities() {
for entity in runtime_state.get_configured_entities(){
match entity.ent_type.as_ref() { match entity.ent_type.as_ref() {
"PhysicsEntity" => { "PhysicsEntity" => {
let mesh_name = entity.mesh.unwrap(); let mesh_name = entity.mesh.unwrap();
@ -48,44 +44,52 @@ pub fn runtime_spawn(
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name); log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
continue; continue;
} }
Some(mesh) => mesh Some(mesh) => mesh,
}; };
let mut dynamic_body = RigidBodyBuilder::new_dynamic()
.can_sleep(false)
.mass(1.0)
.translation(0.0, 0.0, 0.0)
.build();
let collider = ColliderBuilder::trimesh( // let collider = ColliderBuilder::trimesh(
raw_mesh.vertices // raw_mesh.vertices.iter().map(|v| v.position()).collect(),
.iter() // raw_mesh.indices.clone(),
.map(|v| v.position()) // ).density(1.0)
.collect(), // .build();
raw_mesh.indices.clone(),
).build(); let collider = ColliderBuilder::ball(2.0).build();
let gpu_mesh_buffer = renderer.upload_mesh_to_buffer( let gpu_mesh_buffer = renderer
raw_mesh, .upload_mesh_to_buffer(
Some(wgpu::Color { raw_mesh,
r: 1.0, Some(wgpu::Color {
g: 0.7, r: 1.0,
b: 0.3, g: 0.7,
a: 1.0, b: 0.3,
}) a: 1.0,
).unwrap(); }),
)
let entity: Entity = cmd.push(( .unwrap();
Position {
let position = match entity.position {
None => Position {
x: 0.0, x: 0.0,
y: 20.0, y: rand::random::<f32>(),
z: 0.0, z: 0.0,
rot: Euler { rot: Euler {
x: Deg(25.0), x: Deg(0.0),
y: Deg(45.0), y: Deg(0.0),
z: Deg(15.0), z: Deg(0.0),
}, },
}, },
Some(position) => position
};
let mut dynamic_body = RigidBodyBuilder::new_dynamic()
.can_sleep(false)
.mass(100.0)
.translation(position.x, position.y, position.z)
.build();
let entity: Entity = cmd.push((
position,
gpu_mesh_buffer, gpu_mesh_buffer,
Physics { Physics {
rigid_body: dynamic_body, rigid_body: dynamic_body,
@ -96,7 +100,7 @@ pub fn runtime_spawn(
collider_handle: None, collider_handle: None,
}, },
)); ));
}, }
"Terrain" => { "Terrain" => {
let mesh_name = entity.mesh.unwrap(); let mesh_name = entity.mesh.unwrap();
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) { let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
@ -104,7 +108,7 @@ pub fn runtime_spawn(
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name); log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
continue; continue;
} }
Some(mesh) => mesh Some(mesh) => mesh,
}; };
let mut static_body = RigidBodyBuilder::new_static() let mut static_body = RigidBodyBuilder::new_static()
@ -112,28 +116,27 @@ pub fn runtime_spawn(
.build(); .build();
let mesh_collider = ColliderBuilder::trimesh( let mesh_collider = ColliderBuilder::trimesh(
raw_mesh.vertices raw_mesh.vertices.iter().map(|v| v.position()).collect(),
.iter()
.map(|v| v.position())
.collect(),
raw_mesh.indices.clone(), raw_mesh.indices.clone(),
).build(); )
.build();
let gpu_mesh_buffer = renderer.upload_mesh_to_buffer(
raw_mesh, let gpu_mesh_buffer = renderer
Some(wgpu::Color { .upload_mesh_to_buffer(
r: 1.0, raw_mesh,
g: 0.7, Some(wgpu::Color {
b: 0.3, r: 1.0,
a: 1.0, g: 0.7,
}) b: 0.3,
).unwrap(); a: 1.0,
}),
)
let entity: Entity = cmd.push(( .unwrap();
Position {
let position = match entity.position {
None => Position {
x: 0.0, x: 0.0,
y: -8.0, y: 0.0,
z: 0.0, z: 0.0,
rot: Euler { rot: Euler {
x: Deg(0.0), x: Deg(0.0),
@ -141,6 +144,12 @@ pub fn runtime_spawn(
z: Deg(0.0), z: Deg(0.0),
}, },
}, },
Some(position) => position
};
let entity: Entity = cmd.push((
position,
gpu_mesh_buffer, gpu_mesh_buffer,
Physics { Physics {
rigid_body: static_body, rigid_body: static_body,
@ -151,9 +160,8 @@ pub fn runtime_spawn(
collider_handle: None, collider_handle: None,
}, },
)); ));
}, }
"Camera" => { "Camera" => {
let entity: Entity = cmd.push(( let entity: Entity = cmd.push((
Camera { Camera {
position: cgmath::Point3 { position: cgmath::Point3 {
@ -166,7 +174,7 @@ pub fn runtime_spawn(
}, },
CameraController::new(3.0, 1.0), CameraController::new(3.0, 1.0),
)); ));
}, }
"Light" => { "Light" => {
let mesh_name = entity.mesh.unwrap(); let mesh_name = entity.mesh.unwrap();
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) { let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
@ -174,40 +182,43 @@ pub fn runtime_spawn(
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name); log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
continue; continue;
} }
Some(mesh) => mesh Some(mesh) => mesh,
}; };
let gpu_mesh_buffer = renderer.upload_mesh_to_buffer( let position = match entity.position {
raw_mesh, None => Position {
Some(wgpu::Color { x: 1.0,
r: 1.0, y: 1.0,
g: 0.7, z: 1.0,
b: 0.3,
a: 1.0,
})
).unwrap();
let light_entity: Entity = cmd.push((
Position {
x: 5.0,
y: 10.0,
z: 5.0,
rot: Euler { rot: Euler {
x: Deg(0.0), x: Deg(0.0),
y: Deg(-25.0), y: Deg(0.0),
z: Deg(0.0), z: Deg(0.0),
}, },
}, },
Some(position) => position
};
let gpu_mesh_buffer = renderer
.upload_mesh_to_buffer(
raw_mesh,
Some(wgpu::Color {
r: 1.0,
g: 0.7,
b: 0.3,
a: 1.0,
}),
)
.unwrap();
let light_entity: Entity = cmd.push((
position,
gpu_mesh_buffer, gpu_mesh_buffer,
renderer.create_light(), renderer.create_light(),
)); ));
} }
_ => {}
_ => {},
} }
} }
} }

Loading…
Cancel
Save