Solved Creating a half-circle in the players direction

Discussion in 'Plugin Development' started by mine2012craft, Oct 27, 2016.

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

    mine2012craft

    Hello,

    So what I am currently trying to accomplish is to create a half circle that fires in the players direction that deals damage to anything that touches it. I am able to accomplish the damaging part, thats simple.

    Currently I'm trying to make a half circle that will fire in the players direction. Please keep in mind that I totally suck at math currently so I have been having major problems with altering the math if I need to.

    I haven't been able to find much about this at all, just how to create a circle around the player, and I just reduce the time the circle runs to be a half circle.

    This is my current code. It creates a half circle that always faces south.

    Code:
     new BukkitRunnable(){
                                            double degree = 0;
                                            Location loc = player.getLocation();
                                            Vector direction = loc.getDirection().normalize(); 
    
                                            @Override
                                            public void run() {
                                                degree += 20;
                                                double radian1 = Math.toRadians(degree) ;
                                                 double x = radian1 + Math.toRadians(direction.getX());
                                                 double y = 1;
                                                 double z = radian1 + Math.toRadians(direction.getZ());
    
    
                                                loc.add(Math.cos(x)*2,y,Math.sin(z)*2);
                                                //loc.add(Math.cos(radian1)*2 , 1, Math.sin(radian1)*2);
                                                //loc.add(Math.cos(x)*2, z, Math.sin(y)*2);
                                                ParticleEffect.CRIT.display(0, 0F, 0, 0.1F, 5, loc, 40);
                                                for (Entity e : loc.getChunk().getEntities()){
                                                      if (e.getLocation().distance(loc) < 2){
                                                          if (!e.equals(player)){
                                                          if(e.getType().isAlive()) {
                                                              Damageable d = (Damageable) e;
                                                          d.damage(8, player);   
                                                          }
                                                      }
                                                  }
                                                  }
                                                if (degree == 180){
                                                    this.cancel();
                                                }
                                                loc.subtract(Math.cos(x)*2,y,Math.sin(z)*2);
                                               
                                            }
                                           
                                        }.runTaskTimer(Core.getInstance(), 0, 1);      
    I know I have to add the direction of the player in someway, I'm pretty sure of that, but I cannot figure out how, even though I have tried numerous ways to do so.

    If anyone can give me any pointers that would be greatly appreciated.

    Thank you,
    WarlordWeaponry
     
  2. Offline

    Whoneedspacee

    You want to find the block where your player is looking, then to get the vector you subtract the players vector from the blocks vector to get the vector of the player to the block. Then you can use atan sine and cosine to get the vectors radians, and add the radians inside the cosine and sine functions.
     
  3. Offline

    mine2012craft

    @Whoneedspacee Hmm.... I am a bit confused on what you are suggesting here, so let me see if I can tell you what I think you're trying to tell me and also what I assume and what I'm unsure of:

    Try to get the block the player is looking at (I am assuming using the getDirection() method), then to get the vector I need I need to subtract the players vector (When you say player vector, do you mean location?) from the blocks vector to get the vector. Then I use atan, sine, and cos to get the radians (But where exactly do I use them?) then add the radians inside the cos and sine functions (I assume its this line:
    Code:
    loc.add(Math.cos(x)*2,y,Math.sin(z)*2);
    right?)

    Like I said, I really suck at math, so I don't really know
     
  4. ChipDev likes this.
  5. Offline

    ChipDev

    If you have the code to create a circle, which is 360 degrees (2π radians), you can make a half circle (180 degrees, or π radians). I think you can just multiply it by the players facing direction; try that and send me a screenshot (I don't think it will work; but let's try anyway)
     
  6. Offline

    ShaneCraftDev

    To get the direction the player is looking at, you can use Location#getYaw and mod 360 will return the remaining rotation which can be used as your degrees:
    Code:java
    1.  
    2. double rotation = ((double) target.getLocation().getYaw()) % 360;
    3.  


    I have actually created this a long time ago in a Forge mod to make an entity AI circulate a target, I've managed to get it working in Bukkit. I haven't tested your code, but if the degrees variable actually works for you, you can replace it with the fetched rotation of the targeted player.
     
  7. Offline

    mine2012craft

    @ChipDev I'm pretty sure I've tried that too, but ShaneCraftDev gave me the final piece of the puzzle.

    @ShaneCraftDev Thank you so much man! I'm very grateful for your help! Hopefully I can repay you someday.
     
Thread Status:
Not open for further replies.

Share This Page