# Calculate max motion Y

Comments in 'Plugin Development' started by Creeperface, Aug 10, 2016.

1. Offline

### CreeperfaceNotable MemberPlugin Developer

Joined:
Nov 8, 2014
Posts:
1,346
Plugins:
4
Minecraft User:
i add motion to player but i want to calculte max Y coordinate

i can use for loop
PHP:
``` \$gravity //entity gravity\$motion = 5;\$maxY = 0;while(\$motion > 0){      \$maxY += \$motion;      \$motion -= \$gravity;} ```
but its not good i think and there must be direct calculation
2. Offline

### CreeperfaceNotable MemberPlugin Developer

Joined:
Nov 8, 2014
Posts:
1,346
Plugins:
4
Minecraft User:
or is it
PHP:
``` (\$motionY / 2) * (\$motionY / \$gravity) ```
?
3. Offline

### SOFeBanned

Joined:
May 28, 2016
Posts:
386
Minecraft User:
Time for some physics or mathematics.

https://en.wikipedia.org/wiki/Equations_of_motion#Kinematic_equations_for_one_particle

Summary:

For example, when an object is thrown upwards at the initial velocity of u ms-1, with downwards gravity of g ms-2 to find the maximum height h (assuming the object is thrown from the ground):

The object has velocity 0 when it is at the highest position (previously positive velocity, changing to negative velocity).
Therefore, using this equation:
Code:
`v^2 = u^2 + 2as     (4)`
To substitute variables:
0 = u^2 + 2 (-g) (h)

We want to find h, so make h the subject of this equation:
2 gh = u^2
h = u^2 / 2g

Hence, you can write this formula for h into your program:

PHP:
``` public function maxHeight(\$initialVelocity, \$gravity){  // gravity is negative, because we take upward direction as positive  return (\$initialVelocity ** 2) / (2 * (-\$gravity));} ```
Last edited: Aug 11, 2016
4. Offline

### CreeperfaceNotable MemberPlugin Developer

Joined:
Nov 8, 2014
Posts:
1,346
Plugins:
4
Minecraft User:
Thx! And my calcualation is wrong?
5. Offline

### SOFeBanned

Joined:
May 28, 2016
Posts:
386
Minecraft User:
If you are referring to this post,
yes, it is correct, but watch out for +-.
Creeperface likes this.
6. Offline

### CreeperfaceNotable MemberPlugin Developer

Joined:
Nov 8, 2014
Posts:
1,346
Plugins:
4
Minecraft User:
7. Offline

### SOFeBanned

Joined:
May 28, 2016
Posts:
386
Minecraft User:
8. Offline

### CreeperfaceNotable MemberPlugin Developer

Joined:
Nov 8, 2014
Posts:
1,346
Plugins:
4
Minecraft User:
hmmm but what about max X or Z? its calculated in different way