Cybernetics Wiki
Advertisement

Класс v_Noise_[]

 1 /**
 2  * Генерирует вектор случайным образом на определенное время
 3  * Generates a vector in a random direction for a specified time.
 4  * (c)1997 Georgia Tech Research Corporation
 5  * @author Tucker Balch
 6  */
 7 
 8 package EDU.gatech.cc.is.clay;
 9 
10 import java.lang.*;
11 import java.util.Random;
12 import EDU.gatech.cc.is.util.Vec2;
13 import EDU.gatech.cc.is.util.Units;
14 
15 
16 public class v_Noise_ extends NodeVec2
17 {
18 	/**
19 	 * Turns debug printing on or off.
20 	 */
21 	public static final boolean DEBUG = Node.DEBUG;
22 	private double TIMEOUT = 5.0;
23 	private	double accum = Math.random()*TIMEOUT;
24 	private Random r = new Random(0); // constant seed
25 
26 	/**
27 	 * Instantiate an v_Noise_ schema.
28 	 *
29 	 * @param t double, how long the random direction 
30 	 *	should persist in seconds.
31 	 */
32 	public v_Noise_(double t)
33 	{
34 		if (DEBUG) System.out.println("v_Noise_: instantiated.");
35 		last_val.sett(Math.random()*2*Math.PI);
36 		TIMEOUT = t;
37 		r.setSeed(System.currentTimeMillis());
38 	}
39 
40 
41 	/**
42 	 * Instantiate an v_Noise_ schema.
43 	 *
44 	 * @param t double, how long the random direction 
45 	 *	should persist in seconds.
46 	 * @param s long, the random number seed.
47 	 */
48 	public v_Noise_(double t, long s)
49 	{
50 		if (DEBUG) System.out.println("v_Noise_: instantiated.");
51 		last_val.sett(Math.random()*2*Math.PI);
52 		TIMEOUT = t;
53 		r.setSeed(s);
54 	}
55 
56 
57 	Vec2	last_val = new Vec2();
58 	long	lasttime = 0;
59 	/**
60 	Return a Vec2 representing a random direction to go
61 	for a period of time.
62 	@param timestamp only get new information if timestamp > than last call
63                 or timestamp == -1.
64 	@return the movement vector.
65 	*/
66 	public Vec2 Value(long timestamp)
67 	{
68 		double	tempmag;
69 
70 		if ((timestamp > lasttime)||(timestamp == -1))
71 		{
72 			/*--- reset the timestamp ---*/
73 			double time_incd = (double)(timestamp-lasttime)/1000;
74 			if (timestamp > 0) lasttime = timestamp;
75 			else
76 				timestamp = lasttime + 1;
77 			accum += time_incd;
78 
79 			/*--- reset output ---*/
80 			if (accum > TIMEOUT)
81 			{
82 				accum = 0;
83 				last_val.sett(r.nextDouble()*2*Math.PI);
84 			}
85 		}
86 		return (new Vec2(last_val.x, last_val.y));
87 	}
88 }
Advertisement