So i am trying to create a game sort of like angry bird, which requires player to swipe the ball and shoot based on the angle and distance. I found a tutorial based on Flash Box2D http://www.emanueleferonato.com/2011/10/10/develop-a-flash-game-like-angry-birds-using-box2d/
But i have hard time trying to figure out the conversion.
so in my init, i have create my body object.
CCSprite *tail = [CCSprite spriteWithFile:@"Ball.jpg"];
[self addChild:tail z:1];
b2BodyDef tailBodyDef;
tailBodyDef.type = b2_dynamicBody;
tailBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
tailBodyDef.userData = tail;
tailBody = world->CreateBody(&tailBodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
b2FixtureDef tailShapeDef;
tailShapeDef.shape = &circle;
tailShapeDef.density = 1.0f;
tailShapeDef.friction = 0.2f;
tailShapeDef.restitution = 0.8f;
tailBody->CreateFixture(&tailShapeDef);
[self schedule: @selector(tick:)];
I have these code on my touch dispatcher.
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint != nil) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
if (locationWorld.x < tailBody->GetWorldCenter().x + 50.0/PTM_RATIO)
{
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = tailBody;
md.target = locationWorld;
md.maxForce = 2000;
mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint == nil) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float distanceX = location.x-170;
float distanceY =location.y-270;
if (distanceX*distanceX+distanceY*distanceY>10000) {
CGFloat angle = atan2f(distanceY,distanceX);
location.x=170+100*cosf(angle);
location.y=270+100*sinf(angle);
}
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
mouseJoint->SetTarget(locationWorld);
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint != nil) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
float distanceX = location.x-170;
float distanceY =location.y-270;
float distance = sqrtf(distanceX*distanceX+distanceY*distanceY);
CGFloat angle = atan2f(distanceY,distanceX);
// Apply an impulse to the body, using the angle
tailBody->ApplyLinearImpulse(b2Vec2(-distance*cosf(angle)/4,-distance*sinf(angle)/4), tailBody->GetPosition());
if (mouseJoint) {
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}
My problem is i couldn't get it to shoot, i was able to drag the body object around, but when i release my finger. the object stays there instead of shoot itself out.
It'll be great if some of you could help me with this!