复合形状的图像视图中的box2d角度旋转

时间:2010-07-02 14:23:52

标签: ipad uiimageview shapes box2d cgaffinetransform

我想使用compund对象和box2D将旋转应用于uiimageview以获取“U”和“T”等形状,因为“U”形状是凹多边形,所以我不能仅使用b2polygondef来完成它。

这是在用户点击屏幕时制作“u”形状框的方法

// map from the world to the screen and screen to the world
#define S2W(s) ((s)/50)
#define W2S(w) ((w)*50)

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (currBox>59) {
    return;
}

UITouch *touch=[touches anyObject];
CGPoint touchPos=[touch locationInView:self.view];

UIImageView *boxTestViewToAdd = [[UIImageView alloc] init];

[boxTestViewToAdd setImage:[UIImage imageNamed:@"BoxTest2.png"]];
[boxTestViewToAdd setFrame:CGRectMake(touchPos.x, touchPos.y, 180, 120)];
[self.view addSubview:boxTestViewToAdd];

b2Body *boxTestBodyToAdd;
b2BodyDef boxBodyDef;
boxBodyDef.position.Set(S2W(touchPos.x), 
                        S2W(touchPos.y));
boxBodyDef.linearDamping=0.5;
boxBodyDef.angularDamping=0.5;
boxTestBodyToAdd=myWorld->CreateBody(&boxBodyDef);


b2PolygonDef boxDef2;
b2Vec2 vecBox2;
vecBox2 = b2Vec2(0,S2W((boxTestViewToAdd.bounds.size.height/2 - boxTestViewToAdd.bounds.size.height/4)));
boxDef2.SetAsBox(S2W(boxTestViewToAdd.bounds.size.width/6), S2W(boxTestViewToAdd.bounds.size.height/4),vecBox2, 0);
boxDef2.density= 0.1f;
boxDef2.friction= 0.3f;
boxDef2.restitution = 0.2f;
boxTestBodyToAdd->CreateShape(&boxDef2);

b2PolygonDef boxDef3;
b2Vec2 vecBox3;
vecBox3 = b2Vec2(-(S2W(boxTestViewToAdd.bounds.size.width/3)),S2W((boxTestViewToAdd.bounds.size.height/2 - boxTestViewToAdd.bounds.size.height/4)));
boxDef3.SetAsBox(S2W(boxTestViewToAdd.bounds.size.width/6), S2W(boxTestViewToAdd.bounds.size.height/4),vecBox3, 0);
boxDef3.density= 0.1f;
boxDef3.friction= 0.3f;
boxDef3.restitution = 0.2f;
boxTestBodyToAdd->CreateShape(&boxDef3);

b2PolygonDef boxDef4;
b2Vec2 vecBox4;
vecBox4 = b2Vec2(S2W(boxTestViewToAdd.bounds.size.width/3),S2W((boxTestViewToAdd.bounds.size.height/2 - boxTestViewToAdd.bounds.size.height/4)));
boxDef4.SetAsBox(S2W(boxTestViewToAdd.bounds.size.width/6), S2W(boxTestViewToAdd.bounds.size.height/4),vecBox4, 0);
boxDef4.density= 0.1f;
boxDef4.friction= 0.3f;
boxDef4.restitution = 0.2f;
boxTestBodyToAdd->CreateShape(&boxDef4);

b2PolygonDef boxDef5;
b2Vec2 vecBox5;
vecBox5 = b2Vec2(-(S2W(boxTestViewToAdd.bounds.size.width/3)),-(S2W(boxTestViewToAdd.bounds.size.height/4)));
boxDef5.SetAsBox(S2W(boxTestViewToAdd.bounds.size.width/6), S2W(boxTestViewToAdd.bounds.size.height/4),vecBox5, 0);
boxDef5.density= 0.1f;
boxDef5.friction= 0.3f;
boxDef5.restitution = 0.2f;
boxTestBodyToAdd->CreateShape(&boxDef5);

b2PolygonDef boxDef6;
b2Vec2 vecBox6;
vecBox6 = b2Vec2(S2W(boxTestViewToAdd.bounds.size.width/3),-(S2W(boxTestViewToAdd.bounds.size.height/4)));
boxDef6.SetAsBox(S2W(boxTestViewToAdd.bounds.size.width/6), S2W(boxTestViewToAdd.bounds.size.height/4),vecBox6, 0);
boxDef6.density= 0.1f;
boxDef6.friction= 0.3f;
boxDef6.restitution = 0.2f;
boxTestBodyToAdd->CreateShape(&boxDef6);


boxTestBodyToAdd->SetMassFromShapes();

ballBoxBody[currBox]=boxTestBodyToAdd;
ballBoxView[currBox]=boxTestViewToAdd;

currBox += 1;   
}

这里我有更新UIImageView位置和角度的方法

-(void) runWorld {
// step the world forward
myWorld->Step(1.0f/30.0f, 10, 10);

int i=0;

while (i < currBox) {
    b2Body *ballBody;
    ballBody = ballBoxBody[i];

    UIImageView *ballView;
    ballView = ballBoxView[i];

    b2Vec2 position = ballBody->GetPosition();
    //ballView.center=CGPointMake(W2S(position.x), W2S(position.y));
    ballView.center=CGPointMake(W2S(position.x),W2S(position.y));

    CGAffineTransform transform = CGAffineTransformMakeRotation(- ballBody->GetAngle());

    ballView.transform = transform;

    i+=1;
}   
}

问题我猜它是形状的新角度值: 在run world方法中

CGAffineTransform transform = CGAffineTransformMakeRotation(- ballBody->GetAngle());

或图的中心: 在接触开始的方法:

boxBodyDef.position.Set(S2W(touchPos.x), 
                    S2W(touchPos.y));

形状旋转,除了它们之外有时相互重叠或碰撞,然后进行视觉接触。

我想做的是“U”形,但没有相互重叠。 有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你设定的角度是错误的 它必须是积极的而不是:

CGAffineTransform transform = CGAffineTransformMakeRotation(- ballBody->GetAngle());

必须是:

CGAffineTransform transform = CGAffineTransformMakeRotation(ballBody->GetAngle());