How to limit camera translation along upVector

Hi, I’m new to babylon. I’m trying to create a camera that can hover over meshes at a specific (human eye-level) height. I noticed that by just using a freeCamera without constraints, moving the camera forward or backward via arrow keys or WASD depends on the direction the camera pitch axis is pointed via the mouse. So if you point the camera down, and press backwards, then the camera moves up. I don’t want this. I was thinking about using gravity to maintain consistent height, but because I want to hover, I would have to decrease the size of the camera ellipsoid to avoid colliding with things that are below the camera, and gravity would then just pull that smaller ellipsoid down to the ground until the ellipsoid mesh collides with the ground. That being said, I still want to be able to collide with walls and things that are at the same level as the camera. Is there a way to use the camera as it is, shrink the size of the ellipsoid, but still maintain the camera’s height? I want to be able to move the camera’s orientation with the mouse, but still be able to move forwards and backwards along the x-y plane.
Thanks in advance for the help!

One of doing it is to inherit from the camera type you want and override _checkInputs as such:

    public _updatePosition(): void {
        this.cameraDirection.y = yourConstant;
        super._updatePosition();
    }

Sorry if I misunderstood, but this just keeps the camera moving up each frame. This doesn’t limit it.

True but you could apply your own logic there to limit the direction based on position for instance ?

Okay, so I am very confused about what you are suggesting I do.

_updatePosition() seems to be a private function, which means it is not found listed in the API (from what I could find). You are suggesting to override _checkInputs(), however. I looked for _checkInputs() and there are many classes that have a checkInputs() or _checkInputs() function. I am not sure which one to call, and how to override it. Camera has a checkInputs(), as does FreeCameraKeyboardInputs, FreeCameraInputsManager, etc. I had to go into the source code to find what checkInputs() even looks like under one of the classes, as it just lists what it does, but not how to use it in the API. I found this:

FreeCameraKeyboardMoveInput.prototype.checkInputs = function () {
if (this._onKeyboardObserver) {
var camera = this.camera;
// Keyboard
for (var index = 0; index < this._keys.length; index++) {
var keyCode = this._keys[index];
var speed = camera._computeLocalCameraSpeed();
if (this.keysLeft.indexOf(keyCode) !== -1) {
camera._localDirection.copyFromFloats(-speed, 0, 0);
}
else if (this.keysUp.indexOf(keyCode) !== -1) {
camera._localDirection.copyFromFloats(0, 0, speed);
}
else if (this.keysRight.indexOf(keyCode) !== -1) {
camera._localDirection.copyFromFloats(speed, 0, 0);
}
else if (this.keysDown.indexOf(keyCode) !== -1) {
camera._localDirection.copyFromFloats(0, 0, -speed);
}
else if (this.keysUpward.indexOf(keyCode) !== -1) {
camera._localDirection.copyFromFloats(0, speed, 0);
}
else if (this.keysDownward.indexOf(keyCode) !== -1) {
camera._localDirection.copyFromFloats(0, -speed, 0);
}
if (camera.getScene().useRightHandedSystem) {
camera._localDirection.z *= -1;
}
camera.getViewMatrix().invertToRef(camera.cameraTransformMatrix);
Maths_math_vector__WEBPACK_IMPORTED_MODULE_4
[“Vector3”].TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
camera.cameraDirection.addInPlace(camera._transformedDirection);
}
}
};

That being said, I am not sure how to override this for what I am doing. I tried creating a HoverCamera class that extends UniversalCamera, and then I override the _updatePosition() function. This just caused the camera to constantly ascend up. Some constructive feedback to the devs writing documentation: It would help to know that if we can call the private functions, to list the private functions in the API, and also list examples of how to use the functions besides just linking to where the function is mentioned in a playground example, which may or may not (often not) be an example of how the function is used correctly, as the playground seems to often be submitted by someone who is debugging and/or doesn’t include all of the imports and class setup beyond just createScene(), which is confusing for beginners.

Thank you for being patient with me, but since I am new to Babylonjs and TypeScript, would you mind walking me through at least the function/class setup of how to achieve what you are suggesting? Thanks!

@msDestiny14 will be able to help on this one :slight_smile:

1 Like

This may be of help https://doc.babylonjs.com/how_to/customizing_camera_inputs

In any open source project you always have availability to the private properties and methods and you can use them if you wish. However there is never any guarantee that they wont be changed or deleted in the future. In Babylon.js there is almost always backward compatibility to public properties and methods. Hence public ones are in API never private ones.

Using the docs page mentioned above you produce your own functions which you have control over and so do not have to worry about changes to private functions.

As we now know you are a beginner I would recommend you stay away from private properties and methods.

3 Likes

Hi, and welcome to Babylon.

I made a little example on playground that just has the camera moving back and forth by updating the the position. Note you can still move the camera around but the position moves independent of this.

With this you can create your custom camera to check for input and then clamp however you would like. The link posted by @JohnK is a great example for that! :smiley:

3 Likes

this was exactly what I needed! thanks!

1 Like