Merge branch 'bike-fields'

This commit is contained in:
Joe Ardent 2022-03-13 23:37:55 -07:00
commit 97c9b637ef
12 changed files with 270 additions and 280 deletions

92
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "ab_glyph" name = "ab_glyph"
version = "0.2.14" version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54a65e0d4f66f8536c98cb3ca81ca33b7e2ca43442465507a3a62291ec0d9e4" checksum = "24606928a235e73cdef55a0c909719cadd72fce573e5713d58cb2952d8f5794c"
dependencies = [ dependencies = [
"ab_glyph_rasterizer", "ab_glyph_rasterizer",
"owned_ttf_parser", "owned_ttf_parser",
@ -73,9 +73,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.55" version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27"
[[package]] [[package]]
name = "approx" name = "approx"
@ -128,9 +128,9 @@ dependencies = [
[[package]] [[package]]
name = "async-task" name = "async-task"
version = "4.1.0" version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d306121baf53310a3fd342d88dc0824f6bbeace68347593658525565abee8" checksum = "30696a84d817107fc028e049980e09d5e140e8da8f1caeb17e8e950658a3cea9"
[[package]] [[package]]
name = "autocfg" name = "autocfg"
@ -461,7 +461,8 @@ dependencies = [
[[package]] [[package]]
name = "bevy_rapier3d" name = "bevy_rapier3d"
version = "0.12.1" 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 = [ dependencies = [
"bevy", "bevy",
"nalgebra", "nalgebra",
@ -740,9 +741,9 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.7.3" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439989e6b8c38d1b6570a384ef1e49c8848128f5a97f3914baef02920842712f" checksum = "0e851ca7c24871e7336801608a4797d7376545b6928a10d32d75685687141ead"
dependencies = [ dependencies = [
"bytemuck_derive", "bytemuck_derive",
] ]
@ -1724,14 +1725,15 @@ checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff"
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.8.0" version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" checksum = "7ba42135c6a5917b9db9cd7b293e5409e1c6b041e6f9825e92e55a894c63b6f8"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
"miow", "miow",
"ntapi", "ntapi",
"wasi 0.11.0+wasi-snapshot-preview1",
"winapi", "winapi",
] ]
@ -1963,18 +1965,18 @@ dependencies = [
[[package]] [[package]]
name = "num_enum" name = "num_enum"
version = "0.5.6" version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "720d3ea1055e4e4574c0c0b0f8c3fd4f24c4cdaf465948206dea090b57b526ad" checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9"
dependencies = [ dependencies = [
"num_enum_derive", "num_enum_derive",
] ]
[[package]] [[package]]
name = "num_enum_derive" name = "num_enum_derive"
version = "0.5.6" version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d992b768490d7fe0d8586d9b5745f6c49f557da6d81dc982b1d167ad4edbb21" checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2003,9 +2005,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.9.0" version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]] [[package]]
name = "owned_ttf_parser" name = "owned_ttf_parser"
@ -2274,7 +2276,6 @@ dependencies = [
"num-derive", "num-derive",
"num-traits", "num-traits",
"parry3d", "parry3d",
"rayon",
"rustc-hash", "rustc-hash",
"simba", "simba",
"vec_map", "vec_map",
@ -2295,31 +2296,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" 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]] [[package]]
name = "rectangle-pack" name = "rectangle-pack"
version = "0.4.2" version = "0.4.2"
@ -2337,9 +2313,9 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.5.4" version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -2624,9 +2600,9 @@ checksum = "8fb1df15f412ee2e9dfc1c504260fa695c1c3f10fe9f4a6ee2d2184d7d6450e2"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.86" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" checksum = "1e59d925cf59d8151f25a3bedf97c9c157597c9df7324d32d68991cc399ed08b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2635,9 +2611,9 @@ dependencies = [
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.2" version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [ dependencies = [
"winapi-util", "winapi-util",
] ]
@ -2683,9 +2659,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.31" version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6c650a8ef0cd2dd93736f033d21cbd1224c5a967aa0c258d00fcf7dafef9b9f" checksum = "4a1bdf54a7c28a2bbf701e1d2233f6c77f473486b94bee4f9678da5a148dca7f"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"pin-project-lite", "pin-project-lite",
@ -2695,9 +2671,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-attributes" name = "tracing-attributes"
version = "0.1.19" version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716" checksum = "2e65ce065b4b5c53e73bb28912318cb8c9e9ad3921f1d669eb0e68b4c8143a2b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2706,9 +2682,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.22" version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23" checksum = "aa31669fa42c09c34d94d8165dd2012e8ff3c66aca50f3bb226b68f216f2706c"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"valuable", "valuable",
@ -2824,6 +2800,12 @@ version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 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]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.79" version = "0.2.79"

View file

@ -26,11 +26,11 @@ features = [
] ]
[dependencies.bevy_rapier3d] [dependencies.bevy_rapier3d]
git = "https://github.com/nebkor/bevy_rapier" #git = "https://github.com/nebkor/bevy_rapier"
#path = "../bevy_rapier/bevy_rapier3d" #path = "../bevy_rapier/bevy_rapier3d"
branch = "debug-render-capsule" #branch = "debug-render-capsule"
features = ["parallel", "simd-nightly"] features = ["simd-nightly"]
# version = "0.12" version = "0.12"
# Maybe also enable only a small amount of optimization for our code: # Maybe also enable only a small amount of optimization for our code:
[profile.dev] [profile.dev]

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use bevy_rapier3d::{na::Vector3, prelude::*}; use bevy_rapier3d::{na::Vector3, prelude::*};
use crate::{ use crate::{
colliders::{CyberBikeBody, CyberBikeCollider}, bike::{CyberBikeBody, CyberBikeCollider},
input::InputState, input::InputState,
}; };
@ -39,11 +39,20 @@ fn falling_cat(
let cam_up = bike_xform.up(); let cam_up = bike_xform.up();
let cos = up.dot(cam_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 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; let torque = cam_up.cross(up).normalize() * force_mag;
forces.torque = torque.into(); forces.torque = (r_tork + torque).into();
} }
fn drag( fn drag(
@ -56,7 +65,7 @@ fn drag(
if let Some(vel) = vels.linvel.try_normalize(0.001) { if let Some(vel) = vels.linvel.try_normalize(0.001) {
let v2 = vels.linvel.magnitude_squared(); let v2 = vels.linvel.magnitude_squared();
forces.force -= vel * v2 * 0.002; forces.force -= vel * v2 * 0.02;
} }
} }

148
src/bike.rs Normal file
View file

@ -0,0 +1,148 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::planet::PLANET_RADIUS;
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.01;
#[derive(Component)]
pub struct CyberBikeBody;
#[derive(Component)]
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<AssetServer>) {
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();
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.82).into(),
flags: ColliderFlags {
collision_groups: BIKE_BODY_GROUP,
..Default::default()
}
.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();
// seriously the cyberbike is so fucking huge what the heck
let wheel_positions = vec![-5.1, 4.7, 4.7, -5.1];
let wheel_rad = 1.55;
let wheel_y = -1.5f32;
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,
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.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.011, 0.25);
let prismatic = PrismaticJoint::new(Vector::y_axis())
.local_anchor1(offset.into())
.motor_position(-0.2, stiffness, damping);
commands.spawn_bundle(((JointBuilderComponent::new(prismatic, bike, _wheel_rb)),));
}
}
pub struct CyberBikePlugin;
impl Plugin for CyberBikePlugin {
#[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);
}
}

