-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
218 lines (193 loc) · 6.69 KB
/
index.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
let myModal;
let canvas;
let context;
const fps = 30;
const mspf = 1000 / fps;
function showModal() {
myModal.style.display = 'block';
}
let updateHandler;
function closeModal() {
myModal.style.display = 'none';
lastTime = Date.now();
updateHandler = setInterval(() => {updatePhysics(); updateDraw(); }, mspf);
//canvas.addEventListener('mousemove', updatePlayerPos, false);
}
function resizeCanvas() {
canvas.width = window.innerWidth*0.95;
canvas.height = window.innerHeight*0.95;
}
function clearCanvas() {
context.clearRect(0, 0, canvas.width, canvas.height);
}
/* from https://stackoverflow.com/questions/17130395/real-mouse-position-in-canvas */
function getMousePos(evt) {
const rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
let world;
let engine;
let ball;
let bounds;
let mouseInstance;
let player1;
let player1mouse;
let player2; // AI Player
function physicsInit() {
world = Matter.World.create();
world.gravity.scale = 0;
engine = Matter.Engine.create();
engine.world = world;
console.log('physics init');
// ball
const radius = 0.02 * canvas.width;
ball = Matter.Bodies.circle(0.5*canvas.width, 0.5*canvas.height, radius, {
mass: 1,
friction: 0,
restitution: 1
});
Matter.World.addBody(world, ball);
ball.collisionFilter.group = 1;
//Matter.Body.applyForce(ball, ball.position, Matter.Vector.create(0.0005, 0.0005));
// bounds
const bold = 0.05*canvas.width;
const rect1 = Matter.Bodies.rectangle(bold/2, 0.165*canvas.height, bold, 0.33*canvas.height)
const rect2 = Matter.Bodies.rectangle(bold/2, 0.825*canvas.height, bold, 0.33*canvas.height);
const rect3 = Matter.Bodies.rectangle(0.50*canvas.width, bold/2, canvas.width, bold);
const rect4 = Matter.Bodies.rectangle(canvas.width - bold/2, 0.165*canvas.height, bold, 0.33*canvas.height);
const rect5 = Matter.Bodies.rectangle(canvas.width - bold/2, 0.825*canvas.height, bold, 0.33*canvas.height);
const rect6 = Matter.Bodies.rectangle(0.50*canvas.width, canvas.height - bold/2, canvas.width, bold);
const bounds = [rect1, rect2, rect3, rect4, rect5, rect6];
bounds.forEach(r=>{
Matter.Body.setStatic(r, true);
r.collisionFilter.group = 1;
r.slop = 0.01;
Matter.World.addBody(world, r);
console.log(r);
});
// player
mouseInstance = Matter.Mouse.create(canvas);
const playerradius = 0.025 * canvas.width;
player1 = Matter.Bodies.circle(0.25*canvas.width, 0.5*canvas.height, playerradius, {
mass: 1,
friction: 1,
frictionAir: 1,
restitution: 1
});
player1.collisionFilter.group = 1;
player1mouse = Matter.MouseConstraint.create(engine, {
mouse: mouseInstance,
constraint: {
stiffness: 0.2
},
collisionFilter: {
group: 1
}
})
Matter.World.addBody(world, player1);
Matter.World.add(world, player1mouse);
// player 2
player2 = Matter.Bodies.circle(0.75*canvas.width, 0.5*canvas.height, playerradius, {
mass: 1,
restitution: 1
});
player2.collisionFilter.group = 1;
Matter.World.addBody(world, player2);
//Matter.Body.applyForce(player2, player2.position, Matter.Vector.create(0.0001, 0));
}
let lastTime;
function updatePhysics() {
const deltaMillis = Date.now() - lastTime;
restrictPlayerMove();
aiMove();
Matter.Engine.update(engine, deltaMillis);
}
function restrictPlayerMove() {
if (player1.position.x > canvas.width / 2) {
Matter.Body.setPosition(player1, {x: canvas.width / 2 - 1, y: player1.position.y})
}
}
function aiMove() {
const Vec = Matter.Vector;
const maxVel = 25;
if (ball.position.x < canvas.width / 2) {
const myX = 0.8 * canvas.width;
const tc = Math.abs((myX - ball.position.x) / ball.velocity.x);
const expectY = ball.position.y + ball.velocity.y * tc;
let aiPlan = Vec.create((myX - player2.position.x) / tc, (expectY - player2.position.y) / tc);
aiPlan = Vec.mult(Vec.div(aiPlan, Vec.magnitude(aiPlan)), Math.min(Vec.magnitude(aiPlan), maxVel));
Matter.Body.setVelocity(player2, aiPlan);
} else {
const ballTarget = Vec.add(ball.position, Vec.create(0.04*canvas.width, 0));
const rv = Vec.sub(ball.velocity, player2.velocity);
const rp = Vec.sub(ballTarget, player2.position);
const tc = Vec.magnitude(rp) / Vec.magnitude(rv);
if (!Number.isFinite(tc)) return;
const target = Vec.add(ballTarget, Vec.mult(ball.velocity, tc));
const targetVel = Vec.sub(target, player2.position);
let aiPlan = Vec.div(targetVel, tc);
aiPlan = Vec.mult(Vec.div(aiPlan, Vec.magnitude(aiPlan)), Math.min(Vec.magnitude(aiPlan), maxVel));
Matter.Body.setVelocity(player2, aiPlan);
}
}
function initialDraw() {
// Draw background
context.strokeStyle = 'rgb(0, 0, 0)';
context.fillStyle = 'rgb(0, 0, 0)';
const bold = 0.05*canvas.width;
context.fillRect(0, 0, bold, 0.33*canvas.height);
context.fillRect(0, 0.66*canvas.height, bold, 0.33*canvas.height);
context.fillRect(0, 0, canvas.width, bold);
context.fillRect(canvas.width-bold, 0, bold, 0.33*canvas.height);
context.fillRect(canvas.width-bold, 0.66*canvas.height, bold, 0.33*canvas.height);
context.fillRect(0, canvas.height-bold, canvas.width, bold);
context.fillRect(canvas.width*0.495, 0, canvas.width*0.005, canvas.height);
}
function drawBall() {
let radius = 0.02 * canvas.width;
context.beginPath();
context.strokeStyle = 'rgb(255, 50, 50)';
context.fillStyle = 'rgb(255, 50, 50)';
context.arc(ball.position.x, ball.position.y, radius, 0, 2*Math.PI);
context.fill();
context.closePath();
}
function drawPlayer() {
const radius = 0.025 * canvas.width;
// player 1
context.beginPath();
context.strokeStyle = 'rgb(50, 50, 255)';
context.fillStyle = 'rgba(50, 50, 255, 0.5)';
context.arc(player1.position.x, player1.position.y, radius, 0, 2*Math.PI);
context.fill();
context.closePath();
//player 2
context.beginPath();
context.strokeStyle = 'rgb(50, 255, 50)';
context.fillStyle = 'rgba(50, 255, 50, 0.5)';
context.arc(player2.position.x, player2.position.y, radius, 0, 2*Math.PI);
context.fill();
context.closePath();
}
function updateDraw() {
clearCanvas();
initialDraw();
drawBall();
drawPlayer();
}
window.onload = () => {
myModal = document.getElementById('myModal');
canvas = document.getElementById('gameCanvas');
context = canvas.getContext('2d');
/*window.addEventListener('resize', () => {
resizeCanvas();
updateDraw();
}, false);*/
resizeCanvas();
physicsInit();
updateDraw();
showModal();
}