get colliders from scene mesh with name "Collider", hide it in render

This commit is contained in:
yuni 2024-04-16 04:04:22 +02:00
parent efd85e1433
commit 23a85807a5
4 changed files with 31 additions and 11 deletions

Binary file not shown.

View file

@ -482,12 +482,17 @@ fn spawn_entities(
actor.insert(AngularVelocity(state.angular_momentum)); actor.insert(AngularVelocity(state.angular_momentum));
actor.insert(ColliderDensity(state.density)); actor.insert(ColliderDensity(state.density));
if state.collider_is_mesh { if state.collider_is_mesh {
actor.insert(AsyncSceneCollider::new(None)
.with_shape_for_name("Collider", ComputedCollider::TriMesh)
.with_layers_for_name("Collider", CollisionLayers::ALL)
//.with_density_for_name("Collider", state.density)
);
actor.insert(MassPropertiesBundle::new_computed( actor.insert(MassPropertiesBundle::new_computed(
&Collider::sphere(0.5 * state.model_scale as f64), state.density)); &Collider::sphere(0.5 * state.model_scale as f64), state.density));
actor.insert(AsyncSceneCollider::new(Some( //actor.insert(AsyncSceneCollider::new(Some(
ComputedCollider::TriMesh //ComputedCollider::TriMesh
//ComputedCollider::ConvexDecomposition(VHACDParameters::default()) //ComputedCollider::ConvexDecomposition(VHACDParameters::default())
))); //)));
} }
else { else {
actor.insert(state.collider.clone()); actor.insert(state.collider.clone());

View file

@ -14,10 +14,11 @@ actor 0 593051 0 suit
player yes player yes
id player id player
scale 2 scale 2
density 200
collider mesh
oxygen 0.008 oxygen 0.008
health 0.3 health 0.3
angularmomentum 0 0 0 angularmomentum 0 0 0
collider capsule 1 0.5
thrust 1.2 1 1 400 1.5 thrust 1.2 1 1 400 1.5
rotationy 0.65 rotationy 0.65
engine monopropellant engine monopropellant
@ -188,7 +189,7 @@ actor -3300 10 0 pizzeria
armodel suit_ar_chefhat armodel suit_ar_chefhat
alive yes alive yes
scale 2 scale 2
collider capsule 1 0.5 collider mesh
thrust 1.2 1 1 10 1.5 thrust 1.2 1 1 10 1.5
wants maxrotation 0 wants maxrotation 0
wants maxvelocity 0 wants maxvelocity 0
@ -203,7 +204,7 @@ actor 60 -15 -40 suit
chatid Icarus chatid Icarus
alive yes alive yes
scale 2 scale 2
collider capsule 1 0.5 collider mesh
angularmomentum 0.4 0.2 0.1 angularmomentum 0.4 0.2 0.1
rotationy 0.6 rotationy 0.6
rotationx 1 rotationx 1
@ -219,7 +220,7 @@ actor -300 0 40 suit
chatid Drifter chatid Drifter
oxygen 0.08 oxygen 0.08
scale 2 scale 2
collider capsule 1 0.5 collider mesh
actor 100 -18000 2000 "orb_busstop" actor 100 -18000 2000 "orb_busstop"
relativeto player relativeto player

View file

@ -4,7 +4,7 @@ use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::math::{DVec3, I64Vec3}; use bevy::math::{DVec3, I64Vec3};
use bevy::scene::{InstanceId, SceneInstance}; use bevy::scene::{InstanceId, SceneInstance};
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use bevy_xpbd_3d::plugins::sync::SyncConfig; use bevy_xpbd_3d::plugins::sync;
use std::collections::HashMap; use std::collections::HashMap;
use std::f32::consts::PI; use std::f32::consts::PI;
use fastrand; use fastrand;
@ -23,7 +23,7 @@ const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0";
const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0"; const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0";
pub fn asset_name_to_path(name: &str) -> &'static str { pub fn asset_name_to_path(name: &str) -> &'static str {
match name { match name {
"suit" => "models/suit.glb#Scene0", "suit" => "models/suit_with_collider.glb#Scene0",
"suit_ar_chefhat" => "models/suit_ar_chefhat.glb#Scene0", "suit_ar_chefhat" => "models/suit_ar_chefhat.glb#Scene0",
"asteroid1" => ASSET_ASTEROID1, "asteroid1" => ASSET_ASTEROID1,
"asteroid2" => ASSET_ASTEROID2, "asteroid2" => ASSET_ASTEROID2,
@ -48,6 +48,7 @@ impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Update, handle_cheats); app.add_systems(Update, handle_cheats);
app.add_systems(PreUpdate, hide_colliders); // gotta do this better
app.add_systems(PostUpdate, handle_despawn); app.add_systems(PostUpdate, handle_despawn);
app.add_systems(Update, spawn_despawn_asteroids); app.add_systems(Update, spawn_despawn_asteroids);
app.add_plugins(PhysicsPlugins::default()); app.add_plugins(PhysicsPlugins::default());
@ -62,12 +63,14 @@ impl Plugin for WorldPlugin {
if CENTER_WORLD_ON_PLAYER { if CENTER_WORLD_ON_PLAYER {
// Disable bevy_xpbd's position->transform sync function // Disable bevy_xpbd's position->transform sync function
app.insert_resource(SyncConfig { app.insert_resource(sync::SyncConfig {
position_to_transform: true, position_to_transform: true,
transform_to_position: false, transform_to_position: false,
}); });
// Add own position->transform sync function // Add own position->transform sync function
app.add_systems(PostUpdate, position_to_transform.after(bevy_xpbd_3d::plugins::sync::position_to_transform).in_set(bevy_xpbd_3d::plugins::sync::SyncSet::PositionToTransform)); app.add_systems(PostUpdate, position_to_transform
.after(sync::position_to_transform)
.in_set(sync::SyncSet::PositionToTransform));
} }
} }
} }
@ -466,6 +469,17 @@ fn handle_cheats(
} }
} }
pub fn hide_colliders(
mut q_mesh: Query<(&mut Visibility, &Name), With<Handle<Mesh>>>,
) {
for (mut visibility, name) in &mut q_mesh {
if name.as_str() == "Collider" {
*visibility = Visibility::Hidden;
}
}
}
// A variant of bevy_xpbd_3d::plugins::position_to_transform that adjusts // A variant of bevy_xpbd_3d::plugins::position_to_transform that adjusts
// the rendering position to center entities at the player camera. // the rendering position to center entities at the player camera.
// This avoids rendering glitches when very far away from the origin. // This avoids rendering glitches when very far away from the origin.