obj:getLinearVelocity() return 0 on kinematic obj

I’m implementing a typical ‘pong’ game.

I have the ball, that is “dynamic” obj, and the rackets, that was “static” obj.
All worked fine.

Now I would like to upgrade the bouncing effect on the rackets and I tried to works with the linearvelocity.

But the obj:getLinearVelocity() does’t works with “static” obj, so I changed it in “kinematic” the rackets. But also on this I receive 0 from function.

On “dynamic” obj my rackets goes around the screen impacting with the ball. Non nice!

Any suggestion ?


When you have problems like this, then you should provide code that demonstrates your issue. This is simply because :getLinearVelocity() method behaves exactly as expected, so the issue is somewhere in your code.

For instance,

local physics = require("physics")

local myRect = display.newRect( 0, 0, 100, 100 )
physics.addBody( myRect, "kinematic" )
myRect:setLinearVelocity( 5, 0 )

print( myRect:getLinearVelocity() ) --> outputs:  5 0

Also, you might want to take a look at https://docs.coronalabs.com/api/type/Body/bodyType.html. The differences between the physics bodies are explained there in detail.

Dynamic objects are supposed to interact with other objects, so without seeing any code, I don’t know what the issue is.

Sorry Xedur,

I coded an example and I found the problem, you can see. But I’m not able to solve it. :thinking:

local physics = require("physics")

local myRect = display.newRect( 0, 0, 100, 100 )

local function dragMyRect( event )
  local phase = event.phase
  if ( "began" == phase ) then
    -- Store initial offset position
    myRect.touchOffsetY = event.y - myRect.y
    myRect.touchOffsetX = event.x - myRect.x
  elseif ( "moved" == phase ) then
    -- Move the obj to the new touch position
    myRect.x = event.x - myRect.touchOffsetX
    myRect.y = event.y - myRect.touchOffsetY
    print( myRect:getLinearVelocity() ) 
  elseif ( "ended" == phase or "cancelled" == phase ) then
    -- Release touch focus on the obj
    display.currentStage:setFocus( nil )
  return true

physics.addBody( myRect, "kinematic" )
myRect:addEventListener( "touch", dragMyRect)

I copied the movement of the square from the tutorial https://docs.coronalabs.com/guide/programming/03/index.html

so now reformulating the question we could say:
how can I know the linear velocity of the square as I move it?

Thank you for help!

See the link about physics body types I linked you above.

You’ve created a kinematic body, which doesn’t react to gravity or collisions with other objects. They only react to the linear velocity that is applied to them.

In your code, you are moving the object by manipulating its coordinates instead of using the physics engine to move it. This means that you are not applying any physics forces to it, so its linear velocity will remain zero.

thanks Xedur, I understand this logic that manages the physic world.

Do you have any idea or tutorial that explains how to move objects, moved by a player, while keeping them in the physical world? the only tutorial I found is that of the site I mentioned above.

Or how can I lock an object on its axis when it receives an impact?


Well, you can use a joint to drag physics bodies around. You can find an example for that in the sample projects in the simulator, or check out the Crate sample project at https://www.solar2dplayground.com/.

If you are creating paddles for pong, then what do you need to get the linear velocity for anyway?

If you need to control the objects via :setLinearVelocity(), then just apply it towards the touch movement. If you have dynamic object that you want to prevent from rotating, just use https://docs.coronalabs.com/api/type/Body/isFixedRotation.html. If you have static or kinematic bodies, then they won’t react to collisions anyway.

If you want to remove gravity from some dynamic bodies then look for gravity scale option. If you need to stop the movement of an object at any point, then just set its linear velocity to 0, 0.

1 Like

XeduR thanks for a lot of suggestions.

the object.isFixedRotation() helped me, but when the ball bounce with the baddle this one moves from the place.

The basic games works well but I want to add some effects when the ball bounce on paddle, otherwise the bounces have no effect and are boring.

The first thing to know is the speed of the paddle which will influence the resulting speed of the ball after the bounce. That’s why I went to look for the linear speed of the baddle (which is nothing but its physical vector).

I tried to play with bounce, friction and density but I’m not arrived to have pretty bouncing ball effects.

I arrived to get the angular speed using the physical motor: I put the mass of the paddle to infinity (a value around 5000) keeping at the same time the object “dynamic”; and the linear speed came out! The object - having almost infinite mass - behaves as if it were “static”, without having its limitations.

So now I can play with vectors!
Thank you for help.