0

i really need help with figuring out a velocity equation to launch a player from one X Y Z location to another. from looking at other threads i created the following code (the code doesn't work)

public void vJump(Entity entity, Location loc, double time)
{
    this.entity = entity;
    entity.setVelocity(getV(entity.getLocation(), loc, time));
}

private Vector getV(Location p1, Location p2, double t)
{
    double x = p2.getX() - p1.getX();
    double y = p2.getY() - p1.getY();
    double z = p2.getZ() - p1.getZ();
    double gravity = -14.266;
    return new Vector(getVelocity(x,gravity,t), getVelocity(y,gravity,t), getVelocity(z,gravity,t));
}

private double getVelocity(double d, double a, double t)
{
    a*=-.5;
    a*=Math.pow(t,2);
    d-=a;
    return d/t;
}

i do not know the gravity in Minecraft nor do i know the movement friction. the problem with the code above is that it doesn't go in the right direction nor does it create the parabola effect i was looking for.

Community
  • 1
  • 1
Ashley
  • 142
  • 7

2 Answers2

2

based on Nick Rhodes answer i removed gravity for all but the y (in minecraft, up and down is y). and then i doubled the velocity for all planes by 2 and it ended up working! here is the code. also i found out that gravity was actually 0.1.

public void vJump(Entity entity, Location loc, double time)
{
    this.entity = entity;
    entity.setVelocity(getV(entity.getLocation(), loc, time));
}

private Vector getV(Location p1, Location p2, double t)
{
    double x = p2.getX() - p1.getX();
    double y = p2.getY() - p1.getY();
    double z = p2.getZ() - p1.getZ();
    double gravity = 0.1;
    return new Vector(getVelocity(x,0,t), getVelocity(y,gravity,t), getVelocity(z,0,t));
}

private double getVelocity(double d, double a, double t)
{
    a*=-.5;
    a*=Math.pow(t,2);
    d-=a;
    return 2*(d/t);
}
Ashley
  • 142
  • 7
0

Part of the problem lies in the fact that gravity will only come into play for the z vector. There will be no gravitational force in the x or y directions. This may explain why your code produces movement in another direction than expected.

try (assuming that t is defined as the total time of transit):

return new Vector(getVelocity(x,0,t), getVelocity(y,0,t), getVelocity(z,gravity,t));
Destruktor
  • 463
  • 1
  • 4
  • 19