simulate jump without gravity

I can make player jump with the following code. I depend on the gravity to pull the player down to the ground after applying impulse and this really look like real world jump. If I set the gravity to 0,0, then is there any way to make look-alike real world jumping as if we turn gravity on?

local physics = require "physics"  
physics.start()  
physics.setGravity(0,40)  
  
local grass = display.newRect(0,460,480,20)  
grass.name = "grass"  
  
local player = display.newCircle(160,440,20)  
  
physics.addBody(player,{ friction=0, bounce=0})  
physics.addBody(grass,"static",{friction=0, bounce=0})  
local function onJump(event)  
 if player.canJump == true and event.phase== "began" then  
 player:applyLinearImpulse(0, -0.35, player.x, player.y)  
 end   
end  
  
Runtime:addEventListener("touch",onJump)  
local function onCollision(self, event )  
 if ( event.phase == "began" ) then  
 if event.other.name=="grass" then  
 player.canJump = true  
 end  
 elseif ( event.phase == "ended" ) then  
 if event.other.name=="grass" then  
 player.canJump = false  
 end  
 end  
end  
player.collision = onCollision  
player:addEventListener( "collision", player )  
  

I hope someone can share me the code. Thank you.
Steve [import]uid: 84159 topic_id: 14153 reply_id: 314153[/import]

You could use a transition on your player’s y coord. With a bit of fiddling you could make it look very natural :slight_smile: [import]uid: 52491 topic_id: 14153 reply_id: 52086[/import]

Can you share me the code? Thanks.

Steve [import]uid: 84159 topic_id: 14153 reply_id: 52100[/import]

Steve, you will have to play around as peach said but here is the api section you need to refer too. http://developer.anscamobile.com/reference/index/transitionto . Additionally you could play around with applying a force to the object until it returns to a certain y-cord or for a specified time. http://developer.anscamobile.com/reference/index/bodyapplyforce [import]uid: 19176 topic_id: 14153 reply_id: 52171[/import]

ne.hannah, before asking the forum I already tried doing it on my own and searched the forum. I’m asking for code just in case someone already did so because it would save much time and it is the reason why Ansca has code sharing place. [import]uid: 84159 topic_id: 14153 reply_id: 52310[/import]

We told you how and did not show you because what you are trying to do could be done a few ways and needs to be tweaked until you are happy with it. It’s not a complexity thing, it’s 6 lines to start the way I would do it and using just these six lines with an added rotation would result in the same look the game “the impossible game” has for jumping in a side scroller. Read the api stuff posted.

[lua]local down = function()
transition.to( square, { time=500, y=square.y+100, transition=easing.inExpo} )
end

local up = funtion()
transition.to( square, { time=500, y=square.y-100, transition=easing.outExpo, onComplete=down } )
end[/lua] [import]uid: 19176 topic_id: 14153 reply_id: 52319[/import]

ne.hannah, thank you very much. When down, using easing.inQuad give better result than easing.inExpo, but I’ll try tweaking it more to make it look natural. Once again, thanks for sharing.

Steve [import]uid: 84159 topic_id: 14153 reply_id: 52438[/import]