Agshin Huseynov
Agshin Huseynov

Reputation: 170

Cocos2dx - Zero restitution does not stop bouncing

Here is my code below.

Adding a sprite falling with gravity;

SampleSprite* sp = SampleSprite::create();
sp->setPosition(screenWidth/ 2, screenHeight/2);
this->layer->addChild(sp);

sp->setContentSize(Size(unitW, unitH));
auto ps = cocos2d::PhysicsBody::createBox(sp->getContentSize(), cocos2d::PhysicsMaterial(1.f, 0.f, 1.f));
ps->setDynamic(true);
ps->setRotationEnable(false);
ps->setMoment(0);
sp->addComponent(ps);

Adding floor that Sprite above will collide with

cocos2d::Sprite* floor = cocos2d::Sprite::create("res/floor.png");
floor->setContentSize(cocos2d::Size(screenWidth - (2*padding), padding));
floor->setPosition(origin + cocos2d::Vec2(padding, 0));
floor->setAnchorPoint(cocos2d::Vec2(0,0));
auto physics = cocos2d::PhysicsBody::createEdgeBox(cocos2d::Size(screenWidth - (2*padding), padding), cocos2d::PhysicsMaterial(1.f, 0.f, 1.f));
physics->setDynamic(true);
physics->setGravityEnable(false);
floor->addComponent(physics);

this->layer->addChild(floor);

Screenshot of test game. Bouncing sprite on floor

Upvotes: 0

Views: 185

Answers (1)

Sunil Singh
Sunil Singh

Reputation: 1

Here,I got the code to fix this :-

define FLT_EPSILON 1.19209290e-07F   
float _filteredUpdateDelta;

scene->getPhysicsWorld()->setAutoStep(false);  

void GameContrler::updatePhysics(float delta)
{
const int phyiscsSubSteps = 3;
float dt = delta / static_cast<float>(phyiscsSubSteps);
_filteredUpdateDelta = dt > __FLT_EPSILON__ ? 0.15 * dt + 0.85 *                                          _filteredUpdateDelta : 0.0;
for (int i=phyiscsSubSteps; i>0; i--)  
{  
  sceneWorld->step(_filteredUpdateDelta);  
}

Upvotes: 0

Related Questions