changing a vectors direction to opposite

Discussion in 'Plugin Development' started by nitrousspark, Oct 28, 2012.

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

    nitrousspark

    so im trying to make the effect of bouncing a snowball of a wall and im trying to recreate the inverting of the direction of it. heres what i have so far.


    @EventHandler
    public void onTomahawkBounce(ProjectileHitEvent event) {
    if (event.getEntity() instanceof Snowball) {
    Snowball sb = (Snowball) event.getEntity();
    Player player = (Player) sb.getShooter();
    Location loc = sb.getLocation();
    Vector vel = sb.getVelocity();
    Snowball sb2 = (Snowball) player.getWorld().spawnEntity(loc, EntityType.SNOWBALL);
    sb2.setVelocity(vel);
    }
    }
     
  2. Offline

    andf54

    Multiply the velocity by -1 if you don’t want to get deep into physics.
     
  3. vector.multiple(-1)
    I think that should work (if I didn't make an spelling mistake at the signature)
     
  4. Offline

    nitrousspark

    ok that just shot it back from the direction it came. im trying to bounce it like a tomahawk would do.
     
  5. Offline

    Courier

    You need to invert only the component that is perpendicular to the wall it is bouncing off. For example, if it is bouncing off a floor or a ceiling, you need to invert only the y component:
    Code:java
    1. vel.setY(vel.getY() * -1D);
     
  6. Offline

    nitrousspark

    ok so now how do i get what relative direction the player is facing so i cant choose to invert the x or z
     
  7. Offline

    Courier

    The direction the player is facing is irrelevant. You need to determine the direction of the wall it hits. It might have even bounced off an entity. There is not an easy way to determine what it hit or what direction the wall was facing. I wrote this up quickly, it works pretty well. It is a little buggy sometimes though.
    Code:java
    1. private static final double MIN_SPEED = .05D;
    2. private static final double BOUNCINESS = .7D; //70% bounciness
    3.  
    4. //you should clear this out periodically
    5. private final HashSet<UUID> projectilesHitEntites = new HashSet<UUID>();
    6.  
    7. @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
    8. public void onHit(EntityDamageByEntityEvent event)
    9. {
    10. if(event.getCause() == DamageCause.PROJECTILE)
    11. {
    12. Entity proj = event.getDamager();
    13. projectilesHitEntites.add(proj.getUniqueId());
    14. }
    15. }
    16.  
    17. @EventHandler
    18. public void onHit(ProjectileHitEvent event)
    19. {
    20. Projectile proj = event.getEntity();
    21. double speedSq = proj.getVelocity().lengthSquared();
    22. //going too slow to bounce (prevents things from bouncing forever)
    23. if(speedSq < MIN_SPEED)
    24. {
    25. return;
    26. }
    27. LivingEntity shooter = proj.getShooter();
    28. if(shooter instanceof Player)
    29. {
    30. Vector vel = proj.getVelocity();
    31. if(projectilesHitEntites.contains(proj.getUniqueId()))
    32. {
    33. //bounce in all directions
    34. vel.multiply(-BOUNCINESS);
    35. }
    36. else
    37. {
    38. Location from = proj.getLocation();
    39. World world = from.getWorld();
    40. int fromX = from.getBlockX();
    41. int fromY = from.getBlockY();
    42. int fromZ = from.getBlockZ();
    43. from.add(vel);
    44. int toX = from.getBlockX();
    45. int toY = from.getBlockY();
    46. int toZ = from.getBlockZ();
    47. if(fromX != toX && doesCollide(world.getBlockAt(toX, fromY, fromZ).getTypeId()))
    48. {
    49. vel.setX(vel.getX() * -1D);
    50. }
    51. if(fromY != toY && doesCollide(world.getBlockAt(fromX, toY, fromZ).getTypeId()))
    52. {
    53. vel.setY(vel.getY() * -1D);
    54. }
    55. if(fromZ != toZ && doesCollide(world.getBlockAt(fromX, fromY, toZ).getTypeId()))
    56. {
    57. vel.setZ(vel.getZ() * -1D);
    58. }
    59. vel.multiply(BOUNCINESS);
    60. }
    61. Entity newProj = proj.getWorld().spawnEntity(proj.getLocation(), proj.getType());
    62. newProj.setVelocity(vel);
    63. }
    64. }
    65.  
    66. private static boolean doesCollide(int typeID)
    67. {
    68. switch(typeID)
    69. {
    70. case 0: case 8: case 9: case 10: case 11: return false;
    71. default: return true;
    72. }
    73. }
     
Thread Status:
Not open for further replies.

Share This Page