-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathsimple.test.js
More file actions
124 lines (98 loc) · 3.22 KB
/
simple.test.js
File metadata and controls
124 lines (98 loc) · 3.22 KB
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
import Module from 'node:module'
import path from 'path'
import { Physics, ServerClock, Loaders, ExtendedObject3D } from '../../packages/ammoOnNodejs/dist/index.js'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import { jest } from '@jest/globals'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const require = Module.createRequire(import.meta.url)
var _ammo = require(path.resolve(__dirname, '../../packages/ammoOnNodejs/ammo/ammo.cjs'))
global.console = {
log: console.log,
error: console.error,
// ignore all console.warn()
warn: jest.fn(),
info: console.info,
debug: console.debug
}
it('should work correctly', done => {
const MainScene = () => {
const physics = new Physics()
// test
// const btVector3 = new Ammo.btVector3()
// console.log('btVector3', btVector3)
// clock
const clock = new ServerClock()
const ground = physics.add.ground({ width: 20, height: 20, name: 'ground' })
let destroyed = 0
ground.body.on.collision((otherObject, event) => {
// console.log(`${ground.name} > ${event} > ${otherObject.name}`)
if (event === 'start')
setTimeout(() => {
// console.log(`destroy ${otherObject.name}`)
physics.destroy(otherObject)
destroyed++
if (destroyed === 2) {
clock.stop()
done()
}
}, 100)
})
let robot
const FBXLoader = new Loaders.FBXLoader()
FBXLoader.load(path.join(__dirname, '../../packages/dev/public/assets/Idle.fbx')).then(fbx => {
robot = new ExtendedObject3D()
robot.name = 'robot'
robot.add(fbx)
robot.scale.set(0.05, 0.05, 0.05)
robot.position.set(5, 12, 0)
const physicsOptions = {
addChildren: false,
shape: 'hull' // or any other shape you want
}
physics.add.existing(robot, physicsOptions)
// this.robot = robot
})
let hero
const GLTFLoader = new Loaders.GLTFLoader()
GLTFLoader.load(path.resolve(__dirname, '../../packages/dev/public/assets/hero.glb')).then(gltf => {
const child = gltf.scene.children[0]
hero = new ExtendedObject3D()
hero.name = 'hero'
hero.add(child)
hero.position.set(-10, 12, 0)
const physicsOptions = {
addChildren: false,
shape: 'hull' // or any other shape you want
}
physics.add.existing(hero, physicsOptions)
// this.hero = hero
})
// for debugging I disable high accuracy
// high accuracy will use much more cpu power
if (process.env.NODE_ENV !== 'production') clock.disableHighAccuracy()
// loop
const animate = delta => {
physics.update(delta * 1000)
if (hero && hero.body) {
const pos = hero.position.y.toFixed(2)
// if (pos > 10) console.log(this.hero.name, pos)
}
if (robot && robot.body) {
const pos = robot.position.y.toFixed(2)
// if (pos > 10) console.log(this.robot.name, pos)
}
}
clock.onTick(delta => {
animate(delta)
})
}
_ammo().then(Ammo => {
globalThis.Ammo = Ammo
// test
// const btVector3 = new Ammo.btVector3()
// console.log('btVector3', btVector3)
MainScene()
})
})