physics test2

This commit is contained in:
2025-10-16 09:53:52 +03:00
parent a60d94fe66
commit 49e0486277
12 changed files with 283 additions and 169 deletions
-2
View File
@@ -20,10 +20,8 @@ class Clickable {
raycaster.setFromCamera(mouse, camera);
let forExecute = [];
objects.forEach(o => {
console.log(1)
o.getWorldPosition(v);
if (camera.position.distanceTo(v) <= o._clickable.distance && o.visible) {
console.log(2)
const intersects = raycaster.intersectObject(o);
// intersects.forEach(i=>{
// forExecute.push({o, i})
+13 -11
View File
@@ -63,22 +63,24 @@ class Hero{
// //colliderBody.sleepSpeedLimit = 1.0;
// gameEngine.phy.world.addBody(colliderBody)
let bodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased().setTranslation(
// characterCollider.position.x,
// characterCollider.position.y,
// characterCollider.position.z
-1, 3, 1
)
let rigidBody = gameEngine.phy.world.createRigidBody(bodyDesc);
let dynamicCollider = RAPIER.ColliderDesc.ball(0.28);
gameEngine.phy.world.createCollider(dynamicCollider, rigidBody.handle);
let po = gameEngine.phy.add(this.model, 'kinematicPositionBased', false, undefined, 'ball', { radius: 0.28})
// let bodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased().setTranslation(
// // characterCollider.position.x,
// // characterCollider.position.y,
// // characterCollider.position.z
// -1, 3, 1
// )
// let rigidBody = gameEngine.phy.world.createRigidBody(bodyDesc);
// let dynamicCollider = RAPIER.ColliderDesc.ball(0.28);
// gameEngine.phy.world.createCollider(dynamicCollider, rigidBody.handle);
this.characterControls = new CharacterControls(this.model, this.mixer,
this.animationsMap, gameEngine.orbitControls, gameEngine.camera, 'idle',
new RAPIER.Ray(
{ x: 0, y: 0, z: 0 },
{ x: 0, y: -1, z: 0}
), rigidBody, this.pointerControls)
), po.rigidBody, this.pointerControls)
// this.characterCollider = characterCollider;
// this.colliderBody = colliderBody;
@@ -146,7 +148,7 @@ class Hero{
this.delta += dlt;
if (this.delta > 0.016){
this.characterControls.update(this.gameEngine.phy.world, this.delta, pc)
this.gameEngine.phy.world.step()
this.gameEngine.phy.step()
this.delta = 0;
}
}
+104
View File
@@ -0,0 +1,104 @@
import * as RAPIER from '@dimforge/rapier3d'
// export type PhysicsObject = {
// mesh: THREE.Mesh
// collider: Rapier.Collider
// rigidBody: Rapier.RigidBody
// fn?: Function
// autoAnimate: boolean
// }
class Physics{
constructor(){
}
init = async ()=>{
let gravity = { x: 0.0, y: -9.81, z: 0.0 };
let world = new RAPIER.World(gravity);
// Create Ground.
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
let body = world.createRigidBody(bodyDesc);
let colliderDesc = RAPIER.ColliderDesc.cuboid(100.0, 0.1, 100.0);
world.createCollider(colliderDesc, body.handle);
this.world = world;
this.physicsObjects = [];
return this;
}
add = (mesh, rigidBodyType, autoAnimate = true, postPhysicsFn, colliderType, colliderSettings) => {
const rigidBodyDesc = RAPIER.RigidBodyDesc[rigidBodyType]()
rigidBodyDesc.setTranslation(mesh.position.x, mesh.position.y, mesh.position.z)
// * Responsible for collision response
const rigidBody = this.world.createRigidBody(rigidBodyDesc)
let colliderDesc
switch (colliderType) {
case 'cuboid':
{
const { width, height, depth } = colliderSettings
colliderDesc = RAPIER.ColliderDesc.cuboid(width, height, depth)
}
break
case 'ball':
{
const { radius } = colliderSettings
colliderDesc = RAPIER.ColliderDesc.ball(radius)
}
break
case 'capsule':
{
const { halfHeight, radius } = colliderSettings
colliderDesc = RAPIER.ColliderDesc.capsule(halfHeight, radius)
}
break
default:
{
colliderDesc = RAPIER.ColliderDesc.trimesh(
mesh.geometry.attributes.position.array,
mesh.geometry.index?.array
)
}
break
}
if (!colliderDesc) {
console.error('Collider Mesh Error: convex mesh creation failed.')
}
// * Responsible for collision detection
const collider = this.world.createCollider(colliderDesc, rigidBody)
const physicsObject = { mesh, collider, rigidBody, fn: postPhysicsFn, autoAnimate }
this.physicsObjects.push(physicsObject)
return physicsObject
}
step(){
this.world.step()
for (let po of this.physicsObjects) {
const autoAnimate = po.autoAnimate
if (autoAnimate) {
const mesh = po.mesh
const collider = po.collider
mesh.position.copy(collider.translation())
mesh.quaternion.copy(collider.rotation() )
}
const fn = po.fn
fn && fn()
}
}
}
export { Physics }
+13 -14
View File
@@ -13,7 +13,7 @@ import { TransformControls } from 'three/addons/controls/TransformControls.js';
import { ARButton } from 'three/addons/webxr/ARButton.js';
import { XRButton } from 'three/addons/webxr/XRButton.js';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
import * as RAPIER from '@dimforge/rapier3d'
import { Physics } from './Physics.js';
import { Clickable } from './Clickable.js';
class GameEngine {
@@ -161,7 +161,7 @@ class GameEngine {
gameEngine.camera.updateProjectionMatrix();
})
this.initPhysics();
await this.initPhysics();
if (opts.ar) {
renderer.xr.enabled = true;
@@ -246,17 +246,17 @@ class GameEngine {
this.cameraYaw = yaw;
}
initPhysics() {
async initPhysics() {
//const world = new CANNON.World()
//world.gravity.set(0, -9.82, 0)
let gravity = { x: 0.0, y: -9.81, z: 0.0 };
let world = new RAPIER.World(gravity);
// let gravity = { x: 0.0, y: -9.81, z: 0.0 };
// let world = new RAPIER.World(gravity);
// Create Ground.
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
let body = world.createRigidBody(bodyDesc);
let colliderDesc = RAPIER.ColliderDesc.cuboid(100.0, 0.1, 100.0);
world.createCollider(colliderDesc, body.handle);
// // Create Ground.
// let bodyDesc = RAPIER.RigidBodyDesc.fixed();
// let body = world.createRigidBody(bodyDesc);
// let colliderDesc = RAPIER.ColliderDesc.cuboid(100.0, 0.1, 100.0);
// world.createCollider(colliderDesc, body.handle);
// const groundMaterial = new CANNON.Material('groundMaterial')
// const slipperyMaterial = new CANNON.Material('slipperyMaterial')
@@ -276,10 +276,9 @@ class GameEngine {
// planeBody.addShape(planeShape)
// planeBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2)
// world.addBody(planeBody)
this.phy = {
world,
//slipperyMaterial
}
this.phy = new Physics();
await this.phy.init();
}
handleXrAction(gameEngine, delta) {