Skip to content

Commit aeefbc7

Browse files
committed
Added compound hacd shape example
1 parent 606929b commit aeefbc7

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

src/assets/glb/Tank.glb

107 KB
Binary file not shown.

src/examples.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ <h3>As Physics Extension for three.js</h3>
6060
<li><a href="./examples/point-to-point-constraint.html">Point to Point Constraint</a></li>
6161
<li><a href="./examples/hollow-cylinder.html">Hollow Cylinder</a></li>
6262
<li><a href="./examples/convex-objects-breaking.html">Convex Objects Breaking</a></li>
63-
<li><a href="./examples/carousel.html">Carousel NEW</a></li>
63+
<li><a href="./examples/carousel.html">Carousel</a></li>
6464
<li>
65-
<a href="./examples/create-and-destroy-objects-and-physics.html"
66-
>Create and Destroy Objects and Physics NEW</a
67-
>
65+
<a href="./examples/create-and-destroy-objects-and-physics.html">Create and Destroy Objects and Physics</a>
6866
</li>
6967
</ul>
7068

@@ -79,9 +77,10 @@ <h3>Standalone Mode</h3>
7977
<li><a href="./examples/use-tweenjs.html">Use tween.js</a></li>
8078
<li><a href="./examples/project-options-and-multiple-scenes.html">Project Options and Multiple Scenes</a></li>
8179
<li><a href="./examples/gltf-loader-and-animations.html">GLTF Loader and Animations</a></li>
82-
<li><a href="./examples/simplify-physics-mesh.html">Simplify Physics Mesh NEW</a></li>
80+
<li><a href="./examples/simplify-physics-mesh.html">Simplify Physics Mesh</a></li>
81+
<li><a href="./examples/standalone/compound-hacd-shape.html">Compound HACD Shape (Tank) NEW</a></li>
8382
<li><a href="./examples/types-of-constraints.html">Types of Constraints</a></li>
84-
<li><a href="./examples/slider-constraints.html">Slider Constraints NEW</a></li>
83+
<li><a href="./examples/slider-constraints.html">Slider Constraints</a></li>
8584
<li><a href="./examples/play-with-physics-bodies.html">Play with Physics Bodies</a></li>
8685
<li><a href="./examples/teleport-a-dynamic-body.html">Teleport a Dynamic Body</a></li>
8786
<li><a href="./examples/per-body-gravity.html">Per Body Gravity</a></li>
@@ -97,15 +96,15 @@ <h3>Standalone Mode</h3>
9796
<li><a href="./examples/post-processing.html">Post-Processing</a></li>
9897
<li><a href="./examples/collisiongroup-and-collisionmask.html">CollisionGroup and CollisionMask</a></li>
9998
<li><a href="./examples/softbody-cloth.html">SoftBody Cloth</a></li>
100-
<li><a href="./examples/raycast-vehicle.html">Car (Raycast Vehicle) NEW</a></li>
99+
<li><a href="./examples/raycast-vehicle.html">Car (Raycast Vehicle)</a></li>
101100
<li><a href="./examples/car-using-physics-constraints.html">Car (Physics Constraints)</a></li>
102-
<li><a href="./examples/standalone/pick-up-objects.html">Pick up Objects (with Robot Hand) NEW</a></li>
101+
<li><a href="./examples/standalone/pick-up-objects.html">Pick up Objects (with Robot Hand)</a></li>
103102

