Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Calculate max motion Y

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

  1. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    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. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

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

    SOFe Banned

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

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

    Summary:
    I guess these equations can answer your question.

    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) / (* (-$gravity));
    }
    See this thread for more information about using equations in programming.
    Last edited: Aug 11, 2016
  4. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

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

    SOFe Banned

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

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    yes your calculation is better :)
  7. SOFe
    Offline

    SOFe Banned

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

    Creeperface Notable Member Plugin Developer

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

Share This Page

Advertisement