From 10b9414989de13be24f204dc256bb473817fc949 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 12 Mar 2022 14:56:32 -0800 Subject: [PATCH 1/4] Fail at adding joints, rename geometry module. Time to move the dynamic geo into its own sitch. --- Cargo.lock | 8 +-- src/camera.rs | 2 +- src/colliders.rs | 83 +++++++++++++++++++++++-- src/glamor.rs | 2 +- src/lib.rs | 2 +- src/lights.rs | 2 +- src/main.rs | 2 +- src/{geometry.rs => static_geometry.rs} | 0 8 files changed, 86 insertions(+), 15 deletions(-) rename src/{geometry.rs => static_geometry.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index e09fddb..6705259 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -740,9 +740,9 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" [[package]] name = "bytemuck" -version = "1.7.3" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439989e6b8c38d1b6570a384ef1e49c8848128f5a97f3914baef02920842712f" +checksum = "0e851ca7c24871e7336801608a4797d7376545b6928a10d32d75685687141ead" dependencies = [ "bytemuck_derive", ] @@ -2635,9 +2635,9 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" dependencies = [ "winapi-util", ] diff --git a/src/camera.rs b/src/camera.rs index 0667d90..d03a231 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -3,7 +3,7 @@ use bevy::{ render::camera::{ActiveCameras, Camera, CameraPlugin}, }; -use crate::{geometry::CyberBikeModel, input::InputState}; +use crate::{input::InputState, static_geometry::CyberBikeModel}; // 85 degrees in radians const MAX_PITCH: f32 = 1.48353; diff --git a/src/colliders.rs b/src/colliders.rs index 0c910ea..242ce55 100644 --- a/src/colliders.rs +++ b/src/colliders.rs @@ -4,7 +4,7 @@ use bevy::{ }; use bevy_rapier3d::prelude::*; -use crate::geometry::{CyberBikeModel, CyberPlanet, PLANET_RADIUS, SPAWN_ALTITUDE}; +use crate::static_geometry::{CyberBikeModel, CyberPlanet, PLANET_RADIUS, SPAWN_ALTITUDE}; #[derive(Component)] pub struct CyberBikeBody; @@ -67,7 +67,7 @@ fn setup_colliders( .insert_bundle(pbody) .insert_bundle(pcollide); - // bike is the easy part + // bike let (bike, xform) = bike_query.single(); setup_bike_collider(bike, xform, &mut commands); } @@ -89,10 +89,11 @@ fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into()); bbody.position = isometry.into(); + // collider let shape = ColliderShape::capsule( Vec3::new(0.0, 0.0, -2.7).into(), - Vec3::new(0.0, 0.0, 2.0).into(), + Vec3::new(0.0, 0.0, 2.5).into(), 1.0, ); let bcollide = ColliderBundle { @@ -110,15 +111,85 @@ fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) .entity(bike) .insert_bundle(bbody) .insert(CyberBikeBody) - .insert(RigidBodyPositionSync::Discrete) - .with_children(|e| { - e.spawn_bundle(bcollide) + .insert(RigidBodyPositionSync::Interpolated { prev_pos: None }) + .with_children(|child_builder| { + child_builder + .spawn_bundle(bcollide) .insert(ColliderDebugRender { color: Color::GREEN, }) .insert(CyberBikeCollider) .insert(ColliderPositionSync::Discrete); }); + + /* { + let wheel_collider = ColliderBundle { + material: ColliderMaterial::new(0.0, 0.0).into(), + shape: ColliderShape::ball(0.25).into(), + mass_properties: ColliderMassProps::Density(0.1).into(), + ..Default::default() + }; + + let shock = PrismaticJoint::new(Vector::y_axis()) + .local_anchor1(Vec3::new(-3.0, -3.0, -3.0).into()) + .motor_position(-1.0, 0.02, 0.5); + + let ccd = RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 0.1, + ccd_max_dist: 0.25, + ..Default::default() + }; + + let mut fw_pos = Isometry::default(); + fw_pos.translation = + (xform.translation + (xform.down() * 4.5 + (xform.forward() * 2.5))).into(); + + let front_wheel_body = RigidBodyBundle { + //activation: RigidBodyActivation::cannot_sleep().into(), + //ccd: ccd.into(), + position: fw_pos.into(), + ..Default::default() + }; + + let front_wheel = commands + .spawn_bundle(front_wheel_body) + .insert_bundle(wheel_collider) + .insert(ColliderPositionSync::Discrete) + .insert(ColliderDebugRender::from(Color::YELLOW)) + .id(); + + //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, front_wheel),)); + + // let bw_pos = Isometry::new( + // (xform.translation + xform.down() + xform.back() * 2.0).into(), + // Vec3::ZERO.into(), + // ); + + // let back_wheel_body = RigidBodyBundle { + // activation: RigidBodyActivation::cannot_sleep().into(), + // ccd: ccd.into(), + // position: bw_pos.into(), + // ..Default::default() + // }; + + // let wheel_collider = ColliderBundle { + // material: ColliderMaterial::new(0.0, 0.0).into(), + // // default shape is a 1-meter ball + // ..Default::default() + // }; + + // let back_wheel = commands + // .spawn_bundle(back_wheel_body) + // .insert_bundle(wheel_collider) + // .insert(ColliderPositionSync::Discrete) + // .insert(ColliderDebugRender::from(Color::RED)) + // .id(); + + //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, + // back_wheel),)); + + }; */ } pub struct CyberCollidersPlugin; diff --git a/src/glamor.rs b/src/glamor.rs index 9ef6dde..60d3412 100644 --- a/src/glamor.rs +++ b/src/glamor.rs @@ -10,7 +10,7 @@ use bevy::{ use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; use rand::{thread_rng, Rng}; -use crate::{geometry::CyberPlanet, lights::AnimateCyberLightWireframe}; +use crate::{lights::AnimateCyberLightWireframe, static_geometry::CyberPlanet}; pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1); diff --git a/src/lib.rs b/src/lib.rs index a218813..2b4190f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,10 +6,10 @@ use bevy::{ pub mod action; pub mod camera; pub mod colliders; -pub mod geometry; pub mod glamor; pub mod input; pub mod lights; +pub mod static_geometry; pub mod ui; #[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)] diff --git a/src/lights.rs b/src/lights.rs index af8da28..ad42c09 100644 --- a/src/lights.rs +++ b/src/lights.rs @@ -3,7 +3,7 @@ use std::f32::consts::TAU; use bevy::prelude::*; use rand::prelude::*; -use crate::geometry::PLANET_RADIUS; +use crate::static_geometry::PLANET_RADIUS; pub const LIGHT_RANGE: f32 = 90.0; diff --git a/src/main.rs b/src/main.rs index 2a96bbe..0ebacf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use cyber_rider::{ camera::CyberCamPlugin, colliders::CyberCollidersPlugin, disable_mouse_trap, - geometry::CyberGeomPlugin, + static_geometry::CyberGeomPlugin, glamor::CyberGlamorPlugin, input::CyberInputPlugin, lights::CyberSpaceLightsPlugin, diff --git a/src/geometry.rs b/src/static_geometry.rs similarity index 100% rename from src/geometry.rs rename to src/static_geometry.rs From 8979660775b5d7872d0bc5d5e0457fa91cc44d7a Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 12 Mar 2022 16:06:36 -0800 Subject: [PATCH 2/4] Major reorg, works like before. Still need to add the child colliders for the "wheels". --- src/action.rs | 2 +- src/bike.rs | 159 +++++++++++++++++++ src/camera.rs | 2 +- src/colliders.rs | 213 -------------------------- src/glamor.rs | 2 +- src/lib.rs | 4 +- src/lights.rs | 2 +- src/main.rs | 4 +- src/{static_geometry.rs => planet.rs} | 59 +++---- src/ui.rs | 2 +- 10 files changed, 200 insertions(+), 249 deletions(-) create mode 100644 src/bike.rs delete mode 100644 src/colliders.rs rename src/{static_geometry.rs => planet.rs} (74%) diff --git a/src/action.rs b/src/action.rs index 23fd85f..a37bf6e 100644 --- a/src/action.rs +++ b/src/action.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use bevy_rapier3d::{na::Vector3, prelude::*}; use crate::{ - colliders::{CyberBikeBody, CyberBikeCollider}, + bike::{CyberBikeBody, CyberBikeCollider}, input::InputState, }; diff --git a/src/bike.rs b/src/bike.rs new file mode 100644 index 0000000..97f0b1c --- /dev/null +++ b/src/bike.rs @@ -0,0 +1,159 @@ +use bevy::prelude::*; +use bevy_rapier3d::prelude::*; + +use crate::planet::PLANET_RADIUS; + +pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015; + +#[derive(Component)] +pub struct CyberBikeBody; + +#[derive(Component)] +pub struct CyberBikeCollider; + +#[derive(Component, Debug)] +pub struct CyberBikeModel; + +fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { + let xform = Transform::from_translation(Vec3::X * SPAWN_ALTITUDE) + .looking_at(Vec3::new(PLANET_RADIUS, 1000.0, 0.0), Vec3::X); + + let mut bbody = RigidBodyBundle::default(); + + bbody.damping.angular_damping = 0.8; + bbody.damping.linear_damping = 0.1; + bbody.activation = RigidBodyActivation::cannot_sleep().into(); + + bbody.ccd = RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 0.2, + ccd_max_dist: 2.7, + ..Default::default() + } + .into(); + + let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into()); + bbody.position = isometry.into(); + + let shape = ColliderShape::capsule( + Vec3::new(0.0, 0.0, -2.7).into(), + Vec3::new(0.0, 0.0, 2.5).into(), + 1.0, + ); + let bcollide = ColliderBundle { + shape: shape.into(), + mass_properties: ColliderMassProps::Density(0.04).into(), + material: ColliderMaterial { + friction: 0.0, + restitution: 0.0, + ..Default::default() + } + .into(), + ..Default::default() + }; + + let bike = commands + .spawn_bundle(bbody) + .insert_bundle((xform, GlobalTransform::default())) + .insert(RigidBodyPositionSync::Interpolated { prev_pos: None }) + .with_children(|child_builder| { + child_builder + .spawn_bundle(bcollide) + .insert(ColliderDebugRender { + color: Color::GREEN, + }) + .insert(CyberBikeCollider) + .insert(ColliderPositionSync::Discrete); + }) + .with_children(|rider| { + rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0")); + }) + .insert(CyberBikeModel) + .insert(CyberBikeBody) + .id(); + + add_wheels(bike, &xform, &mut commands); +} + +fn add_wheels(bike: Entity, xform: &Transform, mut commands: &mut Commands) { + let wheel_collider = ColliderBundle { + material: ColliderMaterial::new(0.0, 0.0).into(), + shape: ColliderShape::ball(0.25).into(), + mass_properties: ColliderMassProps::Density(0.1).into(), + ..Default::default() + }; + + let shock = PrismaticJoint::new(Vector::y_axis()) + .local_anchor1(Vec3::new(-3.0, -3.0, -3.0).into()) + .motor_position(-1.0, 0.02, 0.5); + + let ccd = RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 0.1, + ccd_max_dist: 0.25, + ..Default::default() + }; + + let mut fw_pos = Isometry::default(); + fw_pos.translation = + (xform.translation + (xform.down() * 4.5 + (xform.forward() * 2.5))).into(); + + let front_wheel_body = RigidBodyBundle { + //activation: RigidBodyActivation::cannot_sleep().into(), + //ccd: ccd.into(), + position: fw_pos.into(), + ..Default::default() + }; + + let front_wheel = commands + .spawn_bundle(front_wheel_body) + .insert_bundle(wheel_collider) + .insert(ColliderPositionSync::Discrete) + .insert(ColliderDebugRender::from(Color::YELLOW)) + .id(); + + //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, + // front_wheel),)); + + // let bw_pos = Isometry::new( + // (xform.translation + xform.down() + xform.back() * 2.0).into(), + // Vec3::ZERO.into(), + // ); + + // let back_wheel_body = RigidBodyBundle { + // activation: RigidBodyActivation::cannot_sleep().into(), + // ccd: ccd.into(), + // position: bw_pos.into(), + // ..Default::default() + // }; + + // let wheel_collider = ColliderBundle { + // material: ColliderMaterial::new(0.0, 0.0).into(), + // // default shape is a 1-meter ball + // ..Default::default() + // }; + + // let back_wheel = commands + // .spawn_bundle(back_wheel_body) + // .insert_bundle(wheel_collider) + // .insert(ColliderPositionSync::Discrete) + // .insert(ColliderDebugRender::from(Color::RED)) + // .id(); + + //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, + // back_wheel),)); +} + +pub struct CyberCollidersPlugin; +impl Plugin for CyberCollidersPlugin { + #[cfg(feature = "debug_render")] + fn build(&self, app: &mut App) { + app.add_plugin(RapierRenderPlugin) + .add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike); + } + + #[cfg(not(feature = "debug_render"))] + fn build(&self, app: &mut App) { + app.add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike); + } +} diff --git a/src/camera.rs b/src/camera.rs index d03a231..86c1c3c 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -3,7 +3,7 @@ use bevy::{ render::camera::{ActiveCameras, Camera, CameraPlugin}, }; -use crate::{input::InputState, static_geometry::CyberBikeModel}; +use crate::{bike::CyberBikeModel, input::InputState}; // 85 degrees in radians const MAX_PITCH: f32 = 1.48353; diff --git a/src/colliders.rs b/src/colliders.rs deleted file mode 100644 index 242ce55..0000000 --- a/src/colliders.rs +++ /dev/null @@ -1,213 +0,0 @@ -use bevy::{ - prelude::*, - render::mesh::{Indices, VertexAttributeValues}, -}; -use bevy_rapier3d::prelude::*; - -use crate::static_geometry::{CyberBikeModel, CyberPlanet, PLANET_RADIUS, SPAWN_ALTITUDE}; - -#[derive(Component)] -pub struct CyberBikeBody; - -#[derive(Component)] -pub struct CyberBikeCollider; - -fn setup_colliders( - mut commands: Commands, - meshes: Res>, - planet_query: Query<(Entity, &Handle), With>, - bike_query: Query<(Entity, &Transform), With>, -) { - let (planet, mesh_handle) = planet_query.single(); - - let mesh = meshes.get(mesh_handle).unwrap(); - - let mut shape = ColliderShape::ball(PLANET_RADIUS); - - let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap(); - if let VertexAttributeValues::Float32x3(verts) = vertices { - if let Some(Indices::U32(indices)) = mesh.indices() { - let mut idxs = Vec::new(); - for idx in indices.chunks_exact(3) { - idxs.push([idx[0], idx[1], idx[2]]); - } - let vertices = verts - .iter() - .map(|p| Point::from_slice(p)) - .collect::>>(); - - shape = ColliderShape::trimesh(vertices, idxs); - } - } - - let pbody = RigidBodyBundle { - body_type: RigidBodyType::Static.into(), - ccd: RigidBodyCcd { - ccd_enabled: true, - ccd_thickness: 2.0, - ccd_max_dist: SPAWN_ALTITUDE, - ..Default::default() - } - .into(), - ..Default::default() - }; - let pcollide = ColliderBundle { - shape: shape.into(), - material: ColliderMaterial { - friction: 0.05, - restitution: 0.00, - ..Default::default() - } - .into(), - ..Default::default() - }; - - commands - .entity(planet) - .insert_bundle(pbody) - .insert_bundle(pcollide); - - // bike - let (bike, xform) = bike_query.single(); - setup_bike_collider(bike, xform, &mut commands); -} - -fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) { - let mut bbody = RigidBodyBundle::default(); - - bbody.damping.angular_damping = 0.8; - bbody.damping.linear_damping = 0.1; - bbody.activation = RigidBodyActivation::cannot_sleep().into(); - - bbody.ccd = RigidBodyCcd { - ccd_enabled: true, - ccd_thickness: 0.7, - ccd_max_dist: 1.5, - ..Default::default() - } - .into(); - - let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into()); - bbody.position = isometry.into(); - - // collider - let shape = ColliderShape::capsule( - Vec3::new(0.0, 0.0, -2.7).into(), - Vec3::new(0.0, 0.0, 2.5).into(), - 1.0, - ); - let bcollide = ColliderBundle { - shape: shape.into(), - mass_properties: ColliderMassProps::Density(0.04).into(), - material: ColliderMaterial { - friction: 0.0, - restitution: 0.0, - ..Default::default() - } - .into(), - ..Default::default() - }; - commands - .entity(bike) - .insert_bundle(bbody) - .insert(CyberBikeBody) - .insert(RigidBodyPositionSync::Interpolated { prev_pos: None }) - .with_children(|child_builder| { - child_builder - .spawn_bundle(bcollide) - .insert(ColliderDebugRender { - color: Color::GREEN, - }) - .insert(CyberBikeCollider) - .insert(ColliderPositionSync::Discrete); - }); - - /* { - let wheel_collider = ColliderBundle { - material: ColliderMaterial::new(0.0, 0.0).into(), - shape: ColliderShape::ball(0.25).into(), - mass_properties: ColliderMassProps::Density(0.1).into(), - ..Default::default() - }; - - let shock = PrismaticJoint::new(Vector::y_axis()) - .local_anchor1(Vec3::new(-3.0, -3.0, -3.0).into()) - .motor_position(-1.0, 0.02, 0.5); - - let ccd = RigidBodyCcd { - ccd_enabled: true, - ccd_thickness: 0.1, - ccd_max_dist: 0.25, - ..Default::default() - }; - - let mut fw_pos = Isometry::default(); - fw_pos.translation = - (xform.translation + (xform.down() * 4.5 + (xform.forward() * 2.5))).into(); - - let front_wheel_body = RigidBodyBundle { - //activation: RigidBodyActivation::cannot_sleep().into(), - //ccd: ccd.into(), - position: fw_pos.into(), - ..Default::default() - }; - - let front_wheel = commands - .spawn_bundle(front_wheel_body) - .insert_bundle(wheel_collider) - .insert(ColliderPositionSync::Discrete) - .insert(ColliderDebugRender::from(Color::YELLOW)) - .id(); - - //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, front_wheel),)); - - // let bw_pos = Isometry::new( - // (xform.translation + xform.down() + xform.back() * 2.0).into(), - // Vec3::ZERO.into(), - // ); - - // let back_wheel_body = RigidBodyBundle { - // activation: RigidBodyActivation::cannot_sleep().into(), - // ccd: ccd.into(), - // position: bw_pos.into(), - // ..Default::default() - // }; - - // let wheel_collider = ColliderBundle { - // material: ColliderMaterial::new(0.0, 0.0).into(), - // // default shape is a 1-meter ball - // ..Default::default() - // }; - - // let back_wheel = commands - // .spawn_bundle(back_wheel_body) - // .insert_bundle(wheel_collider) - // .insert(ColliderPositionSync::Discrete) - // .insert(ColliderDebugRender::from(Color::RED)) - // .id(); - - //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, - // back_wheel),)); - - }; */ -} - -pub struct CyberCollidersPlugin; -impl Plugin for CyberCollidersPlugin { - #[cfg(feature = "debug_render")] - fn build(&self, app: &mut App) { - app.add_plugin(RapierRenderPlugin) - .add_startup_system_to_stage( - StartupStage::PostStartup, - setup_colliders.label("colliders"), - ); - } - - #[cfg(not(feature = "debug_render"))] - fn build(&self, app: &mut App) { - app.add_startup_system_to_stage( - StartupStage::PostStartup, - setup_colliders.label("colliders"), - ); - } -} diff --git a/src/glamor.rs b/src/glamor.rs index 60d3412..fccc493 100644 --- a/src/glamor.rs +++ b/src/glamor.rs @@ -10,7 +10,7 @@ use bevy::{ use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; use rand::{thread_rng, Rng}; -use crate::{lights::AnimateCyberLightWireframe, static_geometry::CyberPlanet}; +use crate::{lights::AnimateCyberLightWireframe, planet::CyberPlanet}; pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1); diff --git a/src/lib.rs b/src/lib.rs index 2b4190f..76e95ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,12 @@ use bevy::{ }; pub mod action; +pub mod bike; pub mod camera; -pub mod colliders; pub mod glamor; pub mod input; pub mod lights; -pub mod static_geometry; +pub mod planet; pub mod ui; #[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)] diff --git a/src/lights.rs b/src/lights.rs index ad42c09..5f41872 100644 --- a/src/lights.rs +++ b/src/lights.rs @@ -3,7 +3,7 @@ use std::f32::consts::TAU; use bevy::prelude::*; use rand::prelude::*; -use crate::static_geometry::PLANET_RADIUS; +use crate::planet::PLANET_RADIUS; pub const LIGHT_RANGE: f32 = 90.0; diff --git a/src/main.rs b/src/main.rs index 0ebacf9..aa114a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,9 @@ use bevy::prelude::*; use cyber_rider::{ action::{CyberActionPlugin, MovementSettings}, camera::CyberCamPlugin, - colliders::CyberCollidersPlugin, + bike::CyberCollidersPlugin, disable_mouse_trap, - static_geometry::CyberGeomPlugin, + planet::CyberGeomPlugin, glamor::CyberGlamorPlugin, input::CyberInputPlugin, lights::CyberSpaceLightsPlugin, diff --git a/src/static_geometry.rs b/src/planet.rs similarity index 74% rename from src/static_geometry.rs rename to src/planet.rs index a481b89..6afa681 100644 --- a/src/static_geometry.rs +++ b/src/planet.rs @@ -2,6 +2,7 @@ use bevy::{ prelude::{shape::Icosphere, *}, render::mesh::Indices, }; +use bevy_rapier3d::prelude::*; use hexasphere::shapes::IcoSphere; use noise::{HybridMulti, NoiseFn, SuperSimplex}; use wgpu::PrimitiveTopology; @@ -9,10 +10,6 @@ use wgpu::PrimitiveTopology; use crate::Label; pub const PLANET_RADIUS: f32 = 5000.0; -pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015; - -#[derive(Component, Debug)] -pub struct CyberBikeModel; #[derive(Component)] pub struct CyberPlanet; @@ -23,16 +20,32 @@ fn spawn_planet( mut materials: ResMut>, ) { let color = Color::rgb(0.2, 0.1, 0.2); - let isphere = shape::Icosphere { + let isphere = Icosphere { radius: PLANET_RADIUS, subdivisions: 79, }; - let pmesh = gen_planet(isphere); + let (mesh, shape) = gen_planet(isphere); + + let pbody = RigidBodyBundle { + body_type: RigidBodyType::Static.into(), + ..Default::default() + }; + + let pcollide = ColliderBundle { + shape: shape.into(), + material: ColliderMaterial { + friction: 0.05, + restitution: 0.00, + ..Default::default() + } + .into(), + ..Default::default() + }; commands .spawn_bundle(PbrBundle { - mesh: meshes.add(pmesh), + mesh: meshes.add(mesh), material: materials.add(StandardMaterial { base_color: color, metallic: 0.1, @@ -43,30 +56,15 @@ fn spawn_planet( ..Default::default() }) + .insert_bundle(pbody) + .insert_bundle(pcollide) .insert(CyberPlanet); } -fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { - commands - .spawn_bundle(( - Transform { - translation: Vec3::new(SPAWN_ALTITUDE, 0.0, 0.0), - ..Default::default() - } - .looking_at(Vec3::new(PLANET_RADIUS, 1000.0, 0.0), Vec3::X), - GlobalTransform::identity(), - )) - .with_children(|rider| { - rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0")); - }) - .insert(CyberBikeModel); -} - pub struct CyberGeomPlugin; impl Plugin for CyberGeomPlugin { fn build(&self, app: &mut App) { - app.add_startup_system(spawn_planet.label(Label::Geometry)) - .add_startup_system(spawn_cyberbike.label(Label::Geometry)); + app.add_startup_system(spawn_planet.label(Label::Geometry)); } } @@ -74,7 +72,7 @@ impl Plugin for CyberGeomPlugin { // utils //--------------------------------------------------------------------- -fn gen_planet(sphere: Icosphere) -> Mesh { +fn gen_planet(sphere: Icosphere) -> (Mesh, ColliderShape) { // straight-up stolen from Bevy's impl of Mesh from Icosphere, so I can do the // displacement before normals are calculated. let generated = IcoSphere::new(sphere.subdivisions, |point| { @@ -112,11 +110,18 @@ fn gen_planet(sphere: Icosphere) -> Mesh { generated.get_indices(i, &mut indices); } + let mut idxs = Vec::new(); + for idx in indices.chunks_exact(3) { + idxs.push([idx[0], idx[1], idx[2]]); + } + + let shape = ColliderShape::trimesh(points.iter().map(|p| Point::from_slice(p)).collect(), idxs); + let indices = Indices::U32(indices); let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); mesh.set_indices(Some(indices)); mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, points); mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uvs); - mesh + (mesh, shape) } diff --git a/src/ui.rs b/src/ui.rs index f2f3de1..6292c9f 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; -use crate::colliders::CyberBikeBody; +use crate::bike::CyberBikeBody; #[derive(Component)] struct UpText; From 315f35e8e349301a43514b1914951a8eff200fe1 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 13 Mar 2022 15:54:33 -0700 Subject: [PATCH 3/4] Adds "wheels" to cyberbike. The `parallel` feature doesn't work with joints in Rapier. --- Cargo.lock | 77 ++++++++++++-------------------- Cargo.toml | 2 +- src/bike.rs | 120 +++++++++++++++++++------------------------------- src/camera.rs | 2 +- src/glamor.rs | 39 ++++------------ src/lights.rs | 8 ++-- src/main.rs | 8 ++-- src/planet.rs | 11 ++++- 8 files changed, 103 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6705259..3a69ae8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ab_glyph" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54a65e0d4f66f8536c98cb3ca81ca33b7e2ca43442465507a3a62291ec0d9e4" +checksum = "24606928a235e73cdef55a0c909719cadd72fce573e5713d58cb2952d8f5794c" dependencies = [ "ab_glyph_rasterizer", "owned_ttf_parser", @@ -73,9 +73,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.55" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" +checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" [[package]] name = "approx" @@ -128,9 +128,9 @@ dependencies = [ [[package]] name = "async-task" -version = "4.1.0" +version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d306121baf53310a3fd342d88dc0824f6bbeace68347593658525565abee8" +checksum = "30696a84d817107fc028e049980e09d5e140e8da8f1caeb17e8e950658a3cea9" [[package]] name = "autocfg" @@ -1724,14 +1724,15 @@ checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff" [[package]] name = "mio" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +checksum = "7ba42135c6a5917b9db9cd7b293e5409e1c6b041e6f9825e92e55a894c63b6f8" dependencies = [ "libc", "log", "miow", "ntapi", + "wasi 0.11.0+wasi-snapshot-preview1", "winapi", ] @@ -1963,18 +1964,18 @@ dependencies = [ [[package]] name = "num_enum" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "720d3ea1055e4e4574c0c0b0f8c3fd4f24c4cdaf465948206dea090b57b526ad" +checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9" dependencies = [ "num_enum_derive", ] [[package]] name = "num_enum_derive" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d992b768490d7fe0d8586d9b5745f6c49f557da6d81dc982b1d167ad4edbb21" +checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -2003,9 +2004,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" +checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" [[package]] name = "owned_ttf_parser" @@ -2274,7 +2275,6 @@ dependencies = [ "num-derive", "num-traits", "parry3d", - "rayon", "rustc-hash", "simba", "vec_map", @@ -2295,31 +2295,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" -[[package]] -name = "rayon" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" -dependencies = [ - "autocfg", - "crossbeam-deque", - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" -dependencies = [ - "crossbeam-channel", - "crossbeam-deque", - "crossbeam-utils", - "lazy_static", - "num_cpus", -] - [[package]] name = "rectangle-pack" version = "0.4.2" @@ -2337,9 +2312,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.4" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" dependencies = [ "aho-corasick", "memchr", @@ -2683,9 +2658,9 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.31" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6c650a8ef0cd2dd93736f033d21cbd1224c5a967aa0c258d00fcf7dafef9b9f" +checksum = "4a1bdf54a7c28a2bbf701e1d2233f6c77f473486b94bee4f9678da5a148dca7f" dependencies = [ "cfg-if 1.0.0", "pin-project-lite", @@ -2695,9 +2670,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.19" +version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716" +checksum = "2e65ce065b4b5c53e73bb28912318cb8c9e9ad3921f1d669eb0e68b4c8143a2b" dependencies = [ "proc-macro2", "quote", @@ -2706,9 +2681,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.22" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23" +checksum = "aa31669fa42c09c34d94d8165dd2012e8ff3c66aca50f3bb226b68f216f2706c" dependencies = [ "lazy_static", "valuable", @@ -2824,6 +2799,12 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.79" diff --git a/Cargo.toml b/Cargo.toml index 705b307..5f5f031 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ features = [ git = "https://github.com/nebkor/bevy_rapier" #path = "../bevy_rapier/bevy_rapier3d" branch = "debug-render-capsule" -features = ["parallel", "simd-nightly"] +features = ["simd-nightly"] # version = "0.12" # Maybe also enable only a small amount of optimization for our code: diff --git a/src/bike.rs b/src/bike.rs index 97f0b1c..80eaa5f 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -3,7 +3,7 @@ use bevy_rapier3d::prelude::*; use crate::planet::PLANET_RADIUS; -pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015; +pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015 + 90.0; #[derive(Component)] pub struct CyberBikeBody; @@ -15,8 +15,8 @@ pub struct CyberBikeCollider; pub struct CyberBikeModel; fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { - let xform = Transform::from_translation(Vec3::X * SPAWN_ALTITUDE) - .looking_at(Vec3::new(PLANET_RADIUS, 1000.0, 0.0), Vec3::X); + let xform = Transform::from_translation(Vec3::Y * SPAWN_ALTITUDE) + .with_rotation(Quat::from_axis_angle(Vec3::X, -80.0f32.to_radians())); let mut bbody = RigidBodyBundle::default(); @@ -72,80 +72,50 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { .insert(CyberBikeBody) .id(); - add_wheels(bike, &xform, &mut commands); + // seriously the cyberbike is so fucking huge what the heck + let wheel_positions = vec![-5.5, 4.7]; + let wheel_rad = 0.7; + let wheel_y = -2.0; + + for wheel_z in wheel_positions { + let offset = Vec3::new(0.0, wheel_y, wheel_z); + let trans = xform.translation + offset; + let wheel_pos_in_world = Isometry::from_parts(trans.into(), xform.rotation.into()); + let _wheel_rb = commands + .spawn_bundle(RigidBodyBundle { + position: wheel_pos_in_world.into(), + activation: RigidBodyActivation::cannot_sleep().into(), + ccd: RigidBodyCcd { + ccd_enabled: true, + ccd_max_dist: wheel_rad, + ccd_thickness: 0.1, + ..Default::default() + } + .into(), + ..Default::default() + }) + .insert_bundle(ColliderBundle { + material: ColliderMaterial::new(0.0, 0.0).into(), + shape: ColliderShape::ball(wheel_rad).into(), + mass_properties: ColliderMassProps::Density(0.01).into(), + ..Default::default() + }) + .insert(ColliderPositionSync::Discrete) + .insert(ColliderDebugRender::from(Color::YELLOW)) + .id(); + + let (stiffness, damping) = (0.1, 0.4); + + let prismatic = PrismaticJoint::new(Vector::y_axis()) + .local_anchor1(offset.into()) + .motor_position(-0.1, stiffness, damping); + + commands.spawn_bundle(((JointBuilderComponent::new(prismatic, bike, _wheel_rb)),)); + } } -fn add_wheels(bike: Entity, xform: &Transform, mut commands: &mut Commands) { - let wheel_collider = ColliderBundle { - material: ColliderMaterial::new(0.0, 0.0).into(), - shape: ColliderShape::ball(0.25).into(), - mass_properties: ColliderMassProps::Density(0.1).into(), - ..Default::default() - }; - - let shock = PrismaticJoint::new(Vector::y_axis()) - .local_anchor1(Vec3::new(-3.0, -3.0, -3.0).into()) - .motor_position(-1.0, 0.02, 0.5); - - let ccd = RigidBodyCcd { - ccd_enabled: true, - ccd_thickness: 0.1, - ccd_max_dist: 0.25, - ..Default::default() - }; - - let mut fw_pos = Isometry::default(); - fw_pos.translation = - (xform.translation + (xform.down() * 4.5 + (xform.forward() * 2.5))).into(); - - let front_wheel_body = RigidBodyBundle { - //activation: RigidBodyActivation::cannot_sleep().into(), - //ccd: ccd.into(), - position: fw_pos.into(), - ..Default::default() - }; - - let front_wheel = commands - .spawn_bundle(front_wheel_body) - .insert_bundle(wheel_collider) - .insert(ColliderPositionSync::Discrete) - .insert(ColliderDebugRender::from(Color::YELLOW)) - .id(); - - //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, - // front_wheel),)); - - // let bw_pos = Isometry::new( - // (xform.translation + xform.down() + xform.back() * 2.0).into(), - // Vec3::ZERO.into(), - // ); - - // let back_wheel_body = RigidBodyBundle { - // activation: RigidBodyActivation::cannot_sleep().into(), - // ccd: ccd.into(), - // position: bw_pos.into(), - // ..Default::default() - // }; - - // let wheel_collider = ColliderBundle { - // material: ColliderMaterial::new(0.0, 0.0).into(), - // // default shape is a 1-meter ball - // ..Default::default() - // }; - - // let back_wheel = commands - // .spawn_bundle(back_wheel_body) - // .insert_bundle(wheel_collider) - // .insert(ColliderPositionSync::Discrete) - // .insert(ColliderDebugRender::from(Color::RED)) - // .id(); - - //commands.spawn_bundle((JointBuilderComponent::new(shock, bike, - // back_wheel),)); -} - -pub struct CyberCollidersPlugin; -impl Plugin for CyberCollidersPlugin { +pub struct CyberBikePlugin; +impl Plugin for CyberBikePlugin { #[cfg(feature = "debug_render")] fn build(&self, app: &mut App) { app.add_plugin(RapierRenderPlugin) diff --git a/src/camera.rs b/src/camera.rs index 86c1c3c..39ff72a 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -114,7 +114,7 @@ pub struct CyberCamPlugin; impl Plugin for CyberCamPlugin { fn build(&self, app: &mut bevy::prelude::App) { app.add_startup_system(setup_cybercams) - .add_state(CyberCameras::Hero) + .add_state(CyberCameras::Debug) .add_system(cycle_cam_state) .add_system(update_active_camera) .add_system(follow_cyberbike); diff --git a/src/glamor.rs b/src/glamor.rs index fccc493..ee47042 100644 --- a/src/glamor.rs +++ b/src/glamor.rs @@ -1,11 +1,6 @@ use bevy::{ - pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin}, prelude::*, - render::{ - mesh::{Indices, VertexAttributeValues}, - options::WgpuOptions, - render_resource::WgpuFeatures, - }, + render::mesh::{Indices, VertexAttributeValues}, }; use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; use rand::{thread_rng, Rng}; @@ -61,22 +56,15 @@ fn wireframe_planet( }); } -fn wireframify_lights( - mut commands: Commands, - no_wires: Query, Without)>, - wires: Query, With)>, -) { +fn wireframify_lights(mut lights: Query<&mut AnimateCyberLightWireframe>) { let chance = 0.005; let rng = &mut thread_rng(); - for e in no_wires.iter() { + + for mut light in lights.iter_mut() { if rng.gen::() < chance { - commands.entity(e).insert(Wireframe); - } - } - for e in wires.iter() { - if rng.gen::() < chance { - commands.entity(e).remove::(); + let new = !light.wired; + light.wired = new; } } } @@ -85,17 +73,8 @@ fn wireframify_lights( pub struct CyberGlamorPlugin; impl Plugin for CyberGlamorPlugin { fn build(&self, app: &mut App) { - app.insert_resource(WgpuOptions { - features: WgpuFeatures::POLYGON_MODE_LINE, - ..Default::default() - }) - .insert_resource(WireframeConfig { global: false }) - .add_startup_system_to_stage( - StartupStage::PostStartup, - wireframe_planet.after("colliders"), - ) - .add_system(wireframify_lights) - .add_plugin(PolylinePlugin) - .add_plugin(WireframePlugin); + app.add_startup_system_to_stage(StartupStage::PostStartup, wireframe_planet) + .add_system(wireframify_lights) + .add_plugin(PolylinePlugin); } } diff --git a/src/lights.rs b/src/lights.rs index 5f41872..ba0da4c 100644 --- a/src/lights.rs +++ b/src/lights.rs @@ -13,8 +13,10 @@ struct AnimatedCyberLight { rate: f32, } -#[derive(Component)] -pub(crate) struct AnimateCyberLightWireframe; +#[derive(Component, Default)] +pub(crate) struct AnimateCyberLightWireframe { + pub wired: bool, +} fn spawn_moving_lights( mut commands: Commands, @@ -82,7 +84,7 @@ fn spawn_moving_lights( }), ..Default::default() }) - .insert(AnimateCyberLightWireframe); + .insert(AnimateCyberLightWireframe::default()); }); // mesh child }); // light child } diff --git a/src/main.rs b/src/main.rs index aa114a3..ddcd75e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ use bevy::prelude::*; use cyber_rider::{ action::{CyberActionPlugin, MovementSettings}, + bike::CyberBikePlugin, camera::CyberCamPlugin, - bike::CyberCollidersPlugin, disable_mouse_trap, - planet::CyberGeomPlugin, glamor::CyberGlamorPlugin, input::CyberInputPlugin, lights::CyberSpaceLightsPlugin, + planet::CyberPlanetPlugin, ui::CyberUIPlugin, }; @@ -28,14 +28,14 @@ fn main() { }) .insert_resource(MOVEMENT_SETTINGS) .add_plugins(DefaultPlugins) - .add_plugin(CyberGeomPlugin) + .add_plugin(CyberPlanetPlugin) .add_plugin(CyberGlamorPlugin) .add_plugin(CyberInputPlugin) .add_plugin(CyberActionPlugin) .add_plugin(CyberCamPlugin) .add_plugin(CyberSpaceLightsPlugin) .add_plugin(CyberUIPlugin) - .add_plugin(CyberCollidersPlugin) + .add_plugin(CyberBikePlugin) .add_startup_system(disable_mouse_trap) .add_system(bevy::input::system::exit_on_esc_system); diff --git a/src/planet.rs b/src/planet.rs index 6afa681..1104292 100644 --- a/src/planet.rs +++ b/src/planet.rs @@ -29,6 +29,13 @@ fn spawn_planet( let pbody = RigidBodyBundle { body_type: RigidBodyType::Static.into(), + ccd: RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 0.5, + ccd_max_dist: PLANET_RADIUS * 1.05, + ..Default::default() + } + .into(), ..Default::default() }; @@ -61,8 +68,8 @@ fn spawn_planet( .insert(CyberPlanet); } -pub struct CyberGeomPlugin; -impl Plugin for CyberGeomPlugin { +pub struct CyberPlanetPlugin; +impl Plugin for CyberPlanetPlugin { fn build(&self, app: &mut App) { app.add_startup_system(spawn_planet.label(Label::Geometry)); } From 0fc179596af80030d28b9b863d3607a4ecb028c8 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 13 Mar 2022 23:37:11 -0700 Subject: [PATCH 4/4] Add more wheels and mass. --- Cargo.lock | 7 ++++--- Cargo.toml | 6 +++--- src/action.rs | 15 ++++++++++++--- src/bike.rs | 43 +++++++++++++++++++++++++++++++------------ src/camera.rs | 2 +- src/main.rs | 6 +++--- 6 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a69ae8..a0cc2cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,7 +461,8 @@ dependencies = [ [[package]] name = "bevy_rapier3d" version = "0.12.1" -source = "git+https://github.com/nebkor/bevy_rapier?branch=debug-render-capsule#3387b8ff9d3615033144cb2e22ad4a965caa6b2b" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5186c735d7aa202e982f93c71b444d34bf15fed44aca942f9b5f2940e3d06764" dependencies = [ "bevy", "nalgebra", @@ -2599,9 +2600,9 @@ checksum = "8fb1df15f412ee2e9dfc1c504260fa695c1c3f10fe9f4a6ee2d2184d7d6450e2" [[package]] name = "syn" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "1e59d925cf59d8151f25a3bedf97c9c157597c9df7324d32d68991cc399ed08b" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 5f5f031..55ffb65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,11 +26,11 @@ features = [ ] [dependencies.bevy_rapier3d] -git = "https://github.com/nebkor/bevy_rapier" +#git = "https://github.com/nebkor/bevy_rapier" #path = "../bevy_rapier/bevy_rapier3d" -branch = "debug-render-capsule" +#branch = "debug-render-capsule" features = ["simd-nightly"] -# version = "0.12" +version = "0.12" # Maybe also enable only a small amount of optimization for our code: [profile.dev] diff --git a/src/action.rs b/src/action.rs index a37bf6e..6c1a799 100644 --- a/src/action.rs +++ b/src/action.rs @@ -39,11 +39,20 @@ fn falling_cat( let cam_up = bike_xform.up(); let cos = up.dot(cam_up); + let roll_cos = up.dot(bike_xform.right()); + + let r_mag = if roll_cos.is_normal() { + roll_cos.powi(3) * 8.0 + } else { + 0.0 + }; + let r_tork = bike_xform.forward() * r_mag; + let theta = cos.acos(); - let force_mag = if !theta.is_normal() { 0.0 } else { theta * 2.0 }; + let force_mag = if !theta.is_normal() { 0.0 } else { theta * 7.0 }; let torque = cam_up.cross(up).normalize() * force_mag; - forces.torque = torque.into(); + forces.torque = (r_tork + torque).into(); } fn drag( @@ -56,7 +65,7 @@ fn drag( if let Some(vel) = vels.linvel.try_normalize(0.001) { let v2 = vels.linvel.magnitude_squared(); - forces.force -= vel * v2 * 0.002; + forces.force -= vel * v2 * 0.02; } } diff --git a/src/bike.rs b/src/bike.rs index 80eaa5f..7612b24 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -3,7 +3,7 @@ use bevy_rapier3d::prelude::*; use crate::planet::PLANET_RADIUS; -pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015 + 90.0; +pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.01; #[derive(Component)] pub struct CyberBikeBody; @@ -14,9 +14,12 @@ pub struct CyberBikeCollider; #[derive(Component, Debug)] pub struct CyberBikeModel; +const BIKE_BODY_GROUP: InteractionGroups = InteractionGroups::new(0b01, 0b01); +const BIKE_WHEEL_GROUP: InteractionGroups = InteractionGroups::new(0b10, 0b10); + fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { - let xform = Transform::from_translation(Vec3::Y * SPAWN_ALTITUDE) - .with_rotation(Quat::from_axis_angle(Vec3::X, -80.0f32.to_radians())); + let xform = Transform::from_translation(Vec3::X * SPAWN_ALTITUDE) + .with_rotation(Quat::from_axis_angle(Vec3::Z, -89.0f32.to_radians())); let mut bbody = RigidBodyBundle::default(); @@ -42,7 +45,12 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { ); let bcollide = ColliderBundle { shape: shape.into(), - mass_properties: ColliderMassProps::Density(0.04).into(), + mass_properties: ColliderMassProps::Density(0.82).into(), + flags: ColliderFlags { + collision_groups: BIKE_BODY_GROUP, + ..Default::default() + } + .into(), material: ColliderMaterial { friction: 0.0, restitution: 0.0, @@ -73,18 +81,24 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { .id(); // seriously the cyberbike is so fucking huge what the heck - let wheel_positions = vec![-5.5, 4.7]; - let wheel_rad = 0.7; - let wheel_y = -2.0; + let wheel_positions = vec![-5.1, 4.7, 4.7, -5.1]; + let wheel_rad = 1.55; + let wheel_y = -1.5f32; - for wheel_z in wheel_positions { - let offset = Vec3::new(0.0, wheel_y, wheel_z); + for (i, &wheel_z) in wheel_positions.iter().enumerate() { + let wheel_x = if i % 2 == 0 { -2.5 } else { 2.5 }; + let offset = Vec3::new(wheel_x, wheel_y, wheel_z); let trans = xform.translation + offset; let wheel_pos_in_world = Isometry::from_parts(trans.into(), xform.rotation.into()); let _wheel_rb = commands .spawn_bundle(RigidBodyBundle { position: wheel_pos_in_world.into(), activation: RigidBodyActivation::cannot_sleep().into(), + damping: RigidBodyDamping { + angular_damping: 0.8, + ..Default::default() + } + .into(), ccd: RigidBodyCcd { ccd_enabled: true, ccd_max_dist: wheel_rad, @@ -97,18 +111,23 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { .insert_bundle(ColliderBundle { material: ColliderMaterial::new(0.0, 0.0).into(), shape: ColliderShape::ball(wheel_rad).into(), - mass_properties: ColliderMassProps::Density(0.01).into(), + mass_properties: ColliderMassProps::Density(0.001).into(), + flags: ColliderFlags { + collision_groups: BIKE_WHEEL_GROUP, + ..Default::default() + } + .into(), ..Default::default() }) .insert(ColliderPositionSync::Discrete) .insert(ColliderDebugRender::from(Color::YELLOW)) .id(); - let (stiffness, damping) = (0.1, 0.4); + let (stiffness, damping) = (0.011, 0.25); let prismatic = PrismaticJoint::new(Vector::y_axis()) .local_anchor1(offset.into()) - .motor_position(-0.1, stiffness, damping); + .motor_position(-0.2, stiffness, damping); commands.spawn_bundle(((JointBuilderComponent::new(prismatic, bike, _wheel_rb)),)); } diff --git a/src/camera.rs b/src/camera.rs index 39ff72a..86c1c3c 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -114,7 +114,7 @@ pub struct CyberCamPlugin; impl Plugin for CyberCamPlugin { fn build(&self, app: &mut bevy::prelude::App) { app.add_startup_system(setup_cybercams) - .add_state(CyberCameras::Debug) + .add_state(CyberCameras::Hero) .add_system(cycle_cam_state) .add_system(update_active_camera) .add_system(follow_cyberbike); diff --git a/src/main.rs b/src/main.rs index ddcd75e..3e5ca2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,9 @@ use cyber_rider::{ }; const MOVEMENT_SETTINGS: MovementSettings = MovementSettings { - sensitivity: 10.0, // steering - accel: 30.0, // thrust - gravity: 8.0, + sensitivity: 60.0, // steering + accel: 70.0, // thrust + gravity: 7.0, }; fn main() {