Saturday, March 24, 2018

2d - Algorithm for randomly generating reachable platforms


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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...