1

I am developing a game for iOS using cocos-2dx to make the performance better, i have used chipmunk body added successfully but on run time when my sprite is moving on the screen the body stays on its original position let me paste the code.

 void TestGamePlay::onTouchesMoved(const std::vector<cocos2d::Touch *>&touches,cocos2d::Event *event)     
        {            

    auto touch = static_cast<Touch*>(touches[0]);
    auto touchLocation = touch->getLocation();
    cpVect v = cpv((touchLocation.x - littleFishBody->p.x)  ,
                    (touchLocation.y - littleFishBody->p.y)  );    
    v = cpv(250, 250);

    cpBodyApplyImpulse(littleFishBody, v, cpv(0, 0));

    cpBodyApplyForce(littleFishBody, v, cpv(0,0));
}

Here i have created the body.

`

float spriteWidth = sprite->getContentSize().width;
float spriteHeight = sprite->getContentSize().height;

spriteWidth = spriteWidth - factor;
spriteHeight = spriteHeight - factor;

int num = 4;

cpVect verts[] = { cpv(-13, -13), cpv(-13, 13), cpv(13, 13), cpv(13, -13) };

cpBody *body = cpBodyNew(1.0f,
        cpMomentForPoly(1.0f, num, verts, cpv(0, 0)));

cpVect t;
t.x = point.x;
t.y = point.y;
cpBodySetPos(body, t);
cpSpaceAddBody(_space, body);


cpShape *shape = cpPolyShapeNew(body, num, verts, cpv(0, 0));
shape->data = sprite;
cpSpaceAddShape(_space, shape);
littleFishBody = body;



this->addChild(sprite, 3);
sprite->setCPBody(body);

` Kindly let me know where i am going wrong in it.

0 Answers0