a

Lorem ipsum dolor sit amet, elit eget consectetuer adipiscing aenean dolor

Return To Adventure Mountain

HOW TO: Curves editor for simplified jumping

The algorithm required to make a character jump, in modern platformers, basically applies a sudden burst of velocity up each time a player pushes the jump button. At each clock tick, the velocity for gravity is applied against the upward movement so that your jumping character slows until the up velocity reaches zero. At this point, the character stops at the peak of her jump. She then returns to earth accelerating downward due to the same calculation of gravity that slowed her as she was traveling up. It creates a elegant, and although usually an unrealistic exaggeration of how high a human can jump, is familiar to us earthlings who fall at 9.81 m/s per second.

How ever, if you don’t want to make a perfect model of reality, and want to make your character jump in an unrealistic arc that has no acceleration you have a different set of challenges. Games such as pitfall! launch the character at a perfect arc. One way of creating an artificial jump to create a function. In this scenario, you send the function each clock tick that occurs after pushing jump. The function outputs a height that your character should be. The jump function is most likely a SINE wave or a parabola. However, a jump needs nuance. You must bend and stretch it to make it look good which would require some laborious tweaking of function variables.

Enter the curves editor. The engineers behind the App Hub site has mercifully provided a tool that you can use to draw curves that generate functions. In this tutorial I will describe how to set it up.

Download the curve editor from here.
http://create.msdn.com/en-US/education/catalog/utility/curve_editor
Extract the contents to the same directories you store all of your visual studio files.

The curve editor is a stand alone program that you build and run from Visual Studio. Open the file named /CurveEditor/CurveEditor.sln and build it.

1) In the editor that appears, draw the curve by adding “keys” or spots that change the curve’s direction. I would go into more detail but I would just be repeating the excellent documentation that is in
/CurveEditor/CurveEditor.htm

Note: The first and last key points should be on the Y axis. To get the exact point, select the point you want to bring to the Y axis and enter 0.0 in the “Value” field.

Note: The width of your curve does not have to pertain to the time duration of your jump. This is because the program loop will check for a value until it runs out of points on that curve. Then the jump will be over.

The curve I created looks like this

2) Save the curve to your project’s content directory. I made a special directory just for curves:
Content/Curves.

3) Go to your game’s code and load the curves as content.
a) Add the curve to your content: In the Content explorer add a directory for Curves. Right click and select Add > Existing Item.
b) Right-click the curve xml and select Properties.
Set the following properties:

* Build Action: Compile
* Content Importer: XML Content – XNA Framework.
* Content Processor: No Processing Required.

4) Load the curve in your code:
a) Open the class for your character. If you are using the provided Platformer starter template, this would be in the Player.cs file.
b) Create a variable to hold the curve:

private Curve jumpCurve;

c) Add this line to the LoadContent() method:

jumpCurve = Level.Content.Load(“Curves/JumpA”); //JumpA is the name of the curve file

5) Add the code to iterate over your curve and adjust your character’s jump height:
a) In the function that handles your jump add this:

jumpTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (0.0f < jumpTime && jumpTime <= jumpCurve.Keys[jumpCurve.Keys.Count - 1].Position) offset.Y = jumpCurve.Evaluate(jumpTime); //This code says that if your jump has not started and you are not at the end of the jump set the characters “Y” value to the curve hight at your time tic. //The call to [jumpCurve.Keys.Count - 1] evaluates to the last key in the curve. So when you pull that one, you have reached the end of the jump.