Enhance variety of Hit detection ray solid
rotate the ray solid per unit time to compensate with low variety of rays
Simply take the common of all of the vectors and normalize it. Due to the normalizing, it can work advantageous.
It could introduce a little bit of jitter, so to stop it it is best to clean out the output vector primarily based on the earlier occasion vector.
_avoidance_vector_smoothed = lerp(
_avoidance_vector_smoothed,
avoidance_vec.normalized(),
avoidance_smoothing_speed * delta
).normalized()
You must also do the identical factor with the earlier occasion enter vectors.
It is best to add the little bit of the vector behind when you detected successful in entrance – see that -
sign up -0.8
beneath:
var hit_dir := (information.hit_pos - global_position).normalized()
avoidance_vec += hit_dir / pow(information.openness, 2) * -0.8
Under is my model of the code
var _ray_rotation_offset := 0.0 # retailer rotation between frames
func update_vision(delta: float) -> void:
# scan cone + rear to seek out legitimate targets
var eye_pos = global_position
var found_target: Node2D = null
var facing_angle = move_direction.angle()
_sample_info.clear()
_min_openness = 1.0
_ray_rotation_offset += (TAU / sense_ray_count) * delta # used this system to scale back the gap between consecutive ray hint.
_ray_rotation_offset = wrapf(_ray_rotation_offset, 0.0, TAU)
# collect samples across the agent
# rear / peripheral sense (shorter)
for i in vary(sense_ray_count):
var angle = TAU * i / sense_ray_count + _ray_rotation_offset
var dir: Vector2 = Vector2.from_angle(angle)
var hit: Dictionary = _raycast(eye_pos, eye_pos + dir * sense_distance)
var openness := 1.0
var hit_pos: Vector2 = Vector2.ZERO
if hit:
hit_pos = hit.place
var dist = eye_pos.distance_to(hit_pos)
openness = clamp(dist / sense_distance, 0.0, 1.0)
_sample_info.append(SampleInfo.new(dir, openness, hit_pos))
_min_openness = min(_min_openness, openness)
if hit and is_instance_valid(hit.collider) and _is_valid_target(hit.collider):
found_target = hit.collider
func _get_avoidance_vector(delta: float) -> Vector2:
var desired_dir := move_direction.normalized() if !move_direction.is_zero_approx() else Vector2.RIGHT
var avoidance_vec := Vector2.ZERO
for information in _sample_info:
if not information.hit_pos.is_zero_approx():
# If there's a hit, push away from hit path
var hit_dir := (information.hit_pos - global_position).normalized()
avoidance_vec += hit_dir / pow(information.openness, 2) * -0.8
else:
# Bias towards desired path
var dir_dot := desired_dir.dot(information.dir)
avoidance_vec += information.dir * dir_dot
_avoidance_vector_smoothed = lerp(_avoidance_vector_smoothed, avoidance_vec.normalized(), avoidance_smoothing_speed * delta).normalized()
# Normalize to keep away from overshooting or undershoot
return _avoidance_vector_smoothed