I'm making an pool game with cocos2dx.
First, i setup the edgeBox with this parameters PhysicsMaterial(1.0f, 1.0f, 0.8f)
And then these 2 balls PhysicsMaterial(1.0f, 1.0f, 0.5f)
On the update function, i want slow down balls time by time without gravity (like making ground friction) by adding
physicsBody->setLinearDamping(0.3);
On the update function, i set the minimum velocity, if the velocity of each ball reaches lower than 15, reset velocity to 0,0
auto MV = 15;
auto v1 = player1->getPhysicsBody()->getVelocity();
auto v2 = player2->getPhysicsBody()->getVelocity();
if (v1.x > MV || v1.x < -MV ||
v1.y > MV || v1.y < -MV) {
} else if(v1 != Vec2(0,0)) {
player1->getPhysicsBody()->setVelocity(Vec2(0,0));
CCLOG("sx 1 : %f %f",v1.x,v1.y);
}
if (v2.x > MV || v2.x < -MV ||
v2.y > MV || v2.y < -MV) {
} else if(v2 != Vec2(0,0)) {
player2->getPhysicsBody()->setVelocity(Vec2(0,0));
CCLOG("sx 2 : %f %f",v2.x,v2.y);
}
Everything works fine except when the balls stand next to the wall or each other. I see the small blue glue to these objects, this is when the contact has been made.
And in these situation, i can't set the velocity to 0,0.
I think there is some kind of force constantly changing the velocity. You can see the image below to see the blue glue and keep setting velocity = 0.0 like forever.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…