mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.33k stars 35.35k forks source link

It seems impossible to model angular momentum using Euler angles. #24376

Closed 7ombie closed 2 years ago

7ombie commented 2 years ago

Hi there. I tried to start a discussion in the forum, but my post was auto-flagged (as spam), and hasn't been cleared (I'm a new user there), so I wasn't able to find out more before posting here. I've probably misunderstood something (I'm pretty new to 3D graphics), but it seems impossible to (directly) rotate objects in Three.js, based on angular momentum, as the Euler objects used to represent orientation will (no matter which order they use) always rotate around one axis in worldspace.

Imagine a first-person space game with basic Newtonian motion. The camera/ship should just drift (and rotate) through space, until some force is applied etc. The following (simplified) example doesn't work (in Three.js), as camera.rotation is an Euler instance:

const [velocity, momentum] = [new Vector3(), new Vector3()];

const update = function() {
    camera.position.add(velocity);
    camera.rotation.add(momentum); // this is invalid
};

I can use rotateX, rotateY and rotateZ to add each component of momentum separately, but have read that that is deprecated, and I should use Euler angles instead. That doesn't seem possible here, as I need everything to be local (relative to the camera), and rotation using Euler angles always implies one non-local axis (if I understand correctly, which isn't especially likely).

I'd be grateful if somebody could help me figure out what I'm missing. I just need a pointer really. I'm happy to do the homework myself. What am I not getting?

LeviPesin commented 2 years ago

Why can't you just use a quaternion as your angular momentum and multiply camera.quaternion by it?

Mugen87 commented 2 years ago

I tried to start a discussion in the forum, but my post was auto-flagged (as spam), and hasn't been cleared (I'm a new user there),

Can you please send me your user name at the forum so I can increase your trust level? Your posts should not be auto-detected as SPAM anymore (which unfortunately sometimes happens with new users).

In any event, GitHub is for bugs and feature requests only.

7ombie commented 2 years ago

Thank you, @Mugen87. My username is 7ombie (same as on here). Appreciated.

@LeviPesin - That's probably what I'm missing. I'll look into it, thank you. If I have any more questions, I'll use the forum.

Cheers, guys.

7ombie commented 2 years ago

@Mugen87 - I think my post was actually accepted, but I edited the title twice, pretty rapidly (to shorten it a bit), and then it was flagged as spam, so I probably triggered it by repeatedly changing the title on my first post, immediately after signing up (which was innocent, but suspicious). So, the bot probably worked as intended. The system would generally work better if a human could respond fairly soon after, but obviously, that's not always going to be possible.

Mugen87 commented 2 years ago

I've raised your trust level by 1. Hope everything works as expected now.

7ombie commented 2 years ago

@Mugen87 - Thank you. The forum post seems to be lost, but I was able to figure it out (thanks to @LeviPesin's comment) anyway, so it's no problem. My issue is fixed, and the forum is working. Thanks again, guys.