Trouble With Vectors

Discussion in 'Plugin Development' started by Icyene, Aug 15, 2012.

Thread Status:
Not open for further replies.
  1. Offline



    I've been trying to make a small plugin that when the player types in a command, the plugin takes the block they are looking at, spawns a fireball in the air around 10 blocks away, and my plugin tries to calculate a path that the fireball must take, in a straight line, to get to the player's point.

    // Target coords
        final double x0 = targetLoc.getX();
        final double y0 = targetLoc.getY();
        final double z0 = targetLoc.getZ();
        // Spawn coords, calculated by another function
        final double x1 = spawnLoc.getX();
        final double y1 = spawnLoc.getY();
        final double z1 = spawnLoc.getZ();
        //Velocity, to ensure fireball will reach in ~10 seconds
        final double vx = (x1-x0)/10;
        final double vy = (y1-y0)/10;
        final double vz = (z1-z0)/10;
        // Vector direcÅĢia de meteori este egal cu = spawnX - targetX, etc
        CraftWorld cWorld = (CraftWorld) targetLoc.getWorld();
        EntityFireball eMeteor = new EntityFireball(cWorld.getHandle());
        cWorld.getHandle().addEntity(eMeteor, SpawnReason.NATURAL);
        eMeteor.teleport(new Location(cWorld, x1, y1, z1));
        Vector direction = new Vector(x1 - x0, y1 - y0, z1 - z0);
        Vector velocity = new Vector(vx, vy, vz);
        System.out.println("\n----\n" + "Spawned fireball: " + eMeteor + "\n" + "Target coords: "
            + x0 + ": " + y0 + ": " + z0
            + "\n" + "Spawn coords: " + x1 + ": " + y1 + ": " + z1 + "\n"
            + "Velocity vector: " + velocity + "\n" + "Direction vector: "
            + direction + "\n" + "World: " + cWorld.getName() + "\n----");
    The println returns:

    Spawned fireball: EntityFireball['unknown'/1196, l='world', x=-339.00, y=168.00, z=245.00]
    Target coords: -352.0: 61.0: 242.0
    Spawn coords: -339.0: 168.0: 245.0
    Velocity vector: 1.3,10.7,0.3
    Direction vector: 13.0,107.0,3.0
    World: world
    Which has one problem with it: the formula for calculating a vector given the target location and the spawn location is:

    dirX = spawnX - targetX
    dirY = spawnY - targetY
    dirZ = spawnZ - targetZp
     Vector direction = new Vector(x1 - x0, y1 - y0, z1 - z0); 
    should do just that. However, (-352)-(-339)* does NOT equal 13.0, as the log states, but rather equals -13.0. This throws the trajectory off completely, leaving the fireball to perpetually float, and never even come close to the target.

    Anyone have any ideas as to what I'm doing wrong?

    *x1 - x0

    UPDATE: I have added the following code into a custom EntityFireball's h_() method (the method called when it moves):

    System.out.println("Moving vector: " + fireball.getDirection()
            + "\nVelocity vector: " + fireball.getVelocity()
            + "\nLocation: " + fireball.getLocation() + "\nID: "
            + fireball.getEntityId());
    And it never gets called, leading to the suspicion that it never moves (yes, I did initialize the custom fireball properly, added @Override annotations etc. Its constructor is still called by me and outputs a message I put there)
  2. Offline


    Fireball arrow = YOURPLAYER.launchProjectile(Fireball.class);
              Vector velo = arrow.getVelocity().multiply(2);
    i called the fireball arrow lol ^^
    but this works, you might need to tinker a bit with the Velocity, i got it multiplied by 2 which makes it 2 x faster
    but this is basicly all you need
  3. Offline


    You are probably the only 2 who can answer this... I've been trying to work with icy for like 2 hours...

    hes trying to fire a fireball from a point in the sky to a point on the ground... hes making meteors
  4. Offline


    Thanks, but the velocity setting already works, and I'm not trying to make the player launch the projectile: I want it to come from the sky. Think of it like a /meteor command, that meteors the block you look at.

    I've already finished the 'meteor': a fireball that has an exploding trail behind it and has an explosion power of 50, but this is the last part needed.

    Thanks anyway!
  5. Offline


    ahhh sorry xd
  6. Offline


    The spawn location is -339 but you seem to think its -352.
  7. Offline


    Ah, stupid, stupid me. Point taken: now the vector adds up, but its still not ever reaching the ground...
  8. Offline


    Do you really need Minecraft code there ? I remember spawning directional fireballs with bukkit API just fine.

    Anyway, I belive you can't see fireballs too far away... you should make it slow and big (multiple fireballs in a sphere formation).

    Here's how an older plugin did it: line 94
    Pretty simple stuff.
  9. Offline


    Digi Yes but that's a fireball ;) My entity has some major things different from a fireball: on h_() it creates an explosion (makes more visible), it has a hook for c(), to give it a larger brightness, as well as a hook for a(), to make it explode in a giant explosion. I redid all the Bukkit API hooks and added them to my EntityMeteor, so I have all to Bukkit API functions, but from what I have seen there is no "setTarget(Location)" or anything close to that. I have looked at NeoMeteor's code before, but it does not support targetting. The newer version, NeoMeteorites, does, but I quite honestly have no idea how it works, and I wished to try to figure it out myself :)

    Thanks for the help!

    So basically a solution that works with a normal Bukkit fireball would also be fine :D

    Ahaha. Got why it failed to work, and it wasn't because of vectors. To generate its target block, I was looking through loaded chunks and selecting a block on the surface at random. To generate the spawn block, I was changing the XZ coords by random, up to 300 blocks difference. It is very likely that that location would put it in an unloaded chunk, and MC would refuse to update its position.

    Update: Now its about vectors again. After loading the chunk and waiting for a meteor to spawn at the set coordinates, I noticed something that most definitely should NOT be happening: the meteors, instead of falling down, fly up.

    The start position is 5, 160, 5, its target position is 60, 50, 60, and it ends up at -37, 256, -38, and stays there because it has no other place to go.

    Anyone have any idea what is wrong with my vectors?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 27, 2016
  10. Offline


    You want to be calculating the vectors with the projectile's position at 0,0,0 because then you set its direction to the relative position of your target. To do that, you need to reverse all of your calculations. You should have x0-x1 and so on.
    Icyene likes this.
  11. Offline


    Thanks! You beat me to posting it :) I had figured out my mistake, posted, then saw that you had posted before me :p Thanks to everyone to helped! By the way, this is what became of it. Yes, that is a meteor hole, not a ravine.
Thread Status:
Not open for further replies.

Share This Page