jumping algorithm?

Hi! I was wondering if anyone can help me to make my character jump in a nice way.
It’s for a running game i’m developing like Monster Dash.

So, what i want to do is make the character jump a minimum height as soon as the touch begins, and then, depending on how much time you hold the touch, the higher he goes (or the more time he is on air).

I’ve found some ways to accomplish this, but results do not seem very natural (well natural is relative in the gaming world). Anyone knows a nice jumping method or technique??

Thanks a lot guys! [import]uid: 105206 topic_id: 25134 reply_id: 325134[/import]

I consulted another developer on this before. Download the free version of BlooKid and see for yourself. He has the jumping perfected.

What he did was not use any type of physics force to do the jump, he just changed the y coordinates of the character through an enterFrame listener. He did something like this:

player.y = player.y - 5  

and when the player reached its peak of its jump, he eased it back downwards by doing something like this:

player.y = player.y - 5 \* 0.5  

This would slow down the rate of ascent each frame. After the player reached the peak of its jump he just let gravity pull the player back down. Sorry I don’t have any working code, I scrapped any test code because I ran into other issues. That’s the general idea though. [import]uid: 31262 topic_id: 25134 reply_id: 102129[/import]

Thanks for the tip man. I’ve tried something like this before and it didn’t give me results, but maybe i should work on it a little more.
I believe this wouldn’t be compatible with a physics based world, would it? [import]uid: 105206 topic_id: 25134 reply_id: 102255[/import]

Hey nml, see if this is helpful. I found some old code and just tweaked it a bit. It’s very basic but can give you a general idea of what I meant. Put these 2 files in the same folder and run main.lua. Press the white circle to jump. I temporarily hosted “guy.png” on my website but it won’t be there forever. Can substitute it with any image: http://appnews.30belowstudios.com/guy.png

--main.lua  
  
display.setStatusBar( display.HiddenStatusBar )  
  
require("physics")  
  
physics.start()  
  
-- Create a background colour just to make the map look a little nicer  
local back = display.newRect(0, 0, display.contentWidth, display.contentHeight)  
back:setFillColor(165, 210, 255)  
  
local ground = display.newRect (0, display.contentHeight-20,display.contentWidth,20)  
ground:setFillColor(0,50,0)  
physics.addBody( ground, "static", {density=1} )  
ground.type = "ground"  
  
local player = display.newImage("guy.png")  
player.x = display.contentWidth/2; player.y = display.contentHeight-30;  
physics.addBody( player, {density=1, friction=0.2} )  
  
local startJump = 0  
local playerX = 0  
local jumpFactor  
  
local onTouch = function(event)  
 if startJump == 1 then  
 player.y = player.y - jumpFactor  
 else   
 if player.canJump == false and jumpFactor \> 0.1 then  
 jumpFactor = jumpFactor \* 0.9  
 player.y = player.y - jumpFactor  
 end  
 end  
end  
  
Runtime:addEventListener("enterFrame", onTouch)  
  
local function onCollision(self, event )  
 if ( event.phase == "began" ) then  
 if event.other.type == "ground" then  
 player.canJump = true  
 end  
 elseif ( event.phase == "ended" ) then  
 player.canJump = false  
 end  
end  
  
local button = display.newCircle( display.contentWidth-50, display.contentHeight-50, 30)   
  
local function makePlayerJump( event )  
 if event.phase == "began" then  
 if player.canJump then  
 jumpFactor = 7 --Change this to increase/decrease jump height  
 playerX = player.x  
 startJump = 1  
 end  
 elseif event.phase == "ended" or event.phase == "canceled" then  
 startJump = 0  
 end  
end  
  
button:addEventListener("touch", makePlayerJump)  
player.collision = onCollision  
player:addEventListener( "collision", player )  
  

[import]uid: 31262 topic_id: 25134 reply_id: 102266[/import]