The account by Darren Fresco, identified as being with Pistorius on two occasions when a gun was fired in public, corroborated much of the evidence of another witness from the scene of the alleged sunroof shooting.
Mr Fresco recalled Pistorius telling the traffic officer in late 2012: '"You can't just touch another man's gun."
Pistorius, an Olympic athlete and double amputee, has been charged with premeditated murder for the killing of his girlfriend Reeva Steenkamp, 29, in the early hours of February 14, 2013.
The 27-year-old also faces three firearms charges, including two for allegedly shooting a gun in public which feature Mr Fresco as a witness. They relate to the alleged gunshot out of the sunroof, and an incident in which a gun was fired under a table in a busy restaurant in Johannesburg. Pistorius has pleaded not guilty to all four charges.
During the 2012 stop for speeding, according to Mr Fresco, Pistorius also told the police officer: "Now your fingerprints are all over my gun. So if something happens, you're going to be liable for anything that happens."
Mr Fresco said he and Pistorius and Samantha Taylor, the athlete's girlfriend at the time and a state witness who has given a similar account of the episode, then drove away.
Mr Fresco, who was driving the vehicle, said: "Then without prior warning, he shot out the sunroof."
The witness said he "instinctively" moved over to the right side of the vehicle and away from where the gun was shot and then asked Pistorius if he was mad.
"He just laughed," Mr Fresco said.
The trial continues.