/** * Gets all x,y coordinates, that have the lowest z-value, therefore making an appropriate measure * for a 3D->2D conversion * * @param verticesBuffer the vertices buffer * @return the coordinates */ private List<Vector2f> getCoordinates(FloatBuffer verticesBuffer) { // get Coordinates into an array float[] vertices = new float[verticesBuffer.capacity()]; for (int i = 0; i < verticesBuffer.capacity(); i++) { vertices[i] = verticesBuffer.get(i); } // read closest points based on their z-value // find smallest z-value float z = Float.MAX_VALUE; for (int i = 2; i < vertices.length; i += 3) { if (vertices[i] < z) { z = vertices[i]; } } // add all points with a z-value that are equals Vector2f vertex; List<Vector2f> finalList = new ArrayList<Vector2f>(); for (int i = 2; i < vertices.length; i += 9) { if (vertices[i] == z) { vertex = new Vector2f(); vertex.x = vertices[i - 2]; vertex.y = vertices[i - 1]; finalList.add(vertex); } } return finalList; }
public void update(long dtime) { super.update(dtime); if (this.health == 0 && this.dead == false) { if (this == Gamestate.getInstance().getPlayer()) { this.setController(new NoController()); } SOUNDS.TANK_EXPLODE.play(); Vector3f bpos = new Vector3f(this.getBase().getPos()); Gamestate.getInstance().addObject(new ExplosionCluster(bpos, new Vector3f(0, 1.2f, 0), 20)); this.setController(new NoController()); this.dead = true; this.base.unjoin(this.turret); this.parts.remove(this.turret); } else { lastFired++; Vector2f lookdir = turret.getPhys().getDir(); if (lookdir.y > 25.0f) lookdir.y = 25.0f; if (lookdir.y < -25.0f) lookdir.y = -25.0f; if (lookdir.x > 55.0f) lookdir.x = 55.0f; if (lookdir.x < -55.0f) lookdir.x = -55.0f; turret.getPhys().setDir(lookdir); } }
public void mouseReleased(MouseEvent event) { Vector2f vector = new Vector2f((float) event.getX() - x, (float) event.getY() - y); vector.normalize(); if (Math.abs(event.getX() - x) < 0.0001) { camera_pos = new Vector3f(-4.35f, 4.0f, -35.0f); } else { camera_pos = new Vector3f( (float) (camera_pos.x + vector.x * 4.0), (float) (camera_pos.y + vector.y * 4.0), camera_pos.z); // camera_pos = calculateCordinates(vector); } model.transformPoints(camera_pos); list = model.project(new OrthographicProjection()); repaint(); }
/** @return Vector2f vector holding the starting location for a Person Node */ public Vector2f pStartLocation() { // Try to place the new node in a location that won't disrupt the system too much (by being too // close to another person) for (int i = 0; i < 100; i++) { Vector2f testStart = this.randomLocation(); boolean good = true; for (code_swarm.PersonNode n : code_swarm.getLivingPeople()) { Vector2f delta = new Vector2f(); delta.sub(n.mPosition, testStart); if (delta.lengthSquared() < MIN_DISTANCE_SQR) { good = false; break; } } if (good) return testStart; } return randomLocation(); }
public Vertex(Vertex start, Vertex end, float interp) { Vector3f pos = new Vector3f(); pos.interpolate(start.positionvf(), end.positionvf(), interp); positionvf(pos); Vector4f colour = new Vector4f(); colour.interpolate(start.colourv(), end.colourv(), interp); colourv(colour); Vector3f normal = new Vector3f(); normal.interpolate(start.normalv(), end.normalv(), interp); normalvf(normal); Vector2f tex = new Vector2f(); tex.interpolate(start.texv(), end.texv(), interp); texv(tex); Vector2f light = new Vector2f(); light.interpolate(start.lightv(), end.lightv(), interp); lightv(light); }
public double calcDensity(int x, int y, int z) { double height = calcBaseTerrain(x, z); double ocean = calcOceanTerrain(x, z); double river = calcRiverTerrain(x, z); float temp = biomeProvider.getTemperatureAt(x, z); float humidity = biomeProvider.getHumidityAt(x, z); Vector2f distanceToMountainBiome = new Vector2f(temp - 0.25f, humidity - 0.35f); double mIntens = TeraMath.clamp(1.0 - distanceToMountainBiome.length() * 3.0); double densityMountains = calcMountainDensity(x, y, z) * mIntens; double densityHills = calcHillDensity(x, y, z) * (1.0 - mIntens); int plateauArea = (int) (Chunk.SIZE_Y * 0.10); double flatten = TeraMath.clamp(((Chunk.SIZE_Y - 16) - y) / plateauArea); return -y + (((32.0 + height * 32.0) * TeraMath.clamp(river + 0.25) * TeraMath.clamp(ocean + 0.25)) + densityMountains * 1024.0 + densityHills * 128.0) * flatten; }
// Vector arithmetic // A: current position // B: target position // d: distance to travel along the line from A to B // A_moved = A + |B - A| * d where | | represents 'norm' public void updateLocation() { Vector2f currentLoc = new Vector2f((float) getX2(), (float) getY2()); Vector2f update = new Vector2f(target.x, target.y); update.sub(currentLoc); // B - A update.normalize(); // |B - A| update.scale(travelDistance); // |B - A| x dist currentLoc.add(update); // A + |B - A| x d setLine(x1, y1, currentLoc.x, currentLoc.y); }
/** * Method that allows Physics Engine to modify Speed / Position during the relax phase. * * @param pNode the node to which the force apply @Note Standard physics is "Position Variation = * Speed x Duration" with a convention of "Duration=1" between to frames */ public void onRelax(code_swarm.PersonNode pNode) { Vector2f delta = new Vector2f(); // A gentle force to attract pNodes to the center // NOTE: this should be done prior to attraction/repulsion forces, otherwise // tends to generate a grid-like pattern Vector2f midpoint = new Vector2f(code_swarm.width / 2, code_swarm.height / 2); delta = new Vector2f(); delta.sub(midpoint, pNode.mPosition); delta.scale(1 / delta.length() * 0.003f); pNode.mPosition.add(delta); // All person nodes attract each other, but only to a certain point, then they repel with gentle // force for (code_swarm.PersonNode n : code_swarm.getLivingPeople()) { if (pNode != n) { delta.sub(pNode.mPosition, n.mPosition); if (delta.lengthSquared() < MIN_DISTANCE_SQR) { // This calculation gives a 'stiff spring' affect float toMove = ((float) Math.sqrt(MIN_DISTANCE_SQR) - delta.length()) / 10.0f; // This calculation gives a much nicer flow // float toMove = 0.03f; delta.scale((1 / delta.length()) * toMove); n.mPosition.sub(delta); pNode.mPosition.add(delta); } else { float toMove = -0.003f; delta.scale((1 / delta.length()) * toMove); n.mPosition.sub(delta); pNode.mPosition.add(delta); } } } // place the edited files around the person Iterator<code_swarm.FileNode> editedFiles = pNode.editing.iterator(); int index = 0; int radius = 45; final int node_size = 4; final int salt = pNode.hashCode(); // used to randomize orientation of circle of nodes int num_nodes_in_ring = (int) ((2 * radius * Math.PI) / node_size); while (editedFiles.hasNext()) { // if we've placed all the nodes in this ring... if (index == num_nodes_in_ring) { // start on a new ring radius += node_size; num_nodes_in_ring = (int) ((2 * radius * Math.PI) / node_size); index = 0; } index++; code_swarm.FileNode file = editedFiles.next(); // leave a hole for the null files if (file == null) continue; final int place_around_ring = index * num_nodes_in_ring + salt; int x = (int) (radius * Math.sin(place_around_ring)); int y = (int) (radius * Math.cos(place_around_ring)); delta = new Vector2f(); delta.sub(file.mPosition, new Vector2f(pNode.mPosition.x + x, pNode.mPosition.y + y)); float distance = delta.length(); delta.scale(1 / delta.length() * -0.01f * distance); file.mPosition.add(delta); } }
/* * Takes in a 3D location and spits out its texture coordinate. This version * simply returns 1/2 the x and y coordinate, offset by 0.5 This will ensure * that all texture coordinates are valid, based on assumption about the * incoming 3D location. This is valid because we know this method will only * be called from our spheretri methods. */ private static void xyTex(Vector3f v, Vector2f tex) { tex.x = v.x / 2 + 0.5f; tex.y = v.y / 2 + 0.5f; }
/** * Set the layout of the child elements here. Will be executed if the display or a parent window * was resized. */ public void layout() { // reset to the position and size to the original position position.set(positionOriginal); size.set(sizeOriginal); /* Position */ position.x = calcHorizontalAlign(horizontalAlign); position.y = calcVerticalAlign(verticalAlign); // recalculate position x relative to parent if unit is percentage if (unitPositionX == EUnitType.PERCENTAGE && positionType == EPositionType.RELATIVE && parent != null) { getPosition().x += parent.getSize().x * positionOriginal.x / 100f; } else if (unitPositionX == EUnitType.PERCENTAGE && positionType == EPositionType.ABSOLUTE) { // recalculate position x absolute to display if unit is percentage getPosition().x += Display.getWidth() * positionOriginal.x / 100f; } else { getPosition().x += positionOriginal.x; } // recalculate position x relative to parent if unit is percentage if (unitPositionY == EUnitType.PERCENTAGE && positionType == EPositionType.RELATIVE && parent != null) { getPosition().y += parent.getSize().y * positionOriginal.y / 100f; } else if (unitPositionY == EUnitType.PERCENTAGE && positionType == EPositionType.ABSOLUTE) { // recalculate position x absolute to display if unit is percentage getPosition().y += Display.getHeight() * positionOriginal.y / 100f; } else { getPosition().y += positionOriginal.y; } /* Size */ // recalculate width relative to parent if unit is percentage if (unitSizeX == EUnitType.PERCENTAGE && positionType == EPositionType.RELATIVE && parent != null) { getSize().x = parent.getSize().x * size.x / 100f; } else if (unitSizeX == EUnitType.PERCENTAGE && positionType == EPositionType.ABSOLUTE) { // recalculate width absolute to display if unit is percentage getSize().x = Display.getWidth() * size.x / 100f; } // recalculate height relative to parent if unit is percentage if (unitSizeY == EUnitType.PERCENTAGE && positionType == EPositionType.RELATIVE && parent != null) { getSize().y = parent.getSize().y * size.y / 100f; } else if (unitSizeY == EUnitType.PERCENTAGE && positionType == EPositionType.ABSOLUTE) { // recalculate height absolute to display if unit is percentage getSize().y = Display.getHeight() * size.y / 100f; } }