Search in sources :

Example 1 with JoyAxisEvent

use of com.jme3.input.event.JoyAxisEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidJoystickJoyInput14 method onGenericMotion.

public boolean onGenericMotion(MotionEvent event) {
    boolean consumed = false;
    //        logger.log(Level.INFO, "onGenericMotion event: {0}", event);
    event.getDeviceId();
    event.getSource();
    //        logger.log(Level.INFO, "deviceId: {0}, source: {1}", new Object[]{event.getDeviceId(), event.getSource()});
    AndroidJoystick joystick = joystickIndex.get(event.getDeviceId());
    if (joystick != null) {
        for (int androidAxis : joystick.getAndroidAxes()) {
            String axisName = MotionEvent.axisToString(androidAxis);
            float value = event.getAxisValue(androidAxis);
            int action = event.getAction();
            if (action == MotionEvent.ACTION_MOVE) {
                //                    logger.log(Level.INFO, "MOVE axis num: {0}, axisName: {1}, value: {2}",
                //                            new Object[]{androidAxis, axisName, value});
                JoystickAxis axis = joystick.getAxis(androidAxis);
                if (axis != null) {
                    //                        logger.log(Level.INFO, "MOVE axis num: {0}, axisName: {1}, value: {2}, deadzone: {3}",
                    //                                new Object[]{androidAxis, axisName, value, axis.getDeadZone()});
                    JoyAxisEvent axisEvent = new JoyAxisEvent(axis, value);
                    joyInput.addEvent(axisEvent);
                    consumed = true;
                } else {
                //                        logger.log(Level.INFO, "axis was null for axisName: {0}", axisName);
                }
            } else {
            //                    logger.log(Level.INFO, "action: {0}", action);
            }
        }
    }
    return consumed;
}
Also used : JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis)

Example 2 with JoyAxisEvent

use of com.jme3.input.event.JoyAxisEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidSensorJoyInput method updateOrientation.

/**
     * Calculates the device orientation based off the data recieved from the
     * Acceleration Sensor and Mangetic Field sensor
     * Values are returned relative to the Earth.
     *
     * From the Android Doc
     *
     * Computes the device's orientation based on the rotation matrix. When it returns, the array values is filled with the result:
     *  values[0]: azimuth, rotation around the Z axis.
     *  values[1]: pitch, rotation around the X axis.
     *  values[2]: roll, rotation around the Y axis.
     *
     * The reference coordinate-system used is different from the world
     * coordinate-system defined for the rotation matrix:
     *  X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points West).
     *  Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
     *  Z points towards the center of the Earth and is perpendicular to the ground.
     *
     * @return True if Orientation was calculated
     */
