API / misc/steering2d
misc/steering2d
2D 조향 유틸.
2D steering utility.
Steering2Dextends Object
2D 조향 유틸.
- "멀면 다가가고, 사거리 안이면 거리를 유지하며 좌우로 선회" 하는 전투 AI 이동처럼 자주 재발명되는 조향 계산을 순수 함수로 모았다. 반환값은 목표 속도 벡터다.
2D steering utility.
- Gathers frequently reinvented 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.
예제Example다가가는 속도와 선회 속도 구하기Get an approach velocity and an orbit velocity
import { Steering2D, Vector2 } from "./build/vanilla.js";
const position = Vector2.create(0, 0);
const targetPosition = Vector2.create(100, 0);
const seekVelocity = Steering2D.seek(position, targetPosition, 50);
console.log(seekVelocity.x, seekVelocity.y);
const orbitVelocity = Steering2D.orbitAtRange(position, targetPosition, 80, 50, 1.0);
console.log(orbitVelocity.x, orbitVelocity.y, orbitVelocity.length());목표를 향해 다가가는 속도 반환. (정적)
Returns the velocity that approaches the target. (static)
- positionVector2
- 현재 위치.Current position.
- targetPositionVector2
- 목표 위치.Target position.
- speednumber
- 속력. (돌려주는 벡터의 길이)Speed. (length of the returned vector)
반환ReturnsVector2 목표 쪽 속도 벡터. (두 위치가 거의 같으면 영 벡터)Velocity vector toward the target. (a zero vector if the two positions are nearly the same)
짝Counterpart: vanilla-engine Steering2D.seek
정적static
static orbitAtRange(position, targetPosition, preferredRange, speed, elapsedSeconds, strafeRate = 1.7)#사거리를 유지하며 선회하는 속도 반환. (정적)
- 사거리보다 멀면 접근, 가까우면 후퇴 성분을 섞고, 접선 방향으로 sin 파형 좌우 선회(strafe)를 더해 살아 있는 움직임을 만든다.
Returns the velocity that strafes while keeping range. (static)
- Mixes in an approach component when farther than the range and a retreat component when closer, and adds a sin-wave left-right strafe along the tangent to create lively movement.
- positionVector2
- 현재 위치.Current position.
- targetPositionVector2
- 목표 위치.Target position.
- preferredRangenumber
- 유지하려는 거리.Distance to keep.
- speednumber
- 속력. (돌려주는 벡터의 길이)Speed. (length of the returned vector)
- elapsedSecondsnumber
- 선회 위상에 쓰는 누적 시간.Accumulated time used for the strafe phase.
- strafeRatenumber
- 선회 파형 각속도. (기본 1.7)Angular speed of the strafe wave. (default 1.7)
반환ReturnsVector2 목표 속도 벡터. (길이는 speed, 두 위치가 거의 같으면 (speed, 0), 원하는 방향이 0 이면 영 벡터)Desired velocity vector. (Its length is speed; (speed, 0) if the two positions are nearly the same; a zero vector if the desired direction is zero)
짝Counterpart: vanilla-engine Steering2D.orbit_at_range
예제Example프레임마다 누적 시간을 넘겨 목표 둘레를 돌기Circle around a target by passing the accumulated time every frame
import { Steering2D, Vector2 } from "./build/vanilla.js";
const targetPosition = Vector2.create(0, 0);
let position = Vector2.create(200, 0);
let elapsedSeconds = 0;
const timeDelta = 1 / 60;
for (let frame = 0; frame < 180; ++frame) {
elapsedSeconds += timeDelta;
const velocity = Steering2D.orbitAtRange(position, targetPosition, 100, 80, elapsedSeconds);
const step = velocity.multiply(timeDelta);
position = position.add(step);
}
console.log(position.x, position.y, position.length());