5
\$\begingroup\$

I want to randomly generate reachable platforms on the fly for a 2D game. Right now, I am just creating platforms at random x,y positions. Only 4 platforms can exist at once, so once the 5th one is generated the 1st one is removed (fifo).

So, I want to ensure that the player can jump from his current platform to at least one other at any given time.

I was thinking of using some variant of the distance formula to calculate the location of the next platform being generated. I was hoping someone could help me out with the formula for calculating the next platforms possible x,y positions.

The player variables I am assuming will be important are speedX, speedY, and gravity.

From here, I should be able to calculate the maximum distance to plug into the distance formula.

enter image description here

However, I am thinking that some variation of the distance formula that contains a variable for slope would be better in my case.

I want to ensure that the player will never get stuck on a platform with no where it can jump. Also, I want to ensure that platforms aren't generated on top of each other.

Formulas/images/code are a huge plus.

Here is how jumping is currently working.

Field speedY:Float = 4
Field GRAVITY:Float = .05

Method Update:Void()
    If KeyDown(KEY_SPACE) And not jumping Then
        dy=-speedY
        jumping = true
    EndIf


    If jumping
        dy += GRAVITY
        local tempY:Float = y + dy
        y = tempy
    Endif
End Method

Edit: I now understand that the formula is just the displacement of basic projectile motion. enter image description here

enter image description here

Except, in my case, we don't care about cos/sin because the angle will always be the same.

\$\endgroup\$
  • 1
    \$\begingroup\$ That is acceleration (The value you add to dy). So my answer will work for you. \$\endgroup\$ – wolfdawn Mar 9 '14 at 19:03
  • 1
    \$\begingroup\$ I took the time to clear things up \$\endgroup\$ – wolfdawn Mar 9 '14 at 19:15
  • 1
    \$\begingroup\$ I updated it further to make it more accessible. Let me know if you were able to implement it. \$\endgroup\$ – wolfdawn Mar 10 '14 at 21:22
  • 1
    \$\begingroup\$ That was a typo (I added x). Xpos & Ypos depend on each other in the sense that they both depend on t. You basically start from either the left or the right side of the platform (that the player is on) and then randomize a reasonable value for t. You can compute a position the player can land on using the equation I noted by providing t. You may also want to iterate and make sure the path is clear(?) Cause there may be blocking platforms above the one the player is standing on. You can start a new question if you get stuck with that part and see no answer available. \$\endgroup\$ – wolfdawn Mar 11 '14 at 7:48
  • 1
    \$\begingroup\$ gravity is positive (pulling downwards) 'ypos' and 'vy' is supposed to be negative, pulling upwards. If 'vy' is not negative than you need to subtract it instead. \$\endgroup\$ – wolfdawn Mar 11 '14 at 22:30
4
\$\begingroup\$

New Answer: Lets look at the image. The player (represented by a circle) can jump to the left or to the right. What makes the difference is how much time passes since the moment the play jumps until the moments she lands on another platform.

player jump

If we randomize a direction, either left or right and a value for time t than we can place a platform in that direction in the correct position.

let:

g = Gravity
vx, vy = speedX, speedY
x, y = currentX, currentY // either on the left or the right side of the platform

The equation is:

ypos = (g * t^2) / 2 + vy * t + y
xpos = vx * t + x

Again, all you need to do is randomize a value for t and compute the new position. Remember that ought to keep it small enough so the new platform is within the screen.

Old Answer: The general idea is checking how much distance you can cover on x-axis before reaching a certain y altitude (on the way down) that is too low for the platform you want to jump onto.

First, anything within the x0, x1 bounds of the platform the player is standing on is reachable if it is bellow maximal jump height. If you are using gravity as a simple acceleration downwards then the time it takes a player to reach the peak of his jump is:

t = speedY / GRAVITY

This means the player will reach:

OrigY + t * speedY / 2 at her peak (where OrigY is the altitude you jump from). This also means you can have a shot at reaching any platform that is that high if it's t * speedX units away on x-axis.

To figure out the equation for lower platforms maximal distance on x-axis, you need to remeber you can cover about t * speedX by the time you reach your maximal jump height and then begin to fall (GRAVITY takes over). Reaching a certain y that is delta bellow your max height will take:

(gt'^2)/2 = delta
t'^2 = (2 * delta) / g
t' = sqrt(2 * delta / g)

So if a point is delta bellow max height, it can be reached if it is less than (t + t') * speedX units away. The equation is:

speedX * ((speedY / GRAVITY) + sqrt(2 * delta / GRAVITY))

This is of course a "best pixel perfect jump ever" approximation. You may want to give the player a better chance by reducing her speedY and speedX to 80% or 90% when you compute. Also, it does not take into consideration the possibility of blocking platforms.

\$\endgroup\$
  • \$\begingroup\$ Thank you, is there anyway you could sum this up into two formulas? One for calculating a reachable value for x and one for calculating a reachable value for y? What I ultimately want to do is randomly place a platform and ensure it is reachable. \$\endgroup\$ – erebel55 Mar 9 '14 at 22:08
  • 1
    \$\begingroup\$ I understand but the reachable x value depends on the y value you pick. If want I could sum it up where given an x value it will return the highest y values (a bit later). \$\endgroup\$ – wolfdawn Mar 10 '14 at 7:36
  • \$\begingroup\$ That would be great. \$\endgroup\$ – erebel55 Mar 10 '14 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.