private boolean updateOrientation() {
    SensorData sensorData;
    AndroidSensorJoystickAxis axis;
    final float[] curInclinationMat = new float[16];
    final float[] curRotationMat = new float[16];
    final float[] rotatedRotationMat = new float[16];
    final float[] accValues = new float[3];
    final float[] magValues = new float[3];
    final float[] orderedOrientation = new float[3];
    // if the Gravity Sensor is available, use it for orientation, if not
    // use the accelerometer
    // NOTE: Seemed to work worse, so just using accelerometer
    //        sensorData = sensors.get(Sensor.TYPE_GRAVITY);
    //        if (sensorData == null) {
    sensorData = sensors.get(Sensor.TYPE_ACCELEROMETER);
    if (sensorData == null || !sensorData.enabled || !sensorData.haveData) {
        return false;
    }
    if (sensorData.sensorAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return false;
    }
    synchronized (sensorData.valuesLock) {
        accValues[0] = sensorData.lastValues[0];
        accValues[1] = sensorData.lastValues[1];
        accValues[2] = sensorData.lastValues[2];
    }
    sensorData = sensors.get(Sensor.TYPE_MAGNETIC_FIELD);
    if (sensorData == null || !sensorData.enabled || !sensorData.haveData) {
        return false;
    }
    if (sensorData.sensorAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return false;
    }
    synchronized (sensorData.valuesLock) {
        magValues[0] = sensorData.lastValues[0];
        magValues[1] = sensorData.lastValues[1];
        magValues[2] = sensorData.lastValues[2];
    }
    if (SensorManager.getRotationMatrix(curRotationMat, curInclinationMat, accValues, magValues)) {
        final float[] orientValues = new float[3];
        if (remapCoordinates(curRotationMat, rotatedRotationMat)) {
            SensorManager.getOrientation(rotatedRotationMat, orientValues);
            //                logger.log(Level.FINE, "Orientation Values: {0}, {1}, {2}",
            //                        new Object[]{orientValues[0], orientValues[1], orientValues[2]});
            // need to reorder to make it x, y, z order instead of z, x, y order
            orderedOrientation[0] = orientValues[1];
            orderedOrientation[1] = orientValues[2];
            orderedOrientation[2] = orientValues[0];
            sensorData = sensors.get(Sensor.TYPE_ORIENTATION);
            if (sensorData != null && sensorData.axes.size() > 0) {
                for (int i = 0; i < orderedOrientation.length; i++) {
                    axis = sensorData.axes.get(i);
                    if (axis != null) {
                        axis.setCurRawValue(orderedOrientation[i]);
                        if (!sensorData.haveData) {
                            sensorData.haveData = true;
                        } else {
                            if (axis.isChanged()) {
                                joyInput.addEvent(new JoyAxisEvent(axis, axis.getJoystickAxisValue()));
                            }
                        }
                    }
                }
            } else if (sensorData != null) {
                if (!sensorData.haveData) {
                    sensorData.haveData = true;
                }
            }
            return true;
        } else {
            logger.log(Level.FINE, "remapCoordinateSystem failed");
        }
    } else {
        logger.log(Level.FINE, "getRotationMatrix returned false");
    }
    return false;
}
Also used : JoyAxisEvent(com.jme3.input.event.JoyAxisEvent)

Example 3 with JoyAxisEvent

use of com.jme3.input.event.JoyAxisEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidSensorJoyInput method onSensorChanged.

// Start of Android SensorEventListener methods
@Override
public void onSensorChanged(SensorEvent se) {
    if (!loaded) {
        return;
    }
    //        logger.log(Level.FINE, "onSensorChanged for {0}: accuracy: {1}, values: {2}",
    //                new Object[]{se.sensor.getName(), se.accuracy, se.values});
    int sensorType = se.sensor.getType();
    SensorData sensorData = sensors.get(sensorType);
    if (sensorData != null) {
    //            logger.log(Level.FINE, "sensorData name: {0}, enabled: {1}, unreliable: {2}",
    //                    new Object[]{sensorData.sensor.getName(), sensorData.enabled, sensorData.sensorAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE});
    }
    if (sensorData != null && sensorData.sensor.equals(se.sensor) && sensorData.enabled) {
        if (sensorData.sensorAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
            return;
        }
        synchronized (sensorData.valuesLock) {
            for (int i = 0; i < sensorData.lastValues.length; i++) {
                sensorData.lastValues[i] = se.values[i];
            }
        }
        if (sensorData.axes.size() > 0) {
            AndroidSensorJoystickAxis axis;
            for (int i = 0; i < se.values.length; i++) {
                axis = sensorData.axes.get(i);
                if (axis != null) {
                    axis.setCurRawValue(se.values[i]);
                    if (!sensorData.haveData) {
                        sensorData.haveData = true;
                    } else {
                        if (axis.isChanged()) {
                            JoyAxisEvent event = new JoyAxisEvent(axis, axis.getJoystickAxisValue());
                            //                                logger.log(Level.INFO, "adding JoyAxisEvent: {0}", event);
                            joyInput.addEvent(event);
                        //                                joyHandler.addEvent(new JoyAxisEvent(axis, axis.getJoystickAxisValue()));
                        }
                    }
                }
            }
        } else if (sensorData != null) {
            if (!sensorData.haveData) {
                sensorData.haveData = true;
            }
        }
    }
}
Also used : JoyAxisEvent(com.jme3.input.event.JoyAxisEvent)

Example 4 with JoyAxisEvent

use of com.jme3.input.event.JoyAxisEvent in project jmonkeyengine by jMonkeyEngine.

the class GlfwJoystickInput method update.

