Jumping code in javascript

I am eleven and new to programming, and I am making a simple platform game for playing at my school using javascript.

Now I am working on a code that makes the character jump. This is more complicated than a character just rising and then down, because I want the movements to be free and look realistic. When a character jumps, he quickly leaves the ground, then slows down when he rises higher, and when he reaches a certain point, he will begin to fall slowly. It will accelerate when it falls (possibly using some type of variable acceleration), and then hit the ground and completely stop.

I want the character to be able to move left and right in the air, and if the key is held, jump once, and then when the character hits the ground, if the key is still held, jump again. (game character should jump pretty high)

I already tried to do this, but I had funny mistakes.

Here is my (very broken) code:

//movement (x) var maxSpeed = 12.5; var xForce = 0; var kingXPos = 0; //movement (y) var yForce = 0; var kingYPos = 202; //LV design var floorHeight = 150; var draw = function() { //background and basics background(255, 0, 0); image(getImage("creatures/Winston"), kingXPos, kingYPos, 50, 50); //level features (only the floor right now) fill(0, 0, 0); rect(0, 250, 400, floorHeight); //right movement if (keyIsPressed && keyCode === RIGHT) { kingXPos = kingXPos + xForce; xForce = xForce + 0.25; if (xForce >= maxSpeed && keyIsPressed) { xForce = maxSpeed; } } //left movement if (keyIsPressed && keyCode === LEFT) { kingXPos = kingXPos + xForce; xForce = xForce - 0.25; if (xForce <= -maxSpeed && keyIsPressed) { xForce = -maxSpeed; } } //jump (not yet functional) if (keyTyped && keyCode === UP && kingYPos === floorHeight + 50) { kingYPos = kingYPos + yForce; yForce = yForce - 0.5; } //other physics if (!keyIsPressed) { kingXPos = kingXPos + xForce; if (xForce > 0) { xForce = xForce - 0.25; } else if (xForce < 0) { xForce = xForce + 0.25; } } }; 
+1
source share
1 answer

This is pretty impressive for someone just starting out. You seem to have an intuitive understanding of geometry. However, there is some knowledge about domains that you may not know about because of how much education you had.

In physics, the correct system of equations describing motion:

 1. speed = change_in_location / time 2. acceleration = change_in_speed / time 

Note. Here I use the word "speed" because it is shorter than "speed". The correct word is technically "speed." In physics, speed means something a little different from speed.

Another thing you need to know about is that gravity is a form of acceleration. In particular, this acceleration down 9.8 m / s / s

So, we rewrite all of the above:

 new_location = (speed * time) + old_location new_speed = (acceleration * time) + old_speed 

If you accept an animation loop with constant time, you can assume that time = 1. Thus, this simplifies it:

 new_location = speed + old_location new_speed = acceleration + old_speed 

This is enough to simulate gravity. Since gravity is just acceleration:

 gravity = SOME_NUMBER; // tune this to get the gravity you want kingYPos = kingYPos + kingYSpeed; kingYSpeed = kingYSpeed + gravity; 

To jump, just give the object an instant increase in speed:

 // jump: kingYSpeed = -SOME_OTHER_NUMBER; // negative because "up" 

Note. Domain knowledge is knowledge outside of programming that a programmer must understand in order to solve a specific problem. For example, a programmer who writes accounting software must have some knowledge of accounting. In practice, not all programmers in the industry make an effort to acquire domain knowledge, because sometimes there is a systems analyst / consultant writing software requirements. But when you write your own software, you have no choice but to acquire some domain knowledge.

+3
source

All Articles