0

I have what I believe is a fairly common setup for a ThreeJS scene that emulates a first person shooter world. I have this block of code in my animate loop, which came from one of the ThreeJS "controls" example. I can't remember which one since it's been a while.

               const onObject = userIsLookingAtObjects.length > 0;

                let delta = (currentTime - prevTime) / 1000;

                // Limit long delay gaps, which indicate
                //  a background task interfered with us and
                //  we don't want the camera/user
                const limitDelta = 0.2;

                if (delta > limitDelta) {
                    delta = Math.min(limitDelta, delta);
                    console.warn(`${errPrefix}Capping delta time at: ${limitDelta}`);
                }

                velocity.x -= velocity.x * 10.0 * delta;
                velocity.z -= velocity.z * 10.0 * delta;

                velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass

                // ROS: The Number() constructor is simply being used
                //  to convert the TRUE/FALSE move<direction> values
                //  to a number in the following set of values: [-1, 0, 1]
                direction.z = Number(moveForward) - Number(moveBackward);
                direction.x = Number(moveRight) - Number(moveLeft);
                direction.normalize(); // this ensures consistent movements in all directions

                if (moveForward || moveBackward)
                    velocity.z -= direction.z * 400.0 * delta;
                if (moveLeft || moveRight)
                    velocity.x -= direction.x * 400.0 * delta;

                if (onObject === true) {
                    // ROS: This appears to be part of a check to allow jumping or
                    //  not.  See the raycast intersection code above involving
                    //  onObject.
                    velocity.y = Math.max(0, velocity.y);
                    canJump = true;

                    if (g_BreakHerePlease)
                        // This aids tracing using Chrome DevTools.  See pointerlock.js
                        //  for the keystroke that sets g_BreakHerePlease to TRUE.
                        console.info(`${errPrefix}Set DevTools breakpoint here.`);
                }

                g_ThreeJsControls.moveRight(-velocity.x * delta);
                g_ThreeJsControls.moveForward(-velocity.z * delta);

                g_ThreeJsControls.getObject().position.y += (velocity.y * delta); // new behavior

                if (g_ThreeJsControls.getObject().position.y < 10) {
                    velocity.y = 0;
                    g_ThreeJsControls.getObject().position.y = 10;

                    canJump = true;
                }

Everything works fine, but I noticed something today. If one of my animation models is LERP'ing and I move the camera continuously, like when you "strafe" around an object in an FPS game, the object that is LERP'ing slows way down. So slow, that at first I thought that moving the camera actually stopped other animations. It doesn't, it just brings them to a snail's crawl.

What could be causing this? I'm hoping it's not inherent to moving the camera around a lot because "players" in my world will be moving constantly. Can I fix thi?

Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
  • 2
    Nothing in your code sample looks to be too computationally expensive, and camera movement shouldn’t affect other things. I think the problem is somewhere else, or maybe in `g_threeJsControls`. For something this complex, you might want to show a minimal example that reproduces the issue: https://stackoverflow.com/help/minimal-reproducible-example – M - Oct 30 '22 at 04:07
  • 1
    When is this code being called? Please show us more of your code. Or even better, construct a minimal reproducible example as Marquizzo suggested. How do you call your object animation? Are you giving it the same delta value? – Berthur Oct 31 '22 at 11:34
  • @Berthur The code is called every animate loop iteration. I'd like to construct a minimal example, but the code base is huge and I don't know where the problem is occurring so I don't know what code to excerpt. – Robert Oschler Nov 01 '22 at 01:16
  • 1
    @RobertOschler That's half the point of a minimal example :) Isolating the problem to a smaller part of your program. Who knows, maybe you will even discover the cause yourself in the process. If you make an example that we can run on its own, then you are much more likely to get help here. – Berthur Nov 01 '22 at 10:29

0 Answers0