xxxxxxxxxx
62
// Exercise 1.3 from Daniel Shiffman's The Nature of Code
let cam;
let ballPosition;
let ballVelocity;
let bounds;
let topSpeed = 10;
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
ballPosition = createVector(0, 0, 0);
ballVelocity = createVector(1.5, 1.5, 1.5);
bounds = {
x: width / 2,
y: height / 2,
z: height / 2
};
cam = createCamera();
cam.camera(300, -690, 1600, 50, 200, 100);
}
function draw() {
background(30);
orbitControl();
push();
ambientLight(255, 255, 255, 255);
directionalLight(255, 255, 255, 200, 200, 100);
directionalLight(255, 255, 255, 400, 200, 100);
translate(ballPosition.x, ballPosition.y, ballPosition.z);
ballPosition.add(ballVelocity);
ballVelocity.limit(topSpeed);
normalMaterial();
sphere(68);
pop();
strokeWeight(2);
stroke(255);
noFill();
box(bounds.x * 2, bounds.y * 2, bounds.z * 2);
if (ballPosition.x > bounds.x - 78 || ballPosition.x < -bounds.x + 78) {
ballVelocity.x *= -1;
}
if (ballPosition.y > bounds.y - 78 || ballPosition.y < -bounds.y + 78) {
ballVelocity.y *= -1;
}
if (ballPosition.z > bounds.z - 78 || ballPosition.z < -bounds.z + 78) {
ballVelocity.z *= -1;
}
}