-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23-PyhsicsScripting.html
More file actions
57 lines (55 loc) · 2.59 KB
/
23-PyhsicsScripting.html
File metadata and controls
57 lines (55 loc) · 2.59 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Skripting Fisika</title>
<script src="https://aframe.io/releases/1.0.4/aframe.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/n5ro/aframe-physics-system@v4.0.1/dist/aframe-physics-system.min.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent("pushableobject", {
init:function() {
let el = this.el;
el.addEventListener("collide", function(ev) {
console.log("%c" + ev.srcElement.id, "font-size:2em");
});
el.addEventListener("click", function() {
let positionObject = el.getAttribute("position");
let positionVector = new THREE.Vector3(positionObject.x, positionObject.y, positionObject.z);
let camPosObject = document.querySelector("#cam").getAttribute("position");
let camPosVector = new THREE.Vector3(camPosObject.x, camPosObject.y, camPosObject.z);
// Besaran direction dipengaruhi oleh jarak kamera ke object
let direction = camPosVector.sub(positionVector);
direction = direction.normalize(); // Jadi vektor unit
let speed = -10;
direction = direction.multiplyScalar(speed);
// Fungsi di bawah ini ada di Cannon.js
el.body.applyImpulse(
new CANNON.Vec3(direction.x, 0, direction.z), // ke arah z- (menjauh dari kamera)
new CANNON.Vec3().copy(positionVector)
);
});
}
});
</script>
<a-scene physics="debug:true">
<a-camera id="cam">
<a-cursor></a-cursor>
</a-camera>
<a-box
shadow rotation="-90 0 0"
color="#888"
static-body
width="10"
height="10"
depth="0.2"
position="0 0 -5"
></a-box>
<!-- Massa dynamic-body by default adalah 5 -->
<a-sphere id="merah" pushableobject radius="0.2" color="red" shadow dynamic-body="mass:5;" position="0 1.5 -3"></a-sphere>
<a-sphere id="hijau" pushableobject radius="0.2" color="green" shadow dynamic-body="mass:50;" position="2 1.5 -5"></a-sphere>
<a-sphere id="biru" pushableobject radius="0.2" color="blue" shadow dynamic-body="mass:500;" position="-2 1.5 -5"></a-sphere>
</body>
</html>