Cocos2d iphone RevolutionJoint Box2d的问题

Cocos2d iphone RevolutionJoint Box2d的问题,cocos2d-iphone,physics,box2d,Cocos2d Iphone,Physics,Box2d,我对RevolutionJoint有问题。当我与litlle boxes和RevolutionJoint划一条线时,我注意到我的第一个盒子有一种奇怪的行为。它与其他盒子分开 你可以在这里看到: 你可以编译它,你会看到我在说什么 HelloWorldScene.h // When you import this file, you import all the cocos2d classes #import "cocos2d.h" #import "Box2D.h" #import "GLES-

我对RevolutionJoint有问题。当我与litlle boxes和RevolutionJoint划一条线时,我注意到我的第一个盒子有一种奇怪的行为。它与其他盒子分开

你可以在这里看到:

你可以编译它,你会看到我在说什么

HelloWorldScene.h

// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"

// HelloWorld Layer
@interface HelloWorld : CCLayer
{
    b2World* world;
    GLESDebugDraw *m_debugDraw;
}

// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
-(void) Test;

@end
HelloWorldScene.mm

// Import the interfaces
#import "HelloWorldScene.h"

//Pixel to metres ratio. Box2D uses metres as the unit for measurement.
//This ratio defines how many pixels correspond to 1 Box2D "metre"
//Box2D is optimized for objects of 1x1 metre therefore it makes sense
//to define the ratio so that your most common object type is 1x1 metre.
#define PTM_RATIO 32

// enums that will be used as tags
enum {
    kTagTileMap = 1,
    kTagBatchNode = 1,
    kTagAnimation1 = 1,
};

// HelloWorld implementation
@implementation HelloWorld

+(id) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorld *layer = [HelloWorld node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}   

// initialize your instance here
-(id) init
{
    if( (self=[super init])) {

        // enable touches
        self.isTouchEnabled = YES;

        // enable accelerometer
        self.isAccelerometerEnabled = YES;

        CGSize screenSize = [CCDirector sharedDirector].winSize;
        CCLOG(@"Screen width %0.2f screen height %0.2f",screenSize.width,screenSize.height);

        // Define the gravity vector.
        b2Vec2 gravity;
        gravity.Set(0.0f, -10.0f);

        // Do we want to let bodies sleep?
        // This will speed up the physics simulation
        bool doSleep = true;

        // Construct a world object, which will hold and simulate the rigid bodies.
        world = new b2World(gravity, doSleep);

        world->SetContinuousPhysics(true);

        // Debug Draw functions
        m_debugDraw = new GLESDebugDraw( PTM_RATIO );
        world->SetDebugDraw(m_debugDraw);

        uint32 flags = 0;
        flags += b2DebugDraw::e_shapeBit;
        flags += b2DebugDraw::e_jointBit;
//      flags += b2DebugDraw::e_aabbBit;
//      flags += b2DebugDraw::e_pairBit;
//      flags += b2DebugDraw::e_centerOfMassBit;
        m_debugDraw->SetFlags(flags);       

        // Define the ground body.
        b2BodyDef groundBodyDef;
        groundBodyDef.position.Set(0, 0); // bottom-left corner

        // Call the body factory which allocates memory for the ground body
        // from a pool and creates the ground box shape (also from a pool).
        // The body is also added to the world.
        b2Body* groundBody = world->CreateBody(&groundBodyDef);

        // Define the ground box shape.
        b2PolygonShape groundBox;       

        // bottom
        groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
        groundBody->CreateFixture(&groundBox,0);

        // top
        groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
        groundBody->CreateFixture(&groundBox,0);

        // left
        groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
        groundBody->CreateFixture(&groundBox,0);

        // right
        groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
        groundBody->CreateFixture(&groundBox,0);

        //Set up sprite

        [self Test];

        [self schedule: @selector(tick:)];
    }
    return self;
}   

