-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBehaviour__CollisionAvoid_Seek.m
More file actions
44 lines (39 loc) · 1.43 KB
/
Behaviour__CollisionAvoid_Seek.m
File metadata and controls
44 lines (39 loc) · 1.43 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
function Behaviour__CollisionAvoid_Seek (vehicles,vNum, obstacles, obstaclesNum)
%% global variables
global TimeSteps;
%% first draw
[v_Image,v_Alpha,VehiclesPlot,fHandler] = InitializeGraphics(vehicles,vNum);
% draw obstacles
for i = 1:obstaclesNum
obstacle = obstacles(i,:);
p_obstacle = plot(obstacle(1), obstacle(2), 'o','MarkerFaceColor','r','Color','r');
end
%% 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
timeTick = 1;
while (timeTick < TimeSteps)
for vhl = 1:vNum
%seek
target = getappdata(0,'cursorPosition'); % get cursorPosition.
seek_force = steer_seek(vehicles, vhl, target);
avoid_force = steer_collision_avoidance(vehicles, vhl, obstacles, obstaclesNum);
force = seek_force + avoid_force;
vehicles = applyForce(vehicles, vhl, force);
end
% redraw
RedrawGraphics(vehicles,vNum,v_Image,v_Alpha,VehiclesPlot);
timeTick = timeTick+1;
end
end