How does Vectors work in Minecraft?

Discussion in 'Plugin Development' started by WMisiedjan, Feb 27, 2011.

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

    WMisiedjan

    Hello,

    I've been trying to get Vector's work in Minecraft for like hour's now, And It justs doesn't do what I want and if I change something I just don't know what I exactly did.

    Entity's have Vectors right? It's like the place where they are going and how fast. That's what I understand. But it's different with every YAM position or something like that.

    I want for example a Entity to go to the location a player is looking at. I found somewhere this code that I change a little:
    Code:
    	        if ((yaw <= 38) || (yaw >= 320))
    	        {
    	        	side = yaw/100;
    	        	tnt2.setVelocity(new Vector(0.0D, 0.8D, 0.5D));
    //Angle, if not straight? - Height? - Speed?
    	        }
    	        else if ((yaw >= 216) && (yaw < 320))
    	        {
    	        	side = yaw/100;
    	        	tnt2.setVelocity(new Vector(1.0D, 0.8D, 0.0D));
    //Speed, Height?, Angle
    	        }
    	        else if ((yaw >= 143) && (yaw < 216))
    	        {
    	        	side = yaw/100;
    	        	tnt2.setVelocity(new Vector(0.0D, 0.8D, -1.0D));
    //Angle, Height?, Speed
    	        }
    	        else
    	        {
    	        	side = yaw/100;
    	        	tnt2.setVelocity(new Vector(-0.5D, 0.8D, 0.0D));
    //Speed, Height?, Angle
    	        }
    
    I found out that the second argument is the height. How high it spawn's.
    And the others are different based on the YAW of where the player is looking.

    I hope that someone can explain how those Vector's work in detail.

    Greetings, WMisiedjan
     
  2. Offline

    Edward Hand

    the vectors in this case represent speeds in the X, Y and Z directions
    X= North/South
    Y = Up/Down
    Z = East/West

    It looks like you're trying to convert yaw/pitch/speed into X/Y/Z?
    (This is a conversion from spherical polar coordinates to Cartesian coordinates if you want to look up the maths)

    Use the following formulae:
    Code:
    double yaw = Math.toRadians(player.getLocation().getYaw()+90);
    double pitch = Math.toRadians(-player.getLocation().getPitch());
    
    double x = v*Math.cos(yaw)*Math.cos(pitch)
    double y = v*Math.sin(pitch)
    double z = v*Math.sin(yaw)*Math.cos(pitch)
    
    tnt2.setVelocity(new Vector(x,y,z))
    (where v is the speed you want the tnt to go)
     
  3. Offline

    WMisiedjan

    Wo0w! I would never figure that out by myself! Thanks!
     
  4. Offline

    Edward Hand

    Oh, you'll need to swap y and z around. Sorry.
    I've corrected the code in my last post.
     
  5. Offline

    WMisiedjan

    Hmh, I tried this. But the TNT is still not going to where the player is looking at. I can send you my whole command if you want? And you can try it out on my server.

    Edit: Didn't saw your new code, Checking it out now.
    Still acting weird. Can you take a look?
     
  6. Just use the old 'HitBlox' file from hmod :)
     
  7. Offline

    Raphfrk

    The Location class has a method that returns a direction vector

    Code:
    Location loc = player.getLocation();
    Vector facing = loc.getDirection();
    
    This returns a unit vector pointing in the direction the player is facing. You don't have to do the pitch/yaw calculations manually.
     
  8. Offline

    Edward Hand

    .....

    Mind blown.
     
    Vynlar likes this.
  9. Offline

    khyperia

    Code:
    TNTPrimed tnt = player.getEyeLocation().getWorld().spawn(
                    player.getEyeLocation(), TNTPrimed.class);
            Vector direction = player.getEyeLocation().getDirection().multiply(2.5);
            tnt.setVelocity(direction);
    Code directly from my plugin
     
Thread Status:
Not open for further replies.

Share This Page