Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * The MIT License (MIT)
   * Copyright (c) 2014 Christian Gärtner
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
 package org.achtern.AchternEngine.core.scenegraph.entity.controller;
 import lombok.Data;
 @EqualsAndHashCode(callSuper = false)
 public class MouseLook extends QuickEntity implements KeyListenerMouseListener {
     private static final Logger LOGGER = LoggerFactory.getLogger(MouseLook.class);
     protected float sensitivity;
     protected boolean mouselock = false;
     protected Key unlockKey;
     public MouseLook(float sensitivity) {
     public MouseLook(float sensitivityKey unlockKey) {
         this. = sensitivity;
         this. = unlockKey;

     public void attached() {
     protected void registerListener() {
                 .register(new KeyTrigger(this.), this);
                 .register(new MouseButtonTrigger(.), this)
                 .register(this); // MouseMoveEvent
     public void onAction(KeyEvent event) {
         .trace("unlockKey pressed <{}>; mouselock={}"isMouselock());
         if (!isMouselock()) return;
         .trace("Cursor enabled; mouselock={}"isMouselock());
     public void onAction(MouseEvent event) {
         if (event.getButton() == null) {
             // MouseMove
             if (!isMouselock()) {
                 I honestly do not know why this is needed here,
                 but it seems to fix a bug, where the cursor wouldn't get reenabled after unlocking.
                 this action handler gets called and mouseLock gets updated, but the single call to
                 event.getInputAdapter().setCursor(true); is not enough for some reason,
                 maybe the second one fixes it, because... I don't know...
            Vector2f factor = getDeltaMoveFactor(getSensitivity(), event.getMouseDelta()).abs();
            getTransform().rotate(getTransform().getRotation().getRight(), -event.getMouseDelta().getY() * factor.getX());
            getTransform().rotate(.event.getMouseDelta().getX() * factor.getY());
        } else {
            // Mouse Click

This returns the sensitivity aka delta move factor. This returns the sensitivity based on the delta. This is a logistic function, meaning that it tends to 0 for small values and to 1 for big. I this case it tends to getSensitivity() for high values and if the delta is 0, this function returns 0.25. This allows to move the mouse accurately up to a 1/4 of a pixel! Only the value will be taken, on the returned vector will org.achtern.AchternEngine.core.math.Vector2f.abs() be called.

sensitivity sensitivity, supplied by the user
delta mouse move event delta org.achtern.AchternEngine.core.input.event.payload.MouseEvent
move factor for X and Y axis
    protected Vector2f getDeltaMoveFactor(float sensitivityVector2f delta) {
        float f0 = 0.25f; // This is the y-axis value at x = 0
        float k = 0.7f; // k determines how steep/fast the sensitivity increases.
        // We have to take absoulute values, otherwise the sensitivity would be different for
        // downwards motions or upwards motions.
        delta = delta.abs();
        float x = sensitivity / (
                (float) (1 + Math.exp(-k * delta.getX()))
                        * ((1 / f0) - 1)
        float y = sensitivity / (
                (float) (1 + Math.exp(-k * delta.getY()))
                        * ((1 / f0) - 1)
        // This function should return values in the range of 0 and <sensitivity> only.
        assert 0 <= x && x <= sensitivity;
        assert 0 <= y && y <= sensitivity;
        return new Vector2f(xy);
    protected void centerMouse(InputAdapter input) {
New to GrepCode? Check out our FAQ X