webgl_collisions_reaction.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <html>
  2. <head>
  3. <title>Collision reaction</title>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <style type="text/css">
  6. body {
  7. font-family: Monospace;
  8. background-color: #f0f0f0;
  9. margin: 0px;
  10. overflow: hidden;
  11. }
  12. #oldie {
  13. background-color: #ddd !important
  14. }
  15. #info {
  16. position: absolute;
  17. top: 30px;
  18. left: 10px;
  19. width: 800px;
  20. color: #000000;
  21. padding: 5px;
  22. font-family: Monospace;
  23. font-size: 13px;
  24. text-align: left;
  25. z-index: 100;
  26. }
  27. #options {
  28. position: absolute;
  29. top: 10px;
  30. left: 10px;
  31. width: 800px;
  32. color: #000000;
  33. padding: 5px;
  34. font-family: Monospace;
  35. font-size: 13px;
  36. text-align: left;
  37. z-index: 100;
  38. }
  39. </style>
  40. <script type="text/javascript" src="lib/Three.js">
  41. </script>
  42. <script type="text/javascript" src="lib/RequestAnimationFrame.js">
  43. </script>
  44. <script type="text/javascript" src="src/Physics.js">
  45. </script>
  46. <script type="text/javascript" src="src/PhysicsUtils.js">
  47. </script>
  48. <script type="text/javascript">
  49. var scene, camera, renderer, info, mouse2d, sun, loader, sphere, debugNormal;
  50. var range = 400;
  51. var speed = 1;
  52. var sphereSize = 4;
  53. var cubes = [];
  54. function init(){
  55. container = document.createElement('div');
  56. document.body.appendChild(container);
  57. info = document.getElementById("info");
  58. camera = new THREE.Camera(40, window.innerWidth / window.innerHeight, 1, 10000);
  59. camera.position.y = 120;
  60. camera.position.x = 300;
  61. camera.position.z = 0;
  62. mouse2d = new THREE.Vector3(0, 0, 1);
  63. loader = new THREE.Loader(true);
  64. scene = new THREE.Scene();
  65. sphere = new THREE.Mesh(new Sphere(sphereSize, 10, 10), new THREE.MeshLambertMaterial({
  66. color: 0xff0000
  67. }));
  68. scene.addObject(sphere);
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setSize(window.innerWidth, window.innerHeight);
  71. container.appendChild(renderer.domElement);
  72. var ambientLight = new THREE.AmbientLight(0xdddddd);
  73. scene.addLight(ambientLight);
  74. sun = new THREE.DirectionalLight(0xffffff);
  75. sun.position = new THREE.Vector3(1, -1, 1).normalize();
  76. scene.addLight(sun);
  77. createObstacles();
  78. var geometry = new THREE.Geometry();
  79. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(0, 0, 0) ) );
  80. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(10, 0, 0) ) );
  81. debugNormal = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xff0000 } ) );
  82. scene.addObject( debugNormal );
  83. container.onmousemove = onDocumentMouseMove;
  84. animate();
  85. }
  86. function createObstacles(){
  87. createCube(100, 50, 10, new THREE.Vector3( 20, 0, 100), Math.PI / 4);
  88. camera.target = createCube(100, 50, 10, new THREE.Vector3(-20, 0, 200), -Math.PI / 4);
  89. createCube(100, 50, 10, new THREE.Vector3( 20, 0, 300), Math.PI / 4);
  90. }
  91. function createCube(sx, sy, sz, p, ry){
  92. var cube = new THREE.Mesh(new Cube(sx, sy, sz, 1, 1, 1), new THREE.MeshLambertMaterial({
  93. color: 0x003300//, wireframe: true
  94. }));
  95. cube.position = p;
  96. cube.rotation.y = ry;
  97. scene.addObject(cube);
  98. Physics.colliders.push( PhysicsUtils.MeshOBB(cube) );
  99. cubes.push(cube);
  100. return cube;
  101. }
  102. function onDocumentMouseMove(event){
  103. event.preventDefault();
  104. mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
  105. mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
  106. mouse2d.z = 1;
  107. }
  108. function animate(){
  109. requestAnimationFrame(animate);
  110. for (var i = 0; i < cubes.length; i++) {
  111. cubes[i].materials[0].color = new THREE.Color(0x003300);
  112. }
  113. var ray = new Ray( sphere.position, new THREE.Vector3(0,0,1) );
  114. //debugNormal.position = sphere.position;
  115. var c = Physics.rayCastNearest(ray);
  116. if (!c || c.distance > speed) {
  117. sphere.position.z += speed;
  118. }
  119. if(c && c.normal) {
  120. info.innerHTML = vts(c.normal);
  121. var poi = ray.origin.clone().addSelf( ray.direction.clone().multiplyScalar( c.distance ) );
  122. debugNormal.geometry.vertices[0].position = poi.clone().addSelf( new THREE.Vector3(2,2,2) );
  123. debugNormal.geometry.vertices[1].position = poi.clone().addSelf( c.normal.clone() );
  124. }
  125. if(sphere.position.z > range) sphere.position = new THREE.Vector3(0,0,0);
  126. camera.position.x = Math.cos(mouse2d.x * Math.PI) * 300;
  127. camera.position.y = Math.cos(mouse2d.y * Math.PI) * 300;
  128. camera.position.z = 200 + Math.sin(mouse2d.x * Math.PI) * 300 + Math.sin(mouse2d.y * Math.PI) * 300;
  129. renderer.render(scene, camera);
  130. }
  131. function vts(v){
  132. if (!v)
  133. return "undefined<br>";
  134. else
  135. return v.x + " , " + v.y + " , " + v.z + "<br>";
  136. }
  137. </script>
  138. </head>
  139. <body onload="init();">
  140. <div id="info">
  141. </div>
  142. <div id="options">
  143. </div>
  144. </body>
  145. </html>