threefiz
Version:
Implementation of a physics engine based on the [Three.js](https://github.com/mrdoob/three.js) library.
2 lines (1 loc) • 1.78 kB
JavaScript
import{c as f,e as w,S as g,M as h,a as E,B as p,T as G,s as y,V as t,b as s,Q as S,E as x,d as K,r as A}from"./init-BHUQqx8w.js";f.position.set(-50,50,10);w.target.set(50,-11,0);const F=new g(3),P=new h({map:E(8,5,"hsl(0, 50%, 60%)","hsl(0, 50%, 80%)")}),V=new p(3,3,3),Q=new h({color:"hsl(240, 50%, 60%)"}),M=new p(50,1,50),u=new p(50,3,1),r=new h({color:"hsl(240, 50%, 80%)"}),o=new G({scene:y});o.addSphere({mesh:new s(F,P),position:new t(0,15,15),friction:1,mass:1,restitution:0});o.addBox({mesh:new s(V,Q),position:new t(0,15,-15),friction:1,mass:10,restitution:0});o.addBox({mesh:new s(M,r),rotation:new S().setFromEuler(new x(0,0,Math.PI/-7)),friction:1,restitution:0,isStatic:!0});o.addBox({mesh:new s(M,r),position:new t(47,-11,0),friction:1,restitution:.1,isStatic:!0});o.addBox({mesh:new s(u,r),position:new t(47,-9,24.5),isStatic:!0});o.addBox({mesh:new s(u,r),position:new t(47,-9,-24.5),isStatic:!0});o.addBox({mesh:new s(u,r),position:new t(47+24.5,-9,0),rotation:new S().setFromEuler(new x(0,Math.PI/2,0)),isStatic:!0});o.init();o.objects.forEach(e=>{e.mesh.castShadow=!0,e.mesh.receiveShadow=!0});const i=new K;i.dom.style.setProperty("position","absolute");i.dom.style.setProperty("top","0");const B=o.objects[0],n=(e,m,c,d)=>{const a=new t(e*d,m*d,c*d);console.log(a),B.setAngularVelocity(l=>(console.clear(),console.log(l,"+",a,"=",l.clone().add(a)),l.add(a)))};window.addEventListener("keydown",e=>{e.code==="KeyW"?n(0,0,-1,1):e.code==="KeyS"?n(0,0,1,1):e.code==="KeyA"?n(-1,0,0,1):e.code==="KeyD"?n(1,0,0,1):e.code==="KeyQ"?n(0,1,0,1):e.code==="KeyE"?n(0,-1,0,1):e.code==="Space"&&B.setAngularVelocity(c=>c.set(0,0,0))});document.body.appendChild(i.dom);w.update();const b=()=>{w.update(),i.update(),o.step(),A.render(y,f),requestAnimationFrame(b)};b();