물리 레이캐스트 (physics_raycast) 함수 설명
물리 레이캐스트 함수는 물리 세계의 특정 부분에 레이를 쏘고, 레이가 충돌한 지점과 그 지점의 법선(normal)에 대한 정보를 담고 있는 구조체 배열을 반환합니다. 레이는 시작점과 끝점을 제공하여 정의됩니다. 충돌은 시작점과 끝점 사이에서 확인되거나, 시작점에서 레이의 일부까지 확인됩니다.
주요 개념
- 레이 정의: 시작점과 끝점을 제공하여 레이를 정의합니다.
- 충돌 확인: 단일 객체, 인스턴스 또는 객체와 인스턴스의 배열에 대해 충돌을 확인합니다.
- 결과 반환: 기본적으로 가장 가까운 충돌만 반환하지만,
all_hits
매개변수를true
로 설정하면 모든 충돌을 반환합니다.
레이캐스트 히트포인트 구조체
변수명 | 타입 | 설명 |
---|---|---|
instance | Object Instance | 충돌한 인스턴스 |
hitpointX | Real | 히트포인트의 x 좌표 |
hitpointY | Real | 히트포인트의 y 좌표 |
normalX | Real | 히트포인트의 법선 x 성분 |
normalY | Real | 히트포인트의 법선 y 성분 |
fraction | Real | 충돌이 발생한 선분의 비율 (0에서 1 사이) |
함수 문법
physics_raycast(x_start, y_start, x_end, y_end, ids, [all_hits], [max_fraction]);
매개변수 설명
매개변수 | 타입 | 설명 |
---|---|---|
x_start | Real | 레이의 시작점 x 좌표 |
y_start | Real | 레이의 시작점 y 좌표 |
x_end | Real | 레이의 끝점 x 좌표 |
y_end | Real | 레이의 끝점 y 좌표 |
ids | Object Asset or Object Instance or Array | 체크할 단일 객체 자산, 인스턴스 또는 이들의 배열 |
all_hits | Boolean | 모든 충돌을 반환할지 여부 (기본값: false) |
max_fraction | Real | 체크할 레이의 최대 비율 (0에서 1 사이) |
활용 예제
예제 1: 기본 사용법
// Create Event
physics_world_create(0.1);
fixtures = [];
var _fix = physics_fixture_create();
physics_fixture_set_density(_fix, 0);
repeat(100) {
var _angle = random(360);
var _x = random(room_width), _y = random(room_height);
physics_fixture_set_edge_shape(_fix, _x, _y, _x + lengthdir_x(40, _angle), _y + lengthdir_y(40, _angle));
var _fix_bound = physics_fixture_bind(_fix, id);
array_push(fixtures, _fix_bound);
}
physics_fixture_delete(_fix);
x1 = room_width / 2;
y1 = room_height / 2;
x2 = mouse_x;
y2 = mouse_y;
hits = undefined;
// Step Event
x2 = mouse_x;
y2 = mouse_y;
hits = physics_raycast(x1, y1, x2, y2, id, true);
// Draw Event
physics_world_draw_debug(phy_debug_render_shapes);
draw_line_colour(x1, y1, x2, y2, c_red, c_blue);
if (is_undefined(hits)) { exit; }
for(var i = 0; i < array_length(hits); i++) {
var _hit = hits[i];
draw_line(_hit.hitpointX, _hit.hitpointY, _hit.hitpointX + _hit.normalX * 40, _hit.hitpointY + _hit.normalY * 40);
}
// Clean Up Event
for(var i = 0; i < array_length(fixtures); i++) {
physics_remove_fixture(id, fixtures[i]);
}
예제 2: 분수 값 사용하기
// Step Event
x2 = mouse_x;
y2 = mouse_y;
hits = physics_raycast(x1, y1, x2, y2, id, false);
// Draw Event
physics_world_draw_debug(phy_debug_render_shapes);
draw_line_colour(x1, y1, x2, y2, c_red, c_blue);
if (is_undefined(hits)) { exit; }
var _fract = hits[0].fraction;
draw_line_colour(x1, y1, x1 + _fract * (x2 - x1), y1 + _fract * (y2 - y1), c_yellow, c_yellow);
예제 3: 여러 객체 및 인스턴스에 대해 확인하기
// Step Event
hits = physics_raycast(x1, y1, x2, y2, [obj_physics_parent, obj_physics_circle, ins_physics_unique], true);
이 예제들은 physics_raycast
함수를 사용하는 다양한 방법을 보여줍니다. 각 예제는 레이캐스트의 기본적인 사용법과 더불어, 분수 값을 활용하거나 여러 객체를 동시에 체크하는 방법을 설명합니다.