我是 Box2D 新手......
我在CCSprite中有球图像。我想使用加速度计在整个屏幕上移动球...... 告诉我 如何在box2d中使用加速度计?
谢谢...提前
最佳答案
标准的 cocos2d-box2d 模板文件通过应用相对于加速度计值的重力来使用加速度计移动盒子。
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
static float prevX=0, prevY=0;
//#define kFilterFactor 0.05f
#define kFilterFactor 1.0f // don't use filter. the code is here just as an example
float accelX = (float) acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = (float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
prevX = accelX;
prevY = accelY;
// accelerometer values are in "Portrait" mode. Change them to Landscape left
// multiply the gravity by 10
b2Vec2 gravity( -accelY * 10, accelX * 10);
world->SetGravity( gravity );
}
您需要根据您移动手机的方式更具体地说明您希望球做什么。以目前的形式很难回答您的问题。
关于iphone - Box2D 中的加速度计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5617773/