I’ve been running into too many issues at hand and wonder if anyone has any tips here.
- Ball, Paddle and Walls have 0 friction, 1.0 restitution (bounce), there is also no gravity in the world. This should mean the ball will never stop boucing, right? Nope… The ball will eventually stop. The paddles are kinematic, the walls are static. I’ve tried messing with angular damping, and even increasing the restitution, but either the ball speeds up too fast or slow down at some point.
- If the paddle is moving while impacting with ball, the ball will slow down, I assume this is due to transfer of motion. This happens to a slow moving ball, increasing the speed of the balls causes less of an issue.
- The paddles are taller than the balls and given enough speed the ball will, at some point, become airborne.
After each paddle hit I apply an impluse to keep the ball moving and slowly increase the ball’s overall velocity. I use the paddle’s center position to apply an additional angle to the ball so that does not keep boucing in the same position. The impact code is in Kotlin but should be readable on what I am doing:
override fun prePhysicsTick(space: PhysicsSpace, tpf: Float) {
val centerOfBall = control.physicsLocation
var impulse = Vector3f.ZERO
ghostObject?.overlappingObjects?.forEach { collisionObj ->
if (collisionObj is PhysicsRigidBody && collisionObj is PaddleControl) {
val centerOfPaddle = collisionObj.physicsLocation
//-- Get the local location from ball and paddle
centerOfPaddle.subtractLocal(centerOfBall)
centerOfPaddle.normalizeLocal()
//-- Get the up / down position to decide on the force to apply
val z = centerOfPaddle.getZ()
val zForce = -1 * z / 100
val xForce = if (centerOfPaddle.getX() > 0) -0.08f else 0.08f
impulse = Vector3f(xForce, 0f, zForce)
}
}
control.applyImpulse(impulse, Vector3f.ZERO)
}
The full code for that specific collision is here: PongJM3/PaddleCollision.kt at master · joseph-montanez/PongJM3 · GitHub
The code that keeps the ball bouncing at a faster speed is the following line:
val xForce = if (centerOfPaddle.getX() > 0) -0.08f else 0.08f
Overall I am thinking is dropping using the physics engine to do what is extremely basic feature, and only using Bullet for collision detection.Althought at that point I can drop the usage of bullet since all I need is AABB collision dection which jMonkeyEngine supports on its own.