View file

@ -3,7 +3,7 @@ use bevy::{
render::camera::{ActiveCameras, Camera, CameraPlugin}, render::camera::{ActiveCameras, Camera, CameraPlugin},
}; };
use crate::{geometry::CyberBikeModel, input::InputState}; use crate::{bike::CyberBikeModel, input::InputState};
// 85 degrees in radians // 85 degrees in radians
const MAX_PITCH: f32 = 1.48353; const MAX_PITCH: f32 = 1.48353;

View file

@ -1,142 +0,0 @@
use bevy::{
prelude::*,
render::mesh::{Indices, VertexAttributeValues},
};
use bevy_rapier3d::prelude::*;
use crate::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<Assets<Mesh>>,
planet_query: Query<(Entity, &Handle<Mesh>), With<CyberPlanet>>,
bike_query: Query<(Entity, &Transform), With<CyberBikeModel>>,
) {
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::<Vec<Point<_>>>();
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 is the easy part
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.0).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::Discrete)
.with_children(|e| {
e.spawn_bundle(bcollide)
.insert(ColliderDebugRender {
color: Color::GREEN,
})
.insert(CyberBikeCollider)
.insert(ColliderPositionSync::Discrete);
});
}
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"),
);
}
}

View file

@ -1,16 +1,11 @@
use bevy::{ use bevy::{
pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin},
prelude::*, prelude::*,
render::{ render::mesh::{Indices, VertexAttributeValues},
mesh::{Indices, VertexAttributeValues},
options::WgpuOptions,
render_resource::WgpuFeatures,
},
}; };
use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin};
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use crate::{geometry::CyberPlanet, lights::AnimateCyberLightWireframe}; use crate::{lights::AnimateCyberLightWireframe, planet::CyberPlanet};
pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1); pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1);
@ -61,22 +56,15 @@ fn wireframe_planet(
}); });
} }
fn wireframify_lights( fn wireframify_lights(mut lights: Query<&mut AnimateCyberLightWireframe>) {
mut commands: Commands,
no_wires: Query<Entity, (With<AnimateCyberLightWireframe>, Without<Wireframe>)>,
wires: Query<Entity, (With<AnimateCyberLightWireframe>, With<Wireframe>)>,
) {
let chance = 0.005; let chance = 0.005;
let rng = &mut thread_rng(); let rng = &mut thread_rng();
for e in no_wires.iter() {
for mut light in lights.iter_mut() {
if rng.gen::<f32>() < chance { if rng.gen::<f32>() < chance {
commands.entity(e).insert(Wireframe); let new = !light.wired;
} light.wired = new;
}
for e in wires.iter() {
if rng.gen::<f32>() < chance {
commands.entity(e).remove::<Wireframe>();
} }
} }
} }
@ -85,17 +73,8 @@ fn wireframify_lights(
pub struct CyberGlamorPlugin; pub struct CyberGlamorPlugin;
impl Plugin for CyberGlamorPlugin { impl Plugin for CyberGlamorPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(WgpuOptions { app.add_startup_system_to_stage(StartupStage::PostStartup, wireframe_planet)
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_system(wireframify_lights)
.add_plugin(PolylinePlugin) .add_plugin(PolylinePlugin);
.add_plugin(WireframePlugin);
} }
} }

