Advertisement

Platform spawn system

Started by October 05, 2020 07:37 PM
1 comment, last by ddlox 4 years, 2 months ago

Hi everyone!

I want to make an system that spawn platforms. The player has to jump on them and get to the top.

The actual code works like this:

This looks boring

I want to make somthing like this:

The higher. the fewer platforms

I can't use just random position, because player has maximum jump distance, and I have no idea how to make this algorithm.

Thanks.

There are many ways to do this…I will tell you of one using: smoothstep

  • I'm assuming that your screen Y coordinates go from 0 at the top down to MAXY at the bottom of your screen
  • Let's assume that at the bottom of your screen i.e. at Y = MAXY you want 10 platforms and at the top i.e. at Y = 0 of your screen you want 1 platform
  • Let's assume each platform has an origin O(x, y)

We need to find O.y for a given screen Y position, using smoothstep you can do this:

#include <iostream>
#include <map>

using namespace std;

float clamp(float x, float lowerlimit, float upperlimit) 
{
  if (x < lowerlimit)
    x = lowerlimit;
  if (x > upperlimit)
    x = upperlimit;
  return x;
}

float smoothstep(float edge0, float edge1, float x) 
{
  // Scale, bias and saturate x to 0..1 range
 // x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); 
  // Evaluate polynomial
  return x * x * (edge1 - edge0 * x);
}



int MINY_SCREEN_TOP = 0;
int MAXY_SCREEN_BOTTOM = 480; // screen height size for example
int height_space_between_platform = 10;

int main()
{
    std::map<int, int> platforms;
    
    for (int y=MINY_SCREEN_TOP; y <= MAXY_SCREEN_BOTTOM; y += height_space_between_platform)
	
	     platforms[y] = (smoothstep(1.0 /* float edge0 */, 
								                    10.0 /* float edge1 */, 
								                    (float)((float)y / (float)MAXY_SCREEN_BOTTOM) /* float x */) + 1);
								        
    for (auto& p : platforms)
        std::cout << "At screen Y position " << p.first << ", I want " << p.second << " platform(s) " << std::endl;
	
								
    return 0;
}

You can also do the same for X.

You can even further randomise p.second.

You can simplify to 1 loop with 1 std::vector instead (so it's a bit faster).

You can do this in reverse from BOTTOM to TOP.

You can do anything really ?.

smoothstep is only one way, there are many ways/algos/techs to do this.


I'll let you fiddle with this code. Have a read, modify/use freely, make mistakes, and most importantly have fun.

That's it. All the best ?

This topic is closed to new replies.

Advertisement