Losing velocity when kinematic object and dynamic object collides

Hi everyone, I am in the process of making a ping pong game. I have two pong which have kinematic bodies with _isSleepingAllowed _set to false, and a dynamic ping pong ball that has a circular physics body. I notice that when the ball collides with the kinematic object it loses some of its velocity. I tried changing the pong to dynamic, but when the ball touches them, they get pushed off of the screen. How can I prevent the ping pong ball from losing its velocity?

What are the friction and bounce values you are using for the paddles, walls, and ball?

Hint: Try bounce = 1, friction = 0

Also, how are you moving the paddle?  

  • Manually by updating x,y  – OR –
  • with a force, – OR –
  • with setLinearVelocity() – OR –
  • with a touch joint

 
If you manually update the position, that’s going to have some nasty side effects.
 
Forces and setLinearVelocity are good.
 
A touch joint is also good.
 
However all physics cases will have weirdness to them in the case of edge, corner collisions.  The velocity of your paddle will affect the ball velocity if you hit on anything but the face of the paddle.

This is why PONG is such a great starting point.  You start out of thinking, “This is pretty easy”, but then you find there are all kinds of weird things to deal with depending on how you choose to implement the game.

Note: If wanted to, you could even implement this without physics. :slight_smile:

Warning: Admonishment coming…
 
You made an assertion in your statement, but I am guessing you did not make a test case to verify it.
 
I can tell you is is very unlikely that a dynamic body colliding with a kinematic body is losing any velocity because of the body types involved.  If it is, this is a bug.
 
I can verify this as follows:

local physics = require "physics" physics.start() physics.setGravity(0,0) local leftWall = ssk.display.newRect( nil, centerX - 200, centerY, {}, { bodyType = "kinematic", bounce = 1, friction = 0} ) local rightWall = ssk.display.newRect( nil, centerX + 200, centerY, {}, { bodyType = "kinematic", bounce = 1, friction = 0} ) local function enterFrame(self) local vx,vy = self:getLinearVelocity() print( math.abs(vx) ) end local ball = ssk.display.newCircle( nil, centerX, centerY, { radius = 10, enterFrame = enterFrame }, { bounce = 1, friction = 0} ) ball:setLinearVelocity( 200, 0 )

When you run this, 200 will print over and over in the console. i.e. No loss of velocity.

I will try implementing the bounce and friction values, and I will also have the ball’s linear velocity printed out.

When checking the linearVelocities with a bounce of 1 and no friction, no velocity is lost. Thank you!

What are the friction and bounce values you are using for the paddles, walls, and ball?

Hint: Try bounce = 1, friction = 0

Also, how are you moving the paddle?  

  • Manually by updating x,y  – OR –
  • with a force, – OR –
  • with setLinearVelocity() – OR –
  • with a touch joint

 
If you manually update the position, that’s going to have some nasty side effects.
 
Forces and setLinearVelocity are good.
 
A touch joint is also good.
 
However all physics cases will have weirdness to them in the case of edge, corner collisions.  The velocity of your paddle will affect the ball velocity if you hit on anything but the face of the paddle.

This is why PONG is such a great starting point.  You start out of thinking, “This is pretty easy”, but then you find there are all kinds of weird things to deal with depending on how you choose to implement the game.

Note: If wanted to, you could even implement this without physics. :slight_smile:

Warning: Admonishment coming…
 
You made an assertion in your statement, but I am guessing you did not make a test case to verify it.
 
I can tell you is is very unlikely that a dynamic body colliding with a kinematic body is losing any velocity because of the body types involved.  If it is, this is a bug.
 
I can verify this as follows:

local physics = require "physics" physics.start() physics.setGravity(0,0) local leftWall = ssk.display.newRect( nil, centerX - 200, centerY, {}, { bodyType = "kinematic", bounce = 1, friction = 0} ) local rightWall = ssk.display.newRect( nil, centerX + 200, centerY, {}, { bodyType = "kinematic", bounce = 1, friction = 0} ) local function enterFrame(self) local vx,vy = self:getLinearVelocity() print( math.abs(vx) ) end local ball = ssk.display.newCircle( nil, centerX, centerY, { radius = 10, enterFrame = enterFrame }, { bounce = 1, friction = 0} ) ball:setLinearVelocity( 200, 0 )

When you run this, 200 will print over and over in the console. i.e. No loss of velocity.

I will try implementing the bounce and friction values, and I will also have the ball’s linear velocity printed out.

When checking the linearVelocities with a bounce of 1 and no friction, no velocity is lost. Thank you!