-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.js
82 lines (68 loc) · 2.02 KB
/
Ball.js
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
class Ball
{
constructor()
{
this.velocity = new THREE.Vector3(0.025, 0, 0);
var geometry = new THREE.SphereGeometry(1, 10, 10);
var material = new THREE.MeshBasicMaterial( { color: 0x2f23cf } );
this.mesh = new THREE.Mesh(geometry, material);
scene.add(this.mesh);
}
Update(deltaTime)
{
var frameDisp = this.velocity.clone().multiplyScalar(deltaTime);
this.mesh.position.add(frameDisp);
}
DetectCollision(batOne, batTwo)
{
var bat1Pos = batOne.mesh.position.clone()
var bat2Pos = batTwo.mesh.position.clone()
console.log(bat1Pos);
if (Math.abs(this.mesh.position.x - bat1Pos.x) < 1 && Math.abs(this.mesh.position.y - bat1Pos.y) < 4)
{
if (this.velocity.x < 0)
{
this.velocity.x = -(this.velocity.x * 1.05);
}
this.velocity.y = -0.0125 + Math.random() * 0.025;
}
if (Math.abs(this.mesh.position.x - bat2Pos.x) < 1 && Math.abs(this.mesh.position.y - bat2Pos.y) < 4)
{
if (this.velocity.x > 0)
{
this.velocity.x = -(this.velocity.x * 1.05);
}
this.velocity.y = -0.0125 + Math.random() * 0.025;
}
if (this.mesh.position.y > 15)
{
if (this.velocity.y > 0)
{
this.velocity.y = -this.velocity.y;
}
}
if (this.mesh.position.y < -15)
{
if (this.velocity.y < 0)
{
this.velocity.y = -this.velocity.y;
}
}
if (this.mesh.position.x > 35)
{
playerScore++;
this.ResetBall();
}
if (this.mesh.position.x < -35)
{
enemyScore++;
this.ResetBall();
}
}
ResetBall()
{
this.mesh.position.x = 0;
this.mesh.position.y = 0;
this.velocity = new THREE.Vector3(0.025, 0, 0);
}
}