Resource Trigonometry in minecraft

Discussion in 'Spigot Plugin Development' started by RestrictedPower, Oct 28, 2016.

Thread Status:
Not open for further replies.
  1. Hey guys today I will explain some things you can do in bukkit/spigot coding using trigonometry. You can use them to easily make particle animations etc.
    First of all lets make clear something about the yaw which is the direction a player is looking at . In Minecraft yaw is not actually a 360°.When it gets to 180° it starts counting down from -180° to 0 so here is a method that makes the minecraft's yaw system to a full 360°
    Code (Text):
        public double fixyaw(float yaw) {
            if (yaw < 0)
                return Math.abs(yaw);
            return yaw + 180;
        }
    Location cords without Y : [Z,X]
    Axis.png
    The Cartesian axis system is a bit different from the minecraft's one (because minecraft is 3D). Instead of X axis there is Z and instead of Y axis there is X.
    Screenshot_2.png
    Okay, let's say we want to get the location of the D point.
    We know the a angle "a" and we know the radius of the cycle (which is the hypotenuse).
    We don't know the opposite(which is the displacement that has to happen in the Z axis) and the adjacent(which is the displacement has to happen in the X axis) but we can find them :D.
    From the trigonometry we know that :
    sin(a) = opposite/hypotenuse => opposite = sin(a) * hypotenuse
    cos(a) = adjacent/hypotenuse => adjacent= cos(a) * hypotenuse
    so the location of the D point is (adjacent, opposite) = (cos(a) * hypotenuse, sin(a) * hypotenuse)
    Lets get into code..
    Code (Text):
    public class BetterLocation extends Location{
        World world;
        double x;
        double y;
        double z;
        float yaw;
        float pitch;
        public double fixyaw(float yaw){
            double y = yaw;
            if(y<0){
                return Math.abs(y);
            }else{
                return y+180;
            }
        }
        public BetterLocation(World world, double x, double y, double z,float yaw,float pitch,double distance) {
            super(world, x, y, z);
            this.world = world;
            this.y = y;
            this.x = x+Math.sin(Math.toRadians(fixyaw(yaw)))*distance;
            this.z = z+Math.cos(Math.toRadians(fixyaw(yaw)))*distance;
            this.yaw = yaw;
            this.pitch = pitch;
        }
        public Location getLocation(){
            return new Location(world, x, y, z,yaw,pitch);
        }
    }
    (The Math class uses Radians to find the cos and the sin . So we use the method Math.toRadians() to transform from degrees to Radians.)
    So lets explain the parameters of the BetterLocation constructor.
    The world, x, y, z, yaw, pitch are the parameters from the Location class.
    The distance ,describes the HYPOTENUSE of the cycle and it is actually the distance between the location and the point we are searching for.
    Here is an example of the usage of this method
    Code (Text):
    public class Main extends JavaPlugin implements Listener {
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }

        @EventHandler
        public void onCommand(PlayerCommandPreprocessEvent e) {
            Player player = e.getPlayer();
            if (e.getMessage().equalsIgnoreCase("/test")) {
                World world = player.getWorld();
                double x = player.getLocation().getX();
                double y = player.getLocation().getY();
                double z = player.getLocation().getZ();
                float yaw = player.getLocation().getYaw();
                float pitch = player.getLocation().getPitch();
                for (double i = 0; i < 10; i=i+0.5) {
                    BetterLocation bl = new BetterLocation(world, x, y-2, z, yaw, pitch, i);
                    world.playEffect(bl.getLocation(), Effect.HEART, 100);
                }
            }
        }
    }
    By using this class with the BetterLocation class we can make something like this:
    Screenshot_4.png

    So that's it! I hope u guys understood the logic of trigonometry and feel free to ask any questions! I apologise for being boring or not understandable... I tried to make it as clear as possible but it is hard to explain :p
     
    • Useful Useful x 3
    • Like Like x 2
  2. Your BetterLocation class unneccessarily redefines world, x, y, z, yaw and pitch as you're extend the Location class which already saves all those things.
    Moreover this turns into errors because Location is mutable and the mentioned variables can be changed in the Location class through the functions of the Location class, but they're not reflected into your BetterLocation class.

    Please learn a bit more about OOP.

    Also the purpose of the BetterLocation class is useless, because the only defined function inside the class does not depend on anything else in that class. So you should change the fixyaw method to be static and be in a static helper class named for example "LocationUtils", which does not extend any class.
     
    #2 Janmm14, Oct 28, 2016
    Last edited: Oct 28, 2016
    • Winner Winner x 1
  3. Okay I guess...
     
  4. Nice explaination and very interesting. Good job.
     
    • Friendly Friendly x 1
  5. Thank you :D
     
Thread Status:
Not open for further replies.