Tuesday, July 3, 2018

physics - How to make a character jump?


I am currently making a game in C# using Direct X 9.0. The game remake is Donkey Kong NES. I have nearly everything completed, but I am having problems with the physics of Mario's jump. I have variables declared for the Y and X co-ordinations.


I was wondering if there was a simple method of doing this. I have searched high and low for an answer but the answers I have found are either irrelevant/ or using a different programming language such as XNA.


I currently have a bool variable set to check to see if W has been pressed then that will trigger any code to make him jump. I have been messing around such as.


  if (jump == true)
{

Playerypos -= vel;
Playerxpos += vel;

}

Which didn't work that well. I have searched high and low for an answer and now I am desperate, if someone could point me in the right direction to a simple solution. That would be great. Much appreciated for any responses.



Answer



First of all, XNA also uses C# too so it's the same programming language. And although the underlying API might have some differences from DirectX, that has nothing to do with jumping, so the same tutorials or answers should apply here. Also, there are numerous ways to implement this, depending on a lot of factors. What I'll describe below is just one of the possibilities.


Basic Physics Requirements


First you need some basic physics variables and calculations on your update loop. In particular, you need position, velocity, and gravity defined, and your update loop should be doing something roughly similar to this (collision detection and response omitted for simplicity):


float positionX, positionY;     // Position of the character
float velocityX, velocityY; // Velocity of the character
float gravity = 0.5f; // How strong is gravity


void Update(float time)
{
positionX += velocityX * time; // Apply horizontal velocity to X position
positionY += velocityY * time; // Apply vertical velocity to X position
velocityY += gravity * time; // Apply gravity to vertical velocity
}

Fixed Height Jump


Now the easiest way to implement a jump that always has the same height, no matter how long you press the key, is simply to change the vertical velocity once. This will make the character start rising, while gravity will automatically take care of bringing him down:



void OnJumpKeyPressed()
{
velocityY = -12.0f; // Give a vertical boost to the players velocity to start jump
}

Important You don't want to be able to start a jump if your character is not on the ground, so you'll need to add a check for that.


Variable Height Jump


But games like Mario and Sonic have a variable jump where the height of the jump depends on how long you press the button down. In this case you'll need to handle both pressing and releasing the jump key. You could add something like:


void OnJumpKeyReleased()
{

if(velocityY < -6.0f) // If character is still ascending in the jump
velocityY = -6.0f; // Limit the speed of ascent
}

Tweaking all of these values (i.e. the numbers like 0.5, -12.0f or -6.0f) in the code above you can change the feel of your jump, how high he jumps, how much momentum does he keep even after releasing the jump button, how fast he falls, etc.


Important Don't call these functions every frame. Call them only once when the key is pressed or released. Otherwise your character will fly instead of jump.


Working Example


I have just created a quick example of the techniques described above, which you can try on your browser here (press mouse button to jump, release it midway to control the height of the jump). It's in JS but like I mentioned earlier, the language is irrelevant in this case.


http://jsfiddle.net/LyM87/


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...