How to move a kinematic object?

I have this function being called every frame to scroll down platforms depending on the player height.

I have all platforms inside scrollableGroup.

If I try to change the y value the platform moves but the collision happens in the same place
1566a81.jpg

applyLinearImpulse() doesnt do anything.
setLinearVelocity() works, but then I need a timer to stop it from moving.

Is there a proper way to move them?

function scrollPlatforms() -- UpdatePlatforms handles gameplay object movements &nbsp;&nbsp;&nbsp;&nbsp;if player.y \< -scrollableGroup.y + 240 then&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--scrollableGroup.y = -(player.y - 240) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--platform:applyLinearImpulse( 0, 10, platform.x, platform.y ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--platform:setLinearVelocity(0, 40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--timer.performWithDelay( 250, test, 1 ) &nbsp;&nbsp;&nbsp;&nbsp;elseif player.y \> -scrollableGroup.y + 480 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--gameOver() &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;

Hi @nelson.gregorio,

There are two things to note here:

  1. You shouldn’t try to move physics objects in a “group” independently of another group (as in, move the X or Y position of the entire group as a method to move the objects). This will cause inaccuracies in the Box2D collision system. Collisions between all objects… no matter which group they reside in… function around the same (0,0) coordinate space.

  2. As you’ve seen, “kinematic” objects are not subject to various forces like gravity or impulses. You can only set the direct linear velocity on them.

Using a timer to stop the objects is a fine approach, but I don’t see your “test” function in the code above. Have you written that yet?

Best regards,

Brent Sorrentino

I forgot to include it in my post.
Like the name says, it was just a test to see if it worked.

function test() platform:setLinearVelocity(0, 0) end

So my next step should be adding a loop to cycle through each platform, set the velocity, and do the same loop in the test function to stop them all ?

It would be also nice if I could trouble you for a look at another question I asked.
http://forums.coronalabs.com/topic/34076-access-physics-body-shape-in-collisions/

Hi Nelson,

Actually, you can control each platform individually and then pass the specific object ID (to the platform) to the timer listener, and thus stop it directly by reference to the object:

[lua]

local function onTimer( event )

   local obj = event.source.objectID

   obj:setLinearVelocity( 0, 0 )

end

local platform = display.newImage( “platform.png” )

physics.addBody( platform, “kinematic” )

platform:setLinearVelocity( 0, 40 )

local t = timer.performWithDelay( 250, onTimer ) ; t.objectID = platform

[/lua]

As for your other question, you might find the following tutorial informative:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Best regards,

Brent

Thank you very much.
I can finally move on with the rest of my game :slight_smile:

And thanks as well for the tutorial.

Hi @nelson.gregorio,

There are two things to note here:

  1. You shouldn’t try to move physics objects in a “group” independently of another group (as in, move the X or Y position of the entire group as a method to move the objects). This will cause inaccuracies in the Box2D collision system. Collisions between all objects… no matter which group they reside in… function around the same (0,0) coordinate space.

  2. As you’ve seen, “kinematic” objects are not subject to various forces like gravity or impulses. You can only set the direct linear velocity on them.

Using a timer to stop the objects is a fine approach, but I don’t see your “test” function in the code above. Have you written that yet?

Best regards,

Brent Sorrentino

I forgot to include it in my post.
Like the name says, it was just a test to see if it worked.

function test() platform:setLinearVelocity(0, 0) end

So my next step should be adding a loop to cycle through each platform, set the velocity, and do the same loop in the test function to stop them all ?

It would be also nice if I could trouble you for a look at another question I asked.
http://forums.coronalabs.com/topic/34076-access-physics-body-shape-in-collisions/

Hi Nelson,

Actually, you can control each platform individually and then pass the specific object ID (to the platform) to the timer listener, and thus stop it directly by reference to the object:

[lua]

local function onTimer( event )

   local obj = event.source.objectID

   obj:setLinearVelocity( 0, 0 )

end

local platform = display.newImage( “platform.png” )

physics.addBody( platform, “kinematic” )

platform:setLinearVelocity( 0, 40 )

local t = timer.performWithDelay( 250, onTimer ) ; t.objectID = platform

[/lua]

As for your other question, you might find the following tutorial informative:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Best regards,

Brent

Thank you very much.
I can finally move on with the rest of my game :slight_smile:

And thanks as well for the tutorial.