#cube gravity

1 messages · Page 1 of 1 (latest)

pastel field
#

so i have

@EventHandler
public void onMove(final PlayerMoveEvent event) {
  final UUID playerID = event.getPlayer().getUniqueId();
  final Deque<Location> locations = this.lastLocations.computeIfAbsent(playerID, key -> new ArrayDeque<>());
  if (locations.size() == 2) locations.poll();
  locations.add(event.getTo());
}

public Vector getLastMoveVector(final UUID playerID) {
  final Deque<Location> locations = this.lastLocations.get(playerID);
  if (locations == null) return null;
  final Location last = locations.poll();
  final Location secondLast = locations.poll();
  if (last == null || secondLast == null) return null;
  final Vector vector = last.toVector().subtract(secondLast.toVector());
  vector.setY(vector.getY() / 2);
  locations.add(last);
  locations.add(secondLast);
  return vector;
}

to get vector of the player, then I use length() from that vector to setVelocity to my cube (slime)
it works great but the only issue is that when you are moving the cube forward and since I also have PlayerInteractEntityEvent#onRightClick() method I can give the cube additional Y which raises the cube in the air, the problem is that no matter how much you raise it the cube continues going forward instead of for example hitting the head of a player when falling down and staying behind

#

this is a problem because when another player kicks the cube, no matter how high the player that's in front will just block it even though its ex 5 blocks above him, no need to hit it or anything

#

old method for power was to update a HashMap with the player's uuid and sqrt of PlayerMoveEvent event.getFrom() - event.getTo()

fallow delta
#

well first off, you probably should not use the moveEvent like this

#

PlayerMoveEvent is extremely expensive

#

it gets called even if the player moves his mouse, from a quick look you don't need that functionality so you should definately add a check to see if the player even moved

#

unless im mistaken and you need the Yaw, Pitch updates

#

if thats the case you should still add some kind of rate limiting

pastel field
#

I don't need yaw nor pitch in that event, I'm just getting the last 2 locations

#

I'm acquiring Player#getDirection() later on in the code

fallow delta
#

right. but right now this event will be called on repeat every single movement the player makes, even his yaw or pitch

#

so i suggest you add a check for an actual location change

pastel field
#

I mean can't I just remove that big method and use

public Vector getVelocity(final Player player) {
  return player.getVelocity().setY(player.getVelocity().getY() / 2);
}```
#

this way I don't use the PlayerMoveEvent and only calculate vector's magnitude when needed