-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Pilot): Add direction and rotation thruster joysticks.
- Loading branch information
1 parent
9a6b6b7
commit f9eaf0f
Showing
5 changed files
with
184 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,63 @@ | ||
export {}; | ||
import {animated as a} from "@react-spring/web"; | ||
import {useJoystick} from "client/src/hooks/useJoystick"; | ||
import {ReactNode} from "react"; | ||
|
||
export const Joystick = ({ | ||
children, | ||
className, | ||
onDrag, | ||
}: { | ||
onDrag: (dir: {x: number; y: number}) => void; | ||
className?: string; | ||
children?: ReactNode; | ||
}) => { | ||
const [xy, bind, containerRef] = useJoystick({axisSnap: true, onDrag}); | ||
|
||
return ( | ||
<div className={`relative aspect-square ${className}`}> | ||
<div | ||
ref={containerRef} | ||
className="top-0 absolute bg-black/50 border-2 border-white/50 rounded-full w-full h-full flex justify-center items-center" | ||
> | ||
<a.div | ||
{...bind()} | ||
style={{transform: xy?.to((x, y) => `translate3d(${x}px,${y}px,0)`)}} | ||
className="z-10 w-10 h-10 rounded-full border-black/50 border-2 bg-gray-500 shadow-md cursor-pointer" | ||
></a.div> | ||
{children} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const LinearJoystick = ({ | ||
className, | ||
onDrag, | ||
children, | ||
vertical, | ||
}: { | ||
className?: string; | ||
onDrag: (dirs: {x: number; y: number}) => void; | ||
children: ReactNode; | ||
vertical?: boolean; | ||
}) => { | ||
const [xy, bind, containerRef] = useJoystick({ | ||
axis: vertical ? "y" : "x", | ||
onDrag, | ||
}); | ||
return ( | ||
<div | ||
ref={containerRef} | ||
className={`${ | ||
vertical ? "h-full" : "w-full" | ||
} relative bg-black/50 border-2 border-white/50 rounded-full flex justify-center items-center ${className}`} | ||
> | ||
<a.div | ||
{...bind()} | ||
style={{transform: xy?.to((x, y) => `translate3d(${x}px,${y}px,0)`)}} | ||
className="z-10 w-10 h-10 rounded-full border-black/50 border-2 bg-gray-500 shadow-md cursor-pointer" | ||
></a.div> | ||
{children} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import throttle from "lodash.throttle"; | ||
import {useSpring} from "@react-spring/web"; | ||
import {useDrag} from "@use-gesture/react"; | ||
import {useRef} from "react"; | ||
|
||
function distance(x1: number, y1: number, x2 = 0, y2 = 0) { | ||
const a = x1 - x2; | ||
const b = y1 - y2; | ||
|
||
return Math.sqrt(a * a + b * b); | ||
} | ||
const minDistance = 10; | ||
export function useJoystick({ | ||
axisSnap, | ||
axis, | ||
onDrag = () => {}, | ||
throttleMs = 100, | ||
}: { | ||
axisSnap?: boolean; | ||
axis?: "x" | "y" | undefined; | ||
onDrag?: (values: {x: number; y: number}) => void; | ||
throttleMs?: number; | ||
} = {}) { | ||
const callback = useRef(throttle(onDrag, throttleMs)); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const maxDistance = useRef(0); | ||
const [{xy}, set] = useSpring(() => ({ | ||
xy: [0, 0], | ||
config: {mass: 1, tension: 280, friction: 30}, | ||
})); | ||
const bind = useDrag( | ||
({down, first, movement: [x, y]}) => { | ||
if (first) { | ||
maxDistance.current = | ||
(containerRef.current?.getBoundingClientRect()[ | ||
axis === "y" ? "height" : "width" | ||
] || 0) / 2; | ||
} | ||
const dist = distance(x, y); | ||
if (dist > maxDistance.current) { | ||
const theta = Math.abs(Math.atan(y / x)); | ||
x = maxDistance.current * Math.cos(theta) * (x > 0 ? 1 : -1); | ||
y = maxDistance.current * Math.sin(theta) * (y > 0 ? 1 : -1); | ||
} | ||
if ( | ||
axisSnap && | ||
(Math.abs(x) > minDistance || Math.abs(y) > minDistance) | ||
) { | ||
if (x < minDistance && x > minDistance * -1) x = 0; | ||
if (y < minDistance && y > minDistance * -1) y = 0; | ||
} | ||
set({ | ||
xy: down ? [x, y] : [0, 0], | ||
immediate: down, | ||
}); | ||
callback.current?.( | ||
down | ||
? {x: x / maxDistance.current, y: y / maxDistance.current} | ||
: {x: 0, y: 0} | ||
); | ||
}, | ||
{axis} | ||
); | ||
return [xy, bind, containerRef] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters