1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| <template> <div></div> </template>
<script setup> import * as THREE from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; import * as CANNON from "cannon-es"; // 初始化物理世界 const world = new CANNON.World(); // 设置重力 world.gravity.set(0, -9.82, 0);
// Sweep and prune broadphase world.broadphase = new CANNON.SAPBroadphase(world);
// Disable friction by default world.defaultContactMaterial.friction = 0;
// 初始化3D场景 const scene = new THREE.Scene(); // 初始化相机 const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); camera.position.set(0, 10, 20); camera.lookAt(0, 0, 0);
// 初始化渲染器 const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
// 初始化控制器 const controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true;
let phyMeshes = []; let meshes = [];
// 创建地面 // const groundShape = new CANNON.Plane(); const groundPhyMaterial = new CANNON.Material("ground"); const groundShape = new CANNON.Box(new CANNON.Vec3(50, 0.2, 50)); const groundBody = new CANNON.Body({ mass: 0, shape: groundShape, material: groundPhyMaterial, }); // groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2); world.addBody(groundBody); phyMeshes.push(groundBody);
const groundMesh = new THREE.Mesh( // new THREE.PlaneGeometry(100, 100), new THREE.BoxGeometry(100, 0.4, 100), new THREE.MeshBasicMaterial({ color: 0x666666, }) ); // groundMesh.rotation.x = -Math.PI / 2; scene.add(groundMesh); meshes.push(groundMesh);
// 创建车身 const chassisShape = new CANNON.Box(new CANNON.Vec3(2, 0.5, 1)); const chassisBody = new CANNON.Body({ mass: 150, shape: chassisShape, }); chassisBody.position.set(0, 14, 0); world.addBody(chassisBody); phyMeshes.push(chassisBody);
// three.js的车身 const chassisMesh = new THREE.Mesh( new THREE.BoxGeometry(4, 1, 2), new THREE.MeshBasicMaterial({ color: 0x660066, }) ); scene.add(chassisMesh); meshes.push(chassisMesh);
// 创建拥有悬架的车辆 const vehicle = new CANNON.RaycastVehicle({ chassisBody, });
const wheelOptions = { // 轮子半径 radius: 1, // 本地坐标系中的轮子位置 // directionLocal: new CANNON.Vec3(0, -1, 0), suspensionStiffness: 30, // 悬架的刚度 suspensionRestLength: 0.3, // 悬架的休息长度 // frictionSlip: 1.4, // 滑动摩擦力 dampingRelaxation: 2.3, // 振动 dampingCompression: 4.4, // 压缩 maxSuspensionForce: 100000, // 最大悬架力 // rollInfluence: 0.1, // 滚动影响 axleLocal: new CANNON.Vec3(0, 0, 1), // 本地坐标系中的轴 chassisConnectionPointLocal: new CANNON.Vec3(-1, 0, 1), // 本地坐标系中的车身连接点 maxSuspensionTravel: 0.2, // 最大悬架行程 // customSlidingRotationalSpeed: -30, // useCustomSlidingRotationalSpeed: true, };
vehicle.addWheel({ ...wheelOptions, chassisConnectionPointLocal: new CANNON.Vec3(-1, 0, 1), });
vehicle.addWheel({ ...wheelOptions, chassisConnectionPointLocal: new CANNON.Vec3(-1, 0, -1), });
vehicle.addWheel({ ...wheelOptions, chassisConnectionPointLocal: new CANNON.Vec3(1, 0, 1), });
vehicle.addWheel({ ...wheelOptions, chassisConnectionPointLocal: new CANNON.Vec3(1, 0, -1), });
vehicle.addToWorld(world);
const wheelBodies = [];
// 创建轮子 // 创建车轮形状 const wheelShape = new CANNON.Cylinder(0.5, 0.5, 0.2, 20); // const wheelShape = new CANNON.Sphere(0.5); // three.js的车轮 const wheelMaterial = new THREE.MeshBasicMaterial({ color: 0x660000, wireframe: true, }); // const wheelGeometry = new THREE.SphereGeometry(0.5, 8, 8); const wheelGeometry = new THREE.CylinderGeometry(0.5, 0.5, 0.2, 20);
const wheelPhyMaterial = new CANNON.Material("wheel");
const wheel_ground = new CANNON.ContactMaterial( wheelPhyMaterial, groundPhyMaterial, { friction: 0, restitution: 0, contactEquationStiffness: 1000, } ); world.addContactMaterial(wheel_ground);
for (let i = 0; i < vehicle.wheelInfos.length; i++) { const wheelBody = new CANNON.Body({ mass: 0, // shape: wheelShape, // type: CANNON.Body.KINEMATIC, // collisionFilterGroup: 0, // turn off collisions material: wheelPhyMaterial, }); wheelBody.type = CANNON.Body.KINEMATIC; wheelBody.collisionFilterGroup = 0; // wheelBody.addShape(wheelShape); wheelBodies.push(wheelBody); // world.addBody(wheelBody); phyMeshes.push(wheelBody);
const wheelMesh = new THREE.Mesh(wheelGeometry, wheelMaterial); wheelMesh.rotation.x = -Math.PI / 2; const wheelObj = new THREE.Object3D(); wheelObj.add(wheelMesh); scene.add(wheelObj); meshes.push(wheelObj); }
world.addEventListener("postStep", () => { // 更新车轮的位置 wheelBodies.forEach((wheelBody, index) => { vehicle.updateWheelTransform(index); wheelBody.position.copy(vehicle.wheelInfos[index].worldTransform.position); wheelBody.quaternion.copy( vehicle.wheelInfos[index].worldTransform.quaternion ); }); });
// 设置完毕之后,将车子添加到世界中 // vehicle.addToWorld(world);
// console.log(vehicle); // console.log(world);
// 控制车子 window.addEventListener("keydown", (event) => { // console.log(event.key); if (event.key === "w") { // setWheelForce(力,轮子编号) vehicle.applyEngineForce(1000, 0); vehicle.applyEngineForce(1000, 1); // vehicle.applyEngineForce(10000, 2); // vehicle.applyEngineForce(10000, 3); } if (event.key === "s") { vehicle.applyEngineForce(-1000, 0); vehicle.applyEngineForce(-1000, 1); } if (event.key == "a") { vehicle.setSteeringValue(Math.PI / 4, 2); vehicle.setSteeringValue(Math.PI / 4, 3); } if (event.key == "d") { vehicle.setSteeringValue(-Math.PI / 4, 2); vehicle.setSteeringValue(-Math.PI / 4, 3); } }); window.addEventListener("keyup", (event) => { if (event.key === "w") { vehicle.applyEngineForce(0, 0); vehicle.applyEngineForce(0, 1); } if (event.key === "s") { vehicle.applyEngineForce(0, 0); vehicle.applyEngineForce(0, 1); } if (event.key == "a") { vehicle.setSteeringValue(0, 2); vehicle.setSteeringValue(0, 3); } if (event.key == "d") { vehicle.setSteeringValue(0, 2); vehicle.setSteeringValue(0, 3); } });
// 渲染 let clock = new THREE.Clock(); function animate() { let delta = clock.getDelta(); world.step(1 / 60, delta);
for (let i = 0; i < phyMeshes.length; i++) { meshes[i].position.copy(phyMeshes[i].position); meshes[i].quaternion.copy(phyMeshes[i].quaternion); }
controls.update(); renderer.render(scene, camera); requestAnimationFrame(animate); }
animate(); </script>
<style> * { margin: 0; padding: 0; box-sizing: border-box; }
canvas { width: 100vw; height: 100vh; position: fixed; top: 0; left: 0; } </style>
|