-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cameras: cameraDirection will now properly decrease when gravity is applied #14508
Conversation
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
Snapshot stored with reference name: Test environment: To test a playground add it to the URL, for example: https://babylonsnapshots.z22.web.core.windows.net/refs/pull/14508/merge/index.html#WGZLGJ#4600 Links to test babylon tools with this snapshot: https://playground.babylonjs.com/?snapshot=refs/pull/14508/merge To test the snapshot in the playground with a playground ID add it after the snapshot query string: https://playground.babylonjs.com/?snapshot=refs/pull/14508/merge#BCU1XR#0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note - if the camera gravity is being changed, so should the mesh moveWithCollision function.
Is there a path where the change is done on the collider level, instead of replicating it for every camera using collisions?
if (this.inertia === 0) { | ||
this._updatePosition(relativeInertia, scaleFactor); | ||
this.cameraDirection.scaleInPlace(0); | ||
} else { | ||
this.cameraDirection.scaleInPlace(relativeInertia); | ||
this._updatePosition(relativeInertia, scaleFactor); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's technically the only change that was needed to go back to the way things were before
Recently an issue was found where the FreeCamera was not slowing down when gravity was active. This PR modifies the
_collideWithWorld
function to fix that issue.Forum Link: https://forum.babylonjs.com/t/bug-free-camera-never-stop/45546/4
Proof for using deltaTime ratio as scaling factor for gravity:
Given the following variables:
r = initial offset
n = inertia
a = relative inertia (with respect to frame deltaTime ratio; currentDeltaTime/standardDeltaTime) = n^t
s = scale factor
t = deltaTime ratio = currentDeltaTime/standardDeltaTime
g = gravity
x = scaling ratio to apply to gravity
Working from our previous proof for relative inertia and scale factor (#14482), we know that a frames worth of work looks something like:
sra = rn^t + ... + rn
When working with gravity applied, the equation will look more like:
sra + xg = (rn^t + g) + ... + (rn + g)
x
in this scenario is some ratio to multiply gravity by so that the same amount of gravity is applied to each frame as though it was 60 FPS. Next, let's reorganize things:sra + xg = rn^t + ... + rn + (g + ... + g) = rn^t + ... + rn + t*g
We know that there are t terms for our geometric sum so that means if we are adding gravity to each, we have
t
number ofg
values. Therefore, we havetg
at the end. By our first line, we know thatsra = rn^t + ... + rn
so we can use substitution to make things cleaner:sra + xg = sra + tg
If we subtract
sra
from each side, we get:xg = tg
x = t
Therefore, since gravity is just a constant and is unaffected by inertia, we can just multiply it by
t
(our deltaTime ratio).