Game Development Reference
In-Depth Information
dart_x = Math.cos(angle) * dartboardDiameter;
if(Math.random() < 0.5){
dart_x *= -1;
}
var velocity = (
Math.sqrt(positionChange[0]*positionChange[0] +
positionChange[1]*positionChange[1])/
timeChange
);
var yAngle = (velocity - .6);
dart_y = Math.sin(yAngle) * dartboardDiameter;
dart_z_velocity = -0.1;
}
Basic collision detection
For our darts game, the only collision detection we will do is to determine if the dart has reached the
dartboard z-axis. In fact, it does not even need to fall within the bounds of the board. We will stop the dart
whenever it reaches the z depth of the dartboard.
function checkIfReachedBoard(){
if(dart_z == DART_START[2] || Math.abs(dart_z - dartboard_z) < 0.001){
paused = true;
$("#reset").attr("disabled", false);
calculateAndDisplayScore();
}
}
Scoring
To calculate the score of the dart throw, we define an array of segment values. Then we calculate the
angle of the throw and which segment it is in. Finally, we look up the value of the segment and display it in
an HTML paragraph. This is shown in Listing 7-32.
Listing 7-32. Scoring
function calculateAndDisplayScore(){
var scores = [13,4,18,1,20,
5,12,9,14,11,
6,10,15,2,17,
3,19,7,16,8];
var final_angle = Math.atan2(dart_y, dart_x);
var final_angle_degrees = final_angle * 180/Math.PI;
var landed_section = Math.floor(final_angle_degrees/
(360.0/dartboard_sections));
if(landed_section < 0){ landed_section = Math.abs(landed_section)+10; }
if(Math.sqrt(dart_x*dart_x + dart_y*dart_y) < (dartboardDiameter/2.0)){
$("#score").html("<strong>Nice Throw: (" + scores[landed_section] +
")</strong>");
}else{
$("#score").html("Practice is needed");
}
}
 
Search WWH ::




Custom Search