From 115b31568fd1b98a7c57f7d84394b35a9ac66b68 Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Thu, 9 Jan 2025 14:25:18 -0700 Subject: [PATCH] Shot setup snaps to ground using raycast --- src/equipment/balls/physics_ball/game_ball.gd | 17 +++++++++++++++++ .../physics_ball/normal_terrain_physics.tres | 2 +- src/player/shot_setup/shot_setup.gd | 5 +---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/equipment/balls/physics_ball/game_ball.gd b/src/equipment/balls/physics_ball/game_ball.gd index b2bbbb2..3c33d2c 100644 --- a/src/equipment/balls/physics_ball/game_ball.gd +++ b/src/equipment/balls/physics_ball/game_ball.gd @@ -15,6 +15,7 @@ enum Type { } const MAGNUS_SQ_EPSILON := 1e-3 +const SURFACE_SNAP_DISTANCE := 2.0 ## If enabled, ball ability cooldown is only reset at end of shot. @export var once_per_shot_ability := false @@ -117,6 +118,22 @@ func get_damage() -> float: return base_damage + linear_velocity.length_squared() * damage_force_scale +## Get the surface position immediately under the ball. +## This will typically be the surface the ball is laying on. +## If the ball is not on a surface, this will be the position of the ball itself. +func get_surface_snap_point() -> Vector3: + var params := PhysicsRayQueryParameters3D.create( + global_position, + global_position - get_reoriented_basis().y * SURFACE_SNAP_DISTANCE, + collision_mask, + [get_rid()] + ) + var collisions := get_world_3d().direct_space_state.intersect_ray(params) + if collisions: + return collisions["position"] as Vector3 + return global_position + + func _magnus_force() -> Vector3: return magnus_coefficient * radius * angular_velocity.cross(linear_velocity) diff --git a/src/equipment/balls/physics_ball/normal_terrain_physics.tres b/src/equipment/balls/physics_ball/normal_terrain_physics.tres index 6c5698c..2ad35ce 100644 --- a/src/equipment/balls/physics_ball/normal_terrain_physics.tres +++ b/src/equipment/balls/physics_ball/normal_terrain_physics.tres @@ -12,7 +12,7 @@ impact_damp = 0.05 [sub_resource type="Resource" id="Resource_3k63c"] script = ExtResource("1_45pis") linear_damp = 0.0 -angular_damp = 0.0 +angular_damp = 0.01 impact_damp = 0.0 [sub_resource type="Resource" id="Resource_xf73q"] diff --git a/src/player/shot_setup/shot_setup.gd b/src/player/shot_setup/shot_setup.gd index e92f688..ced4071 100644 --- a/src/player/shot_setup/shot_setup.gd +++ b/src/player/shot_setup/shot_setup.gd @@ -393,7 +393,7 @@ func travel_to_ball() -> void: return game_ball.freeze = true - global_position = game_ball.global_position + global_position = game_ball.get_surface_snap_point() # Re-orient to the ball's last contact normal if there is one. # Normally this will just be Vector3.UP or something close to it. @@ -401,9 +401,6 @@ func travel_to_ball() -> void: _target_rotation.y = 0 global_basis = game_ball.get_reoriented_basis() - # Adjust position downward to account for ball radius - global_position -= global_basis.y.normalized() * game_ball.radius - ball_point.snap()