Hello. I'm new to Box2d, and i apologize about my english and c++ knowledge.
I modyfied a basic Confined.h test from TestBed, to demonstrate what i asking.
The problem is, when ball with friction=0, restitution=1, linear and angular damping=0 faced with a wall at a small angle of incidence and small speed, it must be perfectly elastic collision. Ball must bounce from wall, but it sticks to the wall, and slides.
Maybe someone knows, how make perfectly elastic collision, when at small angles and speeds ball bounce off the walls?
It is necessary to preserve the total momentum of the system after the collision.
Regards.
Code of modyfied Confined.h, modyfied and added strings marked with "//<-Added--" and "//<-Modyfied--".
Code:
#ifndef CONFINEDMOD_H
#define CONFINEDMOD_H
class ConfinedMod : public Test
{
public:
ConfinedMod()
{
{
b2BodyDef bd;
b2Body* ground = m_world->CreateBody(&bd);
b2PolygonShape shape;
shape.SetAsEdge(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f));
ground->CreateFixture(&shape, 0.0f); // Floor
shape.SetAsEdge(b2Vec2(-10.0f, 0.0f), b2Vec2(-10.0f, 20.0f));
ground->CreateFixture(&shape, 0.0f); // Left wall
shape.SetAsEdge(b2Vec2(10.0f, 0.0f), b2Vec2(10.0f, 20.0f));
ground->CreateFixture(&shape, 0.0f); // Right wall
shape.SetAsEdge(b2Vec2(-10.0f, 20.0f), b2Vec2(10.0f, 20.0f));
ground->CreateFixture(&shape, 0.0f); // Roof
}
m_world->SetGravity(b2Vec2(0.0f, 0.0f));
}
void CreateCircle()
{
float32 radius = 2.0f;
b2CircleShape shape;
shape.m_p.SetZero();
shape.m_radius = radius;
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 1.0f;
fd.friction = 0.0f;
fd.restitution = 1.0f; //<-Added--
b2Vec2 p(-7.0f, 3.0f); //<-Modyfied--
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position = p;
bd.linearDamping = 0.0f; //<-Added--
bd.angularDamping = 0.0f; //<-Added--
//bd.allowSleep = false;
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&fd);
b2Vec2* MyVec = new b2Vec2(11.5f, -1.1f); //<-Added--
body->ApplyLinearImpulse(*MyVec, body->GetWorldCenter()); //<-Added--
}
void Keyboard(unsigned char key)
{
switch (key)
{
case 'c':
CreateCircle();
break;
}
}
void Step(Settings* settings)
{
bool sleeping = true;
for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetType() != b2_dynamicBody)
{
continue;
}
if (b->IsAwake())
{
sleeping = false;
}
}
if (m_stepCount == 180)
{
m_stepCount += 0;
}
//if (sleeping)
//{
// CreateCircle();
//}
Test::Step(settings);
for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetType() != b2_dynamicBody)
{
continue;
}
b2Vec2 p = b->GetPosition();
if (p.x <= -10.0f || 10.0f <= p.x || p.y <= 0.0f || 20.0f <= p.y)
{
p.x += 0.0;
}
}
m_debugDraw.DrawString(5, m_textLine, "Press 'c' to create a circle.");
m_textLine += 15;
}
static Test* Create()
{
return new ConfinedMod;
}
};
#endif
I originally wrote the code for ActionScript 3.0, using Box2dFlash port.
So, I thought that the problem in IEEE-754 Arithmetic, but now i write same test in C++, and see that problem is still here.
My
post for AS3.0.