- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试检测两部手机相互碰撞时产生的运动。
我的问题是,加速度计是适合这种情况的传感器吗?
如果是这样,我将如何实现?
否则,我应该使用什么传感器以及我应该以什么方式使用它?
根据 https://developer.android.com/guide/topics/sensors/sensors_overview 处的指南, TYPE_LINEAR_ACCELERATION
似乎是正确的使用方式,但我不知道如何使用它。
最佳答案
这是您可以做到的。
1- 像这样初始化传感器对象并注册传感器更新事件回调
private void initSensorObject() {
SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor _Sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMgr.registerListener(sensorEventListener, _Sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
2- 按照以下方式处理传感器回调,检测排除重力影响的线性加速度,然后突然停止运动(我写这个是为了两次敲击 Action ,比如敲击然后停止,再次敲击然后再次停止。我将解释整个过程以便更好地理解,您可以轻松修改它以仅进行一次点击检测。)我在代码中添加了注释以使其不言自明。
/*
* Following are the parameters for Tap detection Algorithm
*/
private static float SPEED_THRESHOLD_RISE1;
private static float SPEED_THRESHOLD_DROP1;
private static float SPEED_THRESHOLD_RISE2;
private static float SPEED_THRESHOLD_DROP2;
private static int DROP_DELTA;
private static int RISE2_DELTA;
private int SENSITIVITY_INDEX = TapParam.SEN_DEFAULT;
private static final int TAP_STATE_RISE1 = 0;
private static final int TAP_STATE_DROP1 = 1;
private static final int TAP_STATE_RISE2 = 2;
private static final int TAP_STATE_DROP2 = 3;
private int tappingState = TAP_STATE_RISE1;
private boolean tapLastStateOnce = false;
private long lastSensorUpdate;
private long tap1DroppedAt = 0;
private int mathMeanIndex = 0;
private float[] lastLinearAcc = new float[3];
private float[] acceleSet = new float[TapParam.AM_SIZE];
private int acceleIndex = 0;
private float[] gravity = new float[3];
private float lastAccele = -99; // an arbitrarily very small value
/**
* onSensorChanged is called when the Motion Sensor value
* is changed and then run the algorithm to detect your desired motion.
*
* @return void
*/
private SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
long curSensorTime = System.currentTimeMillis();
if ((curSensorTime - lastSensorUpdate) < TapParam.SENSOR_RE_READ_TIME)
return;
lastSensorUpdate = curSensorTime;
acceleSet[acceleIndex] = getMotionAcceleration(event, curSensorTime);
acceleIndex = (acceleIndex + 1) % TapParam.AM_SIZE;
if (mathMeanIndex < TapParam.AM_SIZE)
mathMeanIndex++;
float accele = Util.getArithmeticMean(acceleSet);
switch (tappingState) {
case TAP_STATE_RISE1:
if (accele > SPEED_THRESHOLD_RISE1) {
tappingState = TAP_STATE_DROP1;
resetTapStateDropping();
mathMeanIndex = 0;
}
break;
case TAP_STATE_DROP1:
if (accele <= SPEED_THRESHOLD_DROP1) {
tappingState = TAP_STATE_RISE2;
resetTapStateRise2();
tap1DroppedAt = curSensorTime;
mathMeanIndex = 0;
}
break;
case TAP_STATE_RISE2:
if (curSensorTime - tap1DroppedAt >= TapParam.DELAY_BETWEEN_TAPS) {
if (accele > SPEED_THRESHOLD_RISE2) {
tappingState = TAP_STATE_DROP2;
resetTapStateDropping();
mathMeanIndex = 0;
}
}
break;
case TAP_STATE_DROP2:
if ((!tapLastStateOnce) && (accele <= SPEED_THRESHOLD_DROP2)) {
tapLastStateOnce = true;
resetTapStateRise2();
mathMeanIndex = 0;
onTapTapDetected();
}
break;
default:
tappingState = TAP_STATE_RISE1;
break;
}
}
/**
* onAccuracyChanged inter shall be called when hardware IMU
* (Inertial Measurement Unit a.k.a Motion Sensor) of the device change
* its accuracy value.
*
* @return void
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
setThresholdValues();
}
};
/**
* It shall return the Linear Acceleration of the device. The force of
* gravity shall be filtered out.
*
* @return float - Linear acceleration
*/
private float getMotionAcceleration(SensorEvent event, long curSensorTime) {
// In this code, alpha is calculated as t / (t + dT),
// where t is the low-pass filter's time-constant and
// dT is the event delivery rate.
final float alpha = 0.8f;
float[] linearAcc = new float[3];
// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
// Remove the gravity contribution with the high-pass filter.
linearAcc[0] = event.values[0] - gravity[0];
linearAcc[1] = event.values[1] - gravity[1];
linearAcc[2] = event.values[2] - gravity[2];
float accele = (Math.abs(lastLinearAcc[0] - linearAcc[0])
+ Math.abs(lastLinearAcc[1] - linearAcc[1]) + Math
.abs(lastLinearAcc[2] - linearAcc[2])) / 3;
lastLinearAcc = linearAcc;
return accele;
}
/**
* resetTapStateRise2 shall reset the tapping state if
* second Tap is not detected within TAP_RISE2_TIME time.
*
* @return void
*/
private void resetTapStateRise2() {
handleResetTapState.removeCallbacks(runResetTapState);
handleResetTapState.postDelayed(runResetTapState, RISE2_DELTA);
}
private Handler handleResetTapState = new Handler();
private Runnable runResetTapState = new Runnable() {
@Override
public void run() {
tappingState = TAP_STATE_RISE1;
tapLastStateOnce = false;
}
};
/**
* resetTapStateDropping shall reset the tapping state if
* Tap Drop is not detected within TAP_DROP_TIME time.
*
* @return void
*/
private void resetTapStateDropping() {
handleResetTapState.removeCallbacks(runResetTapState);
handleResetTapState.postDelayed(runResetTapState, DROP_DELTA);
}
private Handler handleResetTapState = new Handler();
private Runnable runResetTapState = new Runnable() {
@Override
public void run() {
tappingState = TAP_STATE_RISE1;
tapLastStateOnce = false;
}
};
3- 这是一个工作参数文件,可帮助您入门。 thresholds
数组定义了 10 个灵敏度级别,即您想要轻敲手机以检测为有效 Action 的力度或力度
TapParam.java
final class TapParam {
static final int SEN_DEFAULT = 4;
static final int SEN_MIN = 0;
static final int SEN_MAX = 9;
static final int DELAY_BETWEEN_TAPS = 75;
static final int SENSOR_RE_READ_TIME = 1;
static final int AM_SIZE = 5;
// Columns: A B Y D T1 T2 T3
private static final double[][] thresholds = new double[][]{
{0.8483763, 0.33935052, 0.5655842, 0.33935052, 175, 300, 175},
{0.95167595, 0.38067037, 0.6344506, 0.38067037, 175, 300, 175},
{1.0836192, 0.4334477, 0.7224128, 0.4334477, 175, 300, 175},
{1.8552876, 0.742115, 1.2368584, 0.742115, 175, 300, 175},
{2.4327612, 0.9731045, 1.6218408, 0.9731045, 175, 300, 175},
{3.5321822, 1.4128729, 2.354788, 1.4128729, 175, 300, 175},
{6.4446864, 2.5778747, 4.296458, 2.5778747, 175, 300, 175},
{8.2, 3.5, 5.4, 2.6, 175, 300, 175},
{9.8, 4.0, 6.0, 2.9, 175, 300, 175},
{12, 6.0, 8.0, 3.1, 175, 300, 175}
};
private static int indexLimiting(int index) {
return (index > SEN_MAX) ? SEN_MAX : (index < SEN_MIN) ? SEN_MIN : index;
}
static float getRISE1(int index) {
index = indexLimiting(index);
return (float) thresholds[index][0];
}
static float getDROP1(int index) {
index = indexLimiting(index);
return (float) thresholds[index][1];
}
static float getRISE2(int index) {
index = indexLimiting(index);
return (float) thresholds[index][2];
}
static float getDROP2(int index) {
index = indexLimiting(index);
return (float) thresholds[index][3];
}
static float getDROP_DELTA1(int index) {
index = indexLimiting(index);
return (float) thresholds[index][4];
}
static float getRISE_DELTA2(int index) {
index = indexLimiting(index);
return (float) thresholds[index][5];
}
}
更新:
/**
* setThresholdValues method shall calculate the Threshold values according
* to the accuracy value of the Motion Sensor.
*/
private void setThresholdValues() {
if (_Sensor == null)
return;
SPEED_THRESHOLD_RISE1 = TapParam.getRISE1(SENSITIVITY_INDEX);
SPEED_THRESHOLD_DROP1 = TapParam.getDROP1(SENSITIVITY_INDEX);
SPEED_THRESHOLD_RISE2 = TapParam.getRISE2(SENSITIVITY_INDEX);
SPEED_THRESHOLD_DROP2 = TapParam.getDROP2(SENSITIVITY_INDEX);
}
/**
* Method shall return the average (Arithmetic Mean) of the set of values
* passed as parameter.
*
* @param float[] set - the set of values
* @return float - arithmetic mean
*/
static float getArithmeticMean(float[] set) {
double sum = 0;
for (float aSet : set) {
sum += aSet;
}
return (float) sum / set.length;
}
更新 2:
在您的 Activity 的 onCreate()
中调用 setTapTapSensitivity()
private void setTapTapSensitivity() {
setTapTapSensitivity(3); //You can try 0 to 9 for 10 levels of sensitivity defined in TapParam.java. I have tried 3 and it works for a moderate tap
}
private void setTapTapSensitivity(int sensitivityIndex) {
RISE2_DELTA = (int) TapParam.getRISE_DELTA2(sensitivityIndex);
DROP_DELTA = (int) TapParam.getDROP_DELTA1(sensitivityIndex);
SENSITIVITY_INDEX = sensitivityIndex;
}
关于android - 如何使用设备的加速度计检测运动的增加然后突然停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574534/
这是否可以检测到手机何时像图片上那样移动?这个在android中叫什么名字? 是否有处理此类事情的事件?一年前,我看到一个带指南针的应用程序,它可以实时运行。 谢谢! 最佳答案 我希望这段代码能有所帮
我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库
所以我最近一直在研究 DirectX11,但我对它还是很陌生。我现在正在尝试通过翻译来移动一些东西,这就是我所拥有的。我一直在阅读 Frank D Luna 关于 DirectX11 的书,他提供了一
我一直在尝试为绘图元素制作动画,但没有成功。我可以对导入的图像进行动画处理,但是当我尝试对 pygame 生成的绘图进行动画处理时,它们仍然是静态的。 编辑:“动画”是指“移动”。就像使圆在 x 和
好吧,我已经尝试 Java 几个星期了,遵循类和在线教程。我做了一个简单的游戏,其中方 block 落向屏幕底部,而玩家控制一个小球,仅在 x 轴上移动并尝试避开它们。 我遇到的问题是方 block
我的 python 代码遇到一些问题,我正在制作蛇的一个版本,我的问题涉及蛇本身的运动。我已经得到了工作正常的方向,我只需要做到这一点,以便蛇继续沿着通过按键告诉它的方向移动,我还需要使它成为一个 b
那是我的代码。 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationC
我正在构建这个用于慢跑的 Android 应用程序,它应该测量用户从某个时间点开始的距离。 我对使用 GPS 选项(经度、纬度)不感兴趣,所以如果没有 GPS 选项并且只使用 android 传感器,
什么是自律?网上有这样一个回答:“最健康的自律是早睡,最实用的自律是运动,最丰盈内心的自律是读书。”早睡养神,运动养身,读书养脑。坚持把这三件事做好,你就已经超越了很多人。 1 每天早点睡 你有没有过
考虑下面这行 Lisp 代码: (some-function 7 8 | 9) ;; some comment. note the extra indentation 该点位于“8”和
在 Vim 中,如何移动到 xml 文件中的父/表亲标签?我正在寻找类似的东西: vatat " create a selection for second parent tag with all c
用 Dart 做这样的 Canvas 运动的最佳方法是什么? http://jsfiddle.net/loktar/dMYvG/ 我正在尝试使 Canvas 运动平稳,并想看看Dart可以做什么。 还
我试图让一个物体在固定的时间段内沿着圆形路径移动。 这个应用程序是一个“平滑运动”时钟。 因此,我不想每次 .getSeconds() 更新时将位置移动到固定坐标,而是想使用 ( .getSecond
我正在尝试创建一个简单的动画,其中一系列气泡围绕中心点旋转。我有一个动画版本,其中气泡在开始旋转之前从中心点扩散,效果很好,但是一旦我单击其中一个图像(引发动画),屏幕就会卡住一会儿,然后气泡出现在他
不久前我开始学习java作为一种爱好,因为我想制作一个小游戏。我学习了 Java 基础知识,并决定尝试解决游戏开发问题。我的 JFrame 和一切都很好,从技术上讲我没有错误,但我的小矩形家伙不会在屏
我在制作台球游戏时遇到问题,当我模拟击球时,我需要球使用react,程序是这样工作的,您单击击球的方向和力量,然后单击开始, go按钮位于创建标签的GUI类中,该按钮调用我的主类中的一个方法来接收参数
我以前在 2d 项目中使用过类似的东西来移动并且它总是有效。我现在正在使用它,它为我提供了一些输出的正确角度和错误的角度。我认为我的触发器中有一些错误,我弄错了。不过,我已经检查了大约一百万次。 Pr
我的 OpenGL 应用程序有问题,您可以在 this gif 中清楚地看到.基本上我想朝光标指向的方向移动,但这并没有发生,而是“前进”方向保持不变。 例如,如果我转身 180° 并按“w”向前走,
因此,我再次开始使用 C++ 编程并尝试使用 OpenGL。目前我有一个基本的应用程序,我只想用键移动相机。我读了很多关于这个的文章,但我仍然对运动有问题,我猜是因为即使只有一点点,但它们与我的程序不
我在 Android OpenGL 中有一个 Sprite 。这个 Sprite (一只小甲虫)总是向前移动,我使用: sprite.setPosition(posX, posY); 现在我有一个旋转
我是一名优秀的程序员,十分优秀!