Simple camera flip fix (Code)

Hey, I wrote a quick AppState that fixes the bug where the camera can infinitely do backflips/frontflips, making it so players can be upside down. Here’s the code:

[java]
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.math.Vector3f;

/**
*

  • @author Brendan
    */
    public class CameraLockState extends AbstractAppState {

    private SimpleApplication app;

    private Vector3f lastGoodDir;

    @Override
    public void initialize(AppStateManager asm, Application app) {
    super.initialize(asm, app);
    this.app = (SimpleApplication) app;
    }

    @Override
    public void update(float tpf) {
    if(lastGoodDir == null) {
    lastGoodDir = app.getCamera().getDirection();
    }

     Vector3f curDir = app.getCamera().getDirection();
     if(curDir.getY() > 0.98F || curDir.getY() < -0.98F) {
         app.getCamera().lookAtDirection(lastGoodDir, Vector3f.UNIT_Y);
     }
     
     lastGoodDir = app.getCamera().getDirection();
    

    }
    }
    [/java]

1 Like

Awesome code! Thank you! I’m using this until I can find a solution that doesn’t cause camera jitter when you reach the limits. :slight_smile:

Possibly it can be interesting for someone. Infinite rotation.
http://hub.jmonkeyengine.org/forum/topic/simple-chasecamera/