protected void addNormals() { // Each face has only one normal for (IFace face : faces) { // Calculate face normal, shared amongst all vertices Vector3 a = vertices.get(face.v()[1] - 1).cpy().sub(vertices.get(face.v()[0] - 1)); Vector3 b = vertices.get(face.v()[2] - 1).cpy().sub(vertices.get(face.v()[1] - 1)); normals.add(a.crs(b).nor()); // Add index to face int idx = normals.size(); face.setNormals(idx, idx, idx, idx); } }
/** * Get normal at world coordinates. The methods calculates exact point * position in terrain coordinates and returns normal at that point. If * point doesn't belong to terrain -- it returns default * <code>Vector.Y<code> normal. * * @param worldX * the x coord in world * @param worldZ * the z coord in world * @return normal at that point. If point doesn't belong to terrain -- it * returns default <code>Vector.Y<code> normal. */ public Vector3 getNormalAtWordCoordinate(float worldX, float worldZ) { transform.getTranslation(c00); float terrainX = worldX - c00.x; float terrainZ = worldZ - c00.z; float gridSquareSize = terrainWidth / ((float) vertexResolution - 1); int gridX = (int) Math.floor(terrainX / gridSquareSize); int gridZ = (int) Math.floor(terrainZ / gridSquareSize); if (gridX >= vertexResolution - 1 || gridZ >= vertexResolution - 1 || gridX < 0 || gridZ < 0) { return Vector3.Y.cpy(); } return getNormalAt(gridX, gridZ); }
private void addNormals() { for (IFace face : faces) { // Calculate normals if (hardEdges) { // Calculate face normal, shared amongst all vertices Vector3 a = vertices.get(face.v()[1] - 1).cpy().sub(vertices.get(face.v()[0] - 1)); Vector3 b = vertices.get(face.v()[2] - 1).cpy().sub(vertices.get(face.v()[1] - 1)); normals.add(a.crs(b).nor()); // Add index to face int idx = normals.size(); face.setNormals(idx, idx, idx); } else { // Just add the vertex normal normals.add(vertices.get(face.v()[0] - 1).cpy().nor()); normals.add(vertices.get(face.v()[1] - 1).cpy().nor()); normals.add(vertices.get(face.v()[2] - 1).cpy().nor()); // Add indices to face int idx = normals.size(); face.setNormals(idx - 2, idx - 1, idx); } } }
private void addNormals() { for (IFace face : faces) { // Calculate normals if (hardEdges) { // Calculate face normal, shared amongst all vertices Vector3 a = vertices.get(face.v()[1] - 1).cpy().sub(vertices.get(face.v()[0] - 1)); Vector3 b = vertices.get(face.v()[2] - 1).cpy().sub(vertices.get(face.v()[1] - 1)); normals.add(a.crs(b).nor()); // Add index to face int idx = normals.size(); face.setNormals(idx, idx, idx); } else { // Just add the vertex normal normals.add(vertices.get(face.v()[0] - 1).cpy().nor()); normals.add(vertices.get(face.v()[1] - 1).cpy().nor()); normals.add(vertices.get(face.v()[2] - 1).cpy().nor()); // Add indeces to face int idx = normals.size(); face.setNormals(idx - 2, idx - 1, idx); } } }
@Override Vector3 targetPositionOffset() { return Engine.getDefaultCamera().position.cpy().add(0, Engine.getHeight(), 0); }
@Override Vector3 targetPositionOffset() { return Engine.getDefaultCamera().position.cpy().add(0, -Engine.getHeight(), 0); }
@Override Vector3 targetPositionOffset() { return Engine.getDefaultCamera().position.cpy().add(Engine.getWidth(), 0, 0); }
@Override Vector3 targetPositionOffset() { return Engine.getDefaultCamera().position.cpy().add(-Engine.getWidth(), 0, 0); }
private IntArray detectWrappedUVCoordinates() { IntArray indices = new IntArray(); for (int i = faces.size() - 1; i >= 0; i--) { IFace face = faces.get(i); Vector3 texA = new Vector3(uv.get(face.v()[0] - 1), 0); Vector3 texB = new Vector3(uv.get(face.v()[1] - 1), 0); Vector3 texC = new Vector3(uv.get(face.v()[2] - 1), 0); Vector3 a = texB.cpy().sub(texA); Vector3 b = texC.cpy().sub(texA); Vector3 texNormal = a.crs(b); if (texNormal.z < 0) indices.add(i); } return indices; }
@Override Vector3 orgiPosition() { return Engine.getDefaultCamera().position.cpy().set(Engine.getWidth()/2,Engine.getHeight()/2+Engine.getHeight(),0); }
@Override Vector3 orgiPosition() { return Engine.getDefaultCamera().position.cpy().set(Engine.getWidth()/2,Engine.getHeight()/2-Engine.getHeight(),0); }
@Override Vector3 orgiPosition() { return Engine.getDefaultCamera().position.cpy().set(Engine.getWidth()/2-Engine.getWidth(),Engine.getHeight()/2,0); }
@Override Vector3 orgiPosition() { return Engine.getDefaultCamera().position.cpy().set(Engine.getWidth()/2+Engine.getWidth(),Engine.getHeight()/2,0); }
/** * Get the bounding box of an instance of the block at the specified coordinates * * @param c Chunk the block is in * @param x Chunk-relative x position of the block. 0->(chunkWidth-1) inclusive. * @param y Chunk-relative y position of the block. 0->(chunkHeight-1) inclusive. * @param z Chunk-relative z position of the block. 0->(chunkWidth-1) inclusive. */ public BoundingBox calculateBoundingBox(IChunk c, int x, int y, int z) { Vector3 corner1 = new Vector3(c.getStartPosition().x+x, c.getStartPosition().y+y, c.getStartPosition().z+z); Vector3 corner2 = corner1.cpy().add(1, 1, 1); return new BoundingBox(corner1, corner2); }
@Override public boolean touchDragged(int x, int y, int ptr) { if(buttonHeld) { Log.debug("MouseCameraScroller.touchDragged", "X: " + x + " Y: " + y); Vector3 world0 = new Vector3(screen0); camera.unproject(world0); Vector3 world = new Vector3(x, y, 0); camera.unproject(world); Vector3 delta = world.sub(world0); camera.position.set(cameraPosition0.cpy().sub(delta)); } return buttonHeld; }
public int getBlockInFeet(IWorld world) { int x = MathUtils.floor(getPosition().getX()); int y = MathUtils.floor(getPosition().getY()); int z = MathUtils.floor(getPosition().getZ()); IChunk chunk = world.getChunk(x, z); if (chunk != null && y < world.getHeight()) { int cx = x & (world.getChunkSize() - 1); int cz = z & (world.getChunkSize() - 1); try { int id = chunk.getBlockId(cx, y, cz); Block block = RadixAPI.instance.getBlock(id); if (block == null) return 0; BoundingBox blockBox = block.calculateBoundingBox(chunk, cx, y, cz); float halfWidth = width / 2f; Vector3 bottomBackLeft = getPosition().cpy().add(-halfWidth, 0, -halfWidth); Vector3 bottomBackRight = bottomBackLeft.cpy().add(width, 0, 0); Vector3 bottomFrontRight = bottomBackRight.cpy().add(0, 0, width); Vector3 bottomFrontLeft = bottomBackLeft.cpy().add(width, 0, 0); boolean inFeet = blockBox.contains(bottomBackLeft) || blockBox.contains(bottomBackRight) || blockBox.contains(bottomFrontLeft) || blockBox.contains(bottomFrontRight); return inFeet ? id : 0; } catch (BlockStorage.CoordinatesOutOfBoundsException ex) { ex.printStackTrace(); return 0; } } else { return 0; } }
public boolean checkDeltaCollision(LivingEntity e, float deltaX, float deltaY, float deltaZ) { BoundingBox curBB = e.calculateBoundingBox(); BoundingBox newBB = new BoundingBox(curBB.min.cpy().add(deltaX, deltaY, deltaZ), curBB.max.cpy().add(deltaX, deltaY, deltaZ)); boolean collideSuccess = false; int x = MathUtils.floor(e.getPosition().x); int y = MathUtils.floor(e.getPosition().y); int z = MathUtils.floor(e.getPosition().z); IChunk chunk = game.getWorld().getChunk(x, z); if (chunk == null) return true; int cx = x & (game.getWorld().getChunkSize() - 1); int cz = z & (game.getWorld().getChunkSize() - 1); try { Block block = chunk.getBlock(cx, y, cz); for (Vector3 corner : getCorners(newBB)) { collideSuccess = collideSuccess || checkCollision(corner); } return collideSuccess || (block != null && block.isSolid() && block.calculateBoundingBox(chunk, cx, y, cz).intersects(newBB)); } catch(CoordinatesOutOfBoundsException ex) { ex.printStackTrace(); return true; } }
@Override protected void doTransition(int halfDurationMillis) { this.targetCamera = new C2dCamera(Engine.getWidth(), Engine.getHeight()); final Vector3 positionBak = Engine.getDefaultCamera().position.cpy(); outgoing.hide(); Engine.getDefaultCamera().position.set(orgiPosition().x, orgiPosition().y, positionBak.z); Tween .to(Engine.getDefaultCamera(), C2dCameraAccessor.XY, halfDurationMillis*2).target(positionBak.x,positionBak.y) .setCallback(new TweenCallback() { @Override public void onEvent(int type, BaseTween<?> source) { doSetMainScene(incoming); Gdx.input.setInputProcessor(incoming.getInputProcessor()); incoming.show(); Engine.getDefaultCamera().position.set(positionBak); reset(); } }).start(Engine.getTweenManager()); }
public static Model buildCompassModel() { float compassScale = 5; ModelBuilder modelBuilder = new ModelBuilder(); Model arrow = modelBuilder.createArrow(Vector3.Zero, Vector3.Y.cpy().scl(compassScale), null, VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); modelBuilder.begin(); Mesh zArrow = arrow.meshes.first().copy(false); zArrow.transform(new Matrix4().rotate(Vector3.X, 90)); modelBuilder.part("part1", zArrow, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.BLUE))); modelBuilder.node(); Mesh yArrow = arrow.meshes.first().copy(false); modelBuilder.part("part2", yArrow, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.GREEN))); modelBuilder.node(); Mesh xArrow = arrow.meshes.first().copy(false); xArrow.transform(new Matrix4().rotate(Vector3.Z, -90)); modelBuilder.part("part3", xArrow, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.RED))); arrow.dispose(); return modelBuilder.end(); } }