// The Constructor (called when the object is first created) Particle(Vector3D a, Vector3D v, Vector3D l, float r_) { acc = a.copy(); vel = v.copy(); loc = l.copy(); r = r_; timer = 100.0f; }
// 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.setXY(r * sin(theta), r * cos(theta)); // Polar to cartesian conversion loc.add(origin); // Make sure the location is relative to the pendulum's origin }
void wander() { float wanderR = 10.0f; // radius for our "wander circle" float wanderD = 20.0f; // distance for our "wander circle" wandertheta += random(-1, 1); // randomly changet wander theta // now we have to calculate the new location to steer towards on the wander circle Vector3D v = vel.copy(); v.normalize(); // our heading float xoff = wanderD * v.x() + wanderR * cos(wandertheta); // x spot on circle based on heading float yoff = wanderD * v.y() + wanderR * sin(wandertheta); // y spot on circle based on heading Vector3D target = Vector3D.add(loc, new Vector3D(xoff, yoff)); // add the location acc.add(steer(target, false)); // steer towards it }
// look, we can have more than one constructor! Particle(Vector3D l) { acc = new Vector3D(0, 0.05f, 0); // ooooh, bad random, random bad vel = new Vector3D(random(-1, 1), random(-2, 0), 0); loc = l.copy(); r = 10.0f; timer = 100.0f; }
public void drag() { // If we are draging the ball, we calculate the angle between the // pendulum origin and mouse location // we assign that angle to the pendulum if (dragging) { Vector3D diff = Vector3D.sub(origin, new Vector3D(mouseX, mouseY)); // Difference between 2 points theta = atan2(-1 * diff.y, diff.x) - radians(90); // Angle relative to vertical axis } }
// a method that calculates a steering vector towards a target // takes a second argument, if true, it slows down as it approaches the target Vector3D steer(Vector3D target, boolean slowdown) { Vector3D steer; // the steering vector Vector3D desired = Vector3D.sub(target, loc); // a vector pointing from the location to the target float d = desired.magnitude(); // distance from the target is the magnitude of the vector // if the distance is greater than 0, calc steering (otherwise return zero vector) if (d > 0) { // normalize desired desired.normalize(); // two options for magnitude (1 -- based on distance, 2 -- maxspeed) if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed * (d / 100.0f)); else desired.mult(maxspeed); // STEERING VECTOR IS DESIREDVECTOR MINUS VELOCITY steer = Vector3D.sub(desired, vel); steer.limit(maxforce); // limit to maximum steering force } else { steer = new Vector3D(0, 0); } return steer; }
// This constructor could be improved to allow a greater variety of pendulums Pendul(Vector3D origin_, float r_) { // Fill all variables origin = origin_.copy(); r = r_; theta = 0.0f; // calculate the location of the ball using polar to cartesian conversion float x = r * sin(theta); float y = r * cos(theta); loc = new Vector3D(origin.x + x, origin.y + y); theta_vel = 0.0f; theta_acc = 0.0f; damping = 0.995f; // Arbitrary damping ballr = 16.0f; // Arbitrary ball radius }
private void renderOneside(Point2D a, Point2D b, GL gl, double d) { gl.glClear(0); Vector3D t = new Vector3D(b.x - a.x, 0, b.y - a.y); Vector3D n = new Vector3D(0, 1, 0); Vector3D cross = n.cross(t); gl.glNormal3d(cross.x, cross.y, cross.z); // Texture adjustment vars double length = scale * Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)) / 100 + 0.1; double height = scale; // draw the 4 points of it gl.glBegin(GL.GL_POLYGON); gl.glTexCoord2d(0, 0); gl.glVertex3d(a.x, d, a.y); gl.glTexCoord2d(0, height); gl.glVertex3d(a.x, d + 100, a.y); gl.glTexCoord2d(length, height); gl.glVertex3d(b.x, d + 100, b.y); gl.glTexCoord2d(length, 0); gl.glVertex3d(b.x, d, b.y); gl.glEnd(); }
// function to display void render() { ellipseMode(CENTER); noStroke(); fill(0, 100, 255, timer); ellipse(loc.x(), loc.y(), r, r); }
// function to update location void update() { vel.add(acc); loc.add(vel); timer -= 1.0f; }
public void jump(double x, double y) { loc.x = x; loc.y = y; }
public void updateWorld(long elapsedTime) { float angleVelocity; Player player = (Player) gameObjectManager.getPlayer(); MovingTransform3D playerTransform = player.getTransform(); Vector3D velocity = playerTransform.getVelocity(); // playerTransform.stop(); velocity.x = 0; velocity.z = 0; float x = -playerTransform.getSinAngleY(); float z = -playerTransform.getCosAngleY(); if (goForward.isPressed()) { velocity.add(x * PLAYER_SPEED, 0, z * PLAYER_SPEED); } if (goBackward.isPressed()) { velocity.add(-x * PLAYER_SPEED, 0, -z * PLAYER_SPEED); } if (goLeft.isPressed()) { velocity.add(z * PLAYER_SPEED, 0, -x * PLAYER_SPEED); } if (goRight.isPressed()) { velocity.add(-z * PLAYER_SPEED, 0, x * PLAYER_SPEED); } if (jump.isPressed()) { player.setJumping(true); } if (fire.isPressed()) { player.fireProjectile(); } playerTransform.setVelocity(velocity); // look up/down (rotate around x) angleVelocity = Math.min(tiltUp.getAmount(), 200); angleVelocity += Math.max(-tiltDown.getAmount(), -200); playerTransform.setAngleVelocityX(angleVelocity * PLAYER_TURN_SPEED / 200); // turn (rotate around y) angleVelocity = Math.min(turnLeft.getAmount(), 200); angleVelocity += Math.max(-turnRight.getAmount(), -200); playerTransform.setAngleVelocityY(angleVelocity * PLAYER_TURN_SPEED / 200); // update objects gameObjectManager.update(elapsedTime); // limit look up/down float angleX = playerTransform.getAngleX(); float limit = (float) Math.PI / 2; if (angleX < -limit) { playerTransform.setAngleX(-limit); } else if (angleX > limit) { playerTransform.setAngleX(limit); } // set the camera to be 100 units above the player Transform3D camera = polygonRenderer.getCamera(); camera.setTo(playerTransform); camera.getLocation().add(0, CAMERA_HEIGHT, 0); }