Add prelude + use vectors for setting linvel/translation in builders

This commit is contained in:
Crozet Sébastien
2021-05-25 11:00:13 +02:00
parent 47139323e0
commit 1bef66fea9
93 changed files with 1528 additions and 1259 deletions

View File

@@ -87,9 +87,11 @@ impl Box2dWorld {
fn insert_colliders(&mut self, colliders: &ColliderSet) {
for (_, collider) in colliders.iter() {
let b2_body_handle = self.rapier2box2d[&collider.parent()];
let mut b2_body = self.world.body_mut(b2_body_handle);
Self::create_fixture(&collider, &mut *b2_body);
if let Some(parent) = collider.parent() {
let b2_body_handle = self.rapier2box2d[&parent];
let mut b2_body = self.world.body_mut(b2_body_handle);
Self::create_fixture(&collider, &mut *b2_body);
}
}
}
@@ -122,8 +124,8 @@ impl Box2dWorld {
body_a,
body_b,
collide_connected: true,
local_anchor_a: na_vec_to_b2_vec(params.local_anchor1.translation.vector),
local_anchor_b: na_vec_to_b2_vec(params.local_anchor2.translation.vector),
local_anchor_a: na_vec_to_b2_vec(params.local_frame1.translation.vector),
local_anchor_b: na_vec_to_b2_vec(params.local_frame2.translation.vector),
reference_angle: 0.0,
frequency: 0.0,
damping_ratio: 0.0,
@@ -155,7 +157,14 @@ impl Box2dWorld {
}
fn create_fixture(collider: &Collider, body: &mut b2::MetaBody<NoUserData>) {
let center = na_vec_to_b2_vec(collider.position_wrt_parent().translation.vector);
let center = na_vec_to_b2_vec(
collider
.position_wrt_parent()
.copied()
.unwrap_or(na::one())
.translation
.vector,
);
let mut fixture_def = b2::FixtureDef::new();
fixture_def.restitution = collider.material().restitution;
@@ -230,7 +239,9 @@ impl Box2dWorld {
for coll_handle in body.colliders() {
let collider = &mut colliders[*coll_handle];
collider.set_position_debug(pos * collider.position_wrt_parent());
collider.set_position(
pos * collider.position_wrt_parent().copied().unwrap_or(na::one()),
);
}
}
}

View File

@@ -1,6 +1,6 @@
use bevy::prelude::*;
use na::Point3;
use na::{point, Point3};
use crate::math::Isometry;
use crate::objects::node::EntityWithGraphics;
@@ -34,7 +34,7 @@ impl GraphicsManager {
b2sn: HashMap::new(),
b2color: HashMap::new(),
c2color: HashMap::new(),
ground_color: Point3::new(0.5, 0.5, 0.5),
ground_color: point![0.5, 0.5, 0.5],
b2wireframe: HashMap::new(),
prefab_meshes: HashMap::new(),
}
@@ -57,9 +57,10 @@ impl GraphicsManager {
pub fn remove_collider_nodes(
&mut self,
commands: &mut Commands,
body: RigidBodyHandle,
body: Option<RigidBodyHandle>,
collider: ColliderHandle,
) {
let body = body.unwrap_or(RigidBodyHandle::invalid());
if let Some(sns) = self.b2sn.get_mut(&body) {
for sn in sns.iter_mut() {
if sn.collider == collider {
@@ -83,23 +84,23 @@ impl GraphicsManager {
&mut self,
materials: &mut Assets<StandardMaterial>,
b: RigidBodyHandle,
color: Point3<f32>,
color: [f32; 3],
) {
self.b2color.insert(b, color);
self.b2color.insert(b, color.into());
if let Some(ns) = self.b2sn.get_mut(&b) {
for n in ns.iter_mut() {
n.set_color(materials, color)
n.set_color(materials, color.into())
}
}
}
pub fn set_initial_body_color(&mut self, b: RigidBodyHandle, color: Point3<f32>) {
self.b2color.insert(b, color);
pub fn set_initial_body_color(&mut self, b: RigidBodyHandle, color: [f32; 3]) {
self.b2color.insert(b, color.into());
}
pub fn set_initial_collider_color(&mut self, c: ColliderHandle, color: Point3<f32>) {
self.c2color.insert(c, color);
pub fn set_initial_collider_color(&mut self, c: ColliderHandle, color: [f32; 3]) {
self.c2color.insert(c, color.into());
}
pub fn set_body_wireframe(&mut self, b: RigidBodyHandle, enabled: bool) {
@@ -118,18 +119,18 @@ impl GraphicsManager {
}
}
pub fn toggle_wireframe_mode(&mut self, colliders: &ColliderSet, _enabled: bool) {
for n in self.b2sn.values_mut().flat_map(|val| val.iter_mut()) {
let _force_wireframe = if let Some(collider) = colliders.get(n.collider) {
collider.is_sensor()
|| self
.b2wireframe
.get(&collider.parent())
.cloned()
.unwrap_or(false)
} else {
false
};
pub fn toggle_wireframe_mode(&mut self, _colliders: &ColliderSet, _enabled: bool) {
for _n in self.b2sn.values_mut().flat_map(|val| val.iter_mut()) {
// let _force_wireframe = if let Some(collider) = colliders.get(n.collider) {
// collider.is_sensor()
// || self
// .b2wireframe
// .get(&collider.parent())
// .cloned()
// .unwrap_or(false)
// } else {
// false
// };
// if let Some(node) = n.scene_node_mut() {
// if force_wireframe || enabled {
@@ -171,7 +172,7 @@ impl GraphicsManager {
}
}
self.set_body_color(materials, handle, color);
self.set_body_color(materials, handle, color.into());
color
}
@@ -257,10 +258,10 @@ impl GraphicsManager {
colliders: &ColliderSet,
) {
let collider = &colliders[handle];
let color = *self.b2color.get(&collider.parent()).unwrap();
let collider_parent = collider.parent().unwrap_or(RigidBodyHandle::invalid());
let color = *self.b2color.get(&collider_parent).unwrap();
let color = self.c2color.get(&handle).copied().unwrap_or(color);
let mut nodes =
std::mem::replace(self.b2sn.get_mut(&collider.parent()).unwrap(), Vec::new());
let mut nodes = std::mem::replace(self.b2sn.get_mut(&collider_parent).unwrap(), Vec::new());
self.do_add_shape(
commands,
meshes,
@@ -273,7 +274,7 @@ impl GraphicsManager {
color,
&mut nodes,
);
self.b2sn.insert(collider.parent(), nodes);
self.b2sn.insert(collider_parent, nodes);
}
fn do_add_shape(
@@ -338,9 +339,9 @@ impl GraphicsManager {
//
// if bo.is_dynamic() {
// if bo.is_ccd_active() {
// n.set_color(Point3::new(1.0, 0.0, 0.0));
// n.set_color(point![1.0, 0.0, 0.0]);
// } else {
// n.set_color(Point3::new(0.0, 1.0, 0.0));
// n.set_color(point![0.0, 1.0, 0.0]);
// }
// }
// }
@@ -365,9 +366,9 @@ impl GraphicsManager {
// let y = rotmat.column(1) * 0.25f32;
// let z = rotmat.column(2) * 0.25f32;
// window.draw_line(center, &(*center + x), &Point3::new(1.0, 0.0, 0.0));
// window.draw_line(center, &(*center + y), &Point3::new(0.0, 1.0, 0.0));
// window.draw_line(center, &(*center + z), &Point3::new(0.0, 0.0, 1.0));
// window.draw_line(center, &(*center + x), &point![1.0, 0.0, 0.0]);
// window.draw_line(center, &(*center + y), &point![0.0, 1.0, 0.0]);
// window.draw_line(center, &(*center + z), &point![0.0, 0.0, 1.0]);
// // }
// }
// }

View File

@@ -55,15 +55,17 @@ impl NPhysicsWorld {
}
for (_, collider) in colliders.iter() {
let parent = &bodies[collider.parent()];
let nphysics_rb_handle = rapier2nphysics[&collider.parent()];
if let Some(collider) =
nphysics_collider_from_rapier_collider(&collider, parent.is_dynamic())
{
let nphysics_collider = collider.build(BodyPartHandle(nphysics_rb_handle, 0));
nphysics_colliders.insert(nphysics_collider);
} else {
eprintln!("Creating shape unknown to the nphysics backend.")
if let Some(parent_handle) = collider.parent() {
let parent = &bodies[parent_handle];
let nphysics_rb_handle = rapier2nphysics[&parent_handle];
if let Some(collider) =
nphysics_collider_from_rapier_collider(&collider, parent.is_dynamic())
{
let nphysics_collider = collider.build(BodyPartHandle(nphysics_rb_handle, 0));
nphysics_colliders.insert(nphysics_collider);
} else {
eprintln!("Creating shape unknown to the nphysics backend.")
}
}
}
@@ -76,10 +78,10 @@ impl NPhysicsWorld {
let c = FixedConstraint::new(
b1,
b2,
params.local_anchor1.translation.vector.into(),
params.local_anchor1.rotation,
params.local_anchor2.translation.vector.into(),
params.local_anchor2.rotation,
params.local_frame1.translation.vector.into(),
params.local_frame1.rotation,
params.local_frame2.translation.vector.into(),
params.local_frame2.rotation,
);
nphysics_joints.insert(c);
}
@@ -172,7 +174,9 @@ impl NPhysicsWorld {
for coll_handle in rb.colliders() {
let collider = &mut colliders[*coll_handle];
collider.set_position_debug(pos * collider.position_wrt_parent());
collider.set_position(
pos * collider.position_wrt_parent().copied().unwrap_or(na::one()),
);
}
}
}
@@ -183,7 +187,7 @@ fn nphysics_collider_from_rapier_collider(
is_dynamic: bool,
) -> Option<ColliderDesc<f32>> {
let mut margin = ColliderDesc::<f32>::default_margin();
let mut pos = *collider.position_wrt_parent();
let mut pos = collider.position_wrt_parent().copied().unwrap_or(na::one());
let shape = collider.shape();
let shape = if let Some(cuboid) = shape.as_cuboid() {
@@ -209,7 +213,7 @@ fn nphysics_collider_from_rapier_collider(
trimesh
.indices()
.iter()
.map(|idx| na::Point3::new(idx[0] as usize, idx[1] as usize, idx[2] as usize))
.map(|idx| na::point![idx[0] as usize, idx[1] as usize, idx[2] as usize])
.collect(),
None,
))

View File

@@ -2,7 +2,7 @@ use bevy::prelude::*;
use bevy::render::mesh::{Indices, VertexAttributeValues};
//use crate::objects::plane::Plane;
use na::{Point3, Vector3};
use na::{point, Point3, Vector3};
use std::collections::HashMap;
use bevy::render::pipeline::PrimitiveTopology;
@@ -106,7 +106,7 @@ impl EntityWithGraphics {
pub fn select(&mut self, materials: &mut Assets<StandardMaterial>) {
// NOTE: we don't just call `self.set_color` because that would
// overwrite self.base_color too.
self.color = Point3::new(1.0, 0.0, 0.0);
self.color = point![1.0, 0.0, 0.0];
if let Some(material) = materials.get_mut(&self.material) {
material.base_color = Color::rgb(self.color.x, self.color.y, self.color.z);
}
@@ -210,10 +210,10 @@ impl EntityWithGraphics {
// Halfspace
//
let vertices = vec![
Point3::new(-1000.0, 0.0, -1000.0),
Point3::new(1000.0, 0.0, -1000.0),
Point3::new(1000.0, 0.0, 1000.0),
Point3::new(-1000.0, 0.0, 1000.0),
point![-1000.0, 0.0, -1000.0],
point![1000.0, 0.0, -1000.0],
point![1000.0, 0.0, 1000.0],
point![-1000.0, 0.0, 1000.0],
];
let indices = vec![[0, 1, 2], [0, 2, 3]];
let mesh = bevy_mesh((vertices, indices));
@@ -365,7 +365,7 @@ fn generate_collider_mesh(co_shape: &dyn Shape) -> Option<Mesh> {
let vertices = trimesh
.vertices()
.iter()
.map(|p| Point3::new(p.x, p.y, 0.0))
.map(|p| point![p.x, p.y, 0.0])
.collect();
bevy_mesh((vertices, trimesh.indices().to_vec()))
}

View File

@@ -220,23 +220,28 @@ impl PhysxWorld {
if let Some((mut px_shape, px_material, collider_pos)) =
physx_collider_from_rapier_collider(&mut *physics, &mut cooking, &collider)
{
let parent_body = &bodies[collider.parent()];
if let Some(parent_handle) = collider.parent() {
let parent_body = &bodies[parent_handle];
if !parent_body.is_dynamic() {
let actor = rapier2static.get_mut(&collider.parent()).unwrap();
actor.attach_shape(&mut px_shape);
} else {
let actor = rapier2dynamic.get_mut(&collider.parent()).unwrap();
actor.attach_shape(&mut px_shape);
if !parent_body.is_dynamic() {
let actor = rapier2static.get_mut(&parent_handle).unwrap();
actor.attach_shape(&mut px_shape);
} else {
let actor = rapier2dynamic.get_mut(&parent_handle).unwrap();
actor.attach_shape(&mut px_shape);
}
unsafe {
let pose = collider_pos.into_physx();
physx_sys::PxShape_setLocalPose_mut(
px_shape.as_mut_ptr(),
&pose.into(),
);
}
shapes.push(px_shape);
materials.push(px_material);
}
unsafe {
let pose = collider_pos.into_physx();
physx_sys::PxShape_setLocalPose_mut(px_shape.as_mut_ptr(), &pose.into());
}
shapes.push(px_shape);
materials.push(px_material);
}
}
@@ -454,8 +459,8 @@ impl PhysxWorld {
}
}
JointParams::FixedJoint(params) => {
let frame1 = params.local_anchor1.into_physx().into();
let frame2 = params.local_anchor2.into_physx().into();
let frame1 = params.local_frame1.into_physx().into();
let frame2 = params.local_frame2.into_physx().into();
physx_sys::phys_PxFixedJointCreate(
physics.as_mut_ptr(),
@@ -500,7 +505,9 @@ impl PhysxWorld {
for coll_handle in rb.colliders() {
let collider = &mut colliders[*coll_handle];
collider.set_position_debug(pos * collider.position_wrt_parent());
collider.set_position(
pos * collider.position_wrt_parent().copied().unwrap_or(na::one()),
);
}
}
}
@@ -511,7 +518,7 @@ fn physx_collider_from_rapier_collider(
cooking: &PxCooking,
collider: &Collider,
) -> Option<(Owner<PxShape>, Owner<PxMaterial>, Isometry3<f32>)> {
let mut local_pose = *collider.position_wrt_parent();
let mut local_pose = collider.position_wrt_parent().copied().unwrap_or(na::one());
let shape = collider.shape();
let shape_flags = if collider.is_sensor() {
ShapeFlag::TriggerShape.into()

View File

@@ -429,7 +429,7 @@ impl TestbedApp {
}
impl<'a, 'b, 'c, 'd> TestbedGraphics<'a, 'b, 'c, 'd> {
pub fn set_body_color(&mut self, body: RigidBodyHandle, color: Point3<f32>) {
pub fn set_body_color(&mut self, body: RigidBodyHandle, color: [f32; 3]) {
self.manager
.set_body_color(&mut self.materials, body, color);
}
@@ -588,13 +588,13 @@ impl<'a, 'b, 'c, 'd> Testbed<'a, 'b, 'c, 'd> {
}
}
pub fn set_initial_body_color(&mut self, body: RigidBodyHandle, color: Point3<f32>) {
pub fn set_initial_body_color(&mut self, body: RigidBodyHandle, color: [f32; 3]) {
if let Some(graphics) = &mut self.graphics {
graphics.manager.set_initial_body_color(body, color);
}
}
pub fn set_initial_collider_color(&mut self, collider: ColliderHandle, color: Point3<f32>) {
pub fn set_initial_collider_color(&mut self, collider: ColliderHandle, color: [f32; 3]) {
if let Some(graphics) = &mut self.graphics {
graphics.manager.set_initial_collider_color(collider, color);
}
@@ -752,14 +752,14 @@ fn draw_contacts(_nf: &NarrowPhase, _colliders: &ColliderSet) {
// let n = manifold.data.normal;
//
// use crate::engine::GraphicsWindow;
// window.draw_graphics_line(&p, &(p + n * 0.4), &Point3::new(0.5, 1.0, 0.5));
// window.draw_graphics_line(&p, &(p + n * 0.4), &point![0.5, 1.0, 0.5]);
// }
// */
// for pt in manifold.contacts() {
// let color = if pt.dist > 0.0 {
// Point3::new(0.0, 0.0, 1.0)
// point![0.0, 0.0, 1.0]
// } else {
// Point3::new(1.0, 0.0, 0.0)
// point![1.0, 0.0, 0.0]
// };
// let pos1 = colliders[pair.pair.collider1].position();
// let pos2 = colliders[pair.pair.collider2].position();
@@ -771,7 +771,7 @@ fn draw_contacts(_nf: &NarrowPhase, _colliders: &ColliderSet) {
// * manifold.subshape_pos1.unwrap_or(Isometry::identity())
// * manifold.local_n1;
//
// // window.draw_graphics_line(&start, &(start + n * 0.4), &Point3::new(0.5, 1.0, 0.5));
// // window.draw_graphics_line(&start, &(start + n * 0.4), &point![0.5, 1.0, 0.5]);
// // window.draw_graphics_line(&start, &end, &color);
// }
// }
@@ -1228,10 +1228,13 @@ fn highlight_hovered_body(
if let Some((handle, _)) = hit {
let collider = &physics.colliders[handle];
if physics.bodies[collider.parent()].is_dynamic() {
testbed_state.highlighted_body = Some(collider.parent());
for node in graphics_manager.body_nodes_mut(collider.parent()).unwrap() {
node.select(materials)
if let Some(parent_handle) = collider.parent() {
if physics.bodies[parent_handle].is_dynamic() {
testbed_state.highlighted_body = Some(parent_handle);
for node in graphics_manager.body_nodes_mut(parent_handle).unwrap() {
node.select(materials)
}
}
}
}