Cocos2dx - Unable to set velocity = 0.0

无人久伴 提交于 2019-11-29 16:22:26

Firstly reset forces before setting velocity to zero: player2->getPhysicsBody()->resetForces();

Also gravity can be a cause that bodies continue to move. So you can set gravity to zero for whole physics world. For example:

auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setGravity(Vec2(0, 0));

or just for one particular body:

player2->getPhysicsBody()->setGravityEnable(false);

or you can customize velocity function:

#include "chipmunk.h"

cocos2d::PhysicsBody * pBody = player2->getPhysicsBody();
pBody->getCPBody()->velocity_func = customVelFunc;

where customVelFunc could be defined as:

void customVelFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
    cpBodyUpdateVelocity(body, cpvzero, damping, dt);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!