nostrangerdev / jpad-components

A set of web-components to build declarative virtual gamepad layouts
https://nostrangerdev.github.io/jpad-components/
MIT License
1 stars 0 forks source link

D-pads? #1

Open nmccamish opened 2 days ago

nmccamish commented 2 days ago

It looks like I could perhaps create 4-button D-pads with jpad-tiles and jpad-buttons, but what about 8-button D-pads, like that of the Sega Master System? They still wouldn't look like D-pads, though, just clusters of buttons.

nostrangerdev commented 1 day ago

It sure would be quite messy to use 8 buttons like that.

I actually stumbled on a similar situation recently, for a grid-based movement game.

In that case I used the jpad-trackpad with the attribute [normalize] and then rounded the values to "snap" them to the grid.

<jpad-controller>
    <jpad-trackpad name="movement" normalize slot="left"></jpad-trackpad>
    <!-- Other buttons here... -->
</jpad-controller>
const jpad = document.querySelector('jpad-controller');

// Getting the raw data
const { x, y } = jpad.getAxis('movement');

// Snapping them on the grid
const gridMovement = { x: Math.round(x), y: Math.round(y) };

While this works on a game with only cardinal movements, it doesn't address the need for properly scaled diagonal movements if not normalized after rounding.


That said, I can surely add support for dpad-like motion to the trackpad.

Here's my plan:

It would be used like this:

<!-- 8-ways with smooth increment, good for finer controls -->
<jpad-controller>
    <jpad-trackpad name="movement" dpad></jpad-trackpad>
</jpad-controller>

<!-- 8-ways with snap, like in SEGA, GBA, etc. -->
<jpad-controller>
    <jpad-trackpad name="movement" dpad normalize></jpad-trackpad>
</jpad-controller>
const jpad = document.querySelector('jpad-controller');

// Can be used as is, without any extra calculations
const eigthWaysMovement = jpad.getAxis('movement');

What do you think of this solution?

nmccamish commented 1 day ago

That sounds like it could work, yes!