views:

13

answers:

1

I want to apply an rotation to an uiimageview for shapes like "U" and "T" using compund objects and box2D, because "U" shapes are concave polygons so i can't do it using b2polygondef only.

Here it is the method to make an "u" shape box when the user click on the screen

// 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;   
}

And here i have the method that update the UIImageView position and angle

-(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;
}   
}

The problem i guess it is the new angle value for the shape: in the run world method

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

or the center of the figure: in the touches began method:

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

the shapes rotates and all but they are overlapping eachother in sometimes or collide before make visual contact.

What i want to do is that "U" shape but without overlaping each other. Can someone please help me with this issue?

A: 

Thw angle set was wrong :P it must be positive instead of: CGAffineTransform transform = CGAffineTransformMakeRotation(- ballBody->GetAngle()); must be: CGAffineTransform transform = CGAffineTransformMakeRotation(ballBody->GetAngle());

:D

Fracedo