-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcollisiongroup-and-collisionmask.html
108 lines (91 loc) · 3.14 KB
/
collisiongroup-and-collisionmask.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>CollisionGroup and CollisionMask</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
<script src="/lib/enable3d/enable3d.framework.0.25.4.min.js"></script>
</head>
<body>
<div id="info-text">
The Red Balls collide not with them self.<br />
The Blue Balls collide only with the Red Balls.<br />
The Green Cubes collide only with the Ground and them self.
</div>
<script>
const { Project, Scene3D, PhysicsLoader } = ENABLE3D
class MainScene extends Scene3D {
constructor() {
super({ key: 'MainScene' })
}
create() {
this.warpSpeed()
// we have 4 groups
// default 0001 (1)
// red 0010 (2)
// blue 0100 (4)
// green 1000 (8)
// the ground is by default in the default group
// red balls
for (let i = 0; i < 20; i++) {
const x = this.randomPosition(),
y = this.randomHeight(-5),
z = this.randomPosition()
const collisionGroup = 2,
collisionMask = parseInt('1101')
const sphere = this.physics.add.sphere(
{ x, y, z, collisionGroup, collisionMask, radius: 1 },
{ lambert: { color: 'red' } }
)
sphere.body.setBounciness(0.5)
}
// blue balls
setTimeout(() => {
for (let i = 0; i < 20; i++) {
const x = this.randomPosition(),
y = this.randomHeight(),
z = this.randomPosition()
const collisionGroup = 4,
collisionMask = 2
const sphere = this.physics.add.sphere(
{ x, y, z, collisionGroup, collisionMask, radius: 0.5 },
{ lambert: { color: 'blue' } }
)
sphere.body.setBounciness(0.5)
}
}, 2000)
// green boxes
setTimeout(() => {
for (let i = 0; i < 20; i++) {
const x = this.randomPosition(),
y = this.randomHeight(0),
z = this.randomPosition()
const collisionGroup = 8,
collisionMask = 9 // 1 + 8
const box = this.physics.add.box(
{ x, y, z, collisionGroup, collisionMask },
{ lambert: { color: 'green' } }
)
box.body.setBounciness(0.5)
}
}, 4000)
}
randomPosition() {
return (Math.random() - 0.5) * 5
}
randomHeight(offset = 0) {
return Math.random() * 10 + 5 + offset
}
}
PhysicsLoader('/lib/ammo/kripken', () => {
new Project({ scenes: [MainScene] })
})
</script>
</body>
</html>