I'm trying to detect shaking event using Cordova 2.2.0 for android devices.
I found some question related to this topic but t's in native code for example this question and this question.
Does anyone knows how to detect this event using phonegap Cordova ? or I should write a plugin ?
You can try shake.js. Ive been looking into it, but not implemented it. It looks promising.
Use the accelerometer to store the previous values (x, y and z). Defining the thresholds (x,y,z) you can detect shaking if the different betwen the previosValues and the actual once (event.value[i] where i=x,y ans z) is higher than the thresholds.
You also can use the magnitude of the acceleration values (Acc=sqrt(x*x+y*y+z*z)) or the timestamp to obtain better results.
Cordova offers the device-motion plugin, which (surprisingly) exposes a navigator.accelerometer object, instead of aligning with the W3C deviceorientation/devicemotion standard published since 2011.
When the device lays flat on a surface, the (x, y, z) acceleration will be (0, 0, 9.81). The basic idea for detecting a shake is to watch the acceleration with a given frequency, calculate the delta from the previous sample, and decide if it's larger than a threshold.
navigator.accelerometer.watchAcceleration(onSuccess, onError, { frequency: 300 });
// Assess the current acceleration parameters to determine a shake
function onSuccess(acceleration) {
var accelerationChange = {};
if (previousAcceleration.x !== null) {
accelerationChange.x = Math.abs(previousAcceleration.x - acceleration.x);
accelerationChange.y = Math.abs(previousAcceleration.y - acceleration.y);
accelerationChange.z = Math.abs(previousAcceleration.z - acceleration.z);
}
previousAcceleration = {
x: acceleration.x,
y: acceleration.y,
z: acceleration.z
};
if (accelerationChange.x + accelerationChange.y + accelerationChange.z > sensitivity) {
// Shake detected, invoke callback
}
}
A plugin doing that is Lee Crossley's cordova-plugin-shake-detection.
Related
I'm new to Unity and I am trying to build a solar system exploration app through unity. I have the environment set up, and now all I need is the ability to look around (via tilting and moving the phone itself, which is android) smoothly. I have the ability to look around, but if I do a complete 180, it seems to invert the physical orientation of the phone with the visual movements in game, e.g. if I have turn 180 degrees, if I tilt the phone down it shifts my vision in game to the right, up results in visual shift to the left. Here is the code I have thus far:
#pragma strict
private var quatMult : Quaternion;
private var quatMap : Quaternion;
function Start () {
Input.gyro.enabled = true;
}
function Update () {
#if UNITY_ANDROID
quatMap = Input.gyro.attitude;
#endif
transform.localRotation = Quaternion.Euler(90, 0, 0) * quatMap * Quaternion(0,0,1,0) /*quatMult*/;
}
Any help is greatly appreciated. Thanks.
This should be what you're looking for: https://gist.github.com/chanibal/baf46307c4fee3c699d5. Just drag it to the camera and it should work.
You might want to remove the reset on touch part (Input.touchCount > 0 in Update) and debug information (the OnGui method).
n00b question and I don't even understand enough to be able to Google well (or understand the answers appropriately).
I'm exploring Unity 3D and playing with Gyroscope input The issue is that each axis of the gyro returns values between -1 and +1 with 0 being the devices orientation when the gyro is enabled, where +1 & -1 share a border (like 12 on a clockface). However, the gyro's 0,0,0 point is not being reset when the gyro is enabled. The unity forums suggest deactivating the then re-activating the gyro will help - it doesn't.
In effect the gyro's 0 point is static in the current version of iOS & Unity, which means I need to 'offset' it to take into account the phone's starting position.
My plan was to have something like:
var GyroOffset : Quaternion;
var CurrentGyroAttitude : Quaternion;
var DeadSpotXPos : float;
var DeadSpotXPos : float;
var ThresholdPos : float;
var ThresholdNeg : float;
function Start() {
Input.gyro.enabled = true;
Input.gyro.UpdateInterval = 0.001;
GyroOffset = Input.gryo.attitude;
}
function Update() {
CurrentGyroAttitude = input.gyro.attitude - GyroOffset;
if (CurrentGyroAttitude.x > ThresholdPos) DoSomething(1);
if (CurrentGyroAttitude.x < ThresholdNeg) DoSomething(-1);
if (CurrentGyroAttitude.x > DeadSpotXPos &&
CurrentGyroAttitude.X < ThresholdPos) DoSomethingElse(1);
if (CurrentGyroAttitude.x < DeadSpotXNeg &&
CurrentGyroAttitude.x < ThresholdNeg) DoSomethingElse(-1);
}
* Where DoSomething(arbitraryFloat) and DoSomethingElse(anotherArbitraryFloat) does things that I want done and arbitrary float is not related to Gyro output.
As the Gyro output is between -1 and +1, we could reach a case where CurrentGyroAttitude.x could legitimately be -2 or +2. The problem with this plan is that I have to "offset" to my thresholds & deadspots as well which becomes "not neat" for tuning purposes. I'd much rather only manipulate the CurrentGyroAttitude.
However, I don't know how to do "clock face arthematic" in UnityScript. I think I'm talking about modulus mathematics but I keep running into complex mathematics about crypto that's doing my head in. :)
Can anyone walk me through "cleaning" the gyro attitude output so that my clean output stays between -1 and +1, once 'offset' has been taken into effect?
Incidentally if anyone can shed light on Unity's GyroUpdateInterval that would be awesome because the documentation doesn't explain what the updates are measured in, or what the effect of manipulating is.
I don't suppose you really wanted to access members of the Quaternion - they don't work the way you think they do. What you wanted was Quaternion.eulerAngles.
To make the angle relative to the starting position modify your Update:
Vector3 CurrentGyroEuler=(Quaternion.Inverse(Input.gyro.attitude)* GyroOffset).eulerAngles
I used information and code from this answer (rewriting code to Javascript) in my simple demo PhoneGap Buld application, to recalculate gravity (G) to real acceleration (m/s2) with 1 second frequency.
This is actual code (important part):
function onAccelerationSuccess(acceleration)
{
var g = 9.80665;
acceleration.x = (acceleration.x * g).toFixed(2) + ' m/s\u00b2';
acceleration.y = (acceleration.y * g).toFixed(2) + ' m/s\u00b2';
acceleration.z = ((acceleration.z + 1) * g).toFixed(2) + ' m/s\u00b2';
...
}
watchID = navigator.accelerometer.watchAcceleration(onAccelerationSuccess, onAccelerationError, {frequency: 1000});
Mentioned answer and many, many sources claims, that with my phone lying on the table face-up, I should get values of (0, 0, -1) G for the x, y, and z axes respectively. Assuming Earth's natural acceleration (g = 9.80665), I should see real acceleration values of (0, 0, 9.81) m/s2 and these values should not change (as phone is resting still). Am I right?
However, actually I'm seeing, that my real values are:
X axis: -1.87, -1.50, -2.25,
Y axis: 2.26, 1.88, 1.51, 0.76,
Z axis: 101.87, 101.49, 102.25, 102.62, 103.37.
These values are constantly changing, but only between these mentioned, and not every axis gets changed value each second. Sometimes, a value for some axis remains for 2-3 seconds.
What is happening? How can a phone variate its acceleration, if it is holding still on my desk? How can any device, that is not moving in any direction have such enormous acceleration like 100 m/s2?
I have heard that accelerators on-board mobile devices are more like toy than real a measurement device and that they're producing a lot of noise or jitter to returned values. But, for God sake, this is a complete garbage, that is making use of this function completely pointless.
I tested this code on Google Nexus (first edition) phone, with Android 4.2.2. App with Phonegap 2.9.0.
EDIT: I've tested my mobile application with Ripple Emulator and I'm getting perfectly valid values:
Acceleration in the X axis is 0.00 m/s².
Acceleration in the Y axis is 0.00 m/s².
Acceleration in the Z axis is 9.81 m/s².
Is something wrong with accelerometer / compass / gyro on-board my Nexus?
There is no bug. I've read many SO questions about iOS native programming in ObjectiveC, when I was dealing with accelerometer. And I missed PhoneGap API documentation, which says, that values passed are already recalculated:
Acceleration values include the effect of gravity (9.81 m/s^2).
After removing double gravity calculation all seems to be fine.
I'm betting values +/-0.3 m/s2 for x and y axis and around 10.3-10.4 m/s2 for z axis, when phone is lying on my desk. But I assume, these are variations and mentioned noise, that is comming from fairly cheap accelerometer chip used in mobile devices.
I try to create game for Android and I have problem with high speed objects, they don't wanna to collide.
I have Sphere with Sphere Collider and Bouncy material, and RigidBody with this param (Gravity=false, Interpolate=Interpolate, Collision Detection = Continuous Dynamic)
Also I have 3 walls with Box Collider and Bouncy material.
This is my code for Sphere
function IncreaseBallVelocity() {
rigidbody.velocity *= 1.05;
}
function Awake () {
rigidbody.AddForce(4, 4, 0, ForceMode.Impulse);
InvokeRepeating("IncreaseBallVelocity", 2, 2);
}
In project Settings I set: "Min Penetration For Penalty Force"=0.001, "Solver Interation Count"=50
When I play on the start it work fine (it bounces) but when speed go to high, Sphere just passes the wall.
Can anyone help me?
Thanks.
Edited
var hit : RaycastHit;
var mainGameScript : MainGame;
var particles_splash : GameObject;
function Awake () {
rigidbody.AddForce(4, 4, 0, ForceMode.Impulse);
InvokeRepeating("IncreaseBallVelocity", 2, 2);
}
function Update() {
if (rigidbody.SweepTest(transform.forward, hit, 0.5))
Debug.Log(hit.distance + "mts distance to obstacle");
if(transform.position.y < -3) {
mainGameScript.GameOver();
//Application.LoadLevel("Menu");
}
}
function IncreaseBallVelocity() {
rigidbody.velocity *= 1.05;
}
function OnCollisionEnter(collision : Collision) {
Instantiate(particles_splash, transform.position, transform.rotation);
}
EDITED added more info
Fixed Timestep = 0.02 Maximum Allowed Tir = 0.333
There is no difference between running the game in editor player and on Android
No. It looks OK when I set 0.01
My Paddle is Box Collider without Rigidbody, walls are the same
There are all in same layer (when speed is normal it all works) value in PhysicsManager are the default (same like in image) exept "Solver Interation Co..." = 50
No. When I change speed it pass other wall
I am using standard cube but I expand/shrink it to fit my screen and other objects, when I expand wall more then it's OK it bouncing
No. It's simple project simple example from Video http://www.youtube.com/watch?v=edfd1HJmKPY
I don't use gravity
See:
Similar SO Question
A community script that uses ray tracing to help manage fast objects
UnityAnswers post leading to the script in (2)
You could also try changing the fixed time step for physics. The smaller this value, the more times Unity calculates the physics of a scene. But be warned, making this value too small, say <= 0.005, will likely result in an unstable game, especially on a portable device.
The script above is best for bullets or small objects. You can manually force rigid body collisions tests:
public class example : MonoBehaviour {
public RaycastHit hit;
void Update() {
if (rigidbody.SweepTest(transform.forward, out hit, 10))
Debug.Log(hit.distance + "mts distance to obstacle");
}
}
I think the main problem is the manipulation of Rigidbody's velocity. I would try the following to solve the problem.
Redesign your code to ensure that IncreaseBallVelocity and every other manipulation of Rigidbody is called within FixedUpdate. Check that there are no other manipulations to Transform.position.
Try to replace setting velocity directly by using AddForce or similar methods so the physics engine has a higher chance to calculate all dependencies.
If there are more items (main player character, ...) involved related to the physics calculation, ensure that their code runs in FixedUpdate too.
Another point I stumbled upon were meshes that are scaled very much. Having a GameObject with scale <= 0.01 or >= 100 has definitely a negative impact on physics calculation. According to the docs and this Unity forum entry from one of the gurus you should avoid Transform.scale values != 1
Still not happy? OK then the next test is starting with high velocities but no acceleration. At this phase we want to know, if the high velocity itself or the acceleration is to blame for the problem. It would be interesting to know the velocities' values at which the physics engine starts to fail - please post them so that we can compare them.
EDIT: Some more things to investigate
6.7 m/sec does not sound that much so that I guess there is a special reason or a combination of reasons why things go wrong.
Is your Maximum Allowed Timestep high enough? For testing I suggest 5 to 10x Fixed Timestep. Note that this might kill the frame rate but that can be dfixed later.
Is there any difference between running the game in editor player and on Android?
Did you notice any drops in frame rate because of the 0.01 FixedTimestep? This would indicate that the physics engine might be in trouble.
Could it be that there are static colliders (objects having a collider but no Rigidbody) that are moved around or manipulated otherwise? This would cause heavy recalculations within PhysX.
What about the layers: Are all walls on the same layer resp. are the involved layers are configured appropriately in collision detection matrix?
Does the no-bounce effect always happen at the same wall? If so, can you just copy the 1st wall and put it in place of the second one to see if there is something wrong with this specific wall.
If not to much effort, I would try to set up some standard cubes as walls just to be sure that transform.scale is not to blame for it (I made really bad experience with this).
Do you manipulate gravity or TimeManager.timeScale from within a script?
BTW: are you using gravity? (Should be no problem just
I am making a 2d game. The phone is held horizontally and a character moves up/down & left/right to avoid obstacles. The character is controlled by the accelerometer on the phone. Everything works fine if the player doesn't mind (0,0) (the point where the character stands still) being when the phone is held perfectly flat. In this scenario it's possible to just read the Y and X values directly and use them to control the character. The accelerometer values are between -10 and 10 (they get multiplied by an acceleration constant to decide the movement speed of the character), libgdx is the framework used.
The problem is that having (0,0) isn't very comfortable, so the idea is to calibrate it so that 0,0 will be set to the phones position at a specific point in time.
Which brings me to my question, how would I do this? I tried just reading the current X and Y values then subtracting it. The problem with that is that when the phone is held at a 90 degree angle then the X offset value is 10 (which is the max value) so it ends up becoming impossible to move because the value will never go over 10 (10-10 = 0). The Z axis has to come into play here somehow, I'm just not sure how.
Thanks for the help, I tried explaining as best as I can, I did try searching for the solution, but I don't even know what the proper term is for what I'm looking for.
An old question, but I am providing the answer here as I couldn't find a good answer for Android or LibGDX anywhere. The code below is based on a solution someone posted for iOS (sorry, I have lost the reference).
You can do this in three parts:
Capture a vector representing the neutral direction:
Vector3 tiltCalibration = new Vector3(
Gdx.input.getAccelerometerX(),
Gdx.input.getAccelerometerY(),
Gdx.input.getAccelerometerZ() );
Transform this vector into a rotation matrix:
public void initTiltControls( Vector3 tiltCalibration ) {
Vector3.tmp.set( 0, 0, 1 );
Vector3.tmp2.set( tiltCalibration ).nor();
Quaternion rotateQuaternion = new Quaternion().setFromCross( Vector3.tmp, Vector3.tmp2 );
Matrix4 m = new Matrix4( Vector3.Zero, rotateQuaternion, new Vector3( 1f, 1f, 1f ) );
this.calibrationMatrix = m.inv();
}
Whenever you need inputs from the accelerometer, first run them through the rotation matrix:
public void handleAccelerometerInputs( float x, float y, float z ) {
Vector3.tmp.set( x, y, z );
Vector3.tmp.mul( this.calibrationMatrix );
x = Vector3.tmp.x;
y = Vector3.tmp.y;
z = Vector3.tmp.z;
[use x, y and z here]
...
}
For a simple solution you can look at the methods:
Gdx.input.getAzimuth(), Gdx.input.getPitch(), Gdx.input.getRoll()
The downside is that those somehow use the internal compass to give your devices rotation compared to North/South/East/West. I did only test that very shortly so I'm not 100% sure about it though. Might be worth a look.
The more complex method involves some trigonometry, basically you have to calculate the angle the phone is held at from Gdx.input.getAccelerometerX/Y/Z(). Must be something like (for rotation along the longer side of the phone):
Math.atan(Gdx.input.getAccelerometerX() / Gdx.input.getAccelerometerZ());
For both approaches you then store the initial angle and subtract it later on again. You have to watch out for the ranges though, I think Math.atan(...) is within -Pi and Pi.
Hopefully that'll get you started somehow. You might search for "Accelerometer to pitch/roll/rotation" and similar, too.