# changing a vectors direction to opposite

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

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);
}
}

#1
2. Offline

### andf54

Multiply the velocity by -1 if you donâ€™t want to get deep into physics.

#2
3. Offline

### Forge_User_10514669

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.

#4
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
`vel.setY(vel.getY() * -1D);`

#5
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

#6
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
`private static final double MIN_SPEED = .05D;private static final double BOUNCINESS = .7D; //70% bounciness //you should clear this out periodicallyprivate final HashSet<UUID> projectilesHitEntites = new HashSet<UUID>(); @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)public void onHit(EntityDamageByEntityEvent event){    if(event.getCause() == DamageCause.PROJECTILE)    {        Entity proj = event.getDamager();        projectilesHitEntites.add(proj.getUniqueId());    }} @EventHandlerpublic void onHit(ProjectileHitEvent event){    Projectile proj = event.getEntity();    double speedSq = proj.getVelocity().lengthSquared();    //going too slow to bounce (prevents things from bouncing forever)    if(speedSq < MIN_SPEED)    {        return;    }    LivingEntity shooter = proj.getShooter();    if(shooter instanceof Player)    {        Vector vel = proj.getVelocity();        if(projectilesHitEntites.contains(proj.getUniqueId()))        {            //bounce in all directions            vel.multiply(-BOUNCINESS);        }        else        {            Location from = proj.getLocation();            World world = from.getWorld();            int fromX = from.getBlockX();            int fromY = from.getBlockY();            int fromZ = from.getBlockZ();            from.add(vel);            int toX = from.getBlockX();            int toY = from.getBlockY();            int toZ = from.getBlockZ();            if(fromX != toX && doesCollide(world.getBlockAt(toX, fromY, fromZ).getTypeId()))            {                vel.setX(vel.getX() * -1D);            }            if(fromY != toY && doesCollide(world.getBlockAt(fromX, toY, fromZ).getTypeId()))            {                vel.setY(vel.getY() * -1D);            }            if(fromZ != toZ && doesCollide(world.getBlockAt(fromX, fromY, toZ).getTypeId()))            {                vel.setZ(vel.getZ() * -1D);            }            vel.multiply(BOUNCINESS);        }        Entity newProj = proj.getWorld().spawnEntity(proj.getLocation(), proj.getType());        newProj.setVelocity(vel);    }} private static boolean doesCollide(int typeID){    switch(typeID)    {    case 0: case 8: case 9: case 10: case 11: return false;    default: return true;    }}`

#7