njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Loading...
Searching...
No Matches
njin_collision.h
1#pragma once
2#include "_comps.h"
3#include "_math.h"
4#include "_types.h"
5#include <entt/entity/entity.hpp>
6#include <vector>
7
8namespace njin {
9struct context;
10
13
15inline constexpr u32 layer_all = 0xFFFFFFFFu;
16
21constexpr u32 layer_bit(i32 n) { return 1u << (u32)n; }
22
31
46struct collider {
48 vec2 size{16.0f, 16.0f};
49 f32 radius = 8.0f;
55 bool trigger = false;
56 bool enabled = true;
59 bool one_way = false;
60};
61
66
79 entt::entity self = entt::null;
80 entt::entity other = entt::null;
81 bool trigger = false;
82};
83
86 entt::entity self = entt::null;
87 entt::entity other = entt::null;
88 bool trigger = false;
89};
90
96 entt::entity self = entt::null;
97 entt::entity other = entt::null;
98 bool trigger = false;
99};
100
105inline rect collider_bounds(const transform &tr, const collider &col) {
106 const f32 s = tr.scale < 0.0f ? -tr.scale : tr.scale;
107 const vec2 center = tr.pos + rotate(col.offset * s, tr.rot);
108 if (col.shape == collider_circle)
109 return rect_from_center(center, vec2{col.radius, col.radius} * (2.0f * s));
110 if (col.shape == collider_box)
111 return rect_from_center(center, col.size * s);
112 return rect{tr.pos, {}};
113}
114
118 bool hit_x = false;
119 bool hit_y = false;
120 entt::entity other_x = entt::null;
121 entt::entity other_y = entt::null;
124 bool grounded = false;
125 entt::entity ground = entt::null;
126 bool on_slope = false;
127 bool ground_one_way = false;
128};
129
134 bool drop_through = false;
141 bool test_only = false;
142};
143
159collision_move_result collision_move(context &ctx, entt::entity entity, vec2 delta);
160
172collision_move_result collision_move(context &ctx, entt::entity entity, vec2 delta,
173 const collision_move_opts &opts);
174
187void collision_move_platform(context &ctx, entt::entity platform, vec2 delta);
188
196i32 collision_overlap_rect(const context &ctx, rect area,
197 std::vector<entt::entity> *out = nullptr,
198 u32 mask = layer_all, bool include_triggers = true);
199
207i32 collision_overlap_circle(const context &ctx, circle area,
208 std::vector<entt::entity> *out = nullptr,
209 u32 mask = layer_all, bool include_triggers = true);
210
218i32 collision_overlap_point(const context &ctx, vec2 point,
219 std::vector<entt::entity> *out = nullptr,
220 u32 mask = layer_all, bool include_triggers = true);
221
224 bool hit = false;
225 entt::entity entity = entt::null;
228 f32 distance = 0.0f;
229};
230
242raycast_hit collision_raycast(const context &ctx, vec2 from, vec2 to,
243 u32 mask = layer_all, bool include_triggers = false,
244 entt::entity ignore = entt::null);
245
256inline bool collision_line_of_sight(const context &ctx, vec2 from, vec2 to,
257 u32 mask = layer_all, entt::entity ignore = entt::null) {
258 return !collision_raycast(ctx, from, to, mask, false, ignore).hit;
259}
260
265void collision_set_debug(context &ctx, bool on);
266
271void collision_set_cell_size(context &ctx, f32 size);
273} // namespace njin
collider_shape
Hình của một collider.
Definition njin_collision.h:24
void collision_set_cell_size(context &ctx, f32 size)
Cỡ ô của lưới tìm cặp va chạm, đơn vị thế giới.
constexpr u32 layer_bit(i32 n)
Bit của lớp va chạm thứ n (0..31), để đặt tên lớp cho dễ đọc: constexpr u32 layer_enemy = njin::layer...
Definition njin_collision.h:21
i32 collision_overlap_rect(const context &ctx, rect area, std::vector< entt::entity > *out=nullptr, u32 mask=layer_all, bool include_triggers=true)
Tìm mọi entity có collider chồng lên hình chữ nhật area.
i32 collision_overlap_circle(const context &ctx, circle area, std::vector< entt::entity > *out=nullptr, u32 mask=layer_all, bool include_triggers=true)
Như collision_overlap_rect() với một hình tròn: vùng nổ, tầm nhìn.
void collision_set_debug(context &ctx, bool on)
Vẽ khung mọi collider hộp và tròn trong phase_render, trên sprite: xanh lá cho vật cản,...
collision_move_result collision_move(context &ctx, entt::entity entity, vec2 delta)
Di chuyển một entity có collider, dừng lại khi đụng collider không phải trigger (kể cả ô của collider...
rect collider_bounds(const transform &tr, const collider &col)
Hình chữ nhật bao quanh một collider hộp hoặc tròn, trong thế giới.
Definition njin_collision.h:105
constexpr u32 layer_all
Mọi lớp va chạm. Giá trị mặc định của collider::mask.
Definition njin_collision.h:15
bool collision_line_of_sight(const context &ctx, vec2 from, vec2 to, u32 mask=layer_all, entt::entity ignore=entt::null)
Có nhìn thấy nhau không: tia từ from đến to không trúng vật cản nào.
Definition njin_collision.h:256
raycast_hit collision_raycast(const context &ctx, vec2 from, vec2 to, u32 mask=layer_all, bool include_triggers=false, entt::entity ignore=entt::null)
Bắn một tia từ from đến to, trả về vật trúng gần nhất: tầm nhìn của quái, đạn tức thời,...
void collision_move_platform(context &ctx, entt::entity platform, vec2 delta)
Di chuyển một bục (entity có collider hộp, thường là one_way hoặc vật cản) không bị chặn,...
i32 collision_overlap_point(const context &ctx, vec2 point, std::vector< entt::entity > *out=nullptr, u32 mask=layer_all, bool include_triggers=true)
Như collision_overlap_rect() với một điểm: chuột đang chỉ vào gì.
@ collider_box
Hình chữ nhật thẳng trục, cỡ size. Không xoay theo transform.
Definition njin_collision.h:25
@ collider_circle
Hình tròn bán kính radius.
Definition njin_collision.h:26
@ collider_tiles
Mọi ô không trống của njin::tilemap trên cùng entity là vật cản.
Definition njin_collision.h:29
const char * tr(const context &ctx, const char *key)
Chuỗi đã dịch của key trong ngôn ngữ đang dùng.
vec2 rotate(vec2 v, f32 degrees)
Xoay vector theo chiều kim đồng hồ trên màn hình (trục y hướng xuống).
Definition _math.h:83
constexpr rect rect_from_center(vec2 center, vec2 size)
Tạo hình chữ nhật từ tâm và kích thước.
Definition _math.h:228
f32 delta(const context &ctx)
Thời gian của frame trước, tính bằng giây, đã nhân với tốc độ thời gian.
std::int32_t i32
Số nguyên có dấu 32 bit.
Definition _types.h:14
float f32
Số thực 32 bit.
Definition _types.h:28
std::uint32_t u32
Số nguyên không dấu 32 bit.
Definition _types.h:22
Hình tròn.
Definition _math.h:19
Hình va chạm của một entity.
Definition njin_collision.h:46
u32 layer
Các lớp collider này thuộc về.
Definition njin_collision.h:51
bool trigger
Chỉ báo chồng nhau, không chặn đường: vùng nhặt đồ, vùng sát thương, cửa chuyển màn.
Definition njin_collision.h:55
vec2 offset
Độ lệch của tâm so với transform.pos.
Definition njin_collision.h:50
collider_shape shape
Hình.
Definition njin_collision.h:47
bool one_way
Bục một chiều (chỉ với hình hộp): collision_move() chỉ bị chặn khi rơi từ trên xuống mặt trên của nó;...
Definition njin_collision.h:59
bool enabled
Tắt tạm mà không gỡ component.
Definition njin_collision.h:56
vec2 size
Kích thước hình hộp.
Definition njin_collision.h:48
u32 mask
Các lớp collider này va chạm với.
Definition njin_collision.h:52
f32 radius
Bán kính hình tròn.
Definition njin_collision.h:49
Hai collider bắt đầu chồng nhau.
Definition njin_collision.h:78
bool trigger
Một trong hai là trigger.
Definition njin_collision.h:81
entt::entity self
Entity nhận event.
Definition njin_collision.h:79
entt::entity other
Entity kia.
Definition njin_collision.h:80
Hai collider thôi chồng nhau: tách ra, bị tắt, bị gỡ, hoặc bị hủy.
Definition njin_collision.h:95
bool trigger
Một trong hai là trigger.
Definition njin_collision.h:98
entt::entity other
Entity kia, có thể đã bị hủy.
Definition njin_collision.h:97
entt::entity self
Entity nhận event.
Definition njin_collision.h:96
Tùy chọn của collision_move().
Definition njin_collision.h:131
f32 snap_down
Khi lớn hơn 0 và entity không đi lên: nếu sau khi di chuyển có mặt đất trong khoảng này phía dưới,...
Definition njin_collision.h:139
bool drop_through
Bỏ qua bục một chiều (ô tile_one_way và collider one_way): để nhảy xuống khỏi bục.
Definition njin_collision.h:134
bool test_only
Chỉ tính, không ghi vị trí mới vào transform: để dò tường hay mặt đất.
Definition njin_collision.h:141
Kết quả của collision_move().
Definition njin_collision.h:116
entt::entity other_y
Vật chặn trên trục dọc.
Definition njin_collision.h:121
entt::entity ground
Thứ đang đứng lên, nếu grounded.
Definition njin_collision.h:125
bool on_slope
Đang đứng trên ô dốc.
Definition njin_collision.h:126
bool ground_one_way
Đang đứng trên bục một chiều (ô hoặc collider).
Definition njin_collision.h:127
bool hit_x
Bị chặn trên trục ngang.
Definition njin_collision.h:118
entt::entity other_x
Vật chặn trên trục ngang.
Definition njin_collision.h:120
bool hit_y
Bị chặn trên trục dọc. hit_y && delta.y > 0 là đang đứng trên đất.
Definition njin_collision.h:119
bool grounded
Sau khi di chuyển, entity đang đứng trên mặt đất, dốc hay bục (cách không quá nửa pixel phía dưới).
Definition njin_collision.h:124
vec2 moved
Độ dời thật sự đã áp dụng.
Definition njin_collision.h:117
Hai collider vẫn đang chồng nhau, gửi mỗi frame sau frame enter.
Definition njin_collision.h:85
entt::entity other
Entity kia.
Definition njin_collision.h:87
bool trigger
Một trong hai là trigger.
Definition njin_collision.h:88
entt::entity self
Entity nhận event.
Definition njin_collision.h:86
Tag: entity được bục di chuyển chở theo khi đứng trên nó, và bị bục đẩy khi bục đâm vào.
Definition njin_collision.h:65
Kết quả của collision_raycast().
Definition njin_collision.h:223
bool hit
Có trúng gì không.
Definition njin_collision.h:224
vec2 point
Điểm trúng, trong thế giới.
Definition njin_collision.h:226
entt::entity entity
Entity bị trúng (tilemap nếu trúng ô).
Definition njin_collision.h:225
f32 distance
Khoảng cách từ from đến điểm trúng.
Definition njin_collision.h:228
vec2 normal
Pháp tuyến bề mặt tại điểm trúng, độ dài 1.
Definition njin_collision.h:227
Hình chữ nhật thẳng trục: pos là góc trên trái, size là rộng và cao.
Definition _math.h:13
Vị trí, góc xoay và tỉ lệ của một entity trong thế giới.
Definition _comps.h:14
Vector 2 chiều. Dùng cho vị trí, kích thước, độ dời.
Definition _types.h:39