// 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 }
// function to update location void update() { vel.add(acc); loc.add(vel); timer -= 1.0f; }
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); }