xxxxxxxxxx
191
var q;
var obj;
let rotX, rotY, rotZ, rotZDelta, rotXDelta, rotYDelta;
function setup() {
var canv = createCanvas(windowWidth, windowHeight, WEBGL);
obj = new GameObject(0,0,0);
frameRate(60);
rotZDelta = 0;
rotXDelta = 0;
rotYDelta = 0;
cam = createCamera();
cam.setPosition(0, 0, 0);
setCamera(cam);
setPerspective();
}
function draw() {
stroke(255,255,255);
debugMode(GRID);
//orbitControl();
background(0);
directionalLight(255,255,255,0,-1,-1);
directionalLight(122,122,122,0,1,1);
rotZ = new Quaternion.fromAxisAngle([0,0,1], rotZDelta);
if(keyIsDown(81)){ //q
rotZDelta += 0.001 * deltaTime;
}
if(keyIsDown(69)){ // e
rotZDelta -= 0.001 * deltaTime;
}
//rotXDelta = (mouseY - (height / 2)) * 0.0001;
//rotYDelta = (mouseX - (width / 2)) * 0.0001;
if(keyIsDown(65)){ //a
rotYDelta -= 0.001 * deltaTime;
}
if(keyIsDown(68)){ //d
rotYDelta += 0.001 * deltaTime;
}
if(keyIsDown(87)){ //w
rotXDelta -= 0.001 * deltaTime;
}
if(keyIsDown(83)){ //s
rotXDelta += 0.001 * deltaTime;
}
rotXDelta = constrain(rotXDelta, -0.2, 0.2);
rotYDelta = constrain(rotYDelta, -0.2, 0.2);
rotZDelta = constrain(rotZDelta, -0.2, 0.2);
rotX = new Quaternion.fromAxisAngle([1,0,0], rotXDelta);
rotY = new Quaternion.fromAxisAngle([0,1,0], rotYDelta);
rotZ = new Quaternion.fromAxisAngle([0,0,1], rotZDelta);
var newRot = rotX.mul(rotZ).mul(rotY);
var newRotC = newRot.mul(obj.rotation);
//var newRotC = obj.rotation.mul(newRot);
obj.rotation = newRotC;
if(keyIsDown(SHIFT)){
obj.speed += 0.01 * deltaTime;
}
if(keyIsDown(CONTROL)){
obj.speed -= 0.01 * deltaTime;
}
obj.speed = constrain(obj.speed, 0,3);
obj.draw();
box(1000);
}
class GameObject{
constructor(x,y,z){
this.position = createVector(x,y,z);
this.rotation = new Quaternion.fromAxisAngle([1,0,0], 0);
this.velocity = createVector(0,0,-1);
this.speed = 0;
}
draw(rot){
var velocity = this.rotation.inverse().rotateVector([this.velocity.x,this.velocity.y,this.velocity.z]);
velocity = createVector(velocity[0],velocity[1], velocity[2]);
this.position.add(velocity.copy().mult(this.speed * deltaTime));
// rotate the camera to the new rotation and offset it from player
var newCamRight = this.rotation.clone().conjugate().rotateVector([1, 0, 0]);
var newCamForward = this.rotation.clone().conjugate().rotateVector([0, 0, -1]);
var camRightV = createVector(newCamRight[0], newCamRight[1], newCamRight[2]).normalize();
var camForwardV = createVector(newCamForward[0], newCamForward[1], newCamForward[2]).normalize();
var camUp = camRightV.cross(camForwardV).normalize();
var curPos = this.position.copy();
var posNeg = velocity.copy().mult(10 * this.speed + 150);
var curPosO = curPos.copy().sub(posNeg).add(camUp.copy().mult(-50));
//console.log(curPosO.copy().sub(this.position).toString());
var newCamCenter = curPos.copy().add(camUp.copy().mult(-50));
cam.camera(curPosO.x, curPosO.y, curPosO.z, newCamCenter.x, newCamCenter.y, newCamCenter.z, camUp.x, camUp.y, camUp.z);
push();
translate(this.position.x, this.position.y, this.position.z);
applyMatrix(this.rotation.toMatrix4());
stroke(255,0,255);
strokeWeight(2);
line(0, 0, 0, 10, 10, 0);
stroke(255,255,255);
push();
//box(25,25,80);
rotateX(-HALF_PI);
rotateY(PI);
translate(0, 60,0);
cone(20,250, 5,1);
pop();
push();
rotateX(HALF_PI);
triangle(-120, 50, 0, -200, 120, 50);
rotateY(HALF_PI);
triangle(-55, 50, 0, -50, 0, 50);
pop();
strokeWeight(2);
push();
stroke(255,0,0);
line(0,0,200,0);
pop();
push();
rotateX(HALF_PI);
stroke(0,0,255);
line(0,0,0,-200);
pop();
push();
stroke(0,255,0);
line(0,0,0,-200);
pop();
pop();
}
}
function setPerspective() {
cam.perspective(1.6, width / height, 10, 200000);
}