# Physics or Time-Based Animation In Corona SDK (iOS) Using Box2D

I am part way through creating a Doodle Jump clone for iOS using Corona SDK. I'm currently using the physics engine (box 2D). It's not a straight clone, but I need the basic principles in place to progress to the next stage of development.

I have encountered a problem that is prohibiting me to carry on, and I'm wondering whether it's worth re-writing the game using time based animations rather than the physics engine, as it could give me more flexibility.

The problem I'm faced with involves moving the platforms down and the character's height. If you play Doodle Jump and watch how the character behaves you'll see that he doesn't EVER go any higher than a certain point, no matter what height the platform he came off was. The higher he appears to jump is actually the platforms moving down faster. The character also jumps for the same duration every time. If you look at his position on the screen, he may only move up by a few pixels, but he'll appear to be jumping for a lot longer, the same length of time for each jump.

I cannot grasp how this could have been achieved using the physics engine. I am currently moving the platforms down at the same speed as the character moves up - but this doesn't stop him gaining height, and doesn't mean his height on the screen is fixed.

I have an idea how to do it in the physics engine, but it seems VERY complicated for something so simple. It would involve working out a calculation for his current height and then manipulate the gravity and his density so that he always jumps the same height for the same length of time. This would be a trial and error scenario, and to be honest, it's clutching at straws.

My question is, has anyone achieved this behaviour already, know how it has been achieved, or can shed any light on it? Would I be better with the physics engine, or with time-based animation (using timers to manually track the ball's position and use easing libraries to mimic basic physics)?

Here is a link to a YouTube gameplay video of Doodle Jump, hopefully you'll see what I mean - it's a difficult concept to describe in text. http://www.youtube.com/watch?v=kIqG2ICUS-c

• Why are you even using a physics engine for this? The movement of the background is little more than a parabolic function. Sep 28, 2011 at 3:35

Let me describe a bit how I do specific-height physics jumps in my current Corona game:

• Both the hero as well as the platforms are physics objects
• I put my own non-physical functionality and data on top of every sprite in the game, where needed (as I have a sprite object which mixes physics and manual behavior)

When the hero runs right, I apply constant velocity right, in every frame of the game. Sort of like this ("self" is the hero, as this is the hero's main handling function):

self.data.speedX = 100 -- or whatever value
local speedX, speedY = self:getLinearVelocity()
self:setLinearVelocity(self.data.speedX, speedY)


You can see that speedY is "ignored" because I'm passing back the same value it had (I suppose passing nil didn't work, I'd have to check again).

What happens when the hero -- heroine in my case! -- jumps, i.e. when the jump button control I made receive a touch event? Then I do something similar to this (not actual code, again, but snipped to get the point across better):

local speedX, speedY = girl:getLinearVelocity()
if speedY >= -5 then -- I allow multiple jumps, but only when descending or about to descend
local speedY = -9
girl:applyLinearImpulse(0, speedY, girl.x, girl.y)
girl.phase:set('justJumped', 25, 'default') -- ignore if you want, this is just my sprite object handler, which could trigger sounds, animations, further behavior for the "justJumped" phase
end


The result for my game is that I have very specifically timed jumps. Each jump has a known specific height and length, so I can create jump n run style puzzles which rely on this.

Hope this helps!

• Hi, thanks for the comments. It's not really what I am after though unfortunately - this might be down to my description of the problem, though. I'm having trouble managing the character's height. I can easily get him to jump on the platforms, but he continues to jump higher and higher and higher until he is off screen above the ceiling - even though I bring the platforms down at the same speed that he goes up. Like I tried to describe in the initial post, Doodle Jump's character stays centre on the page and won't jump any higher, platforms move down to give illusion of jumping high. Sep 27, 2011 at 10:12
• Gotcha. Well, it looks like a basic scrolling challenge then, I'm not sure Corona natively supports a camera object of sorts -- it would be neat if they did though, because otherwise it seems you'd have a workaround of applying "wrong" physics by moving all the platforms down (instead of moving your character up). If there was a camera approach to this, then you could keep all physics correct and just stick the camera to the character's location. You probably already asked in the Corona forum at anscamobile.com? Sep 27, 2011 at 12:03

I created a game similar to Doodle Jump using Corona (called Dungeon Tap), where the basic game logic was the same: platforms move down as player moves up. The only difference was the platforms slowly moved down on their own, and the player controlled his jump.

The platforms moved down based on gameSpeed variable in an enterFrame listener. When the character reached a certain point, the gameSpeed adjusted until the player fell below that certain point, giving off the illusion that he could never pass a certain point on the screen. Here is a snippet of my code:

if playerObject.y < 185 then
gameSpeed = -playerObject.y + 175
else
gameSpeed = startGameSpeed
end


As you can see, the gameSpeed is adjusted depending on the player's y position. With that, the player never moves past a specific y-position on the screen. The platforms just move quicker downward until player goes back below the "threshold".

Hope that helps!