// These basic sine and cosine threads are used mainly for the bangalore bomb explosions. Usually, the bangalores are placed with 90,180,270,360 yaw-angles. // The explosion emitter lies 100 units away from the bangalore's origin. If the bangalore's angle is at a weird angle like 55,137,253,351 degrees, // then sine & cosine threads help to calculate the x-axis & y-axis coords for the explosion emitter. The 100 units value is the hypotenuse distance from the bangalore's origin. // Below shows the old code example, and 2 modified code examples using sine & cosine threads. // // local.bangaloreold.origin = ( 100 200 -50 ) // local.bangaloreold.angles = ( 0 -90 0 ) // local.explosionold.origin = local.bangalore1.origin + ( 0 -100 0 ) // // local.bangalore2.origin = ( 100 200 -50 ) // local.bangalore2.angles = ( 0 55 0 ) // local.sin2 = (waitthread global/math.scr::sine 55) * 100 // y-axis // local.cos2 = (waitthread global/math.scr::cosine 55) * 100 // x-axis // local.explosion2.origin = local.bangalore2.origin + ( local.cos2 local.sin2 0 ) // // local.bangalore3.origin = ( 100 200 -50 ) // local.bangalore3.angles = ( 0 55 0 ) // local.sin3 = (waitthread global/math.scr::sine local.bangalore3.angles[1]) * 100 // local.cos3 = (waitthread global/math.scr::cosine local.bangalore3.angles[1]) * 100 // local.explosion3.origin = local.bangalore3.origin + ( local.cos3 local.sin3 0 ) // // To verify the sine and cosine values, simply add the code lines below after waitthread maps/global/math.scr:: // iprintlnbold("local.sin2 = " + local.sin2) // iprintlnbold("local.cos2 = " + local.cos2) //////////////////////////////////////////////////////////////////////// // Maclaurin series expansion equations are used for "sin(degrees) = Y coordinate" and "cos(degrees) = X coordinate". // 1/(2!) = 0.5, 1/3! = 0.16666666666, 1/4! = 0.04166666666, 1/5! = 0.00833333333, 1/6! = 0.00138888888, // 1/7! = 0.00019841269, 1/8! = 0.00002480158, 1/9! = 0.00000275573, 1/10! = 0.00000027557, 1/11! = 0.00000002505 // This script only goes up to 11!. For more accuracy, add more factorials to the sine/cosine threads below & add more decimal places to all values, including PI. // sin(n) = n - n^3/3! + n^5/5! - n^7/7! + n^9/9! - n^11/11! + n^13/13! - n^15/15! + n^17/17! ..., "n" radians sine local.degrees: local.PI = 3.14159265359 // MOHAA console may only display first 3 decimals (3.142), but the game does compute beyond 3 decimals. if(local.degrees >= 180) { local.degrees = local.degrees - 360 } // local.player.angles[1] = local.degrees can sometimes be > 180 and < 360. // for sin/cos to work, degrees must be between -180, 0, 180. local.radians = local.degrees * (local.PI / 180) local.rad3rd = local.radians * local.radians * local.radians // = n^3 local.rad5th = local.radians * local.radians * local.radians * local.radians * local.radians // = n^5 local.rad7th = local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians // = n^7 local.rad9th = local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians // = n^9 local.rad11th = local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians // = n^11 local.sine = local.radians - (local.rad3rd * 0.16666666666) + (local.rad5th * 0.00833333333) - (local.rad7th * 0.00019841269) + (local.rad9th * 0.00000275573) - (local.rad11th * 0.00000002505) end local.sine //////////////////////////////////////////////////////////////////////// // cos(n) = 1 - n^2/2! + n^4/4! - n^6/6! + n^8/8! - n^10/10! + n^12/12! - n^14/14! + n^16/16! ..., "n" radians cosine local.degrees: local.PI = 3.14159265359 if(local.degrees >= 180) { local.degrees = local.degrees - 360 } local.radians = local.degrees * (local.PI / 180) local.rad2nd = local.radians * local.radians // = n^2 local.rad4th = local.radians * local.radians * local.radians * local.radians // = n^4 local.rad6th = local.radians * local.radians * local.radians * local.radians * local.radians * local.radians // = n^6 local.rad8th = local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians // = n^8 local.rad10th = local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians * local.radians // = n^10 local.cosine = 1 - (local.rad2nd * 0.5) + (local.rad4th * 0.04166666666) - (local.rad6th * 0.00138888888) + (local.rad8th * 0.00002480158) - (local.rad10th * 0.00000027557) end local.cosine //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// // Not needed for sine & cosine threads, but can be used to verify math. When player faces 45, 135, 225, 315 degrees, tangent should = 1 or -1. // // local.sin = waitthread global/math.scr::sine 45 // 45 degrees // local.cos = waitthread global/math.scr::cosine 45 // // local.mathtest = waitthread global/math.scr::tangent local.sin local.cos // iprintlnbold("sin(45 degrees) = " + local.sin + " ... cos(45 degrees) = " + local.cos) // iprintlnbold("tan(45 degrees) = " + local.mathtest) // // tan(n) = sin(n) / cos(n) tangent local.sine local.cosine: local.tangent = local.sine / local.cosine end local.tangent //////////////////////////////////////////////////////////////////////// // Not needed for sine & cosine threads, but can be used to verify math. Example: if horizontaldist = 100, then hypotenusedist should = 99 or 100 every time. // The hypotenusedist will mostly deviate from 100 as the player faces +/- 180 degrees or +/- 3.142 radians. // // local.mathtest = waitthread global/math.scr::pythagorean local.sin local.cos 100 // iprintlnbold("distance of 100; hypotenusedist = " + local.mathtest) // // hypotenuse^2 = length^2 + width^2 ... p^2 = x^2 + y^2. pythagorean local.sine local.cosine local.distance: local.hypotenusedist = ((local.sine * local.sine) + (local.cosine * local.cosine)) / local.distance end local.hypotenusedist //////////////////////////////////////////////////////////////////////// // Not needed for sine & cosine threads, but can be used to verify math: // // local.mathtest = waitthread global/math.scr::factorial 7 // iprintlnbold("7! = " + local.mathtest) // // n! = n * (n-1) * (n-2) * (n-3) * (n-4) * (n - (n-1)); and (n - (n-1)) = 1; // 0!=1, 1!=1, 2!=2, 3!=6, 4!=24, 5!=120, 6!=720, 7!=5040, 8!=40320 factorial local.n: local.factorial = 1 for(local.p = 0; local.p < local.n; local.p++) { local.factorial = local.factorial * (local.n - local.p) } end local.factorial