View file

@ -4,12 +4,12 @@ use bevy::{
}; };
pub mod action; pub mod action;
pub mod bike;
pub mod camera; pub mod camera;
pub mod colliders;
pub mod geometry;
pub mod glamor; pub mod glamor;
pub mod input; pub mod input;
pub mod lights; pub mod lights;
pub mod planet;
pub mod ui; pub mod ui;
#[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)] #[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)]

View file

@ -3,7 +3,7 @@ use std::f32::consts::TAU;
use bevy::prelude::*; use bevy::prelude::*;
use rand::prelude::*; use rand::prelude::*;
use crate::geometry::PLANET_RADIUS; use crate::planet::PLANET_RADIUS;
pub const LIGHT_RANGE: f32 = 90.0; pub const LIGHT_RANGE: f32 = 90.0;
@ -13,8 +13,10 @@ struct AnimatedCyberLight {
rate: f32, rate: f32,
} }
#[derive(Component)] #[derive(Component, Default)]
pub(crate) struct AnimateCyberLightWireframe; pub(crate) struct AnimateCyberLightWireframe {
pub wired: bool,
}
fn spawn_moving_lights( fn spawn_moving_lights(
mut commands: Commands, mut commands: Commands,
@ -82,7 +84,7 @@ fn spawn_moving_lights(
}), }),
..Default::default() ..Default::default()
}) })
.insert(AnimateCyberLightWireframe); .insert(AnimateCyberLightWireframe::default());
}); // mesh child }); // mesh child
}); // light child }); // light child
} }

View file

