Hi to all. I tried to implement jumping of my character and figured out that body:applyLinearImpulse() works not so good as I expected. First time I press jump button everything is fine, but when I press second time, body quicly moved on the ground and then impulse applied to him. It seems very acward… Of course, I just switched applyLinearImpulse to setLinearVelocity(), but I want to understand - what was my mistake.
Here is a sample code, which you can run and see the issue.
WW = display.actualContentHeight WH = display.actualContentWidth physics = require "physics" physics.start() physics.setGravity (0, 9.8) local back = display.newRect (0, 0, WW, WH) back:setFillColor (255,255,255) local bottom = display.newRect (0, WH-20, WW, 20) bottom:setFillColor (0,0,0) local body = display.newRect (200, 100, 40, 70) body:setFillColor (0,0,0) physics.addBody (bottom, "static", { density = 2, friction = 0.3, bounce = 0.2, }) physics.addBody (body, "dynamic", { density = 2, friction = 0.3, bounce = 0.0, } ) local function onTouch (event) if (event.phase == "began") then body:applyLinearImpulse (0, 200, body.x, body.y) end end back:addEventListener ("touch", onTouch)