public void update() {
    for (final Map.Entry<Integer, GlfwJoystick> entry : joysticks.entrySet()) {
        // Axes
        final FloatBuffer axisValues = glfwGetJoystickAxes(entry.getKey());
        for (final JoystickAxis axis : entry.getValue().getAxes()) {
            final float value = axisValues.get(axis.getAxisId());
            listener.onJoyAxisEvent(new JoyAxisEvent(axis, value));
        }
        // Buttons
        final ByteBuffer byteBuffer = glfwGetJoystickButtons(entry.getKey());
        for (final JoystickButton button : entry.getValue().getButtons()) {
            final boolean pressed = byteBuffer.get(button.getButtonId()) == GLFW_PRESS;
            listener.onJoyButtonEvent(new JoyButtonEvent(button, pressed));
        }
    }
}
Also used : JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent) FloatBuffer(java.nio.FloatBuffer) Map(java.util.Map) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer)

Example 5 with JoyAxisEvent

use of com.jme3.input.event.JoyAxisEvent in project jmonkeyengine by jMonkeyEngine.

the class JInputJoyInput method update.

public void update() {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    Event e = new Event();
    for (int i = 0; i < cs.length; i++) {
        Controller c = cs[i];
        JInputJoystick stick = joystickIndex.get(c);
        if (stick == null)
            continue;
        if (!c.poll())
            continue;
        int joyId = stick.getJoyId();
        EventQueue q = c.getEventQueue();
        while (q.getNextEvent(e)) {
            Identifier id = e.getComponent().getIdentifier();
            if (id == Identifier.Axis.POV) {
                float x = 0, y = 0;
                float v = e.getValue();
                if (v == POV.CENTER) {
                    x = 0;
                    y = 0;
                } else if (v == POV.DOWN) {
                    x = 0;
                    y = -1f;
                } else if (v == POV.DOWN_LEFT) {
                    x = -1f;
                    y = -1f;
                } else if (v == POV.DOWN_RIGHT) {
                    x = 1f;
                    y = -1f;
                } else if (v == POV.LEFT) {
                    x = -1f;
                    y = 0;
                } else if (v == POV.RIGHT) {
                    x = 1f;
                    y = 0;
                } else if (v == POV.UP) {
                    x = 0;
                    y = 1f;
                } else if (v == POV.UP_LEFT) {
                    x = -1f;
                    y = 1f;
                } else if (v == POV.UP_RIGHT) {
                    x = 1f;
                    y = 1f;
                }
                JoyAxisEvent evt1 = new JoyAxisEvent(stick.povX, x);
                JoyAxisEvent evt2 = new JoyAxisEvent(stick.povY, y);
                listener.onJoyAxisEvent(evt1);
                listener.onJoyAxisEvent(evt2);
            } else if (id instanceof Axis) {
                float value = e.getValue();
                JoystickAxis axis = stick.axisIndex.get(e.getComponent());
                JoyAxisEvent evt = new JoyAxisEvent(axis, value);
                listener.onJoyAxisEvent(evt);
            } else if (id instanceof Button) {
                JoystickButton button = stick.buttonIndex.get(e.getComponent());
                JoyButtonEvent evt = new JoyButtonEvent(button, e.getValue() == 1f);
                listener.onJoyButtonEvent(evt);
            }
        }
    }
}
Also used : DefaultJoystickButton(com.jme3.input.DefaultJoystickButton) JoystickButton(com.jme3.input.JoystickButton) Identifier(net.java.games.input.Component.Identifier) JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent) DefaultJoystickButton(com.jme3.input.DefaultJoystickButton) JoystickButton(com.jme3.input.JoystickButton) Button(net.java.games.input.Component.Identifier.Button) JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis) Axis(net.java.games.input.Component.Identifier.Axis)

Aggregations

JoyAxisEvent (com.jme3.input.event.JoyAxisEvent)5 DefaultJoystickAxis (com.jme3.input.DefaultJoystickAxis)2 JoystickAxis (com.jme3.input.JoystickAxis)2 JoyButtonEvent (com.jme3.input.event.JoyButtonEvent)2 DefaultJoystickButton (com.jme3.input.DefaultJoystickButton)1 JoystickButton (com.jme3.input.JoystickButton)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Identifier (net.java.games.input.Component.Identifier)1 Axis (net.java.games.input.Component.Identifier.Axis)1 Button (net.java.games.input.Component.Identifier.Button)1