This is not true for all batteries, but some batteries create current through a chemical reaction between the battery acid and the case that produces hydrogen gas. The hydrogen gas bubbles build up on the inside of case, which stops the reaction from taking place. Knocking the batteries around can shake the hydrogen bubbles lose, allowing better contact between the battery acid and the case.
Latest Answers