hw4 helper
by RGMGx
HTML
<div id="info">Hw4 air Hockey
<br/>
<p id='velprint'>
</p>
<audio autoplay="display:none" controls="" loop="" preload="">
<source src="https://rgmgx.github.io/sounds/bgm_maoudamashii_8bit29.mp3" type="audio/mp3">
</audio>
<audio id="collisionsound" style="display:none">
<source src="
https://rgmgx.github.io/sounds/putting_a_jar.mp3
" type='audio/mp3'>
</audio>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js">
</script>
<script src="https://jyunming-chen.github.io/game3js/js/ccdsys.js"></script>
<script src="https://cdn.jsdelivr.net/gh/jyunming-chen/game3js@13a05103eb51d913dd7815939bf7ad45690e8dc9/js/ccdbox.js"></script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
var r_score = 0
var b_score = 0
var s1, s2, score1, score2
var loader2 = new THREE.FontLoader();
class TwoLinkArm {
constructor() {
this.theta1 = 0;
this.theta2 = 0;
}
init(color) {
let twoLink = new THREE.Object3D();
this.color = color
let mat = new THREE.MeshPhongMaterial({
color: color
})
this.link1 = new THREE.Object3D();
this.link1.add(new THREE.AxisHelper(10))
let mesh = new THREE.Mesh(new THREE.BoxGeometry(10, 3, 5), mat)
this.link1.add(mesh);
mesh.position.x = 5
twoLink.add(this.link1)
this.link2 = new THREE.Object3D();
this.link2.add(new THREE.AxisHelper(10))
let mesh2 = new THREE.Mesh(new THREE.BoxGeometry(10, 3, 5), mat)
this.link2.add(mesh2);
mesh2.position.x = 5
this.link1.add(this.link2)
this.link2.position.x = 10
let paddle = new THREE.Mesh(new THREE.CylinderGeometry(5, 5, 4), mat)
this.link2.add(paddle)
paddle.position.x = 10
return twoLink;
}
update(thetas) {
this.theta1 = thetas[0]
this.theta2 = thetas[1]
this.link1.rotation.y = this.theta1
this.link2.rotation.y = this.theta2
}
}
///////////////////////////
class speed {
constructor(mesh) {
this.pos = new THREE.Vector3()
this.vel = new THREE.Vector3()
this.force = new THREE.Vector3()
this.mesh = mesh;
}
update(dt) {
this.vel.add(this.force.clone().multiplyScalar(dt))
this.pos.add(this.vel.clone().multiplyScalar(dt))
this.collidingPlanes(planes)
this.mesh.position.copy(this.pos)
}
collidingPlanes(planes) {
const EPS = 1e-3
const CR = 0.99
for (let i = 0; i < planes.length; i++) {
let plane = planes[i]
let point = this.pos.clone().sub(plane.ptOnPl)
if (point.dot(plane.normal) < EPS + 2) {
// position correction
this.pos.copy(plane.ptOnPl.clone().add(point.projectOnPlane(plane.normal)))
this.pos.add(plane.normal.clone().multiplyScalar(2))
// velocity update
...