Game Development Community

How do I stop my camera from collapsing to distance 0?

by Daniel Leidal · in Torque 3D Beginner · 03/31/2013 (8:21 am) · 5 replies

I got a camera that works right at least mathematically and functionally, except that as soon as it stops updating itself it zooms all the way to the orbit point distance 0. I know what distance I want it set to, I just need a way to keep it there.

this is my code in server/command.cs

function serverCmdadjustCamera(%client, %level, %isFirstPerson)
{
%dist = %level / 100;
%playerpos = %client.player.getPosition();
%rotationDegrees = getWord(%client.player.getEulerRotation(), 2);
%rotationRads = mDegToRad(%rotationDegrees);

%matRotZ = "0.0 0.0 0.0 0.0 0.0 1.0 " @ %rotationRads;
%matRotX = "0.0 0.0 0.0 1.0 0.0 0.0 " @ mDegToRad(60);
%matRotation = MatrixMultiply(%matRotX, %matRotZ);
%matCameraTransform = %client.player.getPosition() SPC getWord(%matRotation, 3) SPC getWord(%matRotation, 4) SPC getWord(%matRotation, 5) SPC getWord(%matRotation, 6);

%client.camera.setOrbitPoint(%matCameraTransform, 0, %dist, %dist, "0 0 0", true);
%client.setCameraObject(%client.camera);

%client.setFirstPerson(%isFirstPerson);
}

the client triggers this command on a recurring 20 milesecond timer (whether this is too fast I'll test later but it runs fine right now).

What happens on test is, first step pan the camera back so %level is set positively, then as I move or pan the camera, it displays the correct camera settings, albeit jittery. As soon as input is released the camera moves to distance 0.. the players feet.

About the author

Recent Threads

  • Client-side camera

  • #1
    03/31/2013 (10:14 am)
    Ok, first things first - 20ms is too fast, the system runs on 32ms ticks so you're probably losing a few of those.

    Next...
    function serverCmdadjustCamera(%client, %level, %isFirstPerson)
    {
    %dist = %level / 100; // ???
    %playerpos = %client.player.getPosition();
    %rotationDegrees = getWord(%client.player.getEulerRotation(), 2);
    %rotationRads = mDegToRad(%rotationDegrees);
    
    %matRotZ = "0.0 0.0 0.0 0.0 0.0 1.0 " @ %rotationRads;
    %matRotX = "0.0 0.0 0.0 1.0 0.0 0.0 " @ mDegToRad(60);
    %matRotation = MatrixMultiply(%matRotX, %matRotZ);
    %matCameraTransform = %client.player.getPosition() SPC getWord(%matRotation, 3) SPC getWord(%matRotation, 4) SPC getWord(%matRotation, 5) SPC getWord(%matRotation, 6);
    
    %minDist = 5; // just for illustration, set it where you want.
    if (%dist < %minDist)
      %dist = %minDist; // Now it can't be less than 5 units.  No more 0.
    
    %client.camera.setOrbitPoint(%matCameraTransform, 0, %dist, %dist, "0 0 0", true);
    %client.setCameraObject(%client.camera);
    
    %client.setFirstPerson(%isFirstPerson);
    }
    Notice right before %client.camera.setOrbitPoint() I clamp %dist to be >= %minDist - now regardless of your other calculations it will ever be less than this. Adjust to taste.
    #2
    03/31/2013 (3:55 pm)
    I fixed it.. the problem was that my camera was aiming at a point actually underneath the world sometimes. so I added a small Z amount to the player position and it works.

    however this code isn't a finished product yet until I figure out how to get the jitters out. I suspect I can increase the camera velocity largely so it doesn't have travel time, but I don't know if that's it at this point.
    #3
    04/03/2013 (2:40 pm)
    I posted my fix in a new thread because the solution to the problem doesn't relate to this title as well.

    https://www.garagegames.com/community/forums/viewthread/133673

    I apologize for my lack of formatting skills :)
    #4
    04/04/2013 (7:40 am)
    ok so that idea didn't work, so I put the camera back on the server side. But when is the best time to do the updating if it is to be updated on every player move or turn input?
    #5
    04/04/2013 (8:06 am)
    Use %client.camera.setOrbitObject() - then it will follow the player automagically....