Menu
IDS - Gravity
Vector Review
A vector specifies the direction and distance that an object moves in every single time that the clock ticks. Typically we must keep track of the sideways motion (X) and the up/down motion (Y) to specify a vector.
An object that moves straight up/down will have its X vector set to zero. An object that moves left or right will have its Y vector set to zero. If an object moves diagonally, then the X vector and Y vector are both non-zero values. If the X and Y vector are both set to zero, then the object will STOP. |
Until now, the distance specified in a vector has generally remained constant. Pacman moves by the same distance every time that the clock ticks. Some vectors do not remain constant...
Gravity
Consider an object that jumps off the ground. The six arrows in the diagram on the left indicate the up/down motion of the character for each clock tick.
Notice that the distance of each vector is smaller than the one before, then the object starts going in the opposite direction and the vector gradually gets larger. |
If we tried to do this on a MicroBit, object would be on the bottom and in order to move it up, the Y vector might start off as -2, then -1, then 0 (stop) and then start moving down with 1, then 2, until it gets stopped by the ground. How do we accomplish this?
Everytime that a movement is finished, adjust the vector by 1 unless the object is on the ground. If it is on the ground, we want to make sure the movement stops or it will fall through.
Pseudo Code is: Move MarioX by vecX Move MarioY by vecY if MarioY < 4 THEN increase vecY by 1 otherwise (ELSE) set vecY to ZERO |
To get the jump button to work, we need to program the button press to initiate the Y vector. Since an up vector is negative and we want to start with a strong jump, we might set it to -2 or perhaps even 3.
Notice the IF statement in the example on the right. Why do we ask that question? See if you can find an online version of JOUST and play it. |