test project game

專題

by chaiche

HTML

<div id="info">
123
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>

CSS

#info {
  position: absolute;
  width: 100%;
  color: #ffff00
}

JavaScript

var scene, renderer, camera;
var controls;
var ta = 1;
var canRun = false;
var clock = new THREE.Clock();
var keyboard = new KeyboardState();
var raycaster = new THREE.Raycaster(new THREE.Vector3(0,0,0),new THREE.Vector3(0,0,0),0,1);
var planes = [];
var num = 0;
var Plane = function(mesh){
	this.mesh = mesh;
	this.mesh.name = "which"+num;
  
}
function findPlanes(mesh){
	for(var i=0;i<planes.length;i++){
		if(mesh ===planes[i].mesh){
			//console.log(planes[i].mesh.name);
      var x = planes[i].mesh.position.clone();
      var y = car.car.leftfrontSphere.localToWorld(new THREE.Vector3(0,0,0));
      
      var z = x.clone().sub(y);
      var l = Math.sqrt(z.x*z.x+z.y*z.y+z.z*z.z);
      var tString = "x="+z.x+" y"+z.y+" z" + z.z;
      planes[i].mesh.material.color = new THREE.Color( 0xff0000 );
      
      return true;
		}
    else
    	planes[i].mesh.material.color =new THREE.Color( 0x0000ff );;
	}
}

var car;

var Car = function () {
  
  this.name = "123";

  this.car = new CarModel();   // 車模型

  this.sphere = new THREE.Mesh( new THREE.SphereGeometry( 2, 32, 32 ), new THREE.MeshBasicMaterial( {color: 0xffff00} ));  
  
  this.C = new THREE.Vector3();
  
  this.car_speed = 0;   // 車速
  this.car_theta = 0.35;   //  車角度
  this.speed = this.car_speed * 2;   //  更改車速
};
Car.prototype.setCarPosition = function(x,y,z){
  this.car.carModel.position.set(x,y,z);
  this.sphere.position.set(x,y,z);
  this.C = new THREE.Vector3(x,y,z);
}
Car.prototype.update = function(){
	//運算車移動的位置
  this.speed = this.car_speed * 2;
  RC = this.car.carModel.localToWorld (new THREE.Vector3(-12,0,-24/Math.tan(this.car_theta)));
  this.sphere.position.copy (RC);
  var omega = this.speed * Math.tan(this.car_theta)/24;
  var deltaT = clock.getDelta();
  var vv = this.C.clone().sub(RC).applyAxisAngle (new THREE.Vector3(0,1,0), omega*deltaT);
  tempC = this.C.clone();
  tempcar_position = this.car.carModel.position.clone();
  temprocartation = this.car.carModel.rotation.y ;
  this.C =...