View Javadoc

1   package nl.tudelft.pogamut.unreal.agent.module.shooting.util;
2   
3   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
4   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
5   import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
6   import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.AgentInfo;
7   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
8   
9   /**
10   * Utilities to determine if a player is facing some location.
11   * 
12   * @author mpkorstanje
13   * 
14   */
15  public class FacingUtil {
16  
17  	/**
18  	 * What angle is considered to be maximum facing angle by default (in
19  	 * degrees).
20  	 */
21  	public static final double IS_FACING_ANGLE = 6;
22  
23  	/**
24  	 * Tells if the player is currently facing input location.
25  	 * 
26  	 * @param player
27  	 *            the player:
28  	 * 
29  	 * @param location
30  	 *            input location.
31  	 * @return True, if the bot is facing input location.
32  	 */
33  	public static boolean isFacing(AgentInfo info, ILocated location) {
34  		return isFacing(info, location, IS_FACING_ANGLE);
35  	}
36  
37  	/**
38  	 * Tells if the agent is currently facing input location.
39  	 * 
40  	 * @param player
41  	 *            the player:
42  	 * @param location
43  	 *            input location.
44  	 * @param angle
45  	 *            specifies maximum angle (in degrees) that will be still
46  	 *            considered as facing angle.
47  	 * @return True, if the angle between agent facing vector and input location
48  	 *         is smaller or equal to input angle.
49  	 */
50  	public static boolean isFacing(AgentInfo info, ILocated location, double angle) {
51  		if (info == null || location == null)
52  			return false;
53  
54  		return isFacing(info.getLocation(), info.getRotation(), location.getLocation(), angle);
55  	}
56  
57  	/**
58  	 * Tells you if the rotation at the origin is facing the target location.
59  	 * 
60  	 * @param origin
61  	 *            the location of the object that should be facing
62  	 * @param rotation
63  	 *            rotation of the object that should be facing
64  	 * @param target
65  	 *            what it should be facing
66  	 * @param angle
67  	 *            specifies maximum angle (in degrees) that will be still
68  	 *            considered as facing angle.
69  	 * @return True, if the angle between direction vector and input location is
70  	 *         smaller or equal to input angle.
71  	 */
72  	public static boolean isFacing(Location origin, Rotation rotation, Location target, double angle) {
73  		if (origin == null || rotation == null || target == null)
74  			return false;
75  
76  		Location directionVector = target.sub(origin).getNormalized();
77  		Location agentFaceVector = rotation.toLocation().getNormalized();
78  
79  		if (Math.acos(directionVector.dot(agentFaceVector)) <= Math.toRadians(angle))
80  			return true;
81  
82  		return false;
83  
84  	}
85  
86  	/**
87  	 * Tells if the player is currently facing input location.
88  	 * 
89  	 * @param player
90  	 *            the player:
91  	 * 
92  	 * @param location
93  	 *            input location.
94  	 * @return True, if the bot is facing input location.
95  	 */
96  	public static boolean isFacing(Player player, ILocated location) {
97  		return isFacing(player, location, IS_FACING_ANGLE);
98  	}
99  
100 	/**
101 	 * Tells if the player is currently facing input location.
102 	 * 
103 	 * @param player
104 	 *            the player:
105 	 * @param location
106 	 *            input location.
107 	 * @param angle
108 	 *            specifies maximum angle (in degrees) that will be still
109 	 *            considered as facing angle.
110 	 * @return True, if the angle between agent facing vector and input location
111 	 *         is smaller or equal to input angle.
112 	 */
113 	public static boolean isFacing(Player player, ILocated location, double angle) {
114 		if (player == null || location == null)
115 			return false;
116 
117 		return isFacing(player.getLocation(), player.getRotation(), location.getLocation(), angle);
118 	}
119 
120 	/**
121 	 * Tells if the player is currently facing input location.
122 	 * 
123 	 * @param player
124 	 *            the player:
125 	 * 
126 	 * @param location
127 	 *            input location.
128 	 * @return True, if the bot is facing input location.
129 	 */
130 	public static boolean isFacing2D(AgentInfo info, ILocated location) {
131 		return isFacing2D(info, location, IS_FACING_ANGLE);
132 	}
133 
134 	/**
135 	 * Tells if the agent is currently facing input location.
136 	 * 
137 	 * @param player
138 	 *            the player:
139 	 * @param location
140 	 *            input location.
141 	 * @param angle
142 	 *            specifies maximum angle (in degrees) that will be still
143 	 *            considered as facing angle.
144 	 * @return True, if the angle between agent facing vector and input location
145 	 *         is smaller or equal to input angle.
146 	 */
147 	public static boolean isFacing2D(AgentInfo info, ILocated location, double angle) {
148 		if (info == null || location == null)
149 			return false;
150 
151 		return isFacing2D(info.getLocation(), info.getRotation(), location.getLocation(), angle);
152 	}
153 
154 	/**
155 	 * Tells you if the rotation at the origin is facing the target location in
156 	 * the XY (horizontal) plane.
157 	 * 
158 	 * @param origin
159 	 *            the location of the object that should be facing
160 	 * @param rotation
161 	 *            rotation of the object that should be facing
162 	 * @param target
163 	 *            what it should be facing
164 	 * @param angle
165 	 *            specifies maximum angle (in degrees) that will be still
166 	 *            considered as facing angle.
167 	 * @return True, if the angle between direction vector and input location is
168 	 *         smaller or equal to input angle.
169 	 */
170 	public static boolean isFacing2D(Location origin, Rotation rotation, Location target, double angle) {
171 		if (origin == null || rotation == null || target == null)
172 			return false;
173 
174 		Location directionVector = target.sub(origin).setZ(0).getNormalized();
175 		Location agentFaceVector = rotation.toLocation().setZ(0).getNormalized();
176 
177 		if (Math.acos(directionVector.dot(agentFaceVector)) <= Math.toRadians(angle))
178 			return true;
179 
180 		return false;
181 
182 	}
183 
184 	/**
185 	 * Tells if the player is currently facing input location.
186 	 * 
187 	 * @param player
188 	 *            the player:
189 	 * 
190 	 * @param location
191 	 *            input location.
192 	 * @return True, if the bot is facing input location.
193 	 */
194 	public static boolean isFacing2D(Player player, ILocated location) {
195 		return isFacing2D(player, location, IS_FACING_ANGLE);
196 	}
197 
198 	/**
199 	 * Tells if the player is currently facing input location.
200 	 * 
201 	 * @param player
202 	 *            the player:
203 	 * @param location
204 	 *            input location.
205 	 * @param angle
206 	 *            specifies maximum angle (in degrees) that will be still
207 	 *            considered as facing angle.
208 	 * @return True, if the angle between agent facing vector and input location
209 	 *         is smaller or equal to input angle.
210 	 */
211 	public static boolean isFacing2D(Player player, ILocated location, double angle) {
212 		if (player == null || location == null)
213 			return false;
214 
215 		return isFacing2D(player.getLocation(), player.getRotation(), location.getLocation(), angle);
216 	}
217 
218 }