use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories by Hugobros3.
the class VoxelOverlays method drawSelectionBox.
public void drawSelectionBox(RenderingInterface renderingInterface, Location location) {
if (vertexBuffer == null)
vertexBuffer = renderingInterface.newVertexBuffer();
int x = (int) (double) location.x();
int y = (int) (double) location.y();
int z = (int) (double) location.z();
CellData peek = location.getWorld().peekSafely(x, y, z);
if (peek.getVoxel() == null || peek.getVoxel().isAir())
return;
CollisionBox[] boxes = peek.getTranslatedCollisionBoxes();
// TODO: getTranslatedCollisionBoxes(voxelContext)
if (boxes == null)
return;
renderingInterface.setCullingMode(CullingMode.DISABLED);
renderingInterface.setBlendMode(BlendMode.MIX);
renderingInterface.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
// ShadersLibrary.getShaderProgram("overlay");
Shader overlayProgram = renderingInterface.useShader("overlay");
renderingInterface.getCamera().setupShader(overlayProgram);
overlayProgram.setUniform4f("colorIn", new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
ByteBuffer data = ByteBuffer.allocateDirect(4 * 3 * 8 * 3 * boxes.length);
data.order(ByteOrder.nativeOrder());
// FloatBuffer fb = data.asFloatBuffer();
// fb.order(ByteOrder.nativeOrder());
// float[] data = new float[3 * 8 * 6 * boxes.length];
int i = 0;
for (CollisionBox box : boxes) {
i = cubeVertices(data, i, (float) box.xpos, (float) box.ypos, (float) box.zpos, (float) box.xw, (float) box.h, (float) box.zw);
}
data.flip();
vertexBuffer.uploadData(data);
renderingInterface.bindAttribute("vertexIn", vertexBuffer.asAttributeSource(VertexFormat.FLOAT, 3));
// renderingContext.bindAttribute("vertexIn", new FloatBufferAttributeSource(data, 3));
renderingInterface.draw(Primitive.LINE, 0, boxes.length * 3 * 8);
renderingInterface.setBlendMode(BlendMode.DISABLED);
// System.out.println("k");
/*
glColor4f(1, 1, 1, 1.0f);
//GL11.glBlendFunc(GL11.GL_ONE_MINUS_SRC_COLOR, GL11.GL_ZERO);
//GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
//GL11.glBlendEquation(GL11.);
glBegin(GL_LINES);
VoxelContext bri = new VoxelContextOlder(world, x, y, z);
if (bri.getVoxel() == null)
{
System.out.println(bri.getData());
return;
}
for (CollisionBox box : bri.getVoxel().getTranslatedCollisionBoxes(world, x, y, z))
cubeVertices((float) box.xpos, (float) box.ypos, (float) box.zpos, (float) box.xw, (float) box.h, (float) box.zw);
glEnd();
glColor4f(1, 1, 1, 1);*/
}
use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories by Hugobros3.
the class DefaultWorldCollisionsManager method raytraceSolid.
private Location raytraceSolid(Vector3dc initialPosition, Vector3dc directionIn, double limit, boolean outer, boolean selectable) {
Vector3d direction = new Vector3d();
directionIn.normalize(direction);
// direction.scale(0.02);
// float distance = 0f;
CellData cell;
// Voxel vox;
int x, y, z;
x = (int) Math.floor(initialPosition.x());
y = (int) Math.floor(initialPosition.y());
z = (int) Math.floor(initialPosition.z());
// DDA algorithm
// It requires double arrays because it works using loops over each dimension
double[] rayOrigin = new double[3];
double[] rayDirection = new double[3];
rayOrigin[0] = initialPosition.x();
rayOrigin[1] = initialPosition.y();
rayOrigin[2] = initialPosition.z();
rayDirection[0] = direction.x();
rayDirection[1] = direction.y();
rayDirection[2] = direction.z();
int[] voxelCoords = new int[] { x, y, z };
int[] voxelDelta = new int[] { 0, 0, 0 };
double[] deltaDist = new double[3];
double[] next = new double[3];
int[] step = new int[3];
int side = 0;
// Prepare distances
for (int i = 0; i < 3; ++i) {
double deltaX = rayDirection[0] / rayDirection[i];
double deltaY = rayDirection[1] / rayDirection[i];
double deltaZ = rayDirection[2] / rayDirection[i];
deltaDist[i] = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
if (rayDirection[i] < 0.f) {
step[i] = -1;
next[i] = (rayOrigin[i] - voxelCoords[i]) * deltaDist[i];
} else {
step[i] = 1;
next[i] = (voxelCoords[i] + 1.f - rayOrigin[i]) * deltaDist[i];
}
}
do {
// DDA steps
side = 0;
for (int i = 1; i < 3; ++i) {
if (next[side] > next[i]) {
side = i;
}
}
next[side] += deltaDist[side];
voxelCoords[side] += step[side];
voxelDelta[side] += step[side];
x = voxelCoords[0];
y = voxelCoords[1];
z = voxelCoords[2];
cell = world.peekSafely(x, y, z);
if (cell.getVoxel().getDefinition().isSolid() || (selectable && cell.getVoxel().getDefinition().isSelectable())) {
boolean collides = false;
for (CollisionBox box : cell.getTranslatedCollisionBoxes()) {
// System.out.println(box);
Vector3dc collisionPoint = box.lineIntersection(initialPosition, direction);
if (collisionPoint != null) {
collides = true;
// System.out.println("collides @ "+collisionPoint);
}
}
if (collides) {
if (!outer)
return new Location(world, x, y, z);
else {
// Back off a bit
switch(side) {
case 0:
x -= step[side];
break;
case 1:
y -= step[side];
break;
case 2:
z -= step[side];
break;
}
return new Location(world, x, y, z);
}
}
}
// distance += deltaDist[side];
} while (voxelDelta[0] * voxelDelta[0] + voxelDelta[1] * voxelDelta[1] + voxelDelta[2] * voxelDelta[2] < limit * limit);
return null;
}
use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories-core by Hugobros3.
the class VoxelFence method getCollisionBoxes.
@Override
public CollisionBox[] getCollisionBoxes(CellData info) {
// System.out.println("kek");
CollisionBox[] boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.3, 0.4, 1.0, 0.4) };
Voxel vox;
vox = info.getNeightborVoxel(0);
boolean connectLeft = (vox.getDefinition().isSolid() && vox.getDefinition().isOpaque()) || vox.equals(this);
vox = info.getNeightborVoxel(1);
boolean connectFront = (vox.getDefinition().isSolid() && vox.getDefinition().isOpaque()) || vox.equals(this);
vox = info.getNeightborVoxel(2);
boolean connectRight = (vox.getDefinition().isSolid() && vox.getDefinition().isOpaque()) || vox.equals(this);
vox = info.getNeightborVoxel(3);
boolean connectBack = (vox.getDefinition().isSolid() && vox.getDefinition().isOpaque()) || vox.equals(this);
if (connectLeft && connectFront && connectRight && connectBack) {
boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.0, 0.4, 1, 1.0), new CollisionBox(0.0, 0.0, 0.3, 1.0, 1, 0.4) };
} else if (connectLeft && connectFront && connectRight)
boxes = new CollisionBox[] { new CollisionBox(0.0, 0.0, 0.3, 1.0, 1, 0.4), new CollisionBox(0.3, 0.0, 0.25, 0.4, 1, 0.5).translate(0, 0, 0.25) };
else if (connectLeft && connectFront && connectBack)
boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.0, 0.4, 1, 1.0), new CollisionBox(0.25, 0.0, 0.3, 0.5, 1, 0.4).translate(-0.25, 0, 0) };
else if (connectLeft && connectBack && connectRight)
boxes = new CollisionBox[] { new CollisionBox(0.0, 0.0, 0.3, 1.0, 1, 0.4), new CollisionBox(0.3, 0.0, 0.25, 0.4, 1, 0.5).translate(0, 0, -0.25) };
else if (connectBack && connectFront && connectRight)
boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.0, 0.4, 1, 1.0), new CollisionBox(0.25, 0.0, 0.3, 0.5, 1, 0.4).translate(0.25, 0, 0) };
else if (connectLeft && connectRight)
boxes = new CollisionBox[] { new CollisionBox(0.0, 0.0, 0.3, 1.0, 1, 0.4) };
else if (connectFront && connectBack)
boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.0, 0.4, 1, 1.0) };
else if (connectLeft && connectBack)
boxes = new CollisionBox[] { new CollisionBox(0.15, 0.0, 0.3, 0.7, 1, 0.4).translate(-0.15, 0, 0), new CollisionBox(0.3, 0.0, 0.15, 0.4, 1, 0.7).translate(0, 0, -0.15) };
else if (connectRight && connectBack)
boxes = new CollisionBox[] { new CollisionBox(0.15, 0.0, 0.3, 0.7, 1, 0.4).translate(0.15, 0, 0), new CollisionBox(0.3, 0.0, 0.15, 0.4, 1, 0.7).translate(0, 0, -0.15) };
else if (connectLeft && connectFront)
boxes = new CollisionBox[] { new CollisionBox(0.15, 0.0, 0.3, 0.7, 1, 0.4).translate(-0.15, 0, 0), new CollisionBox(0.3, 0.0, 0.15, 0.4, 1, 0.7).translate(0, 0, 0.15) };
else if (connectRight && connectFront)
boxes = new CollisionBox[] { new CollisionBox(0.15, 0.0, 0.3, 0.7, 1, 0.4).translate(0.15, 0, 0), new CollisionBox(0.3, 0.0, 0.15, 0.4, 1, 0.70).translate(0, 0, 0.15) };
else if (connectLeft)
boxes = new CollisionBox[] { new CollisionBox(0.15, 0.0, 0.3, 0.7, 1, 0.4).translate(-0.15, 0, 0) };
else if (connectRight)
boxes = new CollisionBox[] { new CollisionBox(0.15, 0.0, 0.3, 0.7, 1, 0.4).translate(0.15, 0, 0) };
else if (connectFront)
boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.15, 0.4, 1, 0.7).translate(0, 0, 0.15) };
else if (connectBack)
boxes = new CollisionBox[] { new CollisionBox(0.3, 0.0, 0.15, 0.4, 1, 0.7).translate(0.0, 0.0, -0.15) };
return boxes;
}
use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories-core by Hugobros3.
the class VoxelStairs method getCollisionBoxes.
@Override
public CollisionBox[] getCollisionBoxes(CellData info) {
int meta = info.getMetaData();
CollisionBox[] boxes = new CollisionBox[2];
// .translate(0.5, -1, 0.5);
boxes[0] = new CollisionBox(1, 0.5, 1);
switch(meta % 4) {
case 0:
boxes[1] = new CollisionBox(0.5, 0.5, 1.0).translate(0.5, -0.0, 0.0);
break;
case 1:
boxes[1] = new CollisionBox(0.5, 0.5, 1.0).translate(0.0, -0.0, 0.0);
break;
case 2:
boxes[1] = new CollisionBox(1.0, 0.5, 0.5).translate(0.0, -0.0, 0.5);
break;
case 3:
boxes[1] = new CollisionBox(1.0, 0.5, 0.5).translate(0.0, -0.0, 0.0);
break;
default:
boxes[1] = new CollisionBox(0.5, 0.5, 1.0).translate(0.5, -0.0, 0.25);
break;
}
if (meta / 4 == 0) {
boxes[0].translate(0.0, 0.0, 0.0);
boxes[1].translate(0.0, 0.5, 0.0);
} else {
boxes[0].translate(0.0, 0.5, 0.0);
boxes[1].translate(0.0, 0.0, 0.0);
}
return boxes;
}
Aggregations