From 5aca23f9b3b9790b43b65b74004ba900e784d8b4 Mon Sep 17 00:00:00 2001 From: "iwg..ic" Date: Sat, 8 Sep 2012 20:56:29 +0000 Subject: [PATCH] Changed Android Joystick Rumble to pulses. Rumble amount is used to determine length of vibration pulse. Vibration now stays on (pulsing based on rumble amount) until user sets joystick.rumble(0). git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9709 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../input/android/AndroidSensorJoyInput.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/engine/src/android/com/jme3/input/android/AndroidSensorJoyInput.java b/engine/src/android/com/jme3/input/android/AndroidSensorJoyInput.java index cc41eef34..b279894a0 100644 --- a/engine/src/android/com/jme3/input/android/AndroidSensorJoyInput.java +++ b/engine/src/android/com/jme3/input/android/AndroidSensorJoyInput.java @@ -73,8 +73,11 @@ import java.util.logging.Logger; * Rumble needs the following line in the Manifest File * * Because Andorid does not allow for the user to define the intensity of the - * vibration, the rumble amount (ie strength) is converted into the number of - * milliseconds the vibration lasts. + * vibration, the rumble amount (ie strength) is converted into vibration pulses + * The stronger the strength amount, the shorter the delay between pulses. If + * amount is 1, then the vibration stays on the whole time. If amount is 0.5, + * the vibration will a pulse of equal parts vibration and delay. + * To turn off vibration, set rumble amount to 0. * * @author iwgeric */ @@ -84,6 +87,7 @@ public class AndroidSensorJoyInput implements JoyInput, SensorEventListener { private InputManager inputManager = null; private SensorManager sensorManager = null; private Vibrator vibrator = null; + private long maxRumbleTime = 250; // 250ms private RawInputListener listener = null; private IntMap sensors = new IntMap(); private Joystick[] joysticks; @@ -523,9 +527,25 @@ public class AndroidSensorJoyInput implements JoyInput, SensorEventListener { // Start of JoyInput methods public void setJoyRumble(int joyId, float amount) { - // convert amount to milliseconds since Android doesn't allow intensity + // convert amount to pulses since Android doesn't allow intensity if (vibrator != null) { - vibrator.vibrate((long)(amount*1000)); + final long rumbleOnDur = (long)(amount * maxRumbleTime); // ms to pulse vibration on + final long rumbleOffDur = maxRumbleTime - rumbleOnDur; // ms to delay between pulses + final long[] rumblePattern = { + 0, // start immediately + rumbleOnDur, // time to leave vibration on + rumbleOffDur // time to delay between vibrations + }; + final int rumbleRepeatFrom = 0; // index into rumble pattern to repeat from + + logger.log(Level.INFO, "Rumble amount: {0}, rumbleOnDur: {1}, rumbleOffDur: {2}", + new Object[]{amount, rumbleOnDur, rumbleOffDur}); + + if (rumbleOnDur > 0) { + vibrator.vibrate(rumblePattern, rumbleRepeatFrom); + } else { + vibrator.cancel(); + } } }