Ball bouncing: velocity decrease even if m_restitution = 1

Here's the code. Could some one help me? Thanks!

Code:
package org.jbox2d.testbed.tests;

import java.util.ArrayList;

import org.jbox2d.collision.Manifold;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.Contact;
import org.jbox2d.testbed.framework.TestbedSettings;
import org.jbox2d.testbed.framework.TestbedTest;

class Block {
   final static float HALF_WIDTH = 1.5f;
   final static float HALF_HEIGHT = 0.5f;
   
   final static int STATE_IDLE = 0;
   final static int STATE_DIE = 1;
   final static int STATE_DESTROY = 2;
   
   float x;
   float y;
   int m_type;
   int m_hp;
   
   Body m_body;
   World m_world;
   
   int m_state = STATE_IDLE;
   
   public Block(World world, float x, float y) {
      m_world = world;
      
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(HALF_WIDTH, HALF_HEIGHT);

      BodyDef bd = new BodyDef();
      bd.position.set(x, y);
      bd.userData = this;
      m_body = world.createBody(bd);
      m_body.createFixture(shape, 0.0f);
   }
   
   void onBeHit(Body ball) {
      if (m_hp < 0) ; // invincible
      
      switch (m_state) {
      case STATE_IDLE:
         m_hp--;
         
         if (m_hp <= 0)
            m_state = STATE_DIE;
         break;
      }
   }
   
   void update() {
      if (isDead())
         destroy();
   }
   
   void destroy() {
      m_state = STATE_DESTROY;
      m_world.destroyBody(m_body);
   }
   
   boolean isDead() {
      return m_state == STATE_DIE;
   }
}

public class BlockBreaker extends TestbedTest {
   final static float MAP_WIDTH = 40;
   final static float MAP_HEIGHT = 80;

   Fixture m_platform, m_ground;
   Body m_ball;
   ArrayList m_blockList = new ArrayList();

   @Override
   public void preSolve(Contact contact, Manifold oldManifold) {
   }
   
   @Override
   public void endContact(Contact contact) {
      Fixture fa = contact.m_fixtureA;
      Fixture fb = contact.m_fixtureB;
      Fixture fBall = m_ball.m_fixtureList;
      Fixture fBlock = null;
      
      
      if (fa == fBall) fBlock = fb;
      else if (fb == fBall) fBlock = fa;
      
      if (fBlock != null) {
         Body blockBody = fBlock.getBody();
         if (blockBody.getUserData() instanceof Block) {
            ((Block) blockBody.getUserData()).onBeHit(m_ball);
         }
      }
   }
   
   void initBlockList() {
      
   }

   @Override
   public void initTest(boolean argDeserialized) {
      if (argDeserialized) {
         return;
      }
      
      World world = getWorld();
      world.getGravity().setZero();
      
      Body ground = null;
      {
         
         BodyDef bd = new BodyDef();
         ground = world.createBody(bd);

         PolygonShape shape = new PolygonShape();
         shape.setAsEdge(new Vec2(-20.0f, -5f), new Vec2(20.0f, -5f));
         m_ground = ground.createFixture(shape, 0.0f);

         shape.setAsEdge(new Vec2(-20.0f, -5f), new Vec2(-20.0f, 40.0f));
         ground.createFixture(shape, 0.0f);
         shape.setAsEdge(new Vec2(20.0f, -5f), new Vec2(20.0f, 40.0f));
         ground.createFixture(shape, 0.0f);
         shape.setAsEdge(new Vec2(-20.0f, 40f), new Vec2(20.0f, 40f));
         ground.createFixture(shape, 0.0f);
      }

      // platform
      {
         PolygonShape shape = new PolygonShape();
         shape.setAsBox(3.5f, .5f);

         BodyDef bd = new BodyDef();
         bd.position.set(0.0f, .0f);
         Body body = world.createBody(bd);
         m_platform = body.createFixture(shape, .0f);
      }

      // ball
      {
         CircleShape shape = new CircleShape();
         shape.m_radius = 0.5f;

         BodyDef bd = new BodyDef();
         bd.type = BodyType.DYNAMIC;
         bd.position.set(0.0f, 1.0f);
         m_ball = world.createBody(bd);
         Fixture fixture = m_ball.createFixture(shape, 1.0f);
         fixture.m_restitution = 1;
      }
      
      // blocks
      {
         float left = -MAP_WIDTH / 4;
         float top = MAP_HEIGHT / 4;
         float x = left, y = top;
         Block block;
         
         for (int i = 0; i < 4; i++) {
            x = left;
            
            for (int j = 0; j < 6; j++) {
               block = new Block(world, x, y);
               m_blockList.add(block);
               
               x += Block.HALF_WIDTH * 3;
            }
            
            y += Block.HALF_HEIGHT * 4;
         }
      }
      
      // start ball
      m_ball.setLinearVelocity(new Vec2(4.5f, 26.5f));
   }

