http://www.cocos2d-iphone.org/forum/topic/14556
hi, from the above link, I picked clues.
i am trying to flick a ball like that in “paper toss” game.
here is the major part of my code:
init()
{
..................
//box2d
b2Vec2 gravity;
gravity.Set(0.0f, -9.81f);
world=new b2World(gravity);
world->SetAllowSleeping(false);
world->SetContinuousPhysics(true);
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(winSize.width/2/PTM_RATIO,winSize.height/3/PTM_RATIO);
bodyDef.bullet=true;
bodyDef.userData = getChildByTag(kTagBall);//sprite added to body
body = world->CreateBody(&bodyDef);
b2CircleShape circle; // Define spehere shape for our dynamic body.
circle.m_radius=.9f;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution=0.58f;// bouncing effect
body->CreateFixture(&fixtureDef);
.................................
}
void MyScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)(*it);
m_tBeginPos = touch->locationInView(touch->view());
m_tBeginPos = CCDirector::sharedDirector()->convertToGL(m_tBeginPos);
if (CCRect::CCRectContainsPoint(getChildByTag(kTagBall)->boundingBox(), m_tBeginPos))
{
touchedBall=true;
}
else
{
touchedBall=false;
}
}
void MyScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)(*it);
m_tEndPos = touch->locationInView( touch->view() );
m_tEndPos = CCDirector::sharedDirector()->convertToGL( m_tEndPos );
int diff=1;
CCLOG("touched ball= %s",touchedBall ? "true":"false");
if(touchedBall)
{
CCPoint vector = ccpSub(m_tBeginPos,m_tEndPos);
b2Vec2 imp=b2Vec2(vector.x/diff,vector.y/diff);
body->ApplyLinearImpulse(imp, body->GetPosition());
}
}
on clicking and dragging/flicking, my ball is not moving at all.
what am i doing wrong ?
help please,
thnx