Perfect bounce does not work

Hello!

I want to setup a scene with 2 boxes and 1 ball, where the ball endlessly bounces between the two boxes. I want the bounce to be perfect - no friction should occur and the speed of the ball has to stay the same after it bounces of a box.
Both boxes and the ball are drawn per DrawNode.
This is how I create the box and its PhysicsBody:

DrawNode* drawNode = DrawNode::create();
drawNode->setName("Box");
drawNode->setUserData( this );

drawNode->setPosition( position );

float halfWidth  = 0.5f * width;
float halfHeight = 0.5f * height;
Vec2 origin(-halfWidth, -halfHeight);
Vec2 destination(halfWidth, halfHeight);
drawNode->drawSolidRect( origin, destination, colorBlockingBarrier);

PhysicsBody* physicsBody = PhysicsBody::createBox( Size(width, height), PhysicsMaterial(0.5f, 1.0, 0.0f));
physicsBody->setDynamic(false);
physicsBody->setCategoryBitmask( static_cast<int>(PhysicsCategory::Barrier) );
physicsBody->setCollisionBitmask( static_cast<int>(PhysicsCategory::Ball) );
physicsBody->setContactTestBitmask( static_cast<int>(PhysicsCategory::Ball) );

drawNode->setPhysicsBody( physicsBody );

scene->addChild(drawNode);

The most important part is probably the PhysicsBody configuration. As you can see I configure the material as PhysicsMaterial(0.5f, 1.0, 0.0f), thus I set restitution to 1.0 and friction to 0. I thought this would give me a “frictionless” collision.

This is the code how I setup the ball:

DrawNode* drawNode = DrawNode::create();
drawNode->setName("Ball");
drawNode->setUserData( this );
drawNode->setPosition( position );

drawNode->drawSolidCircle(Vec2::ZERO, radius, 2.0f * M_PI, 20, ballColor);
   
drawNode->drawLine(Vec2::ZERO, startDirection * 50, Color4F::BLACK); // Draw ball direction

physicsBody = PhysicsBody::createCircle(radius, PhysicsMaterial(0.1f, 1.0, 0.0f));
physicsBody->setDynamic(true);
physicsBody->setCategoryBitmask( static_cast<int>(PhysicsCategory::Ball) );
physicsBody->setCollisionBitmask( static_cast<int>(PhysicsCategory::Barrier) );
physicsBody->setContactTestBitmask( static_cast<int>(PhysicsCategory::Barrier) | static_cast<int>(PhysicsCategory::Goal) );

drawNode->setPhysicsBody( physicsBody );

scene->addChild(drawNode);

I also register 4 physics Listener (class EventListenerPhysicsContact), but this is probably not relevant for my problem.

I also added a onTouchBegan handler to give the ball an initial impulse:

bool Level::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) {

    for(auto it = mBalls.begin(); it != mBalls.end(); ++it) {
        Ball* ball = *it;
        ball->physicsBody->applyImpulse( ball->startDirection * 200000 );
    }

   return true;

}

Here is how the scene looks like:

And now comes the strange part: The ball indeed bounces off from the 2 boxes, but after some bounces the ball velocity decreases. For most of the bounces the velocity stays the same, but sometimes the ball hits the box and after the collision the velocity has been decreased. To debug the behaviour I print the ball velocity in my update() method and also make a print in onContactBegan(). You can see the print messages on the right side of the image.

As you can see the velocity of the ball initially is (-707.35, 0.000444) for the first 13 bounces, but then it hits the left box for the 14 time, and the velocity immediately drops to (-146.52, 0) and then (146,0) and (238,0). It then stayed (238,0) for some time.

How is it possible, that the velocity decreases, even though I gave both ball and box a friction of 0? And why does this only occur sometimes? Thanks for any help!