   @Override
   public void step(TestbedSettings settings) {
      super.step(settings);
      
      World world = getWorld();
      
      Block block;
      for (int i = 0; i < m_blockList.size(); i++) {
         block = (Block) m_blockList.get(i);
         block.update();
      }
   }

   /**
    * @see org.jbox2d.testbed.framework.TestbedTest#getTestName()
    */
   @Override
   public String getTestName() {
      return "Block breaker";
   }
}
The ball's reflect angle is not as expected sometimes, it seems keep on reducing.

That is, when the ball starts with 75 degree, running to ceiling. And a few seconds later, the ball will straight ahead the ceiling and the floor.
It's the ball's friction, I got it! 😀
Here's the new code, it runs fine as a ball.

Code:
package org.jbox2d.testbed.tests;

import java.util.ArrayList;

import org.jbox2d.collision.Manifold;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.Contact;
import org.jbox2d.testbed.framework.TestbedSettings;
import org.jbox2d.testbed.framework.TestbedTest;

class Block {
   final static int HP_MAX = Integer.MAX_VALUE;
   
   final static float HALF_WIDTH = 1.5f;
   final static float HALF_HEIGHT = 0.5f;
   
   final static int STATE_IDLE = 0;
   final static int STATE_INVINCIBLE = STATE_IDLE + 1;
   final static int STATE_DIE = STATE_INVINCIBLE + 1;
   final static int STATE_DESTROY = STATE_DIE + 1;
   
   float x;
   float y;
   int m_type;
   int m_hp;
   
   Body m_body;
   World m_world;
   
   int m_state;
   
   public Block(World world, float x, float y) {
      m_world = world;
      
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(HALF_WIDTH, HALF_HEIGHT);

      BodyDef bd = new BodyDef();
      bd.position.set(x, y);
      bd.userData = this;
      m_body = world.createBody(bd);
      m_body.createFixture(shape, 0.0f);
      
      m_hp = 1;
      
      m_state = STATE_IDLE;
      if (m_hp == HP_MAX)
         m_state = STATE_INVINCIBLE;
   }
   
   void onBeHit(Body ball) {
      switch (m_state) {
      case STATE_IDLE:
         m_hp--;
         
         if (m_hp <= 0)
            m_state = STATE_DIE;
         break;
      }
   }
   
   void update() {
      if (isDead())
         destroy();
   }
   
   void destroy() {
      m_state = STATE_DESTROY;
      m_world.destroyBody(m_body);
   }
   
   boolean isDead() {
      return m_state == STATE_DIE;
   }
   
   boolean isDestroyed() {
      return m_state == STATE_DESTROY;
   }
}

public class BlockBreaker extends TestbedTest {
   final static int STATE_INIT = 0;
   final static int STATE_GAMEPLAY = STATE_INIT + 1;
   final static int STATE_GAMEOVER = STATE_GAMEPLAY + 1;
   
   final static float MAP_WIDTH = 40;
   final static float MAP_HEIGHT = 80;
   final static float PLATFORM_HALF_WIDTH = 3.5f;
   final static float PLATFORM_HALF_HEIGHT = 0.5f;

   int m_gameState = STATE_INIT;
   Fixture m_ground;
   Body m_ball, m_platform;
   ArrayList m_blockList = new ArrayList();
   
   static float Limit(float value, float min, float max) {
      return value < min ? min : (value > max ? max : value);
   }

   @Override
   public void preSolve(Contact contact, Manifold oldManifold) {
   }
   