104103
<h3 id="2d-elements" style="font-style: italic; line-height: 0; padding-top: 24px">
105104
2D Elements (Experimental)
106105
</h3>
107106
<p>
108-
These 2D elements are new and still in development.<br />
107+
These 2D elements are and still in development.<br />
109108
They are rendered by three.js and<br />
110109
do not need any other 3rd party framework<br />
111110
like Phaser, PixiJS or Two.js.
@@ -138,7 +137,7 @@ <h3>As 3D Extension for Phaser</h3>
138137
<li><a href="./examples/load-and-use-textures.html">Load and Use Textures</a></li>
139138
<li>
140139
<a href="./examples/phaser-gltf-loader-with-animation-speed.html"
141-
>GLTF Loader and Animations with Timescale (Drone) NEW</a
140+
>GLTF Loader and Animations with Timescale (Drone)</a
142141
>
143142
</li>
144143
<li><a href="./examples/fbx-loader-and-animations.html">FBX Loader and Animations</a></li>
@@ -187,7 +186,7 @@ <h3>As 3D Extension for Phaser</h3>
187186
</ul>
188187
</div>
189188
<script>
190-
// highlight all new examples
189+
// highlight all examples
191190
const links = document.querySelectorAll('a')
192191
const makeBadge = (tag, color, background) => {
193192
return `<b style="background-color: ${background}; color:${color}; border-radius: 4px; padding: 2px 8px; font-size: 10px; position: relative; top: -2px;">${tag}</b>`
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
8+
/>
9+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
10+
<title>Compound HACD Shape</title>
11+
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
12+
<script src="/js/examples.js?ver=1.1.1"></script>
13+
<script src="/lib/enable3d/enable3d.framework.0.25.4.min.js"></script>
14+
</head>
15+
16+
<body>
17+
<div id="info-text">This Tank consists of 4 HACD Shapes.</div>
18+
<script>
19+
const { Project, Scene3D, PhysicsLoader, ExtendedObject3D, THREE } = ENABLE3D
20+
21+
class MainScene extends Scene3D {
22+
async create() {
23+
this.physics.debug?.enable()
24+
25+
const { orbitControls } = await this.warpSpeed()
26+
this.camera.position.set(3, 3, 6)
27+
orbitControls?.target.set(0, 1, 0)
28+
this.camera.lookAt(0, 1, 0)
29+
30+
this.load.gltf('/assets/glb/Tank.glb').then(gltf => {
31+
const parts = []
32+
gltf.scene.traverse(child => {
33+
if (child.isMesh) {
34+
parts.push(child)
35+
}
36+
})
37+
38+
const tank = new THREE.Group()
39+
parts.forEach(p => {
40+
tank.add(p)
41+
})
42+
43+
tank.scale.set(100, 100, 100)
44+
tank.rotateX(-Math.PI / 2)
45+
tank.position.set(0, 2, 0)
46+
47+
const compoundShape = new Ammo.btCompoundShape()
48+
parts.forEach((part, i) => {
49+
// get the center of the shape, to set its offset
50+
part.geometry.computeBoundingBox()
51+
const bb = part.geometry.boundingBox
52+
const center = bb.getCenter(new THREE.Vector3())
53+
54+
let collisionShape = this.physics.createCollisionShape('hacd', {}, part)
55+
56+
// adjust center of shape
57+
const transform = new Ammo.btTransform()
58+
transform.setIdentity()
59+
transform.setOrigin(new Ammo.btVector3(center.x, center.y, center.z))
60+
61+
compoundShape.addChildShape(transform, collisionShape)
62+
})
63+
64+
{
65+
const pos = new THREE.Vector3()
66+
const quat = new THREE.Quaternion()
67+
const scale = new THREE.Vector3()
68+
tank.getWorldPosition(pos)
69+
tank.getWorldQuaternion(quat)
70+
tank.getWorldScale(scale)
71+
72+
const localTransform = this.physics.applyPosQuatScaleMargin(compoundShape, pos, quat, scale, 0.01)
73+
const rigidBody = this.physics.collisionShapeToRigidBody(compoundShape, localTransform, 1, false)
74+
75+
this.add.existing(tank)
76+
this.physics.addRigidBodyToWorld(tank, rigidBody, 0, 1, -1)
77+
}
78+
})
79+
}
80+
}
81+
82+
PhysicsLoader('/lib/ammo/moz', () => new Project({ scenes: [MainScene] }))
83+
</script>
84+
</body>
85+
</html>

0 commit comments

Comments
 (0)