How to do animation properly
1 view (last 30 days)
Show older comments
Hi,
I know this has been asked several times in similar topics but I still can't figure it out:
I want to animate a piston moving, it's just a x(t) coordinates with a rectangular shape around that represents the cylinder.
The way I did it works but there's a better way to do it (with "draw now" or something). Especially because my animation is either too slow, or too laggy. Ideally, I'd like to have a smooth animation which I can easily change the speed (i.e. real time, x0.5 slow motion, x2 speed up, ...).
How can I do that ?
Thanks ! Cheers
Here's the code that I did:
DATA.INPUT.ENGINE.Bore = 0.08;
DATA.INPUT.GEN.Stroke = 0.5;
DATA.OUTPUT.Position = [0.0237500000000000;0.0237574994206981;0.0237794266581027;0.0238155875552203;0.0238624455029500;0.0239113910901606;0.0239614976089817;0.0240127572792267;0.0240651628083553;0.0241187070710207;0.0241733827608842;0.0242291820228882;0.0242860960594328;0.0243441147186231;0.0244032260683662;0.0244634159587855;0.0245246674921708;0.0245869614725304;0.0246502775958542;0.0247145952082853;0.0247798933335706;0.0248461506881520;0.0249133456906116;0.0249814564699136;0.0250504608719232;0.0251203364688759;0.0251910605659896;0.0252626102098339;0.0253349621969114;0.0254080930835513;0.0254819791859167;0.0255565965968216;0.0256319211913398;0.0257079286338292;0.0257845943881304;0.0258618937238826;0.0259398017280782;0.0260182933106062;0.0260973432140212;0.0261769260204264;0.0262570161616289;0.0263375879282968;0.0264186154761281;0.0265000728369645;0.0265819339265034;0.0266641725527614;0.0267467624243422;0.0268296771596815;0.0269128902945667;0.0269963752919258;0.0270801055534749;0.0271640544184843;0.0272481951824069;0.0273325011022580;0.0274169454046241;0.0275015012956062;0.0275861419670415;0.0276708406058169;0.0277555704039394;0.0278403045655886;0.0279250163151103;0.0280096789058123;0.0280942656297787;0.0281787498233523;0.0282631048785002;0.0283473042486790;0.0284313214594135;0.0285151301123508;0.0285987038966767;0.0286820165975073;0.0287650421024252;0.0288477544095811;0.0289301276362993;0.0290121360240766;0.0290937539494235;0.0291749559324369;0.0292557166421953;0.0293360109035606;0.0294158137062325;0.0294951002109515;0.0295738457487803;0.0296520258626933;0.0297296162699996;0.0298065928894269;0.0298829318384978;0.0299586094718360;0.0300336023529962;0.0301078872750777;0.0301814412661248;0.0302542415954471;0.0303262657813117;0.0303974915961024;0.0304678970725200;0.0305374605106568;0.0306061604835979;0.0306739758444081;0.0307408857306197;0.0308068695715521;0.0308719070929716;0.0309359783233663;0.0309990635994717;0.0310611435714265;0.0311221992093269;0.0311822118070045;0.0312411629858532;0.0312990347097779;0.0313558092735413;0.0314114693188337;0.0314659978376694;0.0315193781757072;0.0315716098000198;0.0316230614092927;0.0316744330592274;0.0317258198981084;0.0317772494398966;0.0318287262841075;0.0318802517139828;0.0319318261055279;0.0319834496811529;0.0320351226285149;0.0320868451272652;0.0321386173497948;0.0321904394848215;0.0322423117252493;0.0322942342412060;0.0323462072212346;0.0323982308393620;0.0324503052944602;0.0325024307739026;0.0325546074594588;0.0326068355416481;0.0326591152463458;0.0327114467084822;0.0327638301284334;0.0328162657207960;0.0328687536749109;0.0329212941844306;0.0329738874473002;0.0330265336648032;0.0330792330320164;0.0331319857432580;0.0331847920064047;0.0332376520209222;0.0332905659914636;0.0333435341242091;0.0333965566252353;0.0334496337030426;0.0335027655621558;0.0335559524150515;0.0336091944692959;0.0336624919379511;0.0337158450314566;0.0337692537563852;0.0338227187595145;0.0338762400324021;0.0339298178008636;0.0339834522792551;0.0340371436870551;0.0340908922518675;0.0341446981996192;0.0341985617467043;0.0342524831282188;0.0343064625671377;0.0343605003027554;0.0344145965536377;0.0344687515452148;0.0345229655133328;0.0345772386952090;0.0346315713309726;0.0346859636555999;0.0347404159101239;0.0347949283292704;0.0348495011585441;0.0349041346384801;0.0349588290105090;0.0350135845159507;0.0350684014208990;0.0351232799691417;0.0351782204063546;0.0352332229869301;0.0352882879681342;0.0353434155998820;0.0353986061400201;0.0354538598495924;0.0355091769865951;0.0355645578113705;0.0356200025933177;0.0356755115945766;0.0357310850852138;0.0357867233335739;0.0358424266077934;0.0358981951770401;0.0359540293153075;0.0360099293057015;0.0360658954158339;0.0361219279332028;0.0361780273009621;0.0362341933447444;0.0362904267953469;0.0363467277755607;0.0364030965776401;0.0364595334934501;0.0365160388230501;0.0365726128627788;0.0366292559061480;0.0366859682517717;0.0367427502004830;0.0367996020552785;0.0368565241201674;0.0369135167026146;0.0369705801109818;0.0370277146535499;0.0370849206461362;0.0371421984007925;0.0371995482348172;0.0372569704717239;0.0373144654299879;0.0373720334347317;0.0374296748151845;0.0374873898939688;0.0375451790033720;0.0376030424758050;0.0376609806496964;0.0377189938594457;0.0377770824474347;0.0378352467566835;0.0378934871308395;0.0379518039167387;0.0380101974647532;0.0380686681245463;0.0381272162527651;0.0381858422102237;0.0382445463567584;0.0383033290563745;0.0383621906683783;0.0384211315649035;0.0384801521123384;0.0385392526868372;0.0385984336653161;0.0386576954253281;0.0387170383423569;0.0387764628093135;0.0388359692209587;0.0388955579600604;0.0389552294199161;0.0390149839975871;0.0390748220932577;0.0391347441047478;0.0391947504376372;0.0392548415051371;0.0393150177203005;0.0393752794919355;0.0394356272399113;0.0394960613851136;0.0395565823528721;0.0396171904773131;0.0396778863663399;0.0397386704097738;0.0397995429956852;0.0398605045683312;0.0399215555795495;0.0399826964729990;0.0400439277068469;0.0401052497375072;0.0401666630244075;0.0402281680305470;0.0402897652207195;0.0403514550770347;0.0404132380435421;0.0404751146268266;0.0405370851865239;0.0405991502866390;0.0406613105611854;0.0407235664206356;0.0407859183465719;0.0408483668363297;0.0409109123832224;0.0409735555083600;0.0410362967245962;0.0410991365552354;0.0411620755199045;0.0412251142088822;0.0412882530301068;0.0413514925812170;0.0414148334056777;0.0414782759933638;0.0415418209337917;0.0416054688436107;0.0416692202506666;0.0417330757285556;0.0417970358257169;0.0418611011332873;0.0419252722141715;0.0419895496592938;0.0420539340699214;0.0421184260330348;0.0421830261533039;0.0422477350350740;0.0423125532891748;0.0423774815623646;0.0424425204025726;0.0425076705538209;0.0425729325440343;0.0426383070471623;0.0427037947184636;0.0427693962096799;0.0428351121782021;0.0429009432890230;0.0429668902158012;0.0430329536298500;0.0430991342268684;0.0431654326810230;0.0432318497069154;0.0432983859981795;0.0433650422668903;0.0434318192291352;0.0434987175971175;0.0435657381232479;0.0436328815396001;0.0437001485871418;0.0437675400163417;0.0438350565885532;0.0439026990702388;0.0439704682382343;0.0440383648738797;0.0441063897700835;0.0441745437244072;0.0442428275444124;0.0443112420435275;0.0443797880447420;0.0444484663775832;0.0445172778878081;0.0445862234255350;0.0446553038367427;0.0447245199921201;0.0447938727663511;0.0448633630482741;0.0449329917277338;0.0450027597058947;0.0450726678982882;0.0451427172282075;0.0452129086100986;0.0452832429960241;0.0453537213570784;0.0454243446475450;0.0454951138368771;0.0455660299089829;0.0456370938575429;0.0457083066890356;0.0457796694153418;0.0458511830653780;0.0459228486837309;0.0459946673145732;0.0460666400272557;0.0461387678915218;0.0462110519938531;0.0462834934271011;0.0463560933106859;0.0464288527779997;0.0465017729582869;0.0465748551781501;0.0466481002929552;0.0467215095894303;0.0467950842771247;0.0468688255676751;0.0469427346199742;0.0470168127930486;0.0470910612850807;0.0471654813146078;0.0472400742518478;0.0473148413179063;0.0473897838476307;0.0474649031651291;0.0475402006163689;0.0476156775672810;0.0476913353896907;0.0477671756939830;0.0478431995359899;0.0479194084480811;0.0479958039193457;0.0480723874050265;0.0481491604091055;0.0482261244339904;0.0483032810077514;0.0483806316753443;0.0484581779968888;0.0485359215666205;0.0486138639996507;0.0486920069112556;0.0487703519536624;0.0488489007997819;0.0489276551461075;0.0490066167064951;0.0490857872258684;0.0491651684709880;0.0492447613257613;0.0493245690712761;0.0494045931973949;0.0494848354549085;0.0495652976909430;0.0496459818032572;0.0497268897296879;0.0498080234562144;0.0498893849760482;0.0499709763116070;0.0500527942236510;0.0501347850939813;0.0502168890101408;0.0502990814917730;0.0503813521318302;0.0504636965657188;0.0505461129751278;0.0506286006456796;0.0507111593256690;0.0507937889649133;0.0508764895927905;0.0509592612863331;0.0510421041427993;0.0511250182580811;0.0512080037283440;0.0512910606428293;0.0513741891115925;0.0514573892043720;0.0515406610867772;0.0516240048446687;0.0517074205625925;0.0517909083451730;0.0518744682953541;0.0519581006828097;0.0520418051917512;0.0521255821188913;0.0522094317477442;0.0522933541352865;0.0523773493112288;0.0524614173687415;0.0525455584128045;0.0526297725543573;0.0527140599117958;0.0527984205860427;0.0528828546706928;0.0529673622716963;0.0530519435017948;0.0531365984685653;0.0532213272780590;0.0533061300343051;0.0533910068530770;0.0534759578308166;0.0535609830736885;0.0536460827740314;0.0537312569796197;0.0538165057202052;0.0539018291382252;0.0539872273500653;0.0540727004723602;0.0541582486549103;0.0542438719832613;0.0543295705202192;0.0544153443971412;0.0545011937348401;0.0545871186320890;0.0546731192034572;0.0547591955607339;0.0548453478144935;0.0549315760755099;0.0550178804465559;0.0551042610480844;0.0551907179901166;0.0552772513825154;0.0553638613406590;0.0554505479595055;0.0555373113723812;0.0556241516781904;0.0557110689893960;0.0557980634307351;0.0558851351069923;0.0559722841279432;0.0560595106060150;0.0561468146572415;0.0562341963941474;0.0563216559411325;0.0564091934033871;0.0564968088950222;0.0565845025247338;0.0566722744105935;0.0567601246784788;0.0568480534371321;0.0569360607905912;0.0570241468461077;0.0571123117434530;0.0572005556388130;0.0572888786234503;0.0573772807507021;0.0574657621543845;0.0575543229740261;0.0576429633221001;0.0577316833283562;0.0578204831039628;0.0579093627648809;0.0579983224321078;0.0580873622182739];
TIME = [0;0.000200000000000000;0.000400000000000000;0.000600000000000000;0.000800000000000000;0.00100000000000000;0.00120000000000000;0.00140000000000000;0.00160000000000000;0.00180000000000000;0.00200000000000000;0.00220000000000000;0.00240000000000000;0.00260000000000000;0.00280000000000000;0.00300000000000000;0.00320000000000000;0.00340000000000000;0.00360000000000000;0.00380000000000000;0.00400000000000000;0.00420000000000000;0.00440000000000000;0.00460000000000000;0.00480000000000000;0.00500000000000000;0.00520000000000000;0.00540000000000000;0.00560000000000000;0.00580000000000000;0.00600000000000000;0.00620000000000000;0.00640000000000000;0.00660000000000000;0.00680000000000000;0.00700000000000000;0.00720000000000000;0.00740000000000000;0.00760000000000000;0.00780000000000000;0.00800000000000000;0.00820000000000000;0.00840000000000000;0.00860000000000000;0.00880000000000000;0.00900000000000000;0.00920000000000000;0.00940000000000000;0.00960000000000000;0.00980000000000000;0.0100000000000000;0.0102000000000000;0.0104000000000000;0.0106000000000000;0.0108000000000000;0.0110000000000000;0.0112000000000000;0.0114000000000000;0.0116000000000000;0.0118000000000000;0.0120000000000000;0.0122000000000000;0.0124000000000000;0.0126000000000000;0.0128000000000000;0.0130000000000000;0.0132000000000000;0.0134000000000000;0.0136000000000000;0.0138000000000000;0.0140000000000000;0.0142000000000000;0.0144000000000000;0.0146000000000000;0.0148000000000000;0.0150000000000000;0.0152000000000000;0.0154000000000000;0.0156000000000000;0.0158000000000000;0.0160000000000000;0.0162000000000000;0.0164000000000000;0.0166000000000000;0.0168000000000000;0.0170000000000000;0.0172000000000000;0.0174000000000000;0.0176000000000000;0.0178000000000000;0.0180000000000000;0.0182000000000000;0.0184000000000000;0.0186000000000000;0.0188000000000000;0.0190000000000000;0.0192000000000000;0.0194000000000000;0.0196000000000000;0.0198000000000000;0.0200000000000000;0.0202000000000000;0.0204000000000000;0.0206000000000000;0.0208000000000000;0.0210000000000000;0.0212000000000000;0.0214000000000000;0.0216000000000000;0.0218000000000000;0.0220000000000000;0.0222000000000000;0.0224000000000000;0.0226000000000000;0.0228000000000000;0.0230000000000000;0.0232000000000000;0.0234000000000000;0.0236000000000000;0.0238000000000000;0.0240000000000000;0.0242000000000000;0.0244000000000000;0.0246000000000000;0.0248000000000000;0.0250000000000000;0.0252000000000000;0.0254000000000000;0.0256000000000000;0.0258000000000000;0.0260000000000000;0.0262000000000000;0.0264000000000000;0.0266000000000000;0.0268000000000000;0.0270000000000000;0.0272000000000000;0.0274000000000000;0.0276000000000000;0.0278000000000000;0.0280000000000000;0.0282000000000000;0.0284000000000000;0.0286000000000000;0.0288000000000000;0.0290000000000000;0.0292000000000000;0.0294000000000000;0.0296000000000000;0.0298000000000000;0.0300000000000000;0.0302000000000000;0.0304000000000000;0.0306000000000000;0.0308000000000000;0.0310000000000000;0.0312000000000000;0.0314000000000000;0.0316000000000000;0.0318000000000000;0.0320000000000000;0.0322000000000000;0.0324000000000000;0.0326000000000000;0.0328000000000000;0.0330000000000000;0.0332000000000000;0.0334000000000000;0.0336000000000000;0.0338000000000000;0.0340000000000000;0.0342000000000000;0.0344000000000000;0.0346000000000000;0.0348000000000000;0.0350000000000000;0.0352000000000000;0.0354000000000000;0.0356000000000000;0.0358000000000000;0.0360000000000000;0.0362000000000000;0.0364000000000000;0.0366000000000000;0.0368000000000000;0.0370000000000000;0.0372000000000000;0.0374000000000000;0.0376000000000000;0.0378000000000000;0.0380000000000000;0.0382000000000000;0.0384000000000000;0.0386000000000000;0.0388000000000000;0.0390000000000000;0.0392000000000000;0.0394000000000000;0.0396000000000000;0.0398000000000000;0.0400000000000000;0.0402000000000000;0.0404000000000000;0.0406000000000000;0.0408000000000000;0.0410000000000000;0.0412000000000000;0.0414000000000000;0.0416000000000000;0.0418000000000000;0.0420000000000000;0.0422000000000000;0.0424000000000000;0.0426000000000000;0.0428000000000000;0.0430000000000000;0.0432000000000000;0.0434000000000000;0.0436000000000000;0.0438000000000000;0.0440000000000000;0.0442000000000000;0.0444000000000000;0.0446000000000000;0.0448000000000000;0.0450000000000000;0.0452000000000000;0.0454000000000000;0.0456000000000000;0.0458000000000000;0.0460000000000000;0.0462000000000000;0.0464000000000000;0.0466000000000000;0.0468000000000000;0.0470000000000000;0.0472000000000000;0.0474000000000000;0.0476000000000000;0.0478000000000000;0.0480000000000000;0.0482000000000000;0.0484000000000000;0.0486000000000000;0.0488000000000000;0.0490000000000000;0.0492000000000000;0.0494000000000000;0.0496000000000000;0.0498000000000000;0.0500000000000000;0.0502000000000000;0.0504000000000000;0.0506000000000000;0.0508000000000000;0.0510000000000000;0.0512000000000000;0.0514000000000000;0.0516000000000000;0.0518000000000000;0.0520000000000000;0.0522000000000000;0.0524000000000000;0.0526000000000000;0.0528000000000000;0.0530000000000000;0.0532000000000000;0.0534000000000000;0.0536000000000000;0.0538000000000000;0.0540000000000000;0.0542000000000000;0.0544000000000000;0.0546000000000000;0.0548000000000000;0.0550000000000000;0.0552000000000000;0.0554000000000000;0.0556000000000000;0.0558000000000000;0.0560000000000000;0.0562000000000000;0.0564000000000000;0.0566000000000000;0.0568000000000000;0.0570000000000000;0.0572000000000000;0.0574000000000000;0.0576000000000000;0.0578000000000000;0.0580000000000000;0.0582000000000000;0.0584000000000000;0.0586000000000000;0.0588000000000000;0.0590000000000000;0.0592000000000000;0.0594000000000000;0.0596000000000000;0.0598000000000000;0.0600000000000000;0.0602000000000000;0.0604000000000000;0.0606000000000000;0.0608000000000000;0.0610000000000000;0.0612000000000000;0.0614000000000000;0.0616000000000000;0.0618000000000000;0.0620000000000000;0.0622000000000000;0.0624000000000000;0.0626000000000000;0.0628000000000000;0.0630000000000000;0.0632000000000000;0.0634000000000000;0.0636000000000000;0.0638000000000000;0.0640000000000000;0.0642000000000000;0.0644000000000000;0.0646000000000000;0.0648000000000000;0.0650000000000000;0.0652000000000000;0.0654000000000000;0.0656000000000000;0.0658000000000000;0.0660000000000000;0.0662000000000000;0.0664000000000000;0.0666000000000000;0.0668000000000000;0.0670000000000000;0.0672000000000000;0.0674000000000000;0.0676000000000000;0.0678000000000000;0.0680000000000000;0.0682000000000000;0.0684000000000000;0.0686000000000000;0.0688000000000000;0.0690000000000000;0.0692000000000000;0.0694000000000000;0.0696000000000000;0.0698000000000000;0.0700000000000000;0.0702000000000000;0.0704000000000000;0.0706000000000000;0.0708000000000000;0.0710000000000000;0.0712000000000000;0.0714000000000000;0.0716000000000000;0.0718000000000000;0.0720000000000000;0.0722000000000000;0.0724000000000000;0.0726000000000000;0.0728000000000000;0.0730000000000000;0.0732000000000000;0.0734000000000000;0.0736000000000000;0.0738000000000000;0.0740000000000000;0.0742000000000000;0.0744000000000000;0.0746000000000000;0.0748000000000000;0.0750000000000000;0.0752000000000000;0.0754000000000000;0.0756000000000000;0.0758000000000000;0.0760000000000000;0.0762000000000000;0.0764000000000000;0.0766000000000000;0.0768000000000000;0.0770000000000000;0.0772000000000000;0.0774000000000000;0.0776000000000000;0.0778000000000000;0.0780000000000000;0.0782000000000000;0.0784000000000000;0.0786000000000000;0.0788000000000000;0.0790000000000000;0.0792000000000000;0.0794000000000000;0.0796000000000000;0.0798000000000000;0.0800000000000000;0.0802000000000000;0.0804000000000000;0.0806000000000000;0.0808000000000000;0.0810000000000000;0.0812000000000000;0.0814000000000000;0.0816000000000000;0.0818000000000000;0.0820000000000000;0.0822000000000000;0.0824000000000000;0.0826000000000000;0.0828000000000000;0.0830000000000000;0.0832000000000000;0.0834000000000000;0.0836000000000000;0.0838000000000000;0.0840000000000000;0.0842000000000000;0.0844000000000000;0.0846000000000000;0.0848000000000000;0.0850000000000000;0.0852000000000000;0.0854000000000000;0.0856000000000000;0.0858000000000000;0.0860000000000000;0.0862000000000000;0.0864000000000000;0.0866000000000000;0.0868000000000000;0.0870000000000000;0.0872000000000000;0.0874000000000000;0.0876000000000000;0.0878000000000000;0.0880000000000000;0.0882000000000000;0.0884000000000000;0.0886000000000000;0.0888000000000000;0.0890000000000000;0.0892000000000000;0.0894000000000000;0.0896000000000000;0.0898000000000000;0.0900000000000000;0.0902000000000000;0.0904000000000000;0.0906000000000000;0.0908000000000000;0.0910000000000000;0.0912000000000000;0.0914000000000000;0.0916000000000000;0.0918000000000000;0.0920000000000000;0.0922000000000000;0.0924000000000000;0.0926000000000000;0.0928000000000000;0.0930000000000000;0.0932000000000000;0.0934000000000000;0.0936000000000000;0.0938000000000000;0.0940000000000000;0.0942000000000000;0.0944000000000000;0.0946000000000000;0.0948000000000000;0.0950000000000000;0.0952000000000000;0.0954000000000000;0.0956000000000000;0.0958000000000000;0.0960000000000000;0.0962000000000000;0.0964000000000000;0.0966000000000000;0.0968000000000000;0.0970000000000000;0.0972000000000000;0.0974000000000000;0.0976000000000000;0.0978000000000000;0.0980000000000000;0.0982000000000000;0.0984000000000000;0.0986000000000000;0.0988000000000000;0.0990000000000000;0.0992000000000000;0.0994000000000000;0.0996000000000000;0.0998000000000000];
figure(8)
i = 1;
N = length(TIME);
Step = 1e1;
for t = 1:Step:N
% PISTON POSITION
y_Piston = linspace(-DATA.INPUT.ENGINE.Bore/2, DATA.INPUT.ENGINE.Bore/2, 100);
x_Piston = DATA.OUTPUT.Position(t);
plot(x_Piston, y_Piston, 's', 'MarkerSize', 10, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r')
hold on
% CYLINDER
x_Cyl = [0 0 DATA.INPUT.GEN.Stroke DATA.INPUT.GEN.Stroke 0 0];
y_Cyl = [-DATA.INPUT.ENGINE.Bore/2 DATA.INPUT.ENGINE.Bore/2 DATA.INPUT.ENGINE.Bore/2 -DATA.INPUT.ENGINE.Bore/2 -DATA.INPUT.ENGINE.Bore/2 DATA.INPUT.ENGINE.Bore/2];
plot(x_Cyl, y_Cyl, 'k-', 'LineWidth', 8)
xlim([-0.1 DATA.INPUT.GEN.Stroke+0.1]);
ylim([-DATA.INPUT.GEN.Stroke/2-0.1 DATA.INPUT.GEN.Stroke/2+0.1]);
% END
title(['TIME = ', num2str(TIME(t)), ' s'])
FontStyle = {'interpreter','latex'};
xlabel('$POSITION [m]$', FontStyle{:})
anim(i) = getframe;
i = i+1;
hold off
end
0 Comments
Accepted Answer
J Chen
on 29 Nov 2019
Draw the cylinder first. Use the "rectangle" command in a loop to draw the piston, e.g.,
rectangle('position',[x,y,w,h],'FaceColor',[1 0 0],'EdgeColor',[1 0 0])
Update only the position of the rectangle object using the following commands
h = gca;
h.Children.Position = [x_new,y_new,w,h];
1 Comment
More Answers (0)
See Also
Categories
Find more on Animation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!