// Method to update location public void update() { // Update velocity vel.add(acc); // Limit speed vel.limit(maxspeed); loc.add(vel); // Reset accelertion to 0 each cycle acc.mult(0); }
@Override public void mouseWheelMoved(MouseWheelEvent e) { if (camDist > 1.0) { camDist += (0.5f * camDist) * zoomFactor * e.getWheelRotation(); PVector newMouse = getNewMouse(); // PVector toAdd = new PVector(newMouse.x - p.width*0.5f,newMouse.y - p.height*0.5f); PVector toAdd = new PVector(newMouse.x, newMouse.y); toAdd.sub(new PVector(camCenter.x, camCenter.y)); toAdd.mult(-0.05f * e.getWheelRotation()); camPos.add(toAdd); // camPos.mult(0.5f); camCenter.add(toAdd); // camCenter.mult(0.5f); } else camDist = 1.01f; }
void setParent(ControllerGroup<?> theParent) { if (_myParent != null && _myParent != this) { _myParent.remove(this); } _myParent = theParent; if (_myParent != this) { _myParent.add(this); } absolutePosition = new PVector(position.x, position.y); absolutePosition.add(_myParent.absolutePosition); positionBuffer = new PVector(position.x, position.y); _myControlWindow = _myParent.getWindow(); for (int i = 0; i < controllers.size(); i++) { if (controllers.get(i) instanceof Controller<?>) { ((Controller<?>) controllers.get(i))._myControlWindow = _myControlWindow; } else { ((ControllerGroup<?>) controllers.get(i))._myControlWindow = _myControlWindow; } } if (_myControlWindow != null) { setMouseOver(false); } }
public void displayByArea(DisplayArea area) { changeMode(); position.add(direction); if (position.z > 300 || position.z < -300) { // ograniczenie poruszania się obszaru wyświetlania direction.mult(-1); } area.moveCenterTo(position); pApplet.stroke(255, 0, 0); pApplet.pushMatrix(); for (int j = 0; j < model.getSegmentCount(); j++) { Segment segment = model.getSegment(j); Face[] faces = segment.getFaces(); pApplet.beginShape(PConstants.QUADS); for (Face face : faces) { if (area.isColliding(face.getCenter())) { PVector[] v = face.getVertices(); PVector[] n = face.getNormals(); for (int k = 0; k < v.length; k++) { pApplet.normal(n[k].x, n[k].y, n[k].z); pApplet.vertex(v[k].x, v[k].y, v[k].z); } } } pApplet.endShape(); } pApplet.popMatrix(); }
// tällä funktiolla on huono nimi ja toteutuskin o ruma. // tämä on se juttu mikä tekee työt endpoints()ille. // jos tilepisteen reunalla toinen tile vain yhdessä suunnassa, // niin piste on yksinään. tähän tarttis jonkun kuvan selostamaan. PVector head(int x, int y, int z) { PVector pp = new PVector(x, y, z); PVector[] dirs = { new PVector(-1, 0, 0), new PVector(1, 0, 0), new PVector(0, -1, 0), new PVector(0, 1, 0), new PVector(0, 0, -1), new PVector(0, 0, 1), }; int blkfound = -1; for (int i = 0; i < dirs.length; i++) { PVector p = dirs[i]; PVector next = PVector.add(pp, p); if (next.x >= 0 && next.x < size && next.y >= 0 && next.y < size && next.z >= 0 && next.z < size) { if (map[at(next)] != 0) { if (blkfound != -1) return null; blkfound = i; } } } if (blkfound != -1) return PVector.sub(pp, dirs[blkfound]); return null; }
/** {@inheritDoc} */ public T updateAbsolutePosition() { absolutePosition.set(position); absolutePosition.add(_myParent.getAbsolutePosition()); for (int i = 0; i < controllers.size(); i++) { controllers.get(i).updateAbsolutePosition(); } return me; }
public void avoid(ArrayList obstacles) { // Make a vector that will be the position of the object // relative to the Boid rotated in the direction of boid's velocity PVector closestRotated = new PVector(sight + 1, sight + 1); float closestDistance = 99999; Obstacle avoid = null; // Let's look at each obstacle for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); float d = PVector.dist(loc, o.loc); PVector dir = vel.get(); dir.normalize(); PVector diff = PVector.sub(o.loc, loc); // Now we use the dot product to rotate the vector that points from boid to obstacle // Velocity is the new x-axis PVector rotated = new PVector(diff.dot(dir), diff.dot(getNormal(dir))); // Is the obstacle in our path? if (PApplet.abs(rotated.y) < (o.radius + r)) { // Is it the closest obstacle? if ((rotated.x > 0) && (rotated.x < closestRotated.x)) { closestRotated = rotated; avoid = o; } } } // Can we actually see the closest one? if (PApplet.abs(closestRotated.x) < sight) { // The desired vector should point away from the obstacle // The closer to the obstacle, the more it should steer PVector desired = new PVector(closestRotated.x, -closestRotated.y * sight / closestRotated.x); desired.normalize(); desired.mult(closestDistance); desired.limit(maxspeed); // Rotate back to the regular coordinate system rotateVector(desired, vel.heading2D()); // Draw some debugging stuff if (debug) { stroke(0); line(loc.x, loc.y, loc.x + desired.x * 10, loc.y + desired.y * 10); avoid.highlight(true); } // Apply Reynolds steering rules desired.sub(vel); desired.limit(maxforce); acc.add(desired); } }
private PVector getLatticePos(String nodeName, String slotName) { PVector nodeLoc = lattice.getLocation(nodeName); Lattice<Void> slotLat = lattice.getContents(nodeName); if (slotLat == null) { log.warn("No slot lattice for node " + nodeName); return new PVector(0, 0, 0); } // Node position within the lattice, natural coordinates float nodeOffset = nodeSize + 2 * nodePadding; PVector nodePos = nodeLoc.get(); nodePos.mult(nodeOffset); nodePos.add(nodePadding, nodePadding, nodePadding); PVector slotLoc = slotLat.getLocation(slotName); if (slotLoc == null) { log.warn("No slot location for slot " + slotName + " in node " + nodeName); return nodePos.get(); } // TODO: cache these calculations // Slot position within the node, natural coordinates float slotOffset = jobSize + 2 * jobPadding; PVector slotPos = slotLoc.get(); slotPos.mult(slotOffset); slotPos.add(jobPadding, jobPadding, jobPadding); // Combine node and slot position, natural coordinates PVector pos = slotPos.get(); pos.add(nodePos); // Convert into central coordinate system float latticeWidth = lattice.size * nodeOffset; float t = latticeWidth / 2; pos.sub(t, t, t); return pos; }
public void update(float db) { // we can use the db value here to change the speed of the particles loc.add(vel.mult(vel, map(db, -100, 100, .05f, 5))); if (loc.x < 0 || loc.x > width) { vel.x *= -1; } if (loc.y < 0 || loc.y > height) { vel.y *= -1; } radius = constrain(db, 2, 100); }
public Box(float width, float height, float depth /* , DrawMode drawMode */) { outlineFrontFace = new ArrayList<PVector>(); outlineBackFace = new ArrayList<PVector>(); pointCloud = new ArrayList<YPoint>(); // Names for vars are Top||Bottom Left||Right || Back||Font PVector tlf = new PVector(); PVector trf = new PVector(); PVector blf = new PVector(); PVector brf = new PVector(); PVector blb = new PVector(); PVector brb = new PVector(); PVector tlb = new PVector(); PVector trb = new PVector(); tlf.add(width / 2, -height / 2, depth / 2); trf.add(-width / 2, -height / 2, depth / 2); blf.add(width / 2, height / 2, depth / 2); brf.add(-width / 2, height / 2, depth / 2); blb.add(width / 2, height / 2, -depth / 2); brb.add(-width / 2, height / 2, -depth / 2); tlb.add(width / 2, -height / 2, -depth / 2); trb.add(-width / 2, -height / 2, -depth / 2); // this.drawMode = drawMode; outlineFrontFace.add(POV.project(tlf)); outlineFrontFace.add(POV.project(trf)); outlineFrontFace.add(POV.project(brf)); outlineFrontFace.add(POV.project(blf)); outlineBackFace.add(POV.project(tlb)); outlineBackFace.add(POV.project(trb)); outlineBackFace.add(POV.project(brb)); outlineBackFace.add(POV.project(blb)); addFaceToPointCloud(outlineFrontFace); addFaceToPointCloud(outlineBackFace); joinFaces(); }
// Function to update location public void update() { // As long as we aren't dragging the pendulum, let it swing! if (!dragging) { float G = 0.4f; // Arbitrary universal gravitational constant theta_acc = (-1 * G / r) * sin( theta); // Calculate acceleration (see: // http://www.myphysicslab.com/pendulum1.html) theta_vel += theta_acc; // Increment velocity theta_vel *= damping; // Arbitrary damping theta += theta_vel; // Increment theta } loc.set(r * sin(theta), r * cos(theta), 0); // Polar to cartesian conversion loc.add(origin); // Make sure the location is relative to the pendulum's origin }
public void draw() { /*---Update---*/ world.update(); worm.update(); for (int i = 0; i < extraNum; i++) { extraWorms[i].update(); } /*---Draw--*/ // Lights lights(); /* ambientLight(128f, 128f, 128f); directionalLight(128f, 128f, 128f, 0f, 0f, -1f); lightFalloff(1f, 0f, 0f); lightSpecular(0f, 0f, 0f); */ // Camera perspective(fov, aspect, near, far); // Calculate camera position if (PVector.dist(worm.getPosition(), eye) > eyeDist) { eye = PVector.sub(eye, worm.getPosition()); eye.normalize(); eye.mult(eyeDist); eye.add(worm.getPosition()); } camera( eye.x, eye.y, eye.z, // eyeX, eyeY, eyeZ (camera position) worm.getPosition().x, worm.getPosition().y, worm.getPosition().z, // centerX, centerY, centerZ (look at) 0f, 1f, 0f); // upX, upY, upZ // Action world.draw(); worm.draw(); for (int i = 0; i < extraNum; i++) { extraWorms[i].draw(); } // Drama! }
private static void findAwayVector( ArrayList<ProximityStructure> mCloseNeighbors, final PVector pForce) { /* find away vector */ if (!mCloseNeighbors.isEmpty()) { pForce.set(0, 0, 0); /** * @todo the vectors could be weighted according to distance: 1.0 - distance ( for example ) */ for (ProximityStructure p : mCloseNeighbors) { pForce.add(p.distanceVec); } pForce.mult(1.0f / mCloseNeighbors.size()); pForce.normalize(); if (isNaN(pForce)) { pForce.set(0, 0, 0); } } else { pForce.set(0, 0, 0); } }
/** * Will calculate the normals for any 2D mesh. <br> * This is a heavy mathematical process so should be overridden in child classes if there is a * suitable alternative method. */ protected void calcNormals() { PVector c, np, nt; int ns, ew; for (ns = 0; ns < nsSteps; ns++) { for (ew = 0; ew < ewSteps /* - 1*/; ew++) { c = coord[ew][ns]; if (ns == nsSteps - 1) { np = PVector.sub(c, coord[ew][ns - 1]); np.add(c); } else np = coord[ew][ns + 1]; if (ew == ewSteps - 1) nt = coord[1][ns]; else nt = coord[ew + 1][ns]; norm[ew][ns] = PVector.cross(PVector.sub(nt, c, null), PVector.sub(np, c, null), null); norm[ew][ns].normalize(); } } }
/** Triangulate the glyph, but first save the original outline. */ public void triangulate() { // Save the outline and calculate normals outline = new Vector<PlanarEdge>(); for (PlanarEdge e : subdivision.getEdges()) { if (e.isRealEdge()) { outline.add(e); } } // Calculate outline normals outline_normals = new PVector[outline.size()]; for (PlanarEdge e : outline) { TriangulationVertex vert = (TriangulationVertex) e.getDestination(); // Normal 1 PVector normal1 = vert.getOutGoingEdge().getDestination().getPoint().get(); normal1.sub(vert.getPoint()); normal1.normalize(); // Vector3 normal1 = new // Vector3(vert.getOutGoingEdge().getDestination().getPoint()).subtractLocal(vert.getPoint()); normal1.z = -normal1.x; normal1.x = normal1.y; normal1.y = normal1.z; normal1.z = 0; // Normal 2 PVector normal2 = vert.getPoint().get(); normal2.sub(vert.getInGoingEdge().getOrigin().getPoint()); normal2.normalize(); // Vector3 normal2 = new // Vector3(vert.getPoint()).subtractLocal(vert.getInGoingEdge().getOrigin().getPoint()); normal2.z = -normal2.x; normal2.x = normal2.y; normal2.y = normal2.z; normal2.z = 0; normal1.add(normal2); normal1.normalize(); outline_normals[vert.getIndex()] = normal1; } // Calculate the triangulation of the surface. surface = subdivision.triangulate(); }
public void setup() { mFont = createFont("ArialRoundedMTBold-36", 48); size(1440, 900, OPENGL); background(0); frameRate(120); // model = new OBJModel(this, "alyson_laugh.obj", "absolute", TRIANGLES); model = new OBJModel(this, "alyson_scared.obj", "absolute", TRIANGLES); // model = new OBJModel(this, "alyson_crying.obj", "absolute", TRIANGLES); smooth(); colorMode(HSB); strokeWeight(4); model.scale(800); model.translateToCenter(); detailValue = 3; scaleValue = 10; vertices = new ArrayList(); ps = new ParticleSystem(this, scaleValue); PVector averagePosition = new PVector(0, 0, 0); for (int i = 0; i < model.getVertexCount(); i += detailValue) { PVector destinationPoint = model.getVertex(i); vertices.add(destinationPoint); averagePosition.add(destinationPoint); } averagePosition.div(vertices.size()); for (int i = 0; i < vertices.size(); i++) { PVector destination = (PVector) vertices.get(i); Particle p = new Particle(this, averagePosition, destination); ps.addParticle(p); } }
/** Initializes the preview applet layout according to the graph's dimension. */ private void initAppletLayout() { // graphSheet.setMargin(MARGIN); if (model != null && model.getDimensions() != null && model.getTopLeftPosition() != null) { // initializes zoom Dimension dimensions = model.getDimensions(); Point topLeftPostition = model.getTopLeftPosition(); PVector box = new PVector((float) dimensions.getWidth(), (float) dimensions.getHeight()); float ratioWidth = width / box.x; float ratioHeight = height / box.y; scaling = ratioWidth < ratioHeight ? ratioWidth : ratioHeight; // initializes move PVector semiBox = PVector.div(box, 2); PVector topLeftVector = new PVector((float) topLeftPostition.x, (float) topLeftPostition.y); PVector center = new PVector(width / 2f, height / 2f); PVector scaledCenter = PVector.add(topLeftVector, semiBox); trans.set(center); trans.sub(scaledCenter); lastMove.set(trans); } }
public void checkThirtyDegreeRule( ArrayList<Cam> cameras, ArrayList<Character> characters, int selectedIdx) { if (cameras == null || cameras.isEmpty()) { println("No cameras in the scene!"); } if (characters.size() != 2) { println("Only two characters supported for now"); // TODO (sanjeet): Hack! Fix this once more characters are allowed } Cam selectedCamera = cameras.get(selectedIdx); // TODO The characters.get results in a runtime error because there aren't currently any // characters allocated in the input file. Character ch1 = characters.get(0); Character ch2 = characters.get(1); // Obtaining (x,y,z) for characters and selected camera PVector ch1Location = ch1.getTranslation(); PVector ch2Location = ch2.getTranslation(); PVector selectedCameraLocation = selectedCamera.getTranslation(); PVector cameraPoint = new PVector(); cameraPoint.add(selectedCameraLocation); for (int i = 0; i < 100; i++) { cameraPoint.add(selectedCamera.getZAxis()); } PVector intersection = getTwoLinesIntersection( new PVector(ch1Location.x, ch1Location.z), new PVector(ch2Location.x, ch2Location.z), new PVector(selectedCameraLocation.x, selectedCameraLocation.z), new PVector(cameraPoint.x, cameraPoint.z)); PVector diff = PVector.sub(selectedCameraLocation, intersection); diff.normalize(); FloatBuffer fb = selectedCamera.modelViewMatrix; float[] mat = fb.array(); float[] fbMatrix = new float[mat.length]; for (int i = 0; i < fbMatrix.length; i++) { fbMatrix[i] = mat[i]; } fbMatrix[0] = -diff.x; fbMatrix[1] = diff.y; fbMatrix[2] = -diff.z; fbMatrix[9] = diff.x; fbMatrix[10] = diff.y; fbMatrix[11] = diff.z; fbMatrix[13] = intersection.x; fbMatrix[14] = intersection.y; fbMatrix[15] = intersection.z; PMatrix3D matrix = new PMatrix3D(); matrix.set(fbMatrix); matrix.transpose(); pushMatrix(); applyMatrix(matrix); rotateY(radians(30)); line(0, 0, 0, 0, 0, 1000); rotateY(radians(-2 * 30)); line(0, 0, 0, 0, 0, 1000); popMatrix(); for (int i = 0; i < cameras.size(); i++) { if (i == selectedIdx) { continue; } if (!cameras.get(i).isInView(ch1Location) && !cameras.get(i).isInView(ch2Location)) { continue; } PVector currCamLocation = cameras.get(i).getTranslation(); PVector vect1 = PVector.sub(currCamLocation, intersection); PVector vect2 = PVector.sub(selectedCameraLocation, intersection); float dotP = vect1.dot(vect2) / (vect1.mag() * vect2.mag()); if (acos(dotP) <= PI / 6) { cameras.get(i).setColor(255, 0, 0); } else { cameras.get(i).setColor(0, 0, 255); } } }
public void update() { velocity.add(acceleration); location.add(velocity); acceleration.mult(0); lifespan -= 8.0; }
public void setAcceleration(PVector acceleration) { this.acceleration = acceleration; velocity.add(acceleration); position.add(velocity); }