-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.js
154 lines (129 loc) · 4.31 KB
/
physics.js
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
// Copyright 2022 by Croquet Corporation, Inc. All Rights Reserved.
// https://croquet.io
import { ModelService, Model } from "./worldcore";
export let Physics;
export function PhysicsVersion() {
return "Rapier: " + Physics.version();
}
class PhysicsWorld extends Model {
init(options) {
if (options.useCollisionEventQueue) {
this.queue = new Physics.EventQueue(true);
}
const gravity = options.gravity || [0.0, -9.8, 0.0];
const timeStep = options.timeStep || 5; // Gets Converted to Miliseconds (Hz) 5 Approx 200 Hz
const g = new Physics.Vector3(...gravity);
this.world = new Physics.World(g);
this.timeStep = timeStep;
this.world.timestep = this.timeStep / 1000;
this.rigidBodies = new Map();
if (!options.startPaused) {
this.future(0).tick();
}
}
pause() {
this.isPaused = true;
}
resume() {
this.isPaused = false;
}
tick() {
if (!this.isPaused) {
this.world.step(this.queue); // this.queue may be undefined
this.world.forEachActiveRigidBody(body => {
let h = body.handle;
const rb = this.rigidBodies.get(h);
const t = rb.rigidBody.translation();
const r = rb.rigidBody.rotation();
const v = [t.x, t.y, t.z];
const q = [r.x, r.y, r.z, r.w];
rb.moveTo(v);
rb.say("translating", v);
rb.rotateTo(q);
});
if (this.queue) {
if (this.collisionEventHandler) {
this.queue.drainCollisionEvents((handle1, handle2, started) => {
let rb1 = this.rigidBodies.get(handle1);
let rb2 = this.rigidBodies.get(handle2);
this.collisionEventHandler.collision(rb1, rb2, started);
});
}
}
}
this.future(this.timeStep).tick();
}
registerCollisionEventHandler(handler) {
this.collisionEventHandler = handler;
}
destroy() {
this.world.free();
this.world = null;
// super.destroy();
}
}
PhysicsWorld.register("PhysicsWorld");
//------------------------------------------------------------------------------------------
//-- PhysicsManager ------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Maintains a list of players connected to the session.
export class PhysicsManager extends ModelService {
static async asyncStart() {
if (window.RAPIERModule) {
Physics = window.RAPIERModule;
} else {
Physics = await import("@dimforge/rapier3d");
}
console.log("Starting physics " + PhysicsVersion());
}
static types() {
if (!Physics) return {};
return {
"Physics.World": {
cls: Physics.World,
write: world => world.takeSnapshot(),
read: snapshot => Physics.World.restoreSnapshot(snapshot)
},
"Physics.EventQueue": {
cls: Physics.EventQueue,
write: _q => {},
read: _q => new Physics.EventQueue(true)
},
};
}
init() {
super.init("PhysicsManager");
this.worlds = new Map();
// this.globalWorld = this.createWorld({}, this.id);
}
createWorld(options, id) {
let world = PhysicsWorld.create(options);
this.worlds.set(id, world);
return world;
}
createGlobalWorld(options) {
if (this.globalWorld) {return;}
this.globalWorld = this.createWorld(options, this.id);
return this.globalWorld;
}
deleteWorld(id) {
let world = this.worlds.get(id);
if (!world) {return;}
world.destroy();
this.worlds.delete(id);
}
deleteGlobalWorld() {
this.deleteWorld(this.id);
delete this.globalWorld;
}
destroy() {
for (let [_k, v] of this.worlds) {
v.destroy();
}
delete this.globalWorld;
this.worlds = new Map();
super.destroy();
}
}
PhysicsManager.register("PhysicsManager");