MATLAB Answers

need help in making circles move with some velocity

1 view (last 30 days)
Faiza Gul
Faiza Gul on 23 Apr 2020
Commented: Faiza Gul on 29 Apr 2020
I am trying to make my static circles move with some linear velocity. I have created circles with "fill command" and I have separate function for linear movement but now I am unable to merge my linear movement function into actual function. I dont know where to put it.. I tried so many times but I always get static circles.
here is code:
N = numel(xobs);
for k=1:N
fill(xobs(k)+robs(k)*cos(theta),yobs(k)+robs(k)*sin(theta),[1 1 0]);
hold on
where as
xobs=[1.5 4.0 3.2 7.0 8]; yobs=[2.3 4 2 7 5]; robs=[0.5 0.5 0.5 0.5 0.5];
and for linear movement:
function [xp_new,yp_new]= motionlinear( xobs, yobs)
h= 0.1;
for k=1:h:5
xp_new = v*cos(theta)+ xobs;
yp_new = v*sin(theta)+ yobs;
plot (xp_new,yp_new,'-or') % I am trying t fit the plot somewhere in my actual code
hold on

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Apr 2020
Faiza - try the following
function myMainFunction
close all;
% initial position for five circles
xobs=[1.5 4.0 3.2 7.0 8];
yobs=[2.3 4 2 7 5];
robs=[0.5 0.5 0.5 0.5 0.5];
N = numel(xobs);
% create an array of handles to the fill objects (circles)
hFill = zeros(N,1);
for k=1:N
hFill(k) = fill(xobs(k)+robs(k)*cos(theta),yobs(k)+robs(k)*sin(theta),[1 1 0]);
hold on;
% set some arbitraty limits on the axes
ylim([0 50])
xlim([0 50])
while true
% call your linear motion function - the output replaces the input
[xobs, yobs] = motionlinear(xobs, yobs);
for k = 1:N
% updated the x and y data for each fill object
set(hFill(k), 'XData', xobs(k)+robs(k)*cos(theta), 'YData', yobs(k)+robs(k)*sin(theta));
function [xobs, yobs]= motionlinear(xobs, yobs)
% is v supposed to be an array of velocities?
v=[1 1];
% I chose this angle (not sure why you were using an array of values?)
% set the new positions
xobs = v(1)*cos(theta) + xobs;
yobs = v(2)*sin(theta) + yobs;
I made a couple of changes to the motionlinear function (see comments above) as I wasn't exactly sure what they were supposed to represent.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!