@ -1,20 +1,20 @@
use bevy::prelude::*; use bevy::prelude::*;
use cyber_rider::{ use cyber_rider::{
action::{CyberActionPlugin, MovementSettings}, action::{CyberActionPlugin, MovementSettings},
bike::CyberBikePlugin,
camera::CyberCamPlugin, camera::CyberCamPlugin,
colliders::CyberCollidersPlugin,
disable_mouse_trap, disable_mouse_trap,
geometry::CyberGeomPlugin,
glamor::CyberGlamorPlugin, glamor::CyberGlamorPlugin,
input::CyberInputPlugin, input::CyberInputPlugin,
lights::CyberSpaceLightsPlugin, lights::CyberSpaceLightsPlugin,
planet::CyberPlanetPlugin,
ui::CyberUIPlugin, ui::CyberUIPlugin,
}; };
const MOVEMENT_SETTINGS: MovementSettings = MovementSettings { const MOVEMENT_SETTINGS: MovementSettings = MovementSettings {
sensitivity: 10.0, // steering sensitivity: 60.0, // steering
accel: 30.0, // thrust accel: 70.0, // thrust
gravity: 8.0, gravity: 7.0,
}; };
fn main() { fn main() {
@ -28,14 +28,14 @@ fn main() {
}) })
.insert_resource(MOVEMENT_SETTINGS) .insert_resource(MOVEMENT_SETTINGS)
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(CyberGeomPlugin) .add_plugin(CyberPlanetPlugin)
.add_plugin(CyberGlamorPlugin) .add_plugin(CyberGlamorPlugin)
.add_plugin(CyberInputPlugin) .add_plugin(CyberInputPlugin)
.add_plugin(CyberActionPlugin) .add_plugin(CyberActionPlugin)
.add_plugin(CyberCamPlugin) .add_plugin(CyberCamPlugin)
.add_plugin(CyberSpaceLightsPlugin) .add_plugin(CyberSpaceLightsPlugin)
.add_plugin(CyberUIPlugin) .add_plugin(CyberUIPlugin)
.add_plugin(CyberCollidersPlugin) .add_plugin(CyberBikePlugin)
.add_startup_system(disable_mouse_trap) .add_startup_system(disable_mouse_trap)
.add_system(bevy::input::system::exit_on_esc_system); .add_system(bevy::input::system::exit_on_esc_system);

View file

@ -2,6 +2,7 @@ use bevy::{
prelude::{shape::Icosphere, *}, prelude::{shape::Icosphere, *},
render::mesh::Indices, render::mesh::Indices,
}; };
use bevy_rapier3d::prelude::*;
use hexasphere::shapes::IcoSphere; use hexasphere::shapes::IcoSphere;
use noise::{HybridMulti, NoiseFn, SuperSimplex}; use noise::{HybridMulti, NoiseFn, SuperSimplex};
use wgpu::PrimitiveTopology; use wgpu::PrimitiveTopology;
@ -9,10 +10,6 @@ use wgpu::PrimitiveTopology;
use crate::Label; use crate::Label;
pub const PLANET_RADIUS: f32 = 5000.0; 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)] #[derive(Component)]
pub struct CyberPlanet; pub struct CyberPlanet;
@ -23,16 +20,39 @@ fn spawn_planet(
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let color = Color::rgb(0.2, 0.1, 0.2); let color = Color::rgb(0.2, 0.1, 0.2);
let isphere = shape::Icosphere { let isphere = Icosphere {
radius: PLANET_RADIUS, radius: PLANET_RADIUS,
subdivisions: 79, subdivisions: 79,
}; };
let pmesh = gen_planet(isphere); let (mesh, shape) = gen_planet(isphere);
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()
};
let pcollide = ColliderBundle {
shape: shape.into(),
material: ColliderMaterial {
friction: 0.05,
restitution: 0.00,
..Default::default()
}
.into(),
..Default::default()
};
commands commands
.spawn_bundle(PbrBundle { .spawn_bundle(PbrBundle {
mesh: meshes.add(pmesh), mesh: meshes.add(mesh),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: color, base_color: color,
metallic: 0.1, metallic: 0.1,
@ -43,30 +63,15 @@ fn spawn_planet(
..Default::default() ..Default::default()
}) })
.insert_bundle(pbody)
.insert_bundle(pcollide)
.insert(CyberPlanet); .insert(CyberPlanet);
} }
fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) { pub struct CyberPlanetPlugin;
commands impl Plugin for CyberPlanetPlugin {
.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) { fn build(&self, app: &mut App) {
app.add_startup_system(spawn_planet.label(Label::Geometry)) app.add_startup_system(spawn_planet.label(Label::Geometry));
.add_startup_system(spawn_cyberbike.label(Label::Geometry));
} }
} }
@ -74,7 +79,7 @@ impl Plugin for CyberGeomPlugin {
// utils // 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 // straight-up stolen from Bevy's impl of Mesh from Icosphere, so I can do the
// displacement before normals are calculated. // displacement before normals are calculated.
let generated = IcoSphere::new(sphere.subdivisions, |point| { let generated = IcoSphere::new(sphere.subdivisions, |point| {
@ -112,11 +117,18 @@ fn gen_planet(sphere: Icosphere) -> Mesh {
generated.get_indices(i, &mut indices); 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 indices = Indices::U32(indices);
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_indices(Some(indices)); mesh.set_indices(Some(indices));
mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, points); mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, points);
mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uvs); mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
mesh (mesh, shape)
} }

View file

@ -1,7 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::colliders::CyberBikeBody; use crate::bike::CyberBikeBody;
#[derive(Component)] #[derive(Component)]
struct UpText; struct UpText;