use of org.joml.Vector3d in project chunkstories-api by Hugobros3.
the class PacketVelocityDelta method process.
@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, PacketProcessingException {
Vector3d delta = new Vector3d(in.readDouble(), in.readDouble(), in.readDouble());
EntityControllable entity = ((ClientPacketsProcessor) processor).getContext().getPlayer().getControlledEntity();
if (entity != null && entity instanceof EntityWithVelocity) {
System.out.println("Debug: received velocity delta " + delta);
((EntityWithVelocity) entity).getVelocityComponent().addVelocity(delta);
}
}
use of org.joml.Vector3d in project chunkstories-api by Hugobros3.
the class Quaternion4d method mult.
public static Quaternion4d mult(Quaternion4d a, Quaternion4d b, Quaternion4d out) {
if (out == null)
out = new Quaternion4d();
// [ Sa.Sb - a.b ,
out.s = a.s * b.s - a.v.dot(b.v);
// Sa.b + Sb.a + a x b ]
Vector3d aBv = new Vector3d(b.v).mul(a.s);
Vector3d vBa = new Vector3d(a.v).mul(b.s);
out.v = aBv.add(vBa).add(a.v.cross(b.v));
return out;
}
use of org.joml.Vector3d in project chunkstories by Hugobros3.
the class DefaultWorldCollisionsManager method runEntityAgainstWorldVoxels.
/**
* Does a complicated check to see how far the entity can go using the delta direction, from the 'start' position.
* Does not actually move anything
* Returns the remaining distance in each dimension if it got stuck ( with vec3(0.0, 0.0, 0.0) meaning it can safely move without colliding with anything )
*/
public Vector3d runEntityAgainstWorldVoxels(Entity entity, Vector3dc from, Vector3dc delta) {
CellData cell;
// Extract the current position
Vector3d pos = new Vector3d(from);
// Keep biggest distanceToTravel for each dimension collisionBox of our entity
Vector3d maxDistanceToTravel = new Vector3d(0.0);
Vector3d direction = new Vector3d(delta);
direction.normalize();
// Iterate over every box
for (int r = 0; r < entity.getCollisionBoxes().length; r++) {
// Make a normalized double vector and keep the original length
Vector3d vec = new Vector3d(delta);
Vector3d distanceToTravel = new Vector3d(delta);
double len = vec.length();
vec.normalize();
vec.mul(0.25d);
// Do it block per block, face per face
double distanceTraveled = 0;
CollisionBox checkerX = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y(), pos.z());
CollisionBox checkerY = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y(), pos.z());
CollisionBox checkerZ = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y(), pos.z());
double stepDistanceX, stepDistanceY, stepDistanceZ;
while (distanceTraveled < len) {
if (len - distanceTraveled > 0.25) {
// DistanceTraveled is incremented no matter what, for momentum loss while sliding on walls
distanceTraveled += 0.25;
} else {
vec = new Vector3d(delta);
vec.normalize();
vec.mul(len - distanceTraveled);
distanceTraveled = len;
}
stepDistanceX = vec.x();
stepDistanceY = vec.y();
stepDistanceZ = vec.z();
// Z part
checkerZ = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y(), pos.z() + stepDistanceZ);
for (int i = (int) Math.floor(pos.x()) - 1; i < (int) Math.ceil(pos.x() + checkerX.xw); i++) for (int j = (int) Math.floor(pos.y()) - 1; j < (int) Math.ceil(pos.y() + checkerX.h); j++) for (int k = (int) Math.floor(pos.z()) - 1; k < (int) Math.ceil(pos.z() + checkerX.zw); k++) {
cell = world.peekSafely(i, j, k);
if (cell.getVoxel().getDefinition().isSolid()) {
CollisionBox[] boxes = cell.getTranslatedCollisionBoxes();
if (boxes != null)
for (CollisionBox box : boxes) {
if (delta.z() != 0.0) {
if (checkerZ.collidesWith(box)) {
stepDistanceZ = 0;
if (delta.z() < 0) {
double south = Math.min((box.zpos + box.zw + checkerZ.zw) - (pos.z()), 0.0d);
stepDistanceZ = south;
} else {
double north = Math.max((box.zpos) - (pos.z() + checkerZ.zw), 0.0d);
stepDistanceZ = north;
}
vec.z = (0d);
checkerZ = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y(), pos.z() + stepDistanceZ);
}
}
}
}
}
distanceToTravel.z = (distanceToTravel.z() - stepDistanceZ);
pos.z = (pos.z() + stepDistanceZ);
// X-part
checkerX = entity.getCollisionBoxes()[r].translate(pos.x() + stepDistanceX, pos.y(), pos.z());
for (int i = (int) Math.floor(pos.x()) - 1; i < (int) Math.ceil(pos.x() + checkerY.xw); i++) for (int j = (int) Math.floor(pos.y()) - 1; j < (int) Math.ceil(pos.y() + checkerY.h); j++) for (int k = (int) Math.floor(pos.z()) - 1; k < (int) Math.ceil(pos.z() + checkerY.zw); k++) {
cell = world.peekSafely(i, j, k);
if (cell.getVoxel().getDefinition().isSolid()) {
CollisionBox[] boxes = cell.getTranslatedCollisionBoxes();
if (boxes != null)
for (CollisionBox box : boxes) {
if (delta.x() != 0.0) {
if (checkerX.collidesWith(box)) {
stepDistanceX = 0;
if (delta.x() < 0) {
double left = Math.min((box.xpos + box.xw + checkerX.xw) - (pos.x()), 0.0d);
// System.out.println("left:"+left);
stepDistanceX = left;
} else {
double right = Math.max((box.xpos) - (pos.x() + checkerX.xw), 0.0d);
// System.out.println("right"+right);
stepDistanceX = right;
}
vec.x = (0d);
checkerX = entity.getCollisionBoxes()[r].translate(pos.x() + stepDistanceX, pos.y(), pos.z());
}
}
}
}
}
pos.x = (pos.x() + stepDistanceX);
distanceToTravel.x = (distanceToTravel.x() - stepDistanceX);
// Y-part
checkerY = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y() + stepDistanceY, pos.z());
for (int i = (int) Math.floor(pos.x()) - 1; i < (int) Math.ceil(pos.x() + checkerZ.xw); i++) for (int j = (int) Math.floor(pos.y()) - 1; j < (int) Math.ceil(pos.y() + checkerZ.h) + 1; j++) for (int k = (int) Math.floor(pos.z()) - 1; k < (int) Math.ceil(pos.z() + checkerZ.zw); k++) {
cell = world.peekSafely(i, j, k);
if (cell.getVoxel().getDefinition().isSolid()) {
CollisionBox[] boxes = cell.getTranslatedCollisionBoxes();
if (boxes != null)
for (CollisionBox box : boxes) {
if (delta.y() != 0.0) {
if (checkerY.collidesWith(box)) {
stepDistanceY = 0;
if (delta.y() < 0) {
double top = Math.min((box.ypos + box.h) - pos.y(), 0.0d);
// System.out.println(top);
stepDistanceY = top;
} else {
double bot = Math.max((box.ypos) - (pos.y() + checkerY.h), 0.0d);
// System.out.println(bot);
stepDistanceY = bot;
}
vec.y = (0d);
checkerY = entity.getCollisionBoxes()[r].translate(pos.x(), pos.y() + stepDistanceY, pos.z());
}
}
}
}
}
pos.y = (pos.y() + stepDistanceY);
distanceToTravel.y = (distanceToTravel.y() - stepDistanceY);
}
if (Math.abs(distanceToTravel.x()) > Math.abs(maxDistanceToTravel.x()))
maxDistanceToTravel.x = (distanceToTravel.x());
if (Math.abs(distanceToTravel.y()) > Math.abs(maxDistanceToTravel.y()))
maxDistanceToTravel.y = (distanceToTravel.y());
if (Math.abs(distanceToTravel.z()) > Math.abs(maxDistanceToTravel.z()))
maxDistanceToTravel.z = (distanceToTravel.z());
}
return maxDistanceToTravel;
}
use of org.joml.Vector3d in project chunkstories by Hugobros3.
the class WorldEntitiesHolder method getEntitiesInBox.
public NearEntitiesIterator getEntitiesInBox(Vector3dc center, Vector3dc boxSize) {
return new NearEntitiesIterator() {
int centerVoxel_x = (int) (double) center.x();
int centerVoxel_y = (int) (double) center.y();
int centerVoxel_z = (int) (double) center.z();
int box_ceil_x = (int) Math.ceil((double) boxSize.x());
int box_ceil_y = (int) Math.ceil((double) boxSize.y());
int box_ceil_z = (int) Math.ceil((double) boxSize.z());
int box_start_x = sanitizeHorizontalCoordinate(centerVoxel_x - box_ceil_x);
int box_start_y = sanitizeVerticalCoordinate(centerVoxel_y - box_ceil_y);
int box_start_z = sanitizeHorizontalCoordinate(centerVoxel_z - box_ceil_z);
int box_end_x = sanitizeHorizontalCoordinate(centerVoxel_x + box_ceil_x);
int box_end_y = sanitizeVerticalCoordinate(centerVoxel_y + box_ceil_y);
int box_end_z = sanitizeHorizontalCoordinate(centerVoxel_z + box_ceil_z);
// We currently sort this out by regions, chunks would be more appropriate ?
int region_start_x = box_start_x / 256;
int region_start_y = box_start_y / 256;
int region_start_z = box_start_z / 256;
int region_end_x = box_end_x / 256;
int region_end_y = box_end_y / 256;
int region_end_z = box_end_z / 256;
int region_x = region_start_x;
int region_y = region_start_y;
int region_z = region_start_z;
Region currentRegion = world.getRegion(region_x, region_y, region_z);
Iterator<Entity> currentRegionIterator = currentRegion == null ? null : currentRegion.getEntitiesWithinRegion();
Entity next = null;
double distance = 0D;
private void seekNextEntity() {
next = null;
while (true) {
// Break the loop if we find an entity in the region
if (seekNextEntityWithinRegion())
break;
else {
// Seek a suitable region if we failed to find anything above
if (seekNextRegion())
continue;
else
break;
}
}
}
private boolean seekNextEntityWithinRegion() {
if (currentRegionIterator == null)
return false;
while (currentRegionIterator.hasNext()) {
Entity entity = currentRegionIterator.next();
// Check if it's inside the box for realz
Location loc = entity.getLocation();
int locx = (int) (double) loc.x();
// Normal case, check if it's in the bounds, wrap-arround case, check if it's outside
if ((box_start_x > box_end_x) == (locx >= box_start_x && locx <= box_end_x))
continue;
int locy = (int) (double) loc.y();
// Normal case, check if it's in the bounds, wrap-arround case, check if it's outside
if ((box_start_y > box_end_y) == (locy >= box_start_y && locy <= box_end_y))
continue;
int locz = (int) (double) loc.z();
// Normal case, check if it's in the bounds, wrap-arround case, check if it's outside
if ((box_start_z > box_end_z) == (locz >= box_start_z && locz <= box_end_z))
continue;
// if(Math.abs(check.getX()) <= boxSize.getX() && Math.abs(check.getY()) <= boxSize.getY() && Math.abs(check.getZ()) <= boxSize.getZ())
{
// Found a good one
this.next = entity;
Vector3d check = new Vector3d(loc);
check.sub(center);
this.distance = check.length();
return true;
}
}
// We found nothing :(
currentRegionIterator = null;
return false;
}
private boolean seekNextRegion() {
currentRegion = null;
while (true) {
// Found one !
if (currentRegion != null) {
currentRegionIterator = currentRegion.getEntitiesWithinRegion();
return true;
}
region_x++;
// Wrap arround in X dimension to Y
if (region_x > region_end_x) {
region_x = 0;
region_y++;
}
// Then Y to Z
if (region_y > region_end_y) {
region_y = 0;
region_z++;
}
// We are done here
if (region_z > region_end_z)
return false;
currentRegion = world.getRegion(region_x, region_y, region_z);
}
}
@Override
public boolean hasNext() {
if (next == null)
seekNextEntity();
return next != null;
}
@Override
public Entity next() {
Entity entity = next;
seekNextEntity();
return entity;
}
@Override
public double distance() {
return distance;
}
};
}
use of org.joml.Vector3d in project chunkstories by Hugobros3.
the class PacketSoundSource method process.
@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, PacketProcessingException {
String soundName = in.readUTF();
long UUID = in.readLong();
boolean hasPosition = in.readBoolean();
Vector3dc position = null;
if (hasPosition) {
position = new Vector3d(in.readFloat(), in.readFloat(), in.readFloat());
}
// boolean loop = in.readBoolean();
// boolean isAmbient = in.readBoolean();
// boolean buffered = in.readBoolean();
byte modeByte = in.readByte();
Mode mode = Mode.values()[modeByte];
boolean stopped = in.readBoolean();
float pitch = in.readFloat();
float gain = in.readFloat();
float attenuationStart = in.readFloat();
float attenuationEnd = in.readFloat();
if (!(processor instanceof ClientPacketsProcessor))
return;
ClientPacketsProcessor cpe = (ClientPacketsProcessor) processor;
SoundSource soundSource = cpe.getContext().getSoundManager().getSoundSourceByUUID(UUID);
// ALSoundSource soundSource = (ALSoundSource) Client.getInstance().getSoundManager().getSoundSourceByUUID(UUID);
if (soundSource == null && stopped)
return;
if (soundSource == null) {
soundSource = cpe.getContext().getSoundManager().replicateServerSoundSource(soundName, mode, position, pitch, gain, attenuationStart, attenuationEnd, UUID);
return;
}
if (stopped) {
soundSource.stop();
return;
}
// Update the soundSource with all we can
soundSource.setPosition(position);
soundSource.setPitch(pitch);
soundSource.setGain(gain);
soundSource.setAttenuationStart(attenuationStart);
soundSource.setAttenuationEnd(attenuationEnd);
}
Aggregations