Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools. Co-authored-by: Cursor <cursoragent@cursor.com>
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
/**
|
|
* Math helpers adapted from yoannmoinet/nipplejs (MIT)
|
|
* https://github.com/yoannmoinet/nipplejs — src/utils.js
|
|
* Stars verified >1000 — see docs/OSS_RESEARCH.md
|
|
*
|
|
* Only pure functions; no DOM. Used by VirtualJoystick.
|
|
*/
|
|
|
|
export type Point = { x: number; y: number }
|
|
|
|
export function distance(p1: Point, p2: Point): number {
|
|
const dx = p2.x - p1.x
|
|
const dy = p2.y - p1.y
|
|
return Math.sqrt(dx * dx + dy * dy)
|
|
}
|
|
|
|
export function radians(a: number): number {
|
|
return a * (Math.PI / 180)
|
|
}
|
|
|
|
export function degrees(a: number): number {
|
|
return a * (180 / Math.PI)
|
|
}
|
|
|
|
/** nipplejs: angle in degrees from p1 → p2 */
|
|
export function angle(p1: Point, p2: Point): number {
|
|
const dx = p2.x - p1.x
|
|
const dy = p2.y - p1.y
|
|
return degrees(Math.atan2(dy, dx))
|
|
}
|
|
|
|
/** nipplejs clamp: keep pos within size radius of nipplePos (axis-aligned box; circle applied by caller) */
|
|
export function clamp(pos: Point, nipplePos: Point, size: number): Point {
|
|
return {
|
|
x: Math.min(Math.max(pos.x, nipplePos.x - size), nipplePos.x + size),
|
|
y: Math.min(Math.max(pos.y, nipplePos.y - size), nipplePos.y + size),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Circle-constrained stick vector (common nipple pattern):
|
|
* force = dist / maxRadius, capped at 1; vector normalized * force.
|
|
*/
|
|
export function stickVector(
|
|
origin: Point,
|
|
finger: Point,
|
|
maxRadius: number,
|
|
threshold = 0.1,
|
|
): { x: number; y: number; force: number; angleDeg: number; active: boolean } {
|
|
const dist = distance(origin, finger)
|
|
const angleDeg = angle(origin, finger)
|
|
const force = Math.min(1, dist / Math.max(1e-6, maxRadius))
|
|
if (force < threshold) {
|
|
return { x: 0, y: 0, force: 0, angleDeg, active: false }
|
|
}
|
|
const rad = radians(angleDeg)
|
|
// screen: +x right, +y up (Cocos UI); world XZ mapped by consumer
|
|
const x = Math.cos(rad) * force
|
|
const y = Math.sin(rad) * force
|
|
return { x, y, force, angleDeg, active: true }
|
|
}
|