melonjs-math
Skill này làm gì
Skill giải thích các công cụ vector và ma trận trong melonJS và các phương thức thay đổi giá trị tại chỗ. Dành cho lập trình viên trò chơi làm toán cho di chuyển, vị trí hoặc hướng.
Khi nào nên dùng
- ✓Khi giá trị vector của lập trình viên thay đổi bất ngờ sau khi gọi một phương thức toán học.
- ✓Khi lập trình viên muốn di chuyển mượt về một mục tiêu bằng nội suy.
- ✓Khi mã mỗi khung hình của lập trình viên chậm đi vì tạo quá nhiều đối tượng vector mới.
Liên kết skill
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-math/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tại sao vector của tôi thay đổi sau khi tôi gọi cross trên nó, và làm sao để giữ bản gốc?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill giải thích hầu hết phương thức thay đổi vector mà chúng được gọi và trả về nó. Skill chỉ cách sao chép giá trị sang một đối tượng riêng trước khi cần giữ bản gốc.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Vectors, matrices and maths
The one rule that explains most surprises
Methods mutate this and return it. They do not return a new vector.
const a = new Vector3d(1, 0, 0);
const b = new Vector3d(0, 1, 0);
a.cross(b); // a IS NOW the cross product; the original a is gone
a.add(b).normalize().scale(3, 3, 3); // chains, all in place
That is what makes the API allocation-free, and it is the opposite of what a functional style expects. If you need to keep the original, copy into something you own first:
out.copy(a).cross(b); // a untouched, nothing allocated
The exceptions are named. A handful of methods return a NEW vector, and
each has a *Self sibling that does not:
| allocates a new vector | mutates in place |
|---|---|
negate() |
negateSelf() |
floor() |
floorSelf() |
ceil() |
ceilSelf() |
clamp(low, high) |
clampSelf(low, high) |
clone() |
copy(v) |
So v.negate() in a per-frame loop allocates every frame and v.negateSelf()
does not. The names are the only clue; nothing in the call site shows it.
clone() comes from a pool, and only pays off if you give it back
clone() is vector3dPool.get(...). A pool is fast when instances come back:
get() takes a free one and resets it. Nothing comes back on its own, so a
clone that is simply dropped makes get() miss, construct, and write two
registry entries.
Measured, 3M operations:
out.copy(a).cross(b) |
39ms |
new Vector3d(ax, ay, az).cross(b) |
37ms |
a.clone().cross(b) |
2919ms |
So:
- Transient value inside a function? Use a module-level scratch vector, or
new Vector3d(). Notclone(). - Keeping it, then done with it?
clone()andvector3dPool.release(v)when finished. Releasing is what makes the pool a pool. - Writing a helper that returns a vector? Take an optional
out:
const cross = (a, b, out = new Vector3d()) => out.copy(a).cross(b);
That is the engine's own convention, and it lets a hot caller supply scratch while a casual one ignores it.
A returned value can never be shared scratch. The next call overwrites something the caller is still holding, and nothing fails loudly:
const _t = new Vector3d();
const bad = (v) => _t.copy(v).normalize(); // WRONG: two calls alias
Argument defaults that drop a component
Three signatures quietly discard what you did not pass. All three are reachable by writing the obvious thing.
scale(x, y = x, z = 1) — z defaults to 1, not to x. A uniform
scale has to say so three times:
v.scale(2); // (1,2,3) -> (2,4,3) z UNTOUCHED
v.scale(2, 2, 2); // (1,2,3) -> (2,4,6) what you meant
set(x, y, z = 0) — setting two components zeroes the third:
v.set(10, 20); // z is now 0, whatever it was
Vector3d.pos on a renderable is typed Vector2d. Renderable.pos is an
ObservableVector3d at run time but is declared through Polygon as a
Vector2d, so .z, .setMuted and .setCallback need a cast from
TypeScript. See issue #1510.
cross is two different functions
v3.cross(other); // Vector3d: MUTATES v3, returns this
v2.cross(other); // Vector2d: returns a NUMBER, mutates nothing
The 2D one is the z component of the 3D cross, which is the useful scalar for "which side is this on". Same name, different shape, different return.
Interpolation: four of them, and they are not interchangeable
a.lerp(b, t); // straight line, t in 0..1
a.slerp(b, t); // along the ARC, constant angular rate
a.damp(b, lambda, dt); // frame-rate independent ease toward b
a.moveTowards(b, step); // fixed distance per call, never overshoots
moveTowards is X/Y ONLY, on Vector3d as well as Vector2d. It is the
one of the four that is not 3D: it measures the distance from dx and dy,
steps along atan2(dy, dx), and leaves z exactly where it was. The source
says so in a comment and the JSDoc says "on the x and y axis", but the name
gives no hint, and in a list like the one above it reads as the sibling of the
other three. Anything moving in three dimensions wants damp or an explicit
step; reach for moveTowards only when the motion really is planar.
lerp between two directions is uneven: it slows at the ends, runs fast
through the middle, and shortens the vector as it passes halfway. Anything
whose direction matters more than its
Câu hỏi thường gặp
Vì sao vector của tôi thay đổi?
Hầu hết phương thức thay đổi vector tại chỗ và trả về nó, thay vì tạo một vector mới.
Những phương thức nào trả về vector mới?
Skill liệt kê các phương thức như negate, floor, ceil, clamp và clone, mỗi phương thức có phiên bản tại chỗ đặt tên có hậu tố Self.
Nó có bao gồm chuyển động mượt không?
Có. Nó nhắc đến các phương thức nội suy như lerp, slerp, damp và moveTowards.
Chi tiết
Mô tả gốc của tác giả
Use this skill for vector and matrix maths in melonJS — Vector2d, Vector3d, their observable variants, Matrix2d, Matrix3d, and the math utility functions. Covers which methods mutate and which allocate, the vector pool and when clone() is the wrong tool, interpolation with lerp, slerp, damp and moveTowards, and the argument defaults that silently drop a component. Triggers on: Vector2d, Vector3d, ObservableVector2d, ObservableVector3d, Matrix2d, Matrix3d, vector, vector maths, dot, cross, normalize, length, distance, angle, lerp, slerp, damp, moveTowards, project, clamp, degToRad, radToDeg, cl
repo: melonjs/melonJS
Độ phổ biến
- Sao GitHub
- 6.4k
- Vị trí xếp hạng
- #14207
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
melonjs-20-migration
6.4kGiúp agent của bạn viết mã trò chơi melonJS cho phiên bản 20 và tránh các phương thức cũ, như bước khởi động bất đồng bộ bắt buộc.
melonjs-3d-assets
6.4kGiúp agent của bạn tải mô hình 3D như file glTF, GLB và OBJ vào trò chơi trình duyệt melonJS, kèm tùy chọn tỷ lệ và ánh sáng.
melonjs-3d
6.4kGiúp agent của bạn xây dựng cảnh 3D hoặc 2.5D trong trò chơi trình duyệt melonJS, gồm camera, lưới, đèn và độ sâu, theo quy tắc trục của công cụ.
melonjs-audio
6.4kGiúp agent của bạn thêm hiệu ứng âm thanh và nhạc nền vào trò chơi melonJS, gồm thiết lập âm thanh, nhạc nền, điều chỉnh âm lượng và âm thanh không gian.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.