Difference between setLinearVelocity and transition?

Hi,

I’m going through the Getting Started chapters and I’m doing the Star Explorer game. I noticed that we use newAsteroid:setLinearVelocity(x, y) to move the asteroid objects and then use transition.to(obj, param) to move the laser fired.

Both of these methods are moving objects in the physics world that have colliding event listeners. So my question is if I am making a game that has obstacles moving across the screen from right to left at a constant velocity, no changes in speed, direction with zero friction then which one of these two methods would be better to use?

Thanks in advance!

I’ve not seen that code but transition should not be used for physics bodies except when static or sensor bodies are to be moved. Any other use will fight with the Box2D physics engine and likely cause eventual crashes.

The “physics world” and the “display world” are separate entities.  When you create a display object and then turn it into a physics object, the two worlds are paired for that object.  

[lua] 

local circle = display.newCircle()

physics.addBody(circle) 

[/lua]

From then on, all movement should be controlled using the physics (Box2D) methods that act on the physics body  If you do this, Box2D will update the display world for that object.  If you try to use the display methods (like transition.to) to move the physics object, the display engine will note update the physics engine and the physics and display worlds will decouple for that object.  In other words,  the physics world will think the object is in one place and the display world will think it is in another.

You can observe this using physics.setDrawMode( )

[lua]

physics.setDrawMode( “normal” )  – The default Corona renderer (no collision outlines)

physics.setDrawMode( “hybrid” )  – Overlays collision outlines on normal display objects

physics.setDrawMode( “debug” )   – Shows collision engine outlines only

[/lua]

@elifares,

What the others point out is true. I just want to add that it’s generally safe to use transitions on a “kinematic” body type as well, since those bodies are not subject to forces like gravity which would “fight” against the transition you’re trying to do.

Brent

Thanks for the replies.

That makes absolute sense to me but how come the Getting Started tutorial uses transition.to on the newLaser object @sporkfin?

Eli

Hi Eli,

The tutorial moves the laser by a transition because it’s a pretty simple way to have Corona call the “onComplete” function when the transition is finished, and then remove the laser from the screen. The laser could be moved by physics, but then you’d have to test for its position off-screen by some other method to remove it. It’s definitely possible to do it that way, and perfectly valid, but the tutorial presents a transition as an alternative.

Brent

I’ve not seen that code but transition should not be used for physics bodies except when static or sensor bodies are to be moved. Any other use will fight with the Box2D physics engine and likely cause eventual crashes.

The “physics world” and the “display world” are separate entities.  When you create a display object and then turn it into a physics object, the two worlds are paired for that object.  

[lua] 

local circle = display.newCircle()

physics.addBody(circle) 

[/lua]

From then on, all movement should be controlled using the physics (Box2D) methods that act on the physics body  If you do this, Box2D will update the display world for that object.  If you try to use the display methods (like transition.to) to move the physics object, the display engine will note update the physics engine and the physics and display worlds will decouple for that object.  In other words,  the physics world will think the object is in one place and the display world will think it is in another.

You can observe this using physics.setDrawMode( )

[lua]

physics.setDrawMode( “normal” )  – The default Corona renderer (no collision outlines)

physics.setDrawMode( “hybrid” )  – Overlays collision outlines on normal display objects

physics.setDrawMode( “debug” )   – Shows collision engine outlines only

[/lua]

@elifares,

What the others point out is true. I just want to add that it’s generally safe to use transitions on a “kinematic” body type as well, since those bodies are not subject to forces like gravity which would “fight” against the transition you’re trying to do.

Brent

Thanks for the replies.

That makes absolute sense to me but how come the Getting Started tutorial uses transition.to on the newLaser object @sporkfin?

Eli

Hi Eli,

The tutorial moves the laser by a transition because it’s a pretty simple way to have Corona call the “onComplete” function when the transition is finished, and then remove the laser from the screen. The laser could be moved by physics, but then you’d have to test for its position off-screen by some other method to remove it. It’s definitely possible to do it that way, and perfectly valid, but the tutorial presents a transition as an alternative.

Brent