1 /**
2 * Generates a vector towards a goal location
3 * that varies with distance from the goal. The attraction is
4 * increased linearly at greater distances.
5 * (c)1997, 1998 Tucker Balch
6 */
7
8 package EDU.gatech.cc.is.clay;
9
10 import java.lang.*;
11 import EDU.gatech.cc.is.util.Vec2;
12 import EDU.gatech.cc.is.util.Units;
13
14 public class v_LinearAttraction_v extends NodeVec2
15 {
16 /**
17 Turns debug printing on or off.
18 */
19 public static final boolean DEBUG = /*true;*/ Node.DEBUG;
20 private NodeVec2 embedded1;
21 private double controlled_zone = 1.0;
22 private double dead_zone = 0.0;
23
24 /**
25 Instantiate a v_LinearAttraction_v schema.
26 @param czr double, controlled zone radius.
27 @param dzr double, dead zone radius.
28 @param im1 double, the node that generates an
29 egocentric vector to the goal.
30 */
31 public v_LinearAttraction_v(double czr, double dzr, NodeVec2 im1)
32 {
33 if (DEBUG) System.out.println("v_LinearAttraction_v: instantiated.");
34 embedded1 = im1;
35 if ((czr < dzr) || (czr<0) || (dzr<0))
36 {
37 System.out.println("v_LinearAttraction_v: illegal parameters");
38 return;
39 }
40 controlled_zone = czr;
41 dead_zone = dzr;
42 }
43
44
45 Vec2 last_val = new Vec2();
46 long lasttime = 0;
47 /**
48 Return a Vec2 representing the direction to go towards the
49 goal. Magnitude varies with distance.
50 @param timestamp long, only get new information if timestamp > than last call
51 or timestamp == -1.
52 @return the movement vector.
53 */
54 public Vec2 Value(long timestamp)
55 {
56 double mag;
57 Vec2 goal = new Vec2();
58
59 if ((timestamp > lasttime)||(timestamp == -1))
60 {
61 if (DEBUG) System.out.println("v_LinearAttraction_v:");
62
63 /*--- reset the timestamp ---*/
64 if (timestamp > 0) lasttime = timestamp;
65
66 /*--- get the goal ---*/
67 goal = embedded1.Value(timestamp);
68
69 /*--- consider the magnitude ---*/
70 // inside dead zone?
71 if (goal.r < dead_zone)
72 {
73 mag = 0;
74 }
75 // inside control zone?
76 else if (goal.r < controlled_zone)
77 mag = (goal.r - dead_zone)/
78 (controlled_zone - dead_zone);
79 // outside control zone
80 else mag = 1.0;
81 if (DEBUG) System.out.println(mag+" "+goal.r);
82
83 /*--- set the vector ---*/
84 goal.setr(mag);
85 last_val = goal;
86 }
87 if (DEBUG) System.out.println(last_val.r+" "+goal.r);
88 return (new Vec2(last_val.x, last_val.y));
89 }
90 }