misc/collision2d
2D 충돌 유틸.
2D collision utility.
Collision2Dextends Object
2D 충돌 유틸.
- 원-원 겹침 분리(밀어내기)와 반경 최근접 탐색처럼 액션 게임마다 재발명되던 판정을 모았다.
2D collision utility.
- Gathers tests reinvented in every action game, like circle-circle overlap separation (pushing apart) and nearest search within a radius.
import { Collision2D, Vector2 } from "./build/vanilla.js";
let positionA = Vector2.create(0, 0);
let positionB = Vector2.create(15, 0);
console.log(Collision2D.testCircleOverlap(positionA, 10, positionB, 10));
const push = Collision2D.resolveCircleOverlap(positionA, 10, positionB, 10);
if (push !== null) {
positionA = positionA.add(push.pushA);
positionB = positionB.add(push.pushB);
}
console.log(positionA.x, positionB.x);
console.log(Collision2D.testCircleOverlap(positionA, 10, positionB, 10));원-원 겹침 검사. (정적)
Circle-circle overlap check. (static)
- positionAVector2
- 원 A 의 중심.Center of circle A.
- radiusAnumber
- 원 A 의 반지름.Radius of circle A.
- positionBVector2
- 원 B 의 중심.Center of circle B.
- radiusBnumber
- 원 B 의 반지름.Radius of circle B.
반환Returnsboolean 두 원이 겹치면 참. (딱 닿기만 하면 거짓)True if the two circles overlap. (false if they only touch)
짝Counterpart: vanilla-engine Collision2D.test_circle_overlap
원-원 겹침 분리. (정적)
- 겹쳐 있으면 서로 절반씩 밀어낼 이동량을 반환한다. 안 겹치면 null.
- isStaticB 가 참이면 B 는 고정물로 보고 A 만 전체 거리를 밀린다.
- 두 중심이 정확히 같으면 임의의 축(x)으로 밀어낸다.
Circle-circle overlap separation. (static)
- If overlapping, returns the displacement that pushes each apart by half. null if not overlapping.
- If isStaticB is true, B is treated as fixed and only A is pushed the full distance.
- If the two centers are exactly the same, pushes apart along an arbitrary axis (x).
- positionAVector2
- 원 A 의 중심.Center of circle A.
- radiusAnumber
- 원 A 의 반지름.Radius of circle A.
- positionBVector2
- 원 B 의 중심.Center of circle B.
- radiusBnumber
- 원 B 의 반지름.Radius of circle B.
- isStaticBboolean
- B 를 움직이지 않는 고정물로 볼지. (기본 거짓)Whether to treat B as a static object that does not move. (default false)
반환Returnsobject | null { pushA: Vector2, pushB: Vector2 }
짝Counterpart: vanilla-engine Collision2D.resolve_circle_overlap
import { Collision2D, Vector2 } from "./build/vanilla.js";
let playerPosition = Vector2.create(18, 0);
const wallPosition = Vector2.create(0, 0);
const push = Collision2D.resolveCircleOverlap(playerPosition, 8, wallPosition, 12, true);
if (push !== null) {
playerPosition = playerPosition.add(push.pushA);
}
console.log(playerPosition.x, playerPosition.y);반경 안 최근접 대상 찾기. (정적)
- 투사체 명중 판정처럼 "중심에서 radius 안에 있는 것 중 가장 가까운 것" 을 찾는다.
Finds the nearest target within a radius. (static)
- Finds "the closest one among those within radius of the center", as in projectile hit tests.
- targetListT[]
- 찾을 대상 목록.List of targets to search.
- getPositionHandlerFunction
- (target) => Vector2
- centerVector2
- 찾기 중심.Search center.
- radiusnumber
- 찾기 반지름. (경계 포함)Search radius. (inclusive of the boundary)
반환ReturnsT | null 반경 안에서 가장 가까운 대상. (없으면 null, 거리가 같으면 뒤의 것)Nearest target within the radius. (null if none, the later one on equal distance)
짝Counterpart: vanilla-engine Collision2D.find_nearest_in_radius
import { Collision2D, Vector2 } from "./build/vanilla.js";
const enemyList = [
{ name: "slime", position: Vector2.create(40, 0) },
{ name: "bat", position: Vector2.create(10, 5) },
{ name: "golem", position: Vector2.create(300, 0) },
];
const center = Vector2.create(0, 0);
const nearest = Collision2D.findNearestInRadius(enemyList, (enemy) => enemy.position, center, 50);
console.log(nearest.name);반경 안 최근접 대상 번호 찾기. (정적, findNearestInRadius 와 같은 판정으로 번호를 돌려준다, 없으면 -1)
- 거리가 같으면 뒤의 것이다.
Finds the index of the nearest target within a radius. (static, returns the index by the same test as findNearestInRadius, -1 if none)
- On equal distance, the later one is chosen.
- targetListT[]
- 찾을 대상 목록.List of targets to search.
- getPositionHandlerFunction
- (target) => Vector2
- centerVector2
- 찾기 중심.Search center.
- radiusnumber
- 찾기 반지름. (경계 포함)Search radius. (inclusive of the boundary)
반환Returnsnumber 반경 안에서 가장 가까운 대상의 번호. (없으면 -1)Index of the nearest target within the radius. (-1 if none)
짝Counterpart: vanilla-engine Collision2D.find_nearest_index_in_radius
점-원 포함 검사. (정적)
Point-in-circle check. (static)
- pointVector2
- 검사할 점.Point to test.
- centerVector2
- 원의 중심.Center of the circle.
- radiusnumber
- 원의 반지름.Radius of the circle.
반환Returnsboolean 점이 원 안이나 경계 위에 있으면 참.True if the point is inside the circle or on its boundary.
짝Counterpart: vanilla-engine Collision2D.test_point_in_circle