-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
157 lines (138 loc) · 3.34 KB
/
main.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
// Global Variables
// Static Variables
var ballX = parseInt($("circle").attr('cx'));
var ballY = parseInt($("circle").attr('cy'));
var velocityInput;
var angleDeg;
var renderLoop;
var debug;
// Dynamic Variables
var currentVelocity;
var acceleration = -9.81;
var simActive = false;
var velocityX;
var velocityY;
var angleRad;
var ballRadius;
// Time Variable
var time;
var startTime;
var currTime = 0;
var timeDelta;
// DEBUG
// gSA
var gameStateArray;
// UI
// Initiate Simulator State
function simInit() {
// Set Ball Position
$("circle").attr({
cy: windowHeight * 0.75, cx: 20, r: 20
});
}
$(document).ready(function() {
// Canvas Resize
windowHeight = window.innerHeight;
$("#canvas").css('height', windowHeight);
$("svg").css('height', windowHeight * 0.8);
simInit()
});
// Reset
$("#reset-btn").on('click', function(event) {
event.preventDefault();
/* Act on the event */
simActive = false;
$("#velocityInput,#angleInput,#ballRadius").val("");
simInit();
});
// Check if Input is a Number
function numCheck(inputValue) {
if (isNaN(parseInt( inputValue ))) {
alert("One of your input values are not numbers");
console.error("Invalid Input Values");
return false;
}
else {
return true;
}
}
// Button Event
$("#start-btn").on('click', function(event) {
event.preventDefault();
/* Act on the event */
// Set simulator state to active
// simActive = true;
// Set Ball Radius
ballRadius = $("#ballRadius").val();
// Validate Radius
simActive = numCheck(ballRadius);
// Set Velocity
velocityInput = $("#velocityInput").val();
// Validate Velocity
simActive = numCheck(velocityInput);
velocityX = Math.cos(angleRad);
velocityY = Math.sin(angleRad);
// Set Angle
angleDeg = $("#angleInput").val();
// Validate Angle
simActive = numCheck(angleDeg);
angleRad = radToDeg(angleDeg);
// Set Start Time
startTime = new Date();
// Initiate Render Loop
if (simActive) {
renderLoop = setInterval(render,1);
renderLoop;
}
// DEBUG
console.log("Sim Started " + "SimActive: " + simActive);
// Start DEBUG loop
var debug = setInterval(debug,1000);
debug;
})
// Degree to Radians Calculator
function radToDeg(angle) {
return angle / (180 / Math.PI);
}
// Render
function render() {
// Check Sim Active
if (simActive) {
}
else {
clearInterval(renderLoop);
clearInterval(debug);
alert("Simulation has ended");
}
// Update Time
// time = new Date().getTime();
currTime = new Date().getTime();
timeDelta = 0.001 * (currTime - startTime);
// Set Velocities
velocityX = Math.cos(angleRad)*velocityInput;
velocityY = Math.sin(angleRad)*velocityInput;
// Distance
ballX = velocityX * timeDelta;
ballY = (windowHeight * 0.75) - (velocityY*timeDelta + 0.5*acceleration*(Math.pow(timeDelta,2)));
// Apply Attributes
$("circle").attr({
cx: (ballX + ballRadius),
cy: ballY ,
r: ballRadius
});
// Collision
if (ballY > (windowHeight)) {
simActive = false;
}
}
// DEBUG
function debug() {
gameStateArray = {
"simActive": simActive,
"Time": currTime,
"timeDelta": timeDelta,
"ballX": ballX,
"ballY": ballY,
};
console.log(gameStateArray);
}