Hi all,
just wondering, i’ve been using setLinearVelocity for quite a while with physics where the speed of an object is affected (rightly so) by physics so is it better to use applyLinearImpulse ?
thanks
Edualc
Hi all,
just wondering, i’ve been using setLinearVelocity for quite a while with physics where the speed of an object is affected (rightly so) by physics so is it better to use applyLinearImpulse ?
thanks
Edualc
I’m answering the title not the body of your post which I did not quite understand.
No. Those two functions are not equivalent. The end result may seem similar, but one is the application of an instantaneous force resulting in a velocity change and the other is simply setting the velocity directly.
For the same force, the first will have different results based on the object’s mass.
The second will always result in the same velocity as long as the same vx,vy values are used.
Impulses are used to simulate impacts.
Also worth noting that the outcome of impulses, since they’re a quick application of force, can vary radically depending on the density (size) of the object they’re being applied to. Linear velocity, on the other hand, is consistent and predictable.
Brent
yes, thank you both.
i’ve written a little program to try to make up my mind, but i don’t see much difference as Brett said.
As it is about kicking a ball you’d think the linearImpulse is best but repetitive kicks dont look right …
hmmm,.
-- twoballs physics = require("physics") physics.start() floor = display.newRect( display.contentWidth/2 , display.contentHeight - 30 , display.contentWidth, 3 ) physics.addBody(floor,"static") --1 local ball1 = display.newCircle(100,300,15) physics.addBody( ball1, { density=0.8, friction=0.1, bounce=0.4 } ) --2 local ball2 = display.newCircle(200,300,15) physics.addBody( ball2, { density=0.8, friction=0.1, bounce=0.4 } ) function tap\_ball(obj) ball1:setLinearVelocity(0,-200) ball2:applyLinearImpulse(0 , -5,ball2.x, ball2.y) end ball1:addEventListener("tap",tap\_ball) ball2:addEventListener("tap",tap\_ball)
Is this like a top-down view where the ball should slow down, as if it’s coming to a slow stop as it rolls across ground or something? Or are you doing a side-view game?
pretty much like my little program shows, kind of intergalactic volley-ball … don’t ask
… but i get inconsistencies when i apply LinearImpulse while in a moving position.
it’s also harder because the ball can get too fast after multiple taps while in the air. so i need to limit the speed in enterFrame … too much work !
i think the LinearVelocity is easier to use.
Linear velocity is definitely more predictable. If you apply an impulse to an already-moving object, it’s impossible to know what will happen, just like in real life, since the ball is already moving and the amont of impulse applied has to work against/with the existing forces on the ball. On the other hand, for a “volleyball” game, you may find that applying linear velocity looks to “rigid” or “unrealistic”.
Remember that you also stop an object before applying a new impulse to it, by setting its velocity to 0,0 first.
Brent
I’m answering the title not the body of your post which I did not quite understand.
No. Those two functions are not equivalent. The end result may seem similar, but one is the application of an instantaneous force resulting in a velocity change and the other is simply setting the velocity directly.
For the same force, the first will have different results based on the object’s mass.
The second will always result in the same velocity as long as the same vx,vy values are used.
Impulses are used to simulate impacts.
Also worth noting that the outcome of impulses, since they’re a quick application of force, can vary radically depending on the density (size) of the object they’re being applied to. Linear velocity, on the other hand, is consistent and predictable.
Brent
yes, thank you both.
i’ve written a little program to try to make up my mind, but i don’t see much difference as Brett said.
As it is about kicking a ball you’d think the linearImpulse is best but repetitive kicks dont look right …
hmmm,.
-- twoballs physics = require("physics") physics.start() floor = display.newRect( display.contentWidth/2 , display.contentHeight - 30 , display.contentWidth, 3 ) physics.addBody(floor,"static") --1 local ball1 = display.newCircle(100,300,15) physics.addBody( ball1, { density=0.8, friction=0.1, bounce=0.4 } ) --2 local ball2 = display.newCircle(200,300,15) physics.addBody( ball2, { density=0.8, friction=0.1, bounce=0.4 } ) function tap\_ball(obj) ball1:setLinearVelocity(0,-200) ball2:applyLinearImpulse(0 , -5,ball2.x, ball2.y) end ball1:addEventListener("tap",tap\_ball) ball2:addEventListener("tap",tap\_ball)
Is this like a top-down view where the ball should slow down, as if it’s coming to a slow stop as it rolls across ground or something? Or are you doing a side-view game?
pretty much like my little program shows, kind of intergalactic volley-ball … don’t ask
… but i get inconsistencies when i apply LinearImpulse while in a moving position.
it’s also harder because the ball can get too fast after multiple taps while in the air. so i need to limit the speed in enterFrame … too much work !
i think the LinearVelocity is easier to use.
Linear velocity is definitely more predictable. If you apply an impulse to an already-moving object, it’s impossible to know what will happen, just like in real life, since the ball is already moving and the amont of impulse applied has to work against/with the existing forces on the ball. On the other hand, for a “volleyball” game, you may find that applying linear velocity looks to “rigid” or “unrealistic”.
Remember that you also stop an object before applying a new impulse to it, by setting its velocity to 0,0 first.
Brent