Randomly generating reachable platforms

Monkey Forums/Monkey Programming/Randomly generating reachable platforms

erebel55(Posted 2014) [#1]
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.

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.

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


Currently my create platform method looks like the following (it just creates platforms at a random x,y)

Method CreatePlatform:Void()
		' it is time to spawn a new platform
		If Millisecs() > platformSpawnTimer + platformSpawnSpeed
			Local randomX:Int = Rnd(0, width)
			Local randomY:Int = Rnd(1, height-3) ' 3 is the lava height, add a constant for this later
			Local randomLength:Int = Rnd(1, MAX_PLATFORM_LENGTH)
			
			' change initial alpha to 0
			For Local x:Int = randomX To randomX+randomLength-1
				' if the tile is on the map
				If x < width
					map[x][randomY].alpha = 0.0
					map[x][randomY].frame = 1
				End
			Next
			
			' create a platform
			Local tempPlatform:Platform = New Platform(randomX, randomY, randomLength)
			
			' add the platform to the queue
			platforms.AddLast(tempPlatform)
	
			' reset the timer
			platformSpawnTimer = Millisecs()
		End
	End



Gerry Quinn(Posted 2014) [#2]
I guess the player can jump in a parabola from either edge of the platform, and change direction in flight? If so, it seems he could get to any platform that's underneath the outside pointing parabolas from each edge of the current platform. There are probably some places above and below the platform that he could get to too, but that depends on how much the platform bottoms get in the way. Anyway, under the two parabolas should give plenty of scope.


erebel55(Posted 2014) [#3]
I guess the player can jump in a parabola from either edge of the platform, and change direction in flight?

Yes, that is correct. Also, currently the player can jump through the bottoms of platforms on his way up.


Nobuyuki(Posted 2014) [#4]
This may sound dumb, but what if you made a bot that could do every kind of jump possible at maximum jump height and speed, plot those points, and find find a parabolic curve that encapsulates the entire distribution? Then, you would generate platforms based on that. Sampling the data may be easier than modeling the jump parameters as a perfect equation...


erebel55(Posted 2014) [#5]
Currently the speedX is static (without acceleration). So on the x axis you are either moving speedX or 0. I'm not sure sampling will be easier for me, it is an interesting idea though.