Throw items with arch?

Discussion in 'Spigot Plugin Development' started by Snowmite, May 31, 2017.

  1. I know how to drop items with velocity, like shoot dropped items, but how will I add arch to it?

    Like make it go up and back down?
     
  2. Once you have the Item entity that was spawned, just call setVelocity on it. If you want it to go in an upward arc, then the Y component of that velocity should be higher than X or Z.
     
  3. Thank you, I realized the item would disappear since it shoots to far. Perhaps you know a way to actually throw a skull(Not as a dropped skull but like a placed skull)?
     
  4. Velocity on items seems to move them much faster than on other entities. I'd like to think this is Mojang trying to simulate mass and momentum, but ... er it honestly feels like a bug XD

    Anyway, just try setting a lower velocity. I do use this in a spell in my plugin, it tosses a bunch of flower items around that turn into flower blocks, so I know it can be done and look OK. A speed (the length of your velocity vector) between .75 and 1.1 have worked well for me. So for instance maybe try (0.5, 0.7, 0.0) as a test velocity.

    The other technique that could work is putting the skull on an armor stand's head and then applying some velocity to the armor stand. This has some drawbacks, though- mainly that there's a bug where if part of the armor stand goes underground the whole thing renders black, which is something that can happen if you try to use it for projectiles that fall close to the ground. It's workable, though, I use this for my rocket launcher.
     
  5. "Arch" what do you mean by that ?
    Do you that it lands somewhere with going slowly down so it won't be in a line so more like a curve?
    Maybe provide your code too
     
  6. More like a curve
     
  7. Firstly of course, you must actually drop the item, which is a method from the World class.

    Item dropItem(Location location, ItemStack item)
    Drops an item at the specified Location

    With the Item instance, you will notice that it also extends Entity, meaning it includes the following method:

    void setVelocity(Vector velocity)
    Sets this entity's velocity

    Now depending on how you'd like to make it, Location has the following method:

    Vector getDirection()
    Gets a unit-vector pointing in the direction that this Location is facing.

    And you can then implement the various Vector#multiply(...) methods in the class. There are several.

    Regarding the actual arching and solving the physics behind it, @2008Choco made an awesome tutorial for this and described how to do simple math to make some various effects with them. I highly recommend you check it out, here:

    https://www.spigotmc.org/threads/vectors-application-of-velocity-physics.180332/

    Hopefully I helped. Feel free to quote this post or tag me and I'd love to help. Hoping that resource is helpful for you and you can utilize it to the fullest. Let me know how it goes :)
     
    • Like Like x 1
  8. When I try the armor stand method, it doubles while in the air but goes back into one armor stand when it lands.

    Picture:
    [​IMG]

    I tried this with armor stands. It worked, but the armor stand is doubled while in the air. Do you know a fix?

    Picture:
    [​IMG]

    Then it goes back into one armor stand when it lands.
     
    #10 Snowmite, May 31, 2017
    Last edited by a moderator: Jun 1, 2017
  9. Er - you sure you're only launching one? That looks pretty weird. Can you show your code?
     
  10. Code (Text):
        @EventHandler
        public void onSneak(PlayerToggleSneakEvent event){
            Player pl = event.getPlayer();
            ItemStack item = createHead("http://textures.minecraft.net/texture/3cebff90b5c9c59ca3542fd2551ee3999f8e8bffef1085bcc87bf2dcd617fe41");

            ArmorStand stand = (ArmorStand) pl.getWorld().spawnEntity(pl.getLocation(), EntityType.ARMOR_STAND);
            stand.setVisible(false);
            stand.setHelmet(item);
            stand.setVelocity(pl.getEyeLocation().getDirection().add(new Vector(0, 1, 0)));
        }
     
  11. Huh, sorry not sure- that's pretty weird. I've never seen anything like that happen.