Character jump

How to make character jump and fall faster ?

local physics = require ("physics") physics.start() \_W = display.contentWidth \_H = display.contentHeight function moveTo(object, params) local T = display.screenOriginY -- Top local L = display.screenOriginX -- Left local R = display.viewableContentWidth - L -- Right local B = display.viewableContentHeight - T-- Bottom local cX, cY = display.contentCenterX, display.contentCenterY object.x, object.y = 0, 0 local bounds = object.contentBounds local offsetX = (bounds.xMax + bounds.xMin) \* 0.5 local offsetY = (bounds.yMax + bounds.yMin) \* 0.5 local hW, hH = 0.5 \* object.contentWidth , 0.5 \* object.contentHeight if params.left then object.x = params.left+L + hW - offsetX elseif params.right then object.x = R-params.right - hW - offsetX elseif params.centerX then object.x = params.centerX + cX - offsetX end if params.bottom then object.y = B-params.bottom - hH - offsetY elseif params.top then object.y = params.top + T + hH - offsetY elseif params.centerY then object.y = params.centerY + cY - offsetY end return object end local function hasCollided( obj1, obj2 ) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end local land = display.newRect(0,0,480,200) physics.addBody( land, "static", { friction=0.5, bounce=0.3 } ) moveTo(land,{centerX = 0 ,bottom = 0}) land.name = land local player = display.newRect(\_W/2,0,50,50) player:setFillColor(0.2,0.4,0.1) physics.addBody(player,{"dynamic"}) player.isFixedRotation = true moveTo(player,{centerX = 0 ,bottom = 200}) player.name = player local function jump(e) if e.phase == "began" then Runtime:removeEventListener("touch",jump) player:setLinearVelocity( 0, -250 ) elseif e.phase == "ended" then end end local function onCollision(event) if event.phase == "began" then if event.object1.name == land and event.object2.name == player then Runtime:addEventListener("touch",jump) end end end Runtime:addEventListener("collision",onCollision) Runtime:addEventListener("touch",jump)

I don’t use physics, but normally with these things there is a gravity parameter.

When you use :setLinearVelocity you’re telling the object to move at that speed continuously.  When you set an upward amount, gravity will pull them back down at the rate of gravity.  What would be better is to use the :applyLinearImpulse() API ( This is a sudden burst of energy, which is realistically what a jump is.

Rob

I don’t use physics, but normally with these things there is a gravity parameter.

When you use :setLinearVelocity you’re telling the object to move at that speed continuously.  When you set an upward amount, gravity will pull them back down at the rate of gravity.  What would be better is to use the :applyLinearImpulse() API ( This is a sudden burst of energy, which is realistically what a jump is.

Rob