-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8c23f4
commit 7ee3501
Showing
2 changed files
with
99 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::prelude::*; | ||
|
||
/// Inspired by [`CWeaponPhysCannon::FindObjectInCone`](https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/game/server/hl2/weapon_physcannon.cpp#L2690) | ||
pub(super) fn find_object_in_cone( | ||
In(actor_entity): In<Entity>, | ||
spatial_query: SpatialQuery, | ||
q_actor: Query<(&GlobalTransform, &AvianPickupActor)>, | ||
q_collider: Query<&ColliderParent>, | ||
q_rigid_body: Query<(&RigidBody, &GlobalTransform)>, | ||
) -> Option<Entity> { | ||
let (origin, config) = q_actor.get(actor_entity).unwrap(); | ||
|
||
let origin = origin.compute_transform(); | ||
|
||
let mut nearest_dist = config.trace_length + 1.0; | ||
let box_collider = Cuboid::from_size(Vec3::splat(2.0 * nearest_dist)).into(); | ||
|
||
let colliders = spatial_query.shape_intersections( | ||
&box_collider, | ||
origin.translation, | ||
origin.rotation, | ||
config.spatial_query_filter.clone(), | ||
); | ||
let mut nearest_entity = None; | ||
|
||
for collider in colliders { | ||
let rigid_body_entity = q_collider | ||
.get(collider) | ||
.expect("`shape_intersections` returned something without a `Collider`") | ||
.get(); | ||
let (&rigid_body, object_transform) = q_rigid_body | ||
.get(rigid_body_entity) | ||
.expect("Failed to get `RigidBody` for entity"); | ||
if rigid_body != RigidBody::Dynamic { | ||
continue; | ||
} | ||
let object_translation = object_transform.translation(); | ||
|
||
// Closer than other objects | ||
let los = object_translation - origin.translation; | ||
let (los, dist) = Dir3::new_and_length(los).expect("Failed to normalize line of sight"); | ||
if dist >= nearest_dist { | ||
continue; | ||
} | ||
|
||
// Cull to the cone | ||
let max_dot = config.cone; | ||
if los.dot(origin.forward().into()) <= max_dot { | ||
continue; | ||
} | ||
|
||
// Make sure it isn't occluded! | ||
if let Some(hit) = spatial_query.cast_ray( | ||
origin.translation, | ||
los, | ||
dist, | ||
true, | ||
config.spatial_query_filter.clone(), | ||
) { | ||
if hit.entity == rigid_body_entity { | ||
nearest_dist = dist; | ||
nearest_entity.replace(rigid_body_entity); | ||
} | ||
} | ||
} | ||
nearest_entity | ||
} |