-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBehaviour__Evade.m
38 lines (35 loc) · 1.26 KB
/
Behaviour__Evade.m
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
function Behaviour__Evade (vehicles,vNum, target)
%% global variables
global TimeSteps;
%% first draw
[v_Image,v_Alpha,VehiclesPlot,fHandler] = InitializeGraphics(vehicles,vNum);
%% target is optional, if target is undefined,
% then get mouse position on move cursor as target
if ~exist('target','var')
target = [0 0 0];
set(fHandler, 'WindowButtonMotionFcn',@cursorMoveCallback);
end
function cursorPosition = cursorMoveCallback(o,e)
p = get(gca,'CurrentPoint');
cursorPosition(1:2) = p(1,1:2);
cursorPosition(3) = 0;
title( sprintf('(%g,%g)',cursorPosition) );
setappdata(0,'cursorPosition',cursorPosition); % save cursorPosition
end
%% calculate vehicles' positions to move to each iteration
setappdata(0, 'target_past', [0 0 0]);
timeTick = 1;
while (timeTick < TimeSteps)
for vhl = 1:vNum
target = getappdata(0,'cursorPosition'); % get cursorPosition.
target_velocity = target - getappdata(0,'target_past');
setappdata(0, 'target_past', target);
% steering
force = steer_evade(vehicles, vhl, target, target_velocity);
vehicles = applyForce(vehicles, vhl, force);
end
% redraw
RedrawGraphics(vehicles,vNum,v_Image,v_Alpha,VehiclesPlot);
timeTick = timeTick+1;
end
end