简体   繁体   English

Box2D中的加速度计

[英]Accelerometer in Box2D

I am new in Box2D.... 我是Box2D的新手。

I have ball image in CCSprite. 我在CCSprite中有球图像。 I want to move ball in whole screen using accelerometer... tell me How to use accelerometer in box2d?? 我想使用加速度计在整个屏幕上移动球...告诉我如何在box2d中使用加速度计?

Thanks...in advance 提前致谢

The standard cocos2d-box2d template file moves boxes using the accelerometer by applying gravity relative to the accelerometer value. 标准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 );
  }

You need to be more specific on what you want the ball to do dependent on how you move the phone. 您需要更具体地说明您希望球做什么,具体取决于移动电话的方式。 Your question is difficult to answer in its current form. 您的问题很难以当前形式回答。

Get the accelerometer measurements and say Force = coefficient*measurements. 获取加速度计的测量值,然后说“力=系数*测量值”。 The apply this force to your b2Body 向您的b2Body施加力量

Let your Ball Sprite having tag is 1. 让您的Ball Sprite的标签为1。

Replace this code with your Accelerometer delegate, 将此代码替换为您的加速度计代表,

I test it on device, its working. 我在设备上对其进行了测试,可以正常工作。

and your ball will move with accelerometer. 球就会随着加速度计移动。

-(void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
    #define kFilterFactor 0.75
    accelerometer.updateInterval = 1.0f/60.0f;
    static UIAccelerationValue rollingX = 0, rollingY = 0;
    for (b2Body *b = world->GetBodyList(); b; b = b->GetNext()) 
    {
        if (b->GetUserData() != NULL) 
        {
            CCSprite *sprite = (CCSprite*)b->GetUserData();
            if (sprite.tag == 1) {

                rollingX = (acceleration.x * kFilterFactor) + (rollingX * 0.25);
                rollingY = (acceleration.y * kFilterFactor) + (rollingY * 0.25);
                float accelX = rollingX;
                float accelY = rollingY;


                CGPoint moveNewPosition = sprite.position;
                if (accelX > 0.1) {
                    moveNewPosition.y += 2;
                }   if (accelX < 0.1) {
                    moveNewPosition.y -= 2;
                }
                if (accelY > 0.1) {
                    moveNewPosition.x -= 2;
                }   if (accelY < -0.1) {
                    moveNewPosition.x += 2;
                }

                b->SetLinearVelocity(b2Vec2(2,2));

                sprite.position = ccp(moveNewPosition.x , moveNewPosition.y );
                sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
            }
        }
    }
}

I hope it'll work. 我希望它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM