[Resolved] jump logic right or wrong?

HI guys, i am creating one small jump game. Players will have to use accelerometer to move player left or right and help the character to land on the rocks in middle of pond. For score collect coins from midair… simple

For this i have following code… unfortunately i am not able to achieve smooth movements, and i really dont know the logic which i used for jump is correct or not.

[code]
physics.setGravity(0,8)

–CODE FOR MOVING PLAYER
function movePlayer(e)
–(Using this code from brick breaker game tutorial from tuteplus)

frog.x = display.contentCenterX - (display.contentCenterX * (e.yGravity*2))

end

Runtime:addEventListener(“accelerometer”, movePlayer)

–Player Jump logic
(Player will be thrown up in the air again as soon as it touch the base)

local function onCollision(event)

local object1 = event.object1.name
local object2 = event.object2.name

if event.phase == “began” then

if object1 == “base” and object2 == “frog” then
frog:setLinearVelocity(0, -300)
end
end
end

[code]

I want my character to jump this high but little faster. How can i control this… any help will be appreciated.

Thanks in advance. [import]uid: 83799 topic_id: 26807 reply_id: 326807[/import]

You might try a linear impulse instead of setting the velocity outright. This should simulate a one-time jump force, and gravity will take over from there.

object:applyLinearImpulse(X,Y,bodyXpoint,bodyYpoint)

Keep in mind that linear impulses take a MUCH smaller value to affect the body compared to applying linear velocities directly.

Brent Sorrentino
Ignis Design [import]uid: 9747 topic_id: 26807 reply_id: 108790[/import]

Thanks for advise… ill change it right now…

–I have change my code as you suggested… i have replaced my code from this
frog:setLinearVelocity(0, -300)
to
frog:applyLinearImpulse(0,-2,frog.x,frog.y)

I am facing one issue now… Like when my character land on the surface to jump its jumps about 400 pixels high… then second time its jumps for 400 pixel then again 500 and so on…

basically i am getting two different heights of jumps now with same amount … any ideas to fix it ?

Any comments on player handling via accelerometer ? [import]uid: 83799 topic_id: 26807 reply_id: 108807[/import]

Perhaps try setting the velocity (X and Y) to 0 upon collision, to reset any speed values. Then directly after, apply the linear impulse upward. This way you shouldn’t have “opposing speed vectors” which might alter the jump height.

If this doesn’t work quite as expected, you can delay the linear impulse by one game cycle, using a short timer. So, like this…

if event.phase == "began" then  
   
 if object1 == "base" and object2 == "frog" then  
  
 local frog = frog --deep-localize "frog" for this function; minor performance matter  
  
 local function doJump()  
 frog:applyLinearImpulse(0,-2,frog.x,frog.y)  
 end  
  
 frog:setLinearVelocity( 0,0 ) --set linear velocities to 0  
 timer.performWithDelay( 10, doJump ) --call jump impulse function after a tiny delay  
  
 end  
  
end  

Also, go ahead and make your “movePlayer” function local; I see no reason why it needs to be global. Beyond that I don’t have any suggestions on the accelerometer, since I’ve never used it in a project.

Let me know if this helps…
Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 26807 reply_id: 108836[/import]

Yeahhhaa… its worked like a charm… Thanks a lot for the info and brief explanation “Ignis Design”.
This will help me in my future production too…
Once again thanks a lot…

Just out of curiosity why my animation looks little laggy i mean its not smooth… I tried to set frame rate high in setting file but its not helping me much…(I think i have to start new post for it)…

Siddhesh.Meher [import]uid: 83799 topic_id: 26807 reply_id: 108886[/import]

Awesome! The animation issue could be several things. Go ahead and post a new topic, preferably with some code on how you are setting up and running your animations. Sample code is usually the fastest way to resolve a problem (just not 200 lines of it, which most forum users don’t want to read and decipher). :slight_smile:

Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 26807 reply_id: 108889[/import]

Yup yup… Thanks for advise. [import]uid: 83799 topic_id: 26807 reply_id: 108893[/import]