Android的:实现用球加速度计光辊 [英] Android: Achieving smooth roll of ball using accelerometer

查看:207
本文介绍了Android的:实现用球加速度计光辊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制定了Android的一个迷宫游戏,你通过倾斜手机控制球。

I have developed a maze game for Android where you control the ball by tilting the phone.

于是我就用加速度计和集成x和y的加速度值,然后移动球在方向迈出的一步。

So I use the accelerometer and integrate the x and y accelerometer values and then move the ball a step in that direction.

我有一个问题,虽然,我不能达到非常流畅的滚动。当球速度加快这是显而易见的,它跳跃的大不连续的步骤。我见过的其他应用程序像这样的地方球滚快,但流畅。

I have a problem though, I cannot achieve a very smooth roll. When the ball picks up speed it is to obvious that it jumps in big discrete steps. I have seen other apps like this where the ball rolls fast but smoothly.

所以我可能改变我的策略,使用某种时间,而不是解决方案。现在的速度越快越大一步,我移动。相反,也许我应该有一个移动球1个像素每毫秒,如果速度较高或仅每10毫秒如果速度过低或类似的规定计时器。
或者人们如何实现平滑的滚动?

So I might have to change my strategy, use some sort of time solution instead. Now the faster the speed the bigger the step I move. Instead maybe I should have a timer that moves the ball 1 pixel every ms if speed is high or only every 10th ms if the speed is low or something along those lines. Or how do people achieve a smoother roll?

另外:你会使用OpenGL此

Also: Would you use OpenGL for this?

推荐答案

你真正在这里做的集成耦合差分方程。别担心,如果你还没有采取了足够的积分或物理知道这意味着什么。

What you're really doing here is integrating coupled differential equations. Don't worry if you haven't taken enough calculus or physics to know what that means.

谁整合再加微分方程为生的人已经发展很多算法如此高效地完成。

People who integrate coupled differential equations for a living have evolved many algorithms to do so efficiently.

您真的在这里四个方程:加速x和y方向和速度在X和Y方向:

You really have four equations here: acceleration in x- and y-directions and velocity in x- and y-directions:

dvx/dt = ax
dvy/dt = ax
dsx/dt = vx
dxy/dt = vy

(SX,SY)得到在给定时间的球的位置。你所需要的(SX,SY)和(VX,VY)初始条件。

(sx, sy) give the position of the ball at a given time. You'll need initial conditions for (sx, sy) and (vx, vy).

这听起来像您选择集成微分方程最​​简单的方法:欧拉显式积分。你在从值在开始步骤加上的变化倍的速度的结尾计算值时间步:

It sounds like you've chosen the simplest way to integration ODEs: Euler explicit integration. You calculate the values at the end of a step from the values at the beginning plus the rate of change times the time step:

(vx, vy)_1 = (vx, vy)_0 + (ax, ay)_0 * dt
(sx, sy)_1 = (sx, sy)_0 + (vx, vy)_0 * dt

这很容易编程,但它往往从稳定性问题遭受一定的条件下,如果你的时间步长太大。

It's easy to program, but it tends to suffer from stability problems under certain conditions if your time step is too large.

您可以缩小你的时间步长,这将迫使你进行计算更多次,或者切换到其他的整合方案。搜索隐式积分,龙格 - 库塔等。

You can shrink your time step, which will force you to perform the calculations many more times, or switch to another integration scheme. Search for implicit integration, Runge-Kutta, etc.

集成和渲染是分开的问题。

Integration and rendering are separate problems.

这篇关于Android的:实现用球加速度计光辊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