var dx = player_x - enemy_x;var dy = player_y - enemy_y;var distance = Math.sqrt(dx*dx + dy*dy);Abort("My distance away from the enemy is: "+ distance);
var dx = source_x - player_x;var dy = source_y - player_y;var distance = Math.sqrt(dx*dx + dy*dy);sound.setVolume((1- (distance / max_distance)) * 255);
What turns game coders off about mathematics (apart from a difficult writing style) is the apparent lack of relevance to games. This doesn't have to be the case! In this article I will try to show how some simple mathematical ideas are applied to game coding.
function TestMouse(){ var done = false; var arrow = GetSystemArrow(); var a_x = GetScreenWidth() / 2 - arrow.width / 2; var a_y = GetScreenHeight() / 2 - arrow.height / 2; var green = CreateColor(0, 255, 0); while (!done) { var x = GetMouseX(); var y = GetMouseY(); var dx = x - (a_x + arrow.width / 2); var dy = y - (a_y + arrow.height / 2); var angle = Math.atan2(dy, dx); Point(x, y, green); arrow.rotateBlit(a_x, a_y, angle); FlipScreen(); while (AreKeysLeft()) { if (GetKey() == KEY_ENTER) done = true; } }}
// Create a length 624 array to store the state of the generator int[0..623] MT int index = 0 // Initialize the generator from a seed function initialize_generator(int seed) { index := 0 MT[0] := seed for i from 1 to 623 { // loop over each other element MT[i] := last 32 bits of(1812433253 * (MT[i-1] xor (right shift by 30 bits(MT[i-1]))) + i) // 0x6c078965 } } // Extract a tempered pseudorandom number based on the index-th value, // calling generate_numbers() every 624 numbers function extract_number() { if index == 0 { generate_numbers() } int y := MT[index] y := y xor (right shift by 11 bits(y)) y := y xor (left shift by 7 bits(y) and (2636928640)) // 0x9d2c5680 y := y xor (left shift by 15 bits(y) and (4022730752)) // 0xefc60000 y := y xor (right shift by 18 bits(y)) index := (index + 1) mod 624 return y } // Generate an array of 624 untempered numbers function generate_numbers() { for i from 0 to 623 { int y := (MT[i] & 0x80000000) // bit 31 (32nd bit) of MT[i] + (MT[(i+1) mod 624] & 0x7fffffff) // bits 0-30 (first 31 bits) of MT[...] MT[i] := MT[(i + 397) mod 624] xor (right shift by 1 bit(y)) if (y mod 2) != 0 { // y is odd MT[i] := MT[i] xor (2567483615) // 0x9908b0df } } }
The first of the these laws is the Law of Inertia, which, to put in a very blunt and non-technical way, states :* Objects are lazy.Go get a cup of coffee, or a RockStar energy drink, and think about this for a moment. While you are doing this, notice all the lazy objects around you. Then, come back.You may have noticed that nothing moved -- unless you moved it. This is half of the Law of Inertia. Surprisingly, the second part is that if an object is in motion, it will keep moving. Here's the Law of Inertia stated more properly : * An object tend to stay still or keep moving in a straight line if no other forces act on them.You may have heard that if you hit a golf ball in space that it will travel on forever. This is part of the Law of Inertia. The golf ball will not actually travel forever. It will travel until it finds a force capable of changing its motion. This could be the gravity of a star many billions of miles away, or it could possibly smash into an asteroid. But then, the pieces would travel until there is force to act on them.
The only mathematical optimization advice I have that isn't (or won't be after some trial and error) is to use whole numbers whenever possible (the 2 in 'x/2', as opposed to the 0.5 in 'x*0.5').
Barring the aforementioned special case of bit shift optimization, I don't think this really applies in the general case, since all numbers in JS are floating point.