"Forcefield" math

Discussion in 'Plugin Development' started by MCMastery, Aug 18, 2016.

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

    MCMastery

    Hi. I am making an ability, where if you jump in the air and hit the ground with an axe, living entities nearby will be pushed back. However, currently the entities' velocities are quite unpredictable, and it has something to do with my math. Can someone help me find a better way to do this on the math side of things?
    Code:
    if (evt.getAction() == Action.LEFT_CLICK_BLOCK && evt.getPlayer().getVelocity().getY() < -0.2 && ItemUtils.isAxe(evt.getItem())) {
                evt.setCancelled(true);
                Location location = evt.getClickedBlock().getLocation();
    
                // particle effect
                location.getWorld().spawnParticle(Particle.EXPLOSION_HUGE, location.add(0, 1, 0), 0);
                location.getWorld().playEffect(location.add(0, 1, 0), Effect.ZOMBIE_CHEW_WOODEN_DOOR, 0);
    
                double range = 25, power = 5;
                double addY = 2; // makes it so the entities go flying!
                double powerMultiplier = Math.sqrt(-evt.getPlayer().getVelocity().getY());
                for (Entity entity : location.getWorld().getNearbyEntities(location, range, range, range)) {
                    if (entity instanceof LivingEntity && !entity.getUniqueId().equals(evt.getPlayer().getUniqueId())) {
                        double dist = entity.getLocation().distance(location);
                        entity.setVelocity(entity.getLocation().toVector().subtract(location.toVector()).normalize()
                                .multiply(power)
                                .add(new Vector(0, addY, 0))
                                .multiply(powerMultiplier)
                                .multiply(1 / Math.sqrt(dist)));
                    }
                }
            }
    (this is in a PlayerInteractEvent)

    The knockback should increase with how fast you are falling down.

    thanks!
     
  2. Offline

    Zombie_Striker

    Use this instead:
    Code:
    Vector direction = location.subtract(entity.getLocation()).normalize();
    //I think the way you had it made sure the entity would fly TOWARDS the player. If I wrong, switch them again.
    direction.add(0,addY,0);//This will make sure the player will always be thrown upwards.
    direction.multiply(power).multiply(powerMultiplier).multiply(1 / Math.sqrt(dist))); //This is a lot of multiplication.
    
    entity.setVelocity(direction);
     
  3. Offline

    MCMastery

    now the entities always go in the same direction, and are only knocked back a tiny bit (also they don't go into the air)
     
  4. Offline

    Zombie_Striker

    @MCMastery
    Do you know which direction it is? Is it always +x +z? Can you post your updated code?
     
  5. Offline

    MCMastery

    Code:
                        double dist = entity.getLocation().distance(location);
                        Vector direction = location.subtract(entity.getLocation()).toVector().normalize();
                        //I think the way you had it made sure the entity would fly TOWARDS the player. If I wrong, switch them again.
                        direction.add(new Vector(0, addY, 0));//This will make sure the player will always be thrown upwards.
                        direction.multiply(power).multiply(powerMultiplier).multiply(1 / Math.sqrt(dist)); //This is a lot of multiplication.
    
                        entity.setVelocity(direction);
     
  6. Offline

    kameronn

    whats the problem? that looks like it should work
     
Thread Status:
Not open for further replies.

Share This Page