Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

How to create first-person controls which don't have a fixed down "anchor" in the world? #1950

Closed
JPhilipp opened this Issue · 7 comments

3 participants

@JPhilipp

I would like to attach a camera to the first-person perspective player which, when you press left/up/right/down/forward/backward, always moves the camera/ player exactly in that direction and rotation, independent of whether the camera/ player is already facing upwards/ downwards/ etc. So basically a camera/ player which is agnostic to direction and does not know of a fixed "down" in the world. How would I do this? I tried FirstPersonControls and FlyControls but they work differently e.g. when you're facing skywards.

Thanks!

PS: I'm trying to figure out how to add the label "question" here (can't see it when preparing the post).

@mrdoob
Owner

What's wrong with FlyControls?

@JPhilipp

Thanks for following up! When I fly upwards the sky with FlyControls, then an e.g. right-movement will cause the scene to rotate rightways... however, I want this game to always have the same "right" rotation independent of a "world up/ down", but just always relative to the camera. So when flying upwards, I would like a "left" and "right" that would actually rotate left or right based on the current camera direction (as if the engine would have "forgot" what is down now)... basically the same that happens when the FlyControls are flying over ground.

Differently put, the following code was a (non smooth delta-interpolating) test case, where there's one problem: the r (up) and f (down) keys work fine in the beginning, but they stop working as intended once the camera is rotated say 90 degrees right:

var onKeyDown = function (event) {
    var speed = 10, degreesSpeed = .1;
    switch(event.keyCode) {
        case 38: case 87: // up or w
        camera.translateZ(-speed);
        break;

        case 40: case 83: // down or s
        camera.translateZ(speed);
        break;

        case 37: case 65: // left or a
        camera.rotation.y += degreesSpeed;
        break;

        case 39: case 68: // right or d
        camera.rotation.y -= degreesSpeed;
        break;


        case 82: // r (up)
        camera.rotation.x -= degreesSpeed;
        break;

        case 70: // f (down)
        camera.rotation.x += degreesSpeed;
        break;
    }
};
@WestLangley
Collaborator

Are you saying that you want to always rotate the camera either around it's own y-axis (left-right) or it's own x-axis (up-down) -- regardless of the camera's orientation?

@JPhilipp

West, the camera should always rotate and move relative to itself and its current direction. camera.translateZ works perfect for the front movement always being relative to the camera, for instance. It's only when pressing "r" or "f" (as per above) that doesn't get the wanted effect. "r" should be kind of like downwards-rotate-falling into whatever the camera stands on, which works when you start out, but it doesn't work when you're e.g. rotating and skyfacing.

@WestLangley
Collaborator

@JPhilipp

"r" should be kind of like downwards-rotate-falling into whatever the camera stands on

Sorry, but I have no idea what you are saying... so I'll guess. It doesn't hurt to try something. :-)

See this fiddle: http://jsfiddle.net/mdujn/1/

In the fiddle (r49), the camera rotates around either it's own y-axis (left-right) or it's own x-axis (up-down).

The key codes are slightly different from yours: arrows rotate, and w/s translates.

@JPhilipp

Brilliant, this is exactly what I was looking for -- you guessed perfectly right, and thanks so much WestLangley!

In case you have a StackOverflow account please also feel free to post it there (http://gamedev.stackexchange.com/questions/29319/rotate-3d-camera-relative-to-whats-on-screen-in-threejs/29327#comment49069_29327), and if not or you don't have time, I will post it there mentioning you.

@WestLangley
Collaborator

Yay!

@JPhilipp JPhilipp closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.