misc/steering2d
2D 조향 유틸.
- 원본 misc/steering2d.js 를 옮긴 것이다. "멀면 다가가고, 사거리 안이면 거리를 유지하며 좌우로 선회" 하는 전투 AI 이동처럼 자주 다시 만들던 조향 계산을 순수 함수로 모았다. 돌려주는 값은 목표 속도 벡터다.
- 원본과 다른 점: orbit_at_range 의 strafe_rate 기본값(1.7)은 러스트에 기본 인자가 없어 DEFAULT_STRAFE_RATE 상수로 넘긴다.
2D steering utility.
- A port of the original misc/steering2d.js. Gathers frequently rebuilt steering calculations, like combat AI movement that "approaches when far, and keeps distance while circling left and right when within range", into pure functions. The return value is the desired velocity vector.
- Differences from the original: The strafe_rate default (1.7) of orbit_at_range is passed as the DEFAULT_STRAFE_RATE constant because Rust has no default arguments.
Steering2Dstruct
2D 조향 유틸. (정적 함수 묶음)
- 상태가 없다. 매 프레임 목표 속도를 받아 위치에 time_delta 를 곱해 더한다.
2D steering utility. (a set of static functions)
- Stateless. Each frame, take the desired velocity, multiply by time_delta, and add it to the position.
use vanilla::{Steering2D, Vector2};
let mut position = Vector2::create(0.0, 0.0);
let target_position = Vector2::create(30.0, 40.0);
let velocity = Steering2D::seek(position, target_position, 10.0);
assert!((velocity.x - 6.0).abs() < 0.0001);
assert!((velocity.y - 8.0).abs() < 0.0001);
let time_delta = 0.5;
position = position.add(velocity.multiply_scalar(time_delta));
assert!((position.x - 3.0).abs() < 0.0001);orbit_at_range 의 기본 선회 파형 각속도. (원본 strafeRate = 1.7)
Default angular speed of the strafing wave of orbit_at_range. (original strafeRate = 1.7)
반환Returnsf64
목표를 향해 다가가는 속도. (거의 같은 자리면 0)
Velocity approaching the target. (0 if at nearly the same spot)
- positionVector2
- 지금 위치.Current position.
- target_positionVector2
- 목표 위치.Target position.
- speedf64
- 속력. (돌려줄 벡터의 길이, 초당 거리)Speed. (length of the returned vector, distance per second)
반환ReturnsVector2 목표 쪽으로 길이가 speed 인 속도 벡터. 두 위치 사이 거리가 0.000001 보다 작으면 (0, 0).A velocity vector of length speed toward the target. (0, 0) if the distance between the two positions is less than 0.000001.
짝Counterpart: vanilla.js Steering2D.seek
pub fn orbit_at_range(position: Vector2, target_position: Vector2, preferred_range: f64, speed: f64, elapsed_seconds: f64, strafe_rate: f64) -> Vector2#사거리를 유지하며 선회하는 속도.
- 사거리보다 멀면 접근, 가까우면 후퇴 성분을 섞고, 접선 방향으로 sin 파형 좌우 선회를 더해 살아 있는 움직임을 만든다.
- elapsed_seconds 는 선회 위상에 쓰는 누적 시간이다.
Velocity that circles while keeping a range.
- Mixes in an approach component when farther than the range and a retreat component when closer, and adds sine-wave left and right strafing along the tangent to make the movement feel alive.
- elapsed_seconds is the accumulated time used for the strafe phase.
- positionVector2
- 지금 위치.Current position.
- target_positionVector2
- 목표 위치.Target position.
- preferred_rangef64
- 유지할 거리. (거리 오차를 이 값으로 나눠 -1 ~ 1 로 자른다)Distance to keep. (the distance error is divided by this value and clamped to -1 ~ 1)
- speedf64
- 속력. (돌려줄 벡터의 길이)Speed. (length of the returned vector)
- elapsed_secondsf64
- 누적 시간. (초, 선회 파형 sin(elapsed_seconds * strafe_rate) 의 위상)Accumulated time. (seconds, phase of the strafe waveform sin(elapsed_seconds * strafe_rate))
- strafe_ratef64
- 선회 파형 각속도. (라디안/초, 보통 DEFAULT_STRAFE_RATE)Strafe waveform angular speed. (radians/second, usually DEFAULT_STRAFE_RATE)
반환ReturnsVector2 길이가 speed 인 속도 벡터. 두 위치가 거의 같으면 (speed, 0), 사거리에 있고 선회 성분도 0 이면 (0, 0).A velocity vector of length speed. (speed, 0) if the two positions are nearly the same, (0, 0) if at range with a strafe component of 0.
짝Counterpart: vanilla.js Steering2D.orbitAtRange
use vanilla::{Steering2D, Vector2};
let target_position = Vector2::create(100.0, 0.0);
let far_velocity = Steering2D::orbit_at_range(Vector2::create(-200.0, 0.0), target_position, 100.0, 5.0, 0.0, Steering2D::DEFAULT_STRAFE_RATE);
assert!((far_velocity.x - 5.0).abs() < 0.0001);
let near_velocity = Steering2D::orbit_at_range(Vector2::create(50.0, 0.0), target_position, 100.0, 5.0, 0.0, Steering2D::DEFAULT_STRAFE_RATE);
assert!((near_velocity.x + 5.0).abs() < 0.0001);