© opyright ryecatcher Intellectual Property 2023
javascript source code for Baseball Vidgame Pitch Processing Functions
// FASTBALL FUNCTION =========================================================================================================================================================================
// called by throw_pitch() and used to derive and display ball trajectory
function fastball() {
y = y + velocity; //this increments y-axis variable by the amount of the velocity variable
document.querySelector("#baseball").style.top = y + "px"; //this displays baseball at y variable distance from the top of the field container
//console.log("y value: " + y); //for diagnostic
let opposite3 = Math.tan(angle * Math.PI / 180) * (y - 50); //use trigonometry to derive every x-axis value at every increment of y-axis variable value (see illustration below)
//ie. multiply tangent of angle of trajectory times the y-axis value; ie. opposite = Math.tan(radian value of angle)*adjacent
// NOTE: angle variable is in degrees therefore must convert to radians to use javascript Math.tan function
/* angle SOH CAH TOA
/|
hypotenuse / | adjacent side ie. in this case is distance from pitcher release point hypotenuse side will represent trajectory of ball from pitcher release
side / | to strike zone point to the pre-selected deviation distance from centre of stike zone
/___|
opposite side ie. in this case is the deviation distance the ball will end up from center of strike zone
*/
opposite3 = opposite3 * plate_location_left_right; //to decide which side of the strike zone the ball will travel ie. left or right
//plate_location_left_right variable is randomly pre-determined and is equal to either +1 or -1
x = 150 + Math.round(opposite3); //x variable distance is determined by adding trignometrically derived x-axis value (opposite3) to centre of strike zone (150)
//if variable opposite3 is negative then x value will be to left side of centre of strike zone and vice versa
document.querySelector("#baseball").style.left = x + "px"; //this displays baseball at x variable distance from the left of the field container
//console.log("y value: " + y + " x value: " + x); //for diagnostic
bb_left = document.querySelector("#baseball").offsetLeft; //left edge of baseball derived distance from left edge of field container
bb_right = bb_left + document.querySelector("#baseball").offsetWidth; //right edge
bb_top = document.querySelector("#baseball").offsetTop; //top edge of baseball derived distance from top edge of field container
bb_bottom = bb_top + document.querySelector("#baseball").offsetHeight; //bottom edge
//console.log("baseball top: " + document.querySelector("#baseball").offsetTop + bb_top); //diagnostic
//console.log("baseball bottom: " + bb_bottom); //diagnostic
//console.log("strike zone top: " + sz_top); //diagnostic
//console.log("strike zone bottom: " + sz_bottom); //diagnostic
if (bb_bottom >= sz_top) { //checks if ball is touching or below top of strike zone
if ((bb_right >= sz_left && bb_right <= sz_right) || (bb_left >= sz_left && bb_left <= sz_right)) { //checks if ball is touching or within sides of strike zone
inside_strike_zone = true; //this variable indicates this pitch is eligible for strike and/or swing contact
//console.log("inside strike zone: " + inside_strike_zone); //diagnostic
}
}
if (y >= 625) { //check if ball has reached the bottom of the field container
//document.querySelector("#ready").style.display = "block";
pitcher_standing(); //to display pitcher standing ready for next pitch
document.querySelector("#swing").removeEventListener("touchstart", swing); //TOUCHSCREEN >>>>>>>>>>>>>>>>>>>
document.querySelector("#swing").removeEventListener("mousedown", swing);
document.querySelector("#swing").style.display = "none";
window.clearInterval(pitchTimer); //this stops the interval function when ball reaches the bottom of the field container
y = 50; //reset the y value to pitch release point location so its set for next pitch
//if (y >= 625 && (inside_strike_zone === true)) {
if ((inside_strike_zone === true)) { //check if this pitch was eligible for strike status ie.passed thru or clipped strikezone
strikes = strikes + 1; //since it reached bottom of field means it was a strike therefore increment cummultive strikes variable
/* if (strikes > 2) {
window.clearInterval(pitchTimer);
AB_result = "strikeout";
strkout(); //goto strkout function to process out & associated displays & transitions etc
} */
document.querySelector("#strikes_display").textContent = strikes; //update cummulative strikes on pitching field display
document.querySelector("#diamond_strikes").textContent = strikes; //update cummulative strikes on diamond field display
document.querySelector("#swing_called_indicator").textContent = "called";
document.querySelector("#strike_ball_indicator").textContent = "strike " + strikes; //update pitch result indicator display just in front of strike zone
// document.querySelector("#strike_ball").textContent = "STRIKE!!!!"; //diagnostic strike hit indicator
inside_strike_zone = false; //reset variable which indicates pitch is eligible for strike and/or swing contact to default value to prepare for next pitch
if (strikes > 2) { //check if cummulative stikes variable indicates a strikeout ie. 3 strikes
window.clearInterval(pitchTimer);
AB_result = "strikeout"; //assign variable used to display at bat result in diamond field
strkout(); //goto strkout function to process out & associated displays & transitions etc
}
//} else if (y >= 625) {
} else { //check if this pitch was NOT eligible for strike status ie. did not pass thru or clip strikezone
balls = balls + 1; //since it reached bottom of field means it was a ball therefore increment cummultive balls variable
document.querySelector("#balls_display").textContent = balls; //update cummulative balls on pitching field display
document.querySelector("#diamond_balls").textContent = balls; //update cummulative balls on diamond field display
document.querySelector("#swing_called_indicator").textContent = "called";
document.querySelector("#strike_ball_indicator").textContent = "ball " + balls; //update pitch result indicator display just in front of strike zone
//if balls > 3 goto walk function!! ********************************************************
if (balls > 3) { //check if cummulative balls variable indicates a walk ie. 4 balls
window.clearInterval(pitchTimer);
AB_result = "walk"; //assign variable used to display at bat result in diamond field
walk(); //goto walk function to process out & associated displays & transitions etc
}
}
inside_strike_zone = false; //reset variable which indicates pitch is eligible for strike and/or swing contact to default value to prepare for next pitch
window.setTimeout(strike_delay, 1000);
}
} //end of fastball function
// CURVEBALL FUNCTION =========================================================================================================================================================================
// called by throw_pitch() and used to derive and display ball trajectory
function curveball() {
if (y >= 50 && y <= 480) { //check if the ball is within these two points; ball will NOT break within this zone
y = y + velocity;
document.querySelector("#baseball").style.top = y + "px";
//console.log("y value: " + y); //diagnostic
let opposite3 = Math.tan(angle * Math.PI / 180) * (y - 50);
opposite3 = opposite3 * plate_location_left_right;
x = 150 + Math.round(opposite3);
document.querySelector("#baseball").style.left = x + "px";
// console.log("y value: " + y + " x value: " + x); //diagnostic
} else { //if ball passes beyond specified zone is elligible to break along x-axis
y = y + velocity;
document.querySelector("#baseball").style.top = y + "px";
if (pitch_type === "curveball" || pitch_type === "slider") {
// console.log("increment curve left!!"); //diagnostic
x = x + curveball_break_amount; //increment x-axis value by curveball_break_amount variable amount to create increasing break in direction
} else { x = x - curveball_break_amount; } //ie. send curve or break in opposite direction for display of screwball
document.querySelector("#baseball").style.left = x + "px";
// console.log("y value: " + y + " x value: " + x); //diagnostic
}
bb_left = document.querySelector("#baseball").offsetLeft; //left edge of baseball
bb_right = bb_left + document.querySelector("#baseball").offsetWidth; //right edge
bb_top = document.querySelector("#baseball").offsetTop; //top edge of baseball
bb_bottom = bb_top + document.querySelector("#baseball").offsetHeight; //bottom edge
// console.log("baseball top: " + document.querySelector("#baseball").offsetTop + bb_top); //diagnostic
// console.log("baseball bottom: " + bb_bottom);
// console.log("strike zone top: " + sz_top);
// console.log("strike zone bottom: " + sz_bottom);
if (bb_bottom >= sz_top) {
if ((bb_right >= sz_left && bb_right <= sz_right) || (bb_left >= sz_left && bb_left <= sz_right)) {
inside_strike_zone = true;
console.log("inside strike zone: " + inside_strike_zone);
}
}
if (y >= 625) {
//document.querySelector("#ready").style.display = "block";
//y = 50;
pitcher_standing(); //to display pitcher standing ready for next pitch
document.querySelector("#swing").removeEventListener("touchstart", swing); //TOUCHSCREEN >>>>>>>>>>>>>>>>>>>>
document.querySelector("#swing").removeEventListener("mousedown", swing);
document.querySelector("#swing").style.display = "none";
window.clearInterval(pitchTimer);
y = 50;
//if (y >= 625 && (inside_strike_zone === true)) {
if ((inside_strike_zone === true)) {
strikes = strikes + 1;
/* if (strikes > 2) {
window.clearInterval(pitchTimer);
AB_result = "strikeout";
strkout(); //goto strkout function to process out & associated displays & transitions etc
} */
document.querySelector("#strikes_display").textContent = strikes;
document.querySelector("#diamond_strikes").textContent = strikes;
document.querySelector("#swing_called_indicator").textContent = "called";
document.querySelector("#strike_ball_indicator").textContent = "strike " + strikes;
// document.querySelector("#strike_ball").textContent = "STRIKE!!!!"; //diagnostic strike hit indicator
inside_strike_zone = false;
if (strikes > 2) {
window.clearInterval(pitchTimer);
AB_result = "strikeout";
strkout(); //goto strkout function to process out & associated displays & transitions etc
}
//} else if (y >= 625) {
} else {
// document.querySelector("#strike_ball").textContent = "BALL!!!!"; //NOTE: diagnostic
balls = balls + 1;
document.querySelector("#balls_display").textContent = balls;
document.querySelector("#diamond_balls").textContent = balls;
document.querySelector("#swing_called_indicator").textContent = "called";
document.querySelector("#strike_ball_indicator").textContent = "ball " + balls;
//if balls > 3 goto walk function!! ********************************************************
if (balls > 3) {
window.clearInterval(pitchTimer);
AB_result = "walk";
walk(); //goto walk function to process out & associated displays & transitions etc
}
}
inside_strike_zone = false;
window.setTimeout(strike_delay, 1000);
}
} //end of curveball function
// KNUCKLEBALL FUNCTION =========================================================================================================================================================================
// called by throw_pitch() and used to derive and display ball trajectory
function knuckleball() {
if (y >= 50 && y <= 480) { //check if the ball is within these two points; ball will NOT break within this zone
y = y + velocity;
document.querySelector("#baseball").style.top = y + "px";
//console.log("y value: " + y);
let opposite3 = Math.tan(angle * Math.PI / 180) * (y - 50);
opposite3 = opposite3 * plate_location_left_right;
x = 150 + Math.round(opposite3);
let w = Math.round(Math.random() * 10); //derive random variable between 0 and 10
let z = x + w; //add random variable to x-axis value to simulate oscilations or fluttering
document.querySelector("#baseball").style.left = z + "px"; //this displays baseball at x variable distance from the left of the field container
// console.log("y value: " + y + " x value: " + z); //diagnostic
} else {
y = y + velocity;
document.querySelector("#baseball").style.top = y + "px";
if (break_direction === "rright") {
//console.log("knuckleball break amount: " + knuckleball_break_amount); //diagnostic
x = x + knuckleball_break_amount; //increment x-axis value by knuckleball_break_amount variable amount to create increasing break in direction
} else { x = x - knuckleball_break_amount } //ie. send curve or break in opposite direction depending on randomly derived break_direction variable
document.querySelector("#baseball").style.left = x + "px";
// console.log("y value: " + y + " x value: " + x); //diagnostic
// console.log("knuckleball break amount: " + knuckleball_break_amount) //diagnostic
}
bb_left = document.querySelector("#baseball").offsetLeft; //left edge of baseball
bb_right = bb_left + document.querySelector("#baseball").offsetWidth; //right edge
bb_top = document.querySelector("#baseball").offsetTop; //top edge of baseball
bb_bottom = bb_top + document.querySelector("#baseball").offsetHeight; //bottom edge
// console.log("baseball top: " + document.querySelector("#baseball").offsetTop + bb_top); //diagnostic
// console.log("baseball bottom: " + bb_bottom);
// console.log("strike zone top: " + sz_top);
// console.log("strike zone bottom: " + sz_bottom);
if (bb_bottom >= sz_top) {
if ((bb_right >= sz_left && bb_right <= sz_right) || (bb_left >= sz_left && bb_left <= sz_right)) {
inside_strike_zone = true;
// console.log("inside strike zone: " + inside_strike_zone); //diagnostic
}
}
if (y >= 625) {
//document.querySelector("#ready").style.display = "block";
//y = 50;
pitcher_standing(); //to display pitcher standing ready for next pitch
document.querySelector("#swing").removeEventListener("touchstart", swing); //TOUCHSCREEN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
document.querySelector("#swing").removeEventListener("mousedown", swing);
document.querySelector("#swing").style.display = "none"; //experiment ????????????????????????????????
window.clearInterval(pitchTimer);
y = 50;
break_direction = "lleft";
//if (y >= 625 && (inside_strike_zone === true)) {
if ((inside_strike_zone === true)) {
strikes = strikes + 1;
/* if (strikes > 2) {
window.clearInterval(pitchTimer);
AB_result = "strikeout";
strkout(); //goto strkout function to process out & associated displays & transitions etc
} */
document.querySelector("#strikes_display").textContent = strikes;
document.querySelector("#diamond_strikes").textContent = strikes;
document.querySelector("#swing_called_indicator").textContent = "called";
document.querySelector("#strike_ball_indicator").textContent = "strike " + strikes;
// document.querySelector("#strike_ball").textContent = "STRIKE!!!!"; //diagnostic strike hit indicator
inside_strike_zone = false;
if (strikes > 2) {
window.clearInterval(pitchTimer);
AB_result = "strikeout";
strkout(); //goto strkout function to process out & associated displays & transitions etc
}
//} else if (y >= 625) {
} else {
// document.querySelector("#strike_ball").textContent = "BALL!!!!"; //NOTE: diagnostic
balls = balls + 1;
document.querySelector("#balls_display").textContent = balls;
document.querySelector("#diamond_balls").textContent = balls;
document.querySelector("#swing_called_indicator").textContent = "called";
document.querySelector("#strike_ball_indicator").textContent = "ball " + balls;
//if balls > 3 goto walk function!! ********************************************************
if (balls > 3) {
window.clearInterval(pitchTimer);
AB_result = "walk";
walk(); //goto walk function to process out & associated displays & transitions etc
}
}
inside_strike_zone = false;
window.setTimeout(strike_delay, 1000);
}
} // end of knuckleball function