Hi,
I have an object moving at a constant speed. To achieve that I use zero gravity, a body with 100% bounce and a force applied to that physics body.
[lua]
physics.setGravity(0, 0)
…
physics.addBody( ball.object, { density=0, friction=0, bounce=1, radius=ball.size } )
ball.object:applyForce( x, y, ball.object.x, ball.object.y )
[/lua]
After collision the speed stays the same and that’s great. But, I want to manually increase speed from time to time.
How can I do this? Can I get the current X and Y force applied to an moving object? Something like this?
[lua]
ball.object:applyForce( ball.object.currentForceX, ball.object.currentForceY, ball.object.x, ball.object.y )
[/lua]
Daniel
@Daniel,
Hi. I answered this question (page down a bit) with a video and a link to code which should help: http://forums.coronalabs.com/topic/51384-apply-impulse-applies-floating-point-force-instead-of-constant/
PS - Read my answers in the prior part of the post too, where I talk about how applyForce() works.
Thank you. Solved by changing applyForce to applyLinearImpulse:
[lua]
local x = getRandomDirection()
local y = getRandomDirection()
ball.object:applyLinearImpulse( x * ball.object.mass, y * ball.object.mass, ball.object.x, ball.object.y )
[/lua]
I can now increase speed using another impulse:
[lua]
local vx, vy = collision.ball.object:getLinearVelocity()
log:log("Current velocity: ", vx, vy)
local impulseX = (vx * collision.ball.increaseSpeedFactor / collision.ball.multiplyScore) * collision.ball.object.mass
local impulseY = (vy * collision.ball.increaseSpeedFactor / collision.ball.multiplyScore ) * collision.ball.object.mass
collision.ball.object:applyLinearImpulse( impulseX, impulseY, collision.ball.object.x, collision.ball.object.y )
log:log(“Linear impulse applied to increase velocity”, impulseX, impulseY)[/lua]
Daniel
@Daniel,
Hi. I answered this question (page down a bit) with a video and a link to code which should help: http://forums.coronalabs.com/topic/51384-apply-impulse-applies-floating-point-force-instead-of-constant/
PS - Read my answers in the prior part of the post too, where I talk about how applyForce() works.
Thank you. Solved by changing applyForce to applyLinearImpulse:
[lua]
local x = getRandomDirection()
local y = getRandomDirection()
ball.object:applyLinearImpulse( x * ball.object.mass, y * ball.object.mass, ball.object.x, ball.object.y )
[/lua]
I can now increase speed using another impulse:
[lua]
local vx, vy = collision.ball.object:getLinearVelocity()
log:log("Current velocity: ", vx, vy)
local impulseX = (vx * collision.ball.increaseSpeedFactor / collision.ball.multiplyScore) * collision.ball.object.mass
local impulseY = (vy * collision.ball.increaseSpeedFactor / collision.ball.multiplyScore ) * collision.ball.object.mass
collision.ball.object:applyLinearImpulse( impulseX, impulseY, collision.ball.object.x, collision.ball.object.y )
log:log(“Linear impulse applied to increase velocity”, impulseX, impulseY)[/lua]
Daniel