jump + physics = confusion

Hi,
This is my first post :slight_smile:
I’m tring to create a platform game and I want to use physics engine for jump. But I’m a little confused…

I have a “Hero” display object with added body. I use applyLinearImpulse() for jumping, triggered by “touch” event (with phase == “began” condition). A jump looks good and naturally.
But…

How can I know if a jump is finished? Cause I can’t let Hero make a “double jump” from air. Previous jump must be finished befor next Hero’s jump.
I need something like onComplete event…

So I tried to tracking Hero.y values in “enterFrame” event. My idea was: The jump is finished when Hero.y value is constant between two or three following frames.

But I’m completly surprised: Hero.y value is not an Integer, even after jump’s finish! For example: Hero.y before jump was 265 and after jump Hero.y = 265.05017089844. How is it possible?
There is a “solid” platform below the Hero of course.

And, a more important question: how can I prevent “double jump” from air?

Thank you for any help.

nosty [import]uid: 193524 topic_id: 33527 reply_id: 333527[/import]

Hello @nosty,
Welcome to Corona! Your question is actually a very common one (not just in Corona, but 2D environments in general).

To answer your basic question, tracking the hero’s Y value is actually a pretty good idea. When the jump starts, you record the starting Y value, and then “listen” for the ending value. The only thing is, you’ll get decimal values (as you see) because the physics engine is really that precise, to 10 decimal points. So, you would need to listen to a range of Y values, maybe 265-268 or something, in your example.

Another method is to detect a “began” collision on the platform itself. But this can require further levels of detection, because what if the player hits a platform from below? The hero is not on that platform, but rather still in the process of jumping.

If you’re working on a platform game, I suggest you download a recent demo I created. It’s located within the blog post below, and it will show you how to use “PhysicsContact” for one-sided platforms. What it doesn’t do (yet) is prevent the double-jump… but I’m considering the best method for that, and might come up with something reliable. In the meantime, you can see how I did the jumping and platform detection.

http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

Best of luck, keep me posted on how it goes.
Brent Sorrentino [import]uid: 200026 topic_id: 33527 reply_id: 133303[/import]

I was making a platforming game (still am, after the one I’m working on now) and the way I handled it, because checking the y might be difficult with different altitudes, was different. I had separate invisible thin rectangles a little thinner than each platform at the top of each one, that had an onCollision listener to tell whether you were grounded or not. They had to be a little thinner because if you’re right next to a platform and you jump, it’ll say you’re grounded. That’s how I handled it.

Caleb [import]uid: 147322 topic_id: 33527 reply_id: 133342[/import]

Hello @nosty,
Welcome to Corona! Your question is actually a very common one (not just in Corona, but 2D environments in general).

To answer your basic question, tracking the hero’s Y value is actually a pretty good idea. When the jump starts, you record the starting Y value, and then “listen” for the ending value. The only thing is, you’ll get decimal values (as you see) because the physics engine is really that precise, to 10 decimal points. So, you would need to listen to a range of Y values, maybe 265-268 or something, in your example.

Another method is to detect a “began” collision on the platform itself. But this can require further levels of detection, because what if the player hits a platform from below? The hero is not on that platform, but rather still in the process of jumping.

If you’re working on a platform game, I suggest you download a recent demo I created. It’s located within the blog post below, and it will show you how to use “PhysicsContact” for one-sided platforms. What it doesn’t do (yet) is prevent the double-jump… but I’m considering the best method for that, and might come up with something reliable. In the meantime, you can see how I did the jumping and platform detection.

http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

Best of luck, keep me posted on how it goes.
Brent Sorrentino [import]uid: 200026 topic_id: 33527 reply_id: 133303[/import]

I was making a platforming game (still am, after the one I’m working on now) and the way I handled it, because checking the y might be difficult with different altitudes, was different. I had separate invisible thin rectangles a little thinner than each platform at the top of each one, that had an onCollision listener to tell whether you were grounded or not. They had to be a little thinner because if you’re right next to a platform and you jump, it’ll say you’re grounded. That’s how I handled it.

Caleb [import]uid: 147322 topic_id: 33527 reply_id: 133342[/import]

Thank you!
I enchanced my “tracking the hero’s Y” method and it works fine now. But Caleb’s solution can be better. I’ll try to use both methods simultaneously. [import]uid: 193524 topic_id: 33527 reply_id: 134970[/import]

Thank you!
I enchanced my “tracking the hero’s Y” method and it works fine now. But Caleb’s solution can be better. I’ll try to use both methods simultaneously. [import]uid: 193524 topic_id: 33527 reply_id: 134970[/import]