我想使用复合对象和box2D对“U”和“T”等形状的uiimageview应用旋转,因为“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;
}
}
我猜问题是形状的新角度值:在运行世界方法中
CGAffineTransform transform = CGAffineTransformMakeRotation(- ballBody->GetAngle());
或图形的中心:在touches开始方法中:
boxBodyDef.position.Set(S2W(touchPos.x),
S2W(touchPos.y));
形状旋转,但它们有时会相互重叠或在视觉接触之前发生碰撞。
我想要做的是“U”形但不相互重叠。有人可以帮我解决这个问题吗?