- (void) Test {

    // Circle
    b2Body *circle1;
    b2BodyDef bd1;
    bd1.position.Set(45.0f/PTM_RATIO, 180.0f/PTM_RATIO);
    bd1.type = b2_kinematicBody;
    bd1.fixedRotation = false;
    bd1.allowSleep = false;
    circle1 = world->CreateBody(&bd1);

    b2CircleShape shapecircle1;
    shapecircle1.m_radius = 0.5f;

    b2FixtureDef fdcircle1;
    fdcircle1.shape = &shapecircle1;
    fdcircle1.density = 2.0f;
    fdcircle1.friction = 2.0f;

    circle1->CreateFixture(&fdcircle1);

    // Boxes

    b2PolygonShape shape;
    shape.SetAsBox(6.0f/PTM_RATIO, 0.125f);

    b2FixtureDef fd;
    fd.shape = &shape;
    fd.density = 20.0f;
    fd.friction = 0.2f;

    b2RevoluteJointDef jd;
    jd.collideConnected = false;

    const float32 y = 9.0f;
    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position.Set(15.0f/PTM_RATIO, y);
    b2Body* prevBody = world->CreateBody(&bd);
    prevBody->CreateFixture(&fd);
    b2Vec2 anchor(float32(0), y);

    for (int32 i = 1; i < 8; ++i)
    {
        b2BodyDef bd;
        bd.type = b2_dynamicBody;
        bd.position.Set((15.0f + (i*10))/PTM_RATIO, y);
        b2Body* body = world->CreateBody(&bd);
        body->CreateFixture(&fd);
        b2Vec2 anchor(float32(i*10)/PTM_RATIO, y);

        jd.Initialize(prevBody, body, anchor);
        world->CreateJoint(&jd);

        prevBody = body;
    }
}

-(void) draw
{
    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states:  GL_VERTEX_ARRAY,
    // Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    world->DrawDebugData();

    // restore default GL states
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

}   

