Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.java
More file actions
Latest commit
122 lines (95 loc) · 2.85 KB
/
Copy pathAgent.java
File metadata and controls
122 lines (95 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
importjava.awt.geom.Point2D;
publicclassAgent {
publicstaticfinaldoubleRADIUS = .5;
publicstaticfinaldoubleMAX_SPEED_CHANGE = 1;
publicstaticfinaldoubleMAX_ANGLE_CHANGE = 1;
publicstaticfinaldoubleMAX_SPEED = 1;
publicPoint2Dlocation;
publicPoint2Ddestination;
publicdoublevelocity;
publicdoubleheading;
publicAgent()
{
location = newPoint2D.Double();
destination = newPoint2D.Double();
pickRandomDestination();
pickRandomPosition();
}
privatevoidpickRandomPosition() {
location.setLocation(Helper.getRandom() * Model.MAX_SIZE, Helper.getRandom()*Model.MAX_SIZE);
//location.setLocation(0, 0);
}
privatevoidpickRandomDestination() {
destination.setLocation(Helper.getRandom() * Model.MAX_SIZE, Helper.getRandom()*Model.MAX_SIZE);
//destination.setLocation(15, 20);
}
publicvoidupdate()
{
chooseSpeedAndVelocity();
updateDestination();
updatePosition();
}
privatevoidupdatePosition() {
location.setLocation(
location.getX() + Math.cos(heading) * velocity * Model.TIME_STEP,
location.getY() + Math.sin(heading) * velocity * Model.TIME_STEP
);
}
publicvoidchooseSpeedAndVelocity()
{
doubleoffsetToDestinationX = destination.getX() - location.getX();
doubleoffsetToDestinationY = destination.getY() - location.getY();
doubleabsoluteAngle = Math.atan2(offsetToDestinationY, offsetToDestinationX);
updateSpeedAndVelocity(1, absoluteAngle);
}
privatevoidupdateSpeedAndVelocity(doubledesiredSpeed, doubledesiredAngle) {
doubledesiredChangeInSpeedAbs = Math.abs(velocity - desiredSpeed);
if(desiredChangeInSpeedAbs < MAX_SPEED_CHANGE * Model.TIME_STEP)
{
velocity = desiredSpeed;
}
else
{
if(desiredSpeed > velocity)
velocity += MAX_SPEED_CHANGE * Model.TIME_STEP;
else
velocity -= MAX_SPEED_CHANGE * Model.TIME_STEP;
}
doubledesiredChangeInAngle = getMinimumAngle(desiredAngle, heading);
if(Math.abs(desiredChangeInAngle) < MAX_ANGLE_CHANGE * Model.TIME_STEP)
{
heading = desiredAngle;
}
else
{
if(desiredChangeInAngle > 0)
heading += MAX_ANGLE_CHANGE * Model.TIME_STEP;
else
heading -= MAX_ANGLE_CHANGE * Model.TIME_STEP;
}
clampHeadingAndValue();
}
privatedoublegetMinimumAngle(doubledesiredAngle, doubleheading2) {
doubledifference = desiredAngle - heading2;
while (difference < -Math.PI) difference += Math.PI * 2;
while (difference > Math.PI) difference -= Math.PI * 2;
returndifference;
}
privatevoidclampHeadingAndValue() {
if(velocity > MAX_SPEED)
velocity = MAX_SPEED;
if(velocity < 0)
velocity = 0;
while(heading < -Math.PI)
heading += Math.PI * 2;
while(heading > Math.PI)
heading -= Math.PI * 2;
}
publicvoidupdateDestination()
{
if(location.distance(destination) < 2)
{
pickRandomDestination();
}
}
}