[SOLVED] Parabolic vector?

Discussion in 'Spigot Plugin Development' started by finnbon, Jun 18, 2015.

  1. Hi poeple!

    This is my first threads so don't judge! <3 :p

    Anyway, I'm trying to create a spear. I have everything ready, only thing is that I want to make it move in a parabolic motion. If I could just set it's velocity, this wouldn't be a big deal, but since the spear is a location, I have to add the vector to the location. So I'm asking, how can I do this?

    Thanks in advance!

    - Finn
     
  2. just keep adding the velocity to the location, and keep adding ? amount to the Y speed so it goes downwards faster and faster.
    or am I saying something st00pid now? xd
     
  3. Thanks for the fast reply!

    I'm not sure if you know what a parabolic motion is (just wanna make sure you know though :p)
    [​IMG]
    Anyway, just lowering the Y wouldn't look too good, and I know it's possible, so I'd prefer an actual parabolic motion. :)
     
  4. If you'd like some spoonfeeding, here:
    Code (Text):
        public static Vector calculateVelocity(Vector from, Vector to, int heightGain)
        {
            // Gravity of a potion
            double gravity = 0.115;
            // Block locations
            int endGain = to.getBlockY() - from.getBlockY();
            double horizDist = Math.sqrt(distanceSquared(from, to));
            // Height gain
            int gain = heightGain;
            double maxGain = gain > (endGain + gain) ? gain : (endGain + gain);
            // Solve quadratic equation for velocity
            double a = -horizDist * horizDist / (4 * maxGain);
            double b = horizDist;
            double c = -endGain;
            double slope = -b / (2 * a) - Math.sqrt(b * b - 4 * a * c) / (2 * a);
            // Vertical velocity
            double vy = Math.sqrt(maxGain * gravity);
            // Horizontal velocity
            double vh = vy / slope;
            // Calculate horizontal direction
            int dx = to.getBlockX() - from.getBlockX();
            int dz = to.getBlockZ() - from.getBlockZ();
            double mag = Math.sqrt(dx * dx + dz * dz);
            double dirx = dx / mag;
            double dirz = dz / mag;
            // Horizontal velocity components
            double vx = vh * dirx;
            double vz = vh * dirz;
            return new Vector(vx, vy, vz);
        }
        private static double distanceSquared(Vector from, Vector to)
        {
            double dx = to.getBlockX() - from.getBlockX();
            double dz = to.getBlockZ() - from.getBlockZ();
            return dx * dx + dz * dz;
        }
    I googled "bukkit parabolic motion" and this was the first result, I also took the code from there. Enter the thread and look at the answers.
     
  5. That's some horrible code right there.
     
  6. That is exactly why I said "I didn't write this code, I took it from a thread I found and enter it to read whatever they said"
     
  7. I'm not saying you have made horrible code.
    And it might work, I don't know.

    But at least it could be so more simplified, variables could have a better name.
    A lot of things.

    I'm not judging you on anything, I was just pointing out that the code was horrible :p
     
  8. What you could also do is create a discrete parabolic curve from short vector lengths, and have the spear follow their path. But that's probably overcomplicating things ;)
     
  9. sothatsit

    Patron

    Why not just create a parabolic function. Something like f(x) = -5x^2 + 5x + 10. Then use that to figure out the spears y value while incrementing the x and z based on the direction the player was looking. You then use the distance on the x and z planes for x in the function.
     
    • Agree Agree x 1
  10. So, I tried some stuff and failed horribly. If anyone could help me with how I would implent this into a location, that would be great!
    (Basicly what I have to add to the location to make the parabolic motion!)
     
  11. Never mind, I got it! Thanks for the help. :)
     
  12. I love when your looking for information and you get to the bottom of a thread, it says "solved" but no resolution. If you still active you think you could post what you did? :D
     
    • Agree Agree x 2
  13. Code (Text):

      public void progress() {
            this.x+=0.1;
            if(this.x > 10) {
                this.x = -10;
            }

            double y = 2 * (x * x) + 2 * x;
            Location loc1 = new Location(player.getWorld(), this.chest.getX() + x, this.chest.getY() + (y / 60), this.chest.getZ());

            ParticleEffect.HAPPY_VILLAGER.display(loc1, 0F, 0F, 0F, 0F, 1);
        }
    chest is the player's location.
    I put that in a runnable and it did wonders :p
     
    • Useful Useful x 1