comparison ai/utilities.d @ 19:08ddf9e71b88

steer to avoid
author zzzzrrr <mason.green@gmail.com>
date Wed, 25 Mar 2009 14:44:47 -0400
parents 7f74e064dad5
children
comparison
equal deleted inserted replaced
18:7f74e064dad5 19:08ddf9e71b88
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 module openmelee.ai.utilities; 31 module openmelee.ai.utilities;
32 32
33 import tango.math.random.Kiss : Kiss; 33 import tango.math.random.Kiss : Kiss;
34 import blaze.common.bzMath : bzVec2, bzDot;
34 35
35 float scalarRandomWalk(float initial, float walkspeed, float min, float max) { 36 float scalarRandomWalk(float initial, float walkspeed, float min, float max) {
36 37
37 float next = initial + (((randomRange(0, 1) * 2) - 1) * walkspeed); 38 float next = initial + (((randomRange(0, 1) * 2) - 1) * walkspeed);
38 if (next < min) return min; 39 if (next < min) return min;
39 if (next > max) return max; 40 if (next > max) return max;
40 return next; 41 return next;
41 } 42 }
42 43
44 // return component of vector perpendicular to a unit basis vector
45 // IMPORTANT NOTE: assumes "basis" has unit magnitude(length==1)
46 bzVec2 perpendicularComponent(bzVec2 vector, bzVec2 unitBasis) {
47 return (vector - parallelComponent(vector, unitBasis));
48 }
49
50 // return component of vector parallel to a unit basis vector
51 // IMPORTANT NOTE: assumes "basis" has unit magnitude (length == 1)
52 bzVec2 parallelComponent(bzVec2 vector, bzVec2 unitBasis) {
53 float projection = bzDot(vector, unitBasis);
54 return unitBasis * projection;
55 }
56
43 // ---------------------------------------------------------------------------- 57 // ----------------------------------------------------------------------------
44 // classify a value relative to the interval between two bounds: 58 // classify a value relative to the interval between two bounds:
45 // returns -1 when below the lower bound 59 // returns -1 when below the lower bound
46 // returns 0 when between the bounds (inside the interval) 60 // returns 0 when between the bounds (inside the interval)
47 // returns +1 when above the upper bound 61 // returns +1 when above the upper bound
50 if (x < lowerBound) return -1; 64 if (x < lowerBound) return -1;
51 if (x > upperBound) return +1; 65 if (x > upperBound) return +1;
52 return 0; 66 return 0;
53 } 67 }
54 68
69 float square(float x) {
70 return x * x;
71 }
72
55 T randomRange(T = int) (T min, T max) 73 T randomRange(T = int) (T min, T max)
56 { 74 {
57 return min + Kiss.instance.natural() % (max + 1 - min); 75 return min + Kiss.instance.natural() % (max + 1 - min);
58 } 76 }