   @Override
   public void endContact(Contact contact) {
      Fixture fa = contact.m_fixtureA;
      Fixture fb = contact.m_fixtureB;
      Fixture fBall = m_ball.m_fixtureList;
      Fixture fBlock = null;
      
      
      if (fa == fBall) fBlock = fb;
      else if (fb == fBall) fBlock = fa;
      
      if (m_ground == fBlock) {
         gameOver();
      }
      else if (fBlock != null) {
         Body blockBody = fBlock.getBody();
         if (blockBody.getUserData() instanceof Block) {
            ((Block) blockBody.getUserData()).onBeHit(m_ball);
         }
      }
   }
   
   void gameOver() {
      m_ground = null;
      m_platform = null;
      m_ball = null;
      m_blockList.clear();
      
      m_gameState = STATE_GAMEOVER;
      
      reset();
      System.out.println("Game over.. restart!");
   }

   @Override
   public void mouseMove(Vec2 p) {
      Vec2 pos = m_platform.m_xf.position;
      pos.x = Limit(p.x, -MAP_WIDTH / 2 + PLATFORM_HALF_WIDTH, MAP_WIDTH / 2 - PLATFORM_HALF_WIDTH);
      
      m_platform.setTransform(pos, 0);
   }

   @Override
   public void initTest(boolean argDeserialized) {
      if (argDeserialized) {
         return;
      }
      
      World world = getWorld();
      world.getGravity().setZero();
      
      initGround(world);

      initPlatform(world);

      initBall(world);
      initBlocks(world);
      
      // start ball
      m_ball.setLinearVelocity(new Vec2(4.5f, 26.5f));
      
      m_gameState = STATE_GAMEPLAY;
   }

   private void initGround(World world) {
      Body ground = null;
      {
         
         BodyDef bd = new BodyDef();
         ground = world.createBody(bd);

         PolygonShape shape = new PolygonShape();
         shape.setAsEdge(new Vec2(-20.0f, -5f), new Vec2(20.0f, -5f));
         m_ground = ground.createFixture(shape, 0.0f);

         shape.setAsEdge(new Vec2(-20.0f, -5f), new Vec2(-20.0f, 40.0f));
         ground.createFixture(shape, 0.0f);
         shape.setAsEdge(new Vec2(20.0f, -5f), new Vec2(20.0f, 40.0f));
         ground.createFixture(shape, 0.0f);
         shape.setAsEdge(new Vec2(-20.0f, 40f), new Vec2(20.0f, 40f));
         ground.createFixture(shape, 0.0f);
      }
   }

   private void initPlatform(World world) {
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(PLATFORM_HALF_WIDTH, PLATFORM_HALF_HEIGHT);

      BodyDef bd = new BodyDef();
      bd.position.set(0.0f, .0f);
      m_platform = world.createBody(bd);
      m_platform.createFixture(shape, 0.0f);
   }

   private void initBall(World world) {
      CircleShape shape = new CircleShape();
      shape.m_radius = 0.5f;

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.position.set(0.0f, 1.0f);
      m_ball = world.createBody(bd);
      Fixture fixture = m_ball.createFixture(shape, 1.0f);
      fixture.m_friction = 0;
      fixture.m_restitution = 1;
   }

   private void initBlocks(World world) {
      m_blockList.clear();
      
      float left = -MAP_WIDTH / 4;
      float top = MAP_HEIGHT / 4;
      float x = left, y = top;
      Block block;
      
      for (int i = 0; i < 1; i++) {
         x = left;
         
         for (int j = 0; j < 1; j++) {
            block = new Block(world, x, y);
            m_blockList.add(block);
            
            x += Block.HALF_WIDTH * 3;
         }
         
         y += Block.HALF_HEIGHT * 4;
      }
   }

   @Override
   public void step(TestbedSettings settings) {
      super.step(settings);
      
      switch (m_gameState) {
      case STATE_GAMEPLAY:
         updateGameplay();
         break;
      }
   }

   private void updateGameplay() {
      Block block;
      for (int i = m_blockList.size() - 1; i >= 0; i--) {
         block = (Block) m_blockList.get(i);
         block.update();
         
         if (block.isDestroyed()) {
            m_blockList.remove(i);
         }
      }
      
      if (m_blockList.size() < 1) {
         gameWin();
      }
   }

   private void gameWin() {
      // TODO Auto-generated method stub
      reset();
      System.out.println("You win. New round!");
   }

   /**
    * @see org.jbox2d.testbed.framework.TestbedTest#getTestName()
    */
   @Override
   public String getTestName() {
      return "Block breaker";
   }
}

Original thread: https://box2d.org/forum/viewtopic.php?t=8750