AndEngine,使用加速计和加速器平稳地移动 Sprite物理世界
使用 AndEngine,我尝试使用加速计移动精灵(球),该精灵应该会反弹墙壁并撞击其他障碍物。
到目前为止我发现了以下两种方法。
public void onAccelerometerChanged(final AccelerometerData myAccelerometerData) {
mySprite.setPosition(mySprite.getX() + myAccelerometerData.getX(), mySprite.getY() + myAccelerometerData.getY());
}
这工作得非常顺利,但仅限于非物理世界。 (即不会顺利地反弹墙壁和其他障碍物)。
现在使用 AndEngine 提供的示例中的这种方法,球具有良好的物理特性,但移动并不像我希望的那样平滑。
@Override
public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
我还尝试将 pAccelerometerData 与某些值(例如 2 和 5)相乘,是的,它看起来响应更快,但也使球移动得更快并且难以打球。
为了明确我的问题,我希望我的游戏能够像 Android 上的 Labyrinth 或 Rebound 游戏中的球一样流畅。其中的球对加速度计的响应更灵敏。例如,如果您想在迷宫中突然停止移动的球,您可以,但使用 andEngine 示例中的以下代码,它会保持移动一段时间,就好像它在无摩擦的世界中移动一样。
无论如何,我可以让它像迷宫游戏那样反应更快、更流畅吗? https://market.android.com/details?id =se.illusionlabs.labyrinth.lite&hl=en
Using AndEngine, Im trying to move a sprite (ball) with the accelerometer which is supposed to bounce of walls and hit other obstacles.
so far I've found the following two methods.
public void onAccelerometerChanged(final AccelerometerData myAccelerometerData) {
mySprite.setPosition(mySprite.getX() + myAccelerometerData.getX(), mySprite.getY() + myAccelerometerData.getY());
}
This works very smoothly but in a non physics world only. (i.e will not bounce of walls and other obstacles smoothly).
And now using this method which is in the examples provided by AndEngine, the ball has good physics but does not move as smoothly as i would like to.
@Override
public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
I've also tried multiplying pAccelerometerData with certain values such as 2 and 5, yes it appears more responsive but also makes the ball move a lot faster and difficult to play.
To make my question clear, I would like my game to be as smooth as the balls in Labyrinth or Rebound game are for Android. The balls in them are are more responsive to accelerometer. For instance if you want to stop a moving ball suddenly in Labyrinth, you can but with the following code in andEngine example, it keeps moving for a while as if it is moving in a frictionless world.
Is there anyway I can make it more responsive and smooth like that of the Labyrinth game?
https://market.android.com/details?id=se.illusionlabs.labyrinth.lite&hl=en
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以(正如您尝试的那样)乘以 pAccelerometerData,并向球添加阻尼项。因此球会反应更灵敏,停止得更快,并保持合理的速度。
阻尼是物体的属性。所以它会是这样的
You can (as you tried) multiply the pAccelerometerData, and add a damping term to the ball. So the ball will be more responsive, will stop faster, and will keep a reasonable speed.
The damping is a property of bodys. So it would be something like