Creating a 2D moving object

23 ビュー (過去 30 日間)
Joe Bird
Joe Bird 2015 年 9 月 30 日
コメント済み: Atakan Botasun 2021 年 6 月 13 日
I would to create a 2D environment where an object (i.e. a basic shape) moves between defined positions.
Is it possible to set up this kind of system in MATLAB and how would I go about doing it?

採用された回答

Mike Garrity
Mike Garrity 2015 年 9 月 30 日
Sure, here's a simple example.
First we create an object. I'm using patch because it's good at all sorts of 2D shapes, and I'm putting it into a hgtransform because that will make it easy to move around:
x = [-1 , 1/3, 1/3, 1, 1/3, 1/3,-1 ];
y = [-1/3,-1/3,-1/2, 0, 1/2, 1/3, 1/3];
g = hgtransform;
patch('XData',x,'YData',y,'FaceColor','yellow','Parent',g)
Now we set up the coordinate system we want to move around in. The axis equal means that the scale in the X & Y directions will be the same, rather than the arbitrary scaling you use for charts.
axis equal
xlim([-10 10])
ylim([-10 10])
And then we can easily move between two points like this. The basic idea is to do linear interpolation between the two points and give the result to the makehgtform function to get a transform matrix. The hgtransform object will use that to move the patch.
pt1 = [-3 -4 0];
pt2 = [5 2 0];
for t=linspace(0,1,100)
g.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
drawnow
end
We can easily add in scaling and rotating too.
s1 = 1/2;
s2 = 2;
r1 = 0;
r2 = 2*pi/3;
for t=linspace(0,1,100)
g.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1), ...
'scale',s1 + t*(s2-s1), ...
'zrotate',r1 + t*(r2-r1));
drawnow
end
Is that enough to get started?
  4 件のコメント
YUZHE LIU
YUZHE LIU 2020 年 12 月 5 日
Then how to control its speed?
Atakan Botasun
Atakan Botasun 2021 年 6 月 13 日
Could implement a pause line, i.e.
pause(1)
Note that this example line will stop MATLAB execution completely for a second. Nothing will work for that one second. Make sure that it doesn't break things.

サインインしてコメントする。

その他の回答 (1 件)

Ikke dettenei
Ikke dettenei 2018 年 6 月 18 日
I was wondering. Is it possible to create a Electric vehicle simulation where a certain amount of EV`s are moving around and when they are low on battery, moving to the nearest charging station?
I`ve found this: https://se.mathworks.com/matlabcentral/fileexchange/28441-hybrid-electric-vehicle-model-in-simulink
Is it possible or am i dreaming too much? :P

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by