-(void) tick: (ccTime) dt
{
    //It is recommended that a fixed time step is used with Box2D for stability
    //of the simulation, however, we are using a variable time step here.
    //You need to make an informed choice, the following URL is useful
    //http://gafferongames.com/game-physics/fix-your-timestep/

    int32 velocityIterations = 8;
    int32 positionIterations = 1;

    // Instruct the world to perform a single step of simulation. It is
    // generally best to keep the time step and iterations fixed.
    world->Step(dt, velocityIterations, positionIterations);

    //Iterate over the bodies in the physics world
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {
        if (b->GetUserData() != NULL) {
            //Synchronize the AtlasSprites position and rotation with the corresponding body
            CCSprite *myActor = (CCSprite*)b->GetUserData();
            myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
            myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        }
    }
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    delete world;
    world = NULL;

    delete m_debugDraw;

    // don't forget to call "super dealloc"
    [super dealloc];
}
@end
//导入接口
#导入“HelloWorldScene.h”
//像素与米的比率。Box2D使用米作为测量单位。
//此比率定义了1×2D“米”对应的像素数
//Box2D针对1x1米的对象进行了优化,因此很有意义
//定义比率,使最常见的对象类型为1x1米。
#定义PTM_比率32
//将用作标记的枚举
枚举{
kTagTileMap=1,
kTagBatchNode=1,
kTagAnimation1=1,
};
//HelloWorld实现
@HelloWorld的实现
+(id)现场
{
//“场景”是一个自动释放对象。
CCScene*scene=[CCScene节点];
//“层”是一个自动释放对象。
HelloWorld*层=[HelloWorld节点];
//将层作为子层添加到场景中
[场景添加子对象:层];
//返回现场
返回场景;
}   
//在这里初始化您的实例
-(id)init
{
if((self=[super init])){
//启用触控
self.isTouchEnabled=是;
//启用加速计
self.isAccelerometerEnabled=是;
CGSize screenSize=[CCDirector sharedDirector].winSize;
CCLOG(@“屏幕宽度%0.2f屏幕高度%0.2f”,屏幕大小。宽度,屏幕大小。高度);
//定义重力向量。
b2Vec2重力;
重力设置(0.0f,-10.0f);
//我们想让身体睡觉吗?
//这将加快物理模拟的速度
bool-doSleep=true;
//构造一个世界对象,该对象将容纳并模拟刚体。
世界=新世界(重力,doSleep);
世界->设置连续物理(真实);
//调试绘图函数
m_debugDraw=新的GLESDebugDraw(PTM_比率);
world->SetDebugDraw(m_debugDraw);
uint32标志=0;
flags+=b2DebugDraw::e_shapeBit;
flags+=b2DebugDraw::e_jointBit;
//flags+=b2DebugDraw::e_aabbBit;
//flags+=b2DebugDraw::e_pairBit;
//flags+=b2DebugDraw::e_CenterOfMasbit;
m_debugDraw->SetFlags(标志);
//定义地面主体。
b2BodyDef地面BodyDef;
groundBodyDef.position.Set(0,0);//左下角
//调用为地面车身分配内存的车身工厂
//从池中创建地面长方体形状(也从池中创建)。
//身体也被添加到世界中。
b2Body*groundBody=world->CreateBody(&groundBodyDef);
//定义接地盒形状。
b2PolygonShape接地盒;
//底部
接地盒设置边缘(b2Vec2(0,0),b2Vec2(屏幕大小、宽度/PTM_比率,0));
groundBody->CreateFixture(&groundBox,0);
//顶
groundBox.SetAsEdge(b2Vec2(0,屏幕大小。高度/PTM_比率),b2Vec2(屏幕大小。宽度/PTM_比率,屏幕大小。高度/PTM_比率));
groundBody->CreateFixture(&groundBox,0);
//左
接地盒。设置边缘(b2Vec2(0,屏幕尺寸。高度/PTM_比率),b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
//对
groundBox.SetAsEdge(b2Vec2(屏幕大小、宽度/PTM_比率、屏幕大小、高度/PTM_比率)、b2Vec2(屏幕大小、宽度/PTM_比率,0));
groundBody->CreateFixture(&groundBox,0);
//设置精灵
[自我测试];
[自行安排:@选择器(勾选:)];
}
回归自我;
}   
-(无效)试验{
//圈
b2Body*圆圈1;
B21;
bd1.位置设置(45.0f/PTM_比率,180.0f/PTM_比率);
bd1.type=b2_运动学体;
bd1.fixedRotation=false;
bd1.allowSleep=false;
circle1=世界->CreateBody(&bd1);
b2CircleShape shapecircle1;
形状1.m_半径=0.5f;
b2FixtureDef fdcircle1;
fdcircle1.shape=&shapecircle1;
fdcircle1.0密度=2.0华氏度;
fdcircle1.摩擦力=2.0f;
circle1->CreateFixture(&fdcircle1);
//盒子
多边形形状;
形状设置框(6.0f/PTM_比,0.125f);
b2FixtureDef-fd;
fd.shape=&shape;
fd.密度=20.0f;
摩擦系数=0.2f;
B2RevolutionJointdef jd;
jd.com=false;
常数32 y=9.0f;
B2BD;
bd.type=b2_动态车身;
bd.position.Set(15.0f/PTM_比率,y);
b2Body*prevBody=world->CreateBody(&bd);
prevBody->CreateFixture(&fd);
b2Vec2锚(浮动32(0),y);
对于(int32 i=1;i<8;++i)
{
B2BD;
bd.type=b2_动态车身;
bd.position.Set((15.0f+(i*10))/PTM_比值,y);
b2Body*body=world->CreateBody(&bd);
主体->创建夹具(&fd);
b2Vec2锚(浮动32(i*10)/PTM_比,y);
jd.初始化(前主体、主体、锚);
world->CreateJoint(&jd);
身体=身体;
}
}
-(作废)提款
{
//默认GL状态:GL_纹理_2D、GL_顶点_数组、GL_颜色_数组、GL_纹理_坐标_数组
//所需状态:GL_顶点_数组,
//不需要的状态:GL_纹理_2D、GL_颜色_数组、GL_纹理_坐标_数组
glDisable(GL_纹理_2D);
glDisableClientState(GL_颜色_数组);
glDisableClientState(GL_纹理_坐标_数组);
world->DrawDebugData();
//恢复默认总账状态
glEnable(GL_纹理_2D);
glEnableClientState(GL_颜色_阵列);
glEnableClientState(GL_纹理_坐标_阵列);
}   
-(无效)勾选:(ccTime)dt
{
//建议将固定时间步长与Box2D配合使用,以提高稳定性
//然而,对于模拟,我们在这里使用可变时间步长。
//您需要做出明智的选择,下面的URL很有用
//http://gafferongames.com/game-physics/fix-your-timestep/
int32速度迭代=8;
int32=1;
//指示世界执行si
b2RevoluteJointDef jointDef;
jointDef.Initialize(body1, body2, myBody1->GetWorldCenter());
jointDef.lowerAngle = -0.5f * b2_pi; // -90 degrees
jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
jointDef.enableLimit = true;
jointDef.maxMotorTorque = 10.0f;
jointDef.motorSpeed = 0.0f;
jointDef.enableMotor = true;