stalgiag / p5.xr

a library that helps turn p5.js sketches into immersive experiences using WebXR
GNU Lesser General Public License v2.1
117 stars 25 forks source link

Working p5xrInput implementation #115

Closed msub2 closed 3 years ago

msub2 commented 3 years ago

I've managed to hide away all the implementation so that the only code that should be necessary in draw() to show a cube hand is

const right = getXRInput(RIGHT);
if (right != null) {
    push();
    fill(255, 255, 255);
    applyMatrix(right.pose);
    box(.15);
    pop();
}

It looks like I'm getting live pose data from it, but for whatever reason the box is just not drawing anywhere in the scene that I can see.

msub2 commented 3 years ago

Okay, figured out what I was doing wrong; I was trying to pass it an array of values, but applyMatrix() only takes individual parameters, so you still have to spread the matrix contents to get it to work:

const right = getXRInput(RIGHT);
if (right != null) {
    push();
    fill(255, 255, 255);
    applyMatrix(...right.pose);
    box(.15);
    pop();
}

Would it be worth trying to propose an upstream change?

stalgiag commented 3 years ago

Awesome! I've actually wanted to make that change upstream for a while. It will be easy enough to argue for. If you want to open an issue in the p5 GitHub and tag me then I can chime in. I am the primary steward for WebGL stuff so I am able to merge a change if everyone is okay with it.

In the meantime, I will merge this whenever you feel it is complete.

msub2 commented 3 years ago

Alright, I think it's in a pretty good state for now. You can retrieve the pose matrix as well as just the position in a Vector3, and I added getters to access the Gamepad buttons and axes on the controllers. So for instance, to access the value of the trigger I would just do something like this:

const right = getXRInput(RIGHT);
if (right != null) {
  console.log(right.trigger.value);
}

and to access the thumbstick values I can just do

console.log(right.thumbstick2D.x);
console.log(right.thumbstick2D.y);

Let me know what you think!

stalgiag commented 3 years ago

Looks really good! In the future, we might want to add something even more simple for input similar to keyTyped and mousePressed in p5. The ultimate goal would be to make it so that a new coder could do some simple input without knowing about property accessors or conditionals or null. This is primarily for educational contexts. For ex:

function triggerDown(){
    background(100, 100, 200);
}

But in the meantime, this is great for anyone that is slightly more experienced which likely accounts for anyone using the library currently.

@all-contributors please add @msub2 for design. (I know design isn't quite right but there isn't anything specifically for API design)

allcontributors[bot] commented 3 years ago

@stalgiag

I could not determine your intention.

Basic usage: @all-contributors please add @someone for code, doc and infra

For other usages see the documentation

stalgiag commented 3 years ago

@all-contributors please add @msub2 for design.

allcontributors[bot] commented 3 years ago

@stalgiag

I've put up a pull request to add @msub2! :tada: