p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Implemented roll function

Open haroon10725 opened this issue 1 year ago • 7 comments

Implemented roll function #6760

haroon10725 avatar Feb 18 '24 18:02 haroon10725

Hey @davepagurek please review it. Thanks.

haroon10725 avatar Feb 18 '24 18:02 haroon10725

I was taking a look at the example, and it seems like the implementation doesn't quite do what a roll actually is. Here, it seems to do something kind of like tilt(), but where the object is kept centered: image

What I would expect it to do is produce something kind of like how rotate() works in 2D (the image below is just an approximation, made using rotate()): image

It looks like that's because _rotateView only modifies the point the camera is looking at (centerX/Y/Z), leaving the camera position (eyeX/Y/Z) and the up vector in place: https://github.com/processing/p5.js/blob/4c13650450c6caaba795c0e5ebb695a4558cb4be/src/webgl/p5.Camera.js#L1052-L1057

I think the correct output of roll() would leave the center and eye the same, but just rotate the up vector. @SableRaf does that make sense, given how much you've been looking into cameras recently?

If that's true, then I think (but am not certain, definitely test this!) that we could edit _rotateView to also apply the rotation matrix to the up vector:

const rotatedUp = [
  this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8],
  this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9],
  this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10]
];

davepagurek avatar Feb 23 '24 00:02 davepagurek

I think the correct output of roll() would leave the center and eye the same, but just rotate the up vector. @SableRaf does that make sense, given how much you've been looking into cameras recently?

Yes! The camera should rotate around its own forward vector.

A good approach would be to compute the camera rotation using quaternions, then compute the up and forward vectors from the resulting rotation and apply them to the camera. This demo by scudly is a good example of how to do it: https://infinitefunspace.com/p5/fly/

I made a minimal example based on it here: https://editor.p5js.org/SableRaf/sketches/WPYb6PRMu

SableRaf avatar Feb 23 '24 16:02 SableRaf

@haroon10725 are you still working on this? Otherwise we'll look if someone else would like to tackle it.

SableRaf avatar May 28 '24 13:05 SableRaf

@SableRaf I am busy, you can look if some one can solve it.

haroon10725 avatar May 28 '24 13:05 haroon10725

If anyone else is interested in taking this up, feel free to branch off of this PR so that the work @haroon10725 has put in also gets included! A next step could be to try out the suggestion in https://github.com/processing/p5.js/pull/6819#issuecomment-1960584109.

davepagurek avatar May 28 '24 13:05 davepagurek

Hi, Have added the suggested changes with the existing changes of this PR in a new PR here #7093.

rohanjulka19 avatar Jun 13 '24 19:06 rohanjulka19