Вы находитесь на странице: 1из 519

The Nature of Code

?V $>KFBI 3EFCCJ>K 4EB MR?IFPEBO TLRIA DL EBOB, ?RQ QEBOB FPKgQ LKB; FQgP LKIV JB. SBOPFLK 1.0, DBKBO>QBA $B@BJ?BO 6, 2012

4EB .>QROB LC #LAB (S1.0)

Dedication
&LO JV DO>KAJLQEBO, "BII> ->KBI 'OBBKCFBIA (/@QL?BO 13, 1915 - !MOFI 3, 2010)

"BII> ->KBI T>P ?LOK FK .BT 9LOH #FQV. ! MFLKBBOFKD TLJ>K FK J>QEBJ>QF@P, PEB B>OKBA EBO 0E$ FK 1939 COLJ .BT 9LOH 5KFSBOPFQV RKABO QEB PRMBOSFPFLK LC 2F@E>OA #LRO>KQ. 3EB TLOHBA CLO 2>JL-7LLIAOFADB (KLT 427) >KA >Q QEB 2>KA #LOMLO>QFLK TFQE 2F@E>OA "BIIJ>K. ,>QBO, PEB Q>RDEQ J>QEBJ>QF@P >Q QEB #LIIBDB LC .LQOB $>JB (KLT .LQOB $>JB AB .>JRO 5KFSBOPFQV) FK "BIJLKQ, #>IFCLOKF>, >KA >Q 5#,!. 4EB "BII> ->KBI 0OFWB CLO LRQPQ>KAFKD DO>AR>QB TLOH ?V > TLJ>K LO JFKLOFQV T>P BPQ>?IFPEBA >Q .95gP #LRO>KQ )KPQFQRQB FK 1995.

FF

$BAF@>QFLK

#LMVOFDEQ X 2012 ?V $>KFBI 3EFCCJ>K )3".-13: 978-0985930806 )3".-10: 0985930802 4EFP TLOH FP IF@BKPBA RKABO QEB #OB>QFSB #LJJLKP !QQOF?RQFLK-.LK#LJJBO@F>I 3.0 5KMLOQBA ,F@BKPB. 4L SFBT > @LMV LC QEFP IF@BKPB, SFPFQ @OB>QFSB@LJJLKP.LOD (EQQM://@OB>QFSB@LJJLKP.LOD/IF@BKPBP/?V-K@/3.0/) LO PBKA > IBQQBO QL #OB>QFSB #LJJLKP, 444 #>PQOL 3QOBBQ, 3RFQB 900, -LRKQ>FK 6FBT, #>IFCLOKF> 94041, 53!. !II LC QEB ?LLHgP PLRO@B @LAB FP IF@BKPBA RKABO QEB '.5 ,BPPBO 'BKBO>I 0R?IF@ ,F@BKPB (EQQM://@OB>QFSB@LJJLKP.LOD/IF@BKPBP/,'0,/2.1/) >P MR?IFPEBA ?V QEB &OBB 3LCQT>OB &LRKA>QFLK; BFQEBO SBOPFLK 2.1 LC QEB ,F@BKPB, LO (>Q VLRO LMQFLK) >KV I>QBO SBOPFLK. 4EFP ?LLH T>P DBKBO>QBA ?V QEB ->DF@ "LLH 0OLGB@Q (EQQM://J>DF@?LLHMOLGB@Q.@LJ). %AFQLO )IIRPQO>QFLKP #LSBO $BPFDK )KQBOFLO $BPFDK 7B? 3FQB $BPFDK %AFQLOF>I >KA $BPFDK !PPFPQ>KQ ->DF@ "LLH ,B>A $BSBILMBOP ->DF@ "LLH 2BPB>O@EBOP )KABU 3E>KKLK &OV :>KK>E ->OPE $>SFA 7FIPLK $>SFA 7FIPLK 3QBSB +IFPB %S>K %JLIL 2RKB ->APBK, 3QBSB +IFPB %S>K %JLIL, -FDRBI "BOJRABW, ,RFP> 0BFOBO> (LOP 7LOA#L )KABUFKD 3BOSF@BP

FFF

4EB .>QROB LC #LAB (S1.0)

Acknowledgments
"*52 D<?91 .?<B;1 B@ :<C2@ 6; 0<:=960.A21 .;1 D<;12?3B9 D.F@. ,2 @=2;1 A52 2.?962? =.?A@ <3 <B? 96C2@ 92.?;6;4 ./<BA <B? 2;C6?<;:2;A A5?<B45 =2?02=A6<; .;1 6;A2?.0A6<;. ,2 2E=20A A52 =5F@60.9 D<?91 .?<B;1 B@ A< /25.C2 0<;@6@A2;A9F D6A5 <B? =2?02=AB.9 :2:<?F, 2.4. 63 D2 1?<= . ?<08 6A D699 3.99 1B2 A< 4?.C6AF, 63 . 4B@A <3 D6;1 /9<D@, 9645A2? </720A@ D699 /2 A<@@21 /F A52 D6;1 3B?A52?. *56@ 09.@@ 3<0B@2@ <; B;12?@A.;16;4, @6:B9.A6;4, .;1 6;0<?=<?.A6;4 :<A6<;-/.@21 292:2;A@ <3 <B? =5F@60.9 D<?91 6;A< A52 1646A.9 D<?91@ A5.A D2 0?2.A2. &B? 5<=2 6@ A< 0?2.A2 6;AB6A6C2, ?605, .;1 :<?2 @.A6@3F6;4 2E=2?62;02@ /F 1?.D6;4 3?<: A52 =2?02=AB.9 :2:<?62@ <3 <B? B@2?@." James Tu, Dynamic Bodies course description, Spring 2003, ITP

FS

!@HKLTIBADJBKQP

A.1 A little bit of history


)K 2003, >P > DO>AR>QB PQRABKQ >Q QEB )KQBO>@QFSB 4BIB@LJJRKF@>QFLKP 0OLDO>J ()40) FK QEB 4FP@E 3@ELLI LC QEB !OQP >Q .BT 9LOH 5KFSBOPFQV, ) BKOLIIBA FK > @LROPB @>IIBA $VK>JF@ "LAFBP. 4EB @LROPB T>P Q>RDEQ ?V FKQBO>@QFLK ABPFDKBO >KA )40 >AGRK@Q MOLCBPPLO *>JBP 4R. !Q QEB QFJB, JV TLOH T>P CL@RPBA LK > PBOFBP LC PLCQT>OB BUMBOFJBKQP QE>Q DBKBO>QBA OB>IQFJB hKLK-MELQLOB>IFPQF@i FJ>DBOV. 4EB >MMIF@>QFLKP FKSLISBA @>MQROFKD FJ>DBP COLJ > IFSB PLRO@B >KA hM>FKQFKDi QEB @LILOP TFQE BIBJBKQP QE>Q JLSBA >?LRQ QEB P@OBBK >@@LOAFKD QL S>OFLRP ORIBP. 4EB $VK>JF@ "LAFBP @LROPBeTEF@E @LSBOBA SB@QLOP, CLO@BP, LP@FII>QFLKP, M>OQF@IB PVPQBJP, OB@ROPFLK, PQBBOFKD, >KA PMOFKDPe>IFDKBA MBOCB@QIV TFQE JV TLOH. ) E>A ?BBK RPFKD QEBPB @LK@BMQP FKCLOJ>IIV FK JV LTK MOLGB@QP, ?RQ E>A KBSBO Q>HBK QEB QFJB QL @ILPBIV BU>JFKB QEB P@FBK@B ?BEFKA QEB >IDLOFQEJP LO IB>OK L?GB@Q-LOFBKQBA QB@EKFNRBP QL CLOJ>IFWB QEBFO FJMIBJBKQ>QFLK. 4E>Q SBOV PBJBPQBO, ) >IPL BKOLIIBA FK &LRKA>QFLKP LC 'BKBO>QFSB !OQ 3VPQBJP, > @LROPB Q>RDEQ ?V 0EFIFM '>I>KQBO, QE>Q CL@RPBA LK QEB QEBLOV >KA MO>@QF@B LC DBKBO>QFSB >OQ, @LSBOFKD QLMF@P PR@E >P @E>LP, @BIIRI>O >RQLJ>Q>, DBKBQF@ >IDLOFQEJP, KBRO>I KBQTLOHP, >KA CO>@Q>IP. "LQE 4RgP @LROPB >KA '>I>KQBOgP @LROPB LMBKBA JV BVBP QL > TLOIA LC PFJRI>QFLK >IDLOFQEJP >KA QB@EKFNRBP QE>Q @>OOFBA JB QEOLRDE QEB KBUQ PBSBO>I VB>OP LC TLOH >KA QB>@EFKD, >KA PBOSBA >P QEB CLRKA>QFLK >KA FKPMFO>QFLK CLO QEFP ?LLH. "RQ QEBOBgP >KLQEBO MFB@B LC QEB MRWWIB JFPPFKD COLJ QEFP PQLOV. '>I>KQBOgP @LROPB T>P JLPQIV QEBLOV-?>PBA, TEFIB 4RgP T>P Q>RDEQ RPFKD ->@OLJBAF> $FOB@QLO >KA QEB ,FKDL MOLDO>JJFKD I>KDR>DB. 4E>Q PBJBPQBO, ) IB>OKBA J>KV LC QEB >IDLOFQEJP ?V QO>KPI>QFKD QEBJ FKQL #++ (QEB I>KDR>DB ) T>P RPFKD NRFQB >THT>OAIV >Q QEB QFJB, TBII ?BCLOB #++ @OB>QFSB @LAFKD BKSFOLKJBKQP IFHB LMBK&O>JBTLOHP >KA #FKABO E>A >OOFSBA). 4LT>OAP QEB BKA LC QEB PBJBPQBO, ) AFP@LSBOBA PLJBQEFKD @>IIBA 0OL@BPPFKD (EQQM://TTT.MOL@BPPFKD.LOD). 0OL@BPPFKD T>P FK >IME> QEBK (SBOPFLK 0055) >KA, E>SFKD E>A PLJB BUMBOFBK@B TFQE *>S>, FQ FKQOFDRBA JB BKLRDE QL >PH QEB NRBPQFLK: #LRIA QEFP LMBKPLRO@B, >OQFPQ-COFBKAIV MOLDO>JJFKD I>KDR>DB >KA BKSFOLKJBKQ ?B QEB OFDEQ MI>@B QL ABSBILM > PRFQB LC QRQLOF>IP >KA BU>JMIBP >?LRQ MOLDO>JJFKD >KA PFJRI>QFLK? 7FQE QEB PRMMLOQ LC QEB )40 >KA 0OL@BPPFKD @LJJRKFQFBP, ) BJ?>OHBA LK TE>Q E>P KLT ?BBK >K >IJLPQ BFDEQVB>O GLROKBV LC QB>@EFKD > S>OFBQV LC MOLDO>JJFKD @LK@BMQP >KA QEBFO >MMIF@>QFLKP RPFKD 0OL@BPPFKD. )gA IFHB QL CFOPQ QE>KH 2BA "ROKP, )40gP CLRKABO, TEL E>P PRMMLOQBA >KA BK@LRO>DBA JB FK JV TLOH CLO LSBO QBK VB>OP. $>K /g3RIIFS>K, QEB @E>FO LC )40, E>P ?BBK JV QB>@EFKD JBKQLO >KA T>P QEB CFOPQ QL PRDDBPQ QE>Q ) QOV QB>@EFKD > @LROPB LK 0OL@BPPFKD, DFSFKD JB > OB>PLK QL PQ>OQ >PPBJ?IFKD MOLDO>JJFKD QRQLOF>IP FK QEB CFOPQ MI>@B. 3E>TK 6>K %SBOV, ABSBILMBO BUQO>LOAFK>FOB >KA >RQELO LC ,FC AB8FC=8 )98=5, E>P >IPL ?BBK > OF@E PLRO@B LC EBIM >KA FKPMFO>QFLK >Q )40 LSBO QEB VB>OP. )40 C>@RIQV JBJ?BOP #I>V 3EFOHV, $>KKV 2LWFK, +>QEBOFKB $FIILK, ->OF>KKB 0BQFQ, ->OFK> :ROHLT, >KA 4LJ )DLB E>SB MOLSFABA > DOB>Q AB>I LC PRMMLOQ >KA CBBA?>@H QEOLRDELRQ QEB TOFQFKD LC QEFP ?LLH. 4EB OBPQ LC QEB C>@RIQV >KA PQ>CC >Q )40 E>SB >IPL J>AB QEFP MLPPF?IB: "OF>K +FJ, %AT>OA 'LOALK, 'BLODB !DRALT, *LEK $R>KB, ->OILK %S>KP, ->QQ "BODBO, -BD>K $BJ>OBPQ, -FALOF 9>PRA>, >KA 2L? 2V>K.

4EB .>QROB LC #LAB (S1.0)

4EB PQRABKQP LC )40, QLL KRJBOLRP QL JBKQFLK, E>SB ?BBK >K >J>WFKD PLRO@B LC CBBA?>@H QEOLRDELRQ QEFP MOL@BPP. -R@E LC QEB J>QBOF>I FK QEFP ?LLH @LJBP COLJ JV @LROPB LC QEB P>JB QFQIB, TEF@E )gSB KLT Q>RDEQ CLO CFSB VB>OP. ) E>SB PQ>@HP LC AO>CQ MOFKQLRQP LC QEB ?LLH TFQE KLQBP P@O>TIBA >ILKD QEB J>ODFKP >P TBII >P > S>PQ >O@EFSB LC PQRABKQ BJ>FIP TFQE @LOOB@QFLKP, @LJJBKQP, >KA DBKBOLRP TLOAP LC BK@LRO>DBJBKQ. ) >J >IPL FKAB?QBA QL QEB BKBODBQF@ >KA PRMMLOQFSB @LJJRKFQV LC 0OL@BPPFKD MOLDO>JJBOP >KA >OQFPQP. ) TLRIAKgQ ?B TOFQFKD QEFP ?LLH FC FQ TBOBKgQ CLO #>PBV 2B>P >KA "BK &OV, TEL @OB>QBA 0OL@BPPFKD. )gSB IB>OKBA E>IC LC TE>Q ) HKLT PFJMIV COLJ OB>AFKD QEOLRDE QEB 0OL@BPPFKD PLRO@B @LAB; QEB BIBD>KQ PFJMIF@FQV LC QEB 0OL@BPPFKD I>KDR>DB, TB?PFQB, >KA )$% E>P J>AB MOLDO>JJFKD >@@BPPF?IB >KA CRK CLO >II LC JV PQRABKQP. )gSB OB@BFSBA >ASF@B >KA FKPMFO>QFLK COLJ J>KV 0OL@BPPFKD MOLDO>JJBOP FK@IRAFKD !KAO[P #LIR?OF, *BO 4ELOM, ->OFRP 7>QW, +>OPQBK 3@EJFAQ, 2L?BOQ (LADFK, 3B?-,BB $BIFPIB, >KA )O> 'OBBK?BOD. (B>QEBO $BTBV(>D?LOD MOLSFABA > DOB>Q AB>I LC BU@BIIBKQ CBBA?>@H LK #E>MQBO 10 (.BRO>I .BQTLOHP) >KA 0EFIFM '>I>KQBO EBIMBA QL @I>OFCV QEB ABCFKFQFLKP LC @LJMIBUFQV >KA @LJMIBU PVPQBJP. 3@LQQ -ROO>V MOLSFABA PLJB OB>IIV EBIMCRI >ASF@B >?LRQ FKIFKB 36'P LSBO B-J>FI. ->KV LC QEB QFQIBP FK QEB &ROQEBO 2B>AFKD PB@QFLK TBOB PRDDBPQBA ?V 'LI>K ,BSFK. ) >J FKAB?QBA QL 3E>KKLK &OV, TEL BAFQBA QEFP ?LLH BSBOV PQBM LC QEB T>V. 4EB HKLTIBADB QE>Q ) TLRIA >IT>VP E>SB EBO @>OBCRI >KA QELRDEQCRI CBBA?>@H LK JV TOFQFKD >IILTBA JB QL MILT >EB>A, >T>OB QE>Q BSBOVQEFKD TLRIA @LJB LRQ PLRKAFKD ?BQQBO >CQBO PEB DLQ EBO E>KAP LK JV @E>MQBOP. ! PMB@F>I JBKQFLK DLBP QL :>KK>E ->OPE TEL TLOHBA QFOBIBPPIV QL @OB>QB LSBO > ERKAOBA FIIRPQO>QFLKP CLO QEFP ?LLH, ABSBILMFKD > COFBKAIV >KA FKCLOJ>I ILLH. ) BPMB@F>IIV T>KQ QL QE>KH EBO CLO EBO M>QFBK@B >KA TFIIFKDKBPP QL DL TFQE QEB CILT >P TB @E>KDBA QEB FIIRPQO>QFLK OBNRFOBJBKQP PBSBO>I QFJBP. ) >IPL T>KQ QL QE>KH $>SFA 7FIPLK, TEL @>JB QL JV OBP@RB >Q QEB I>PQ JFKRQB >KA ABPFDKBA QEB FKQBOFLO I>VLRQ >KA @LSBO CLO QEB ?LLH. ) >J M>OQF@RI>OIV DO>QBCRI QL 3QBSB +IFPB, TEL ABPFDKBA >KA ?RFIQ QEB ?LLHgP TB?PFQB, EBIMFKD JB QL ABSBILM > "M>V TE>Q VLR T>KQ" JLABI CLO QEB AFDFQ>I 0$&. !P )gII BUMI>FK > ?FQ JLOB FK QEB MOBC>@B, QEFP ?LLH T>P DBKBO>QBA TFQE > KBT LMBK-PLRO@B PVPQBJ CLO MR?IFPEFKD @>IIBA h4EB ->DF@ "LLH.i ! @O>@H QB>J LC )40 MOLDO>JJBOP, ABPFDKBOP, >KA >OQFPQP TLOHBA LSBO QEB @LROPB LC JLOB QE>K > VB>O QL ABSBILM QEFP PVPQBJ, TEF@E DBKBO>QBP > ?LLH FK > S>OFBQV LC CLOJ>QP (0$&, (4-,, >KA JLOB) COLJ LKB PFKDIB !3#))$/# CFIB, >II ABPFDKBA TFQE #33 I>VLRQ. 2RKB ->APBK ?BD>K QEB MOLGB@Q >KA ABSBILMBA QEB LOFDFK>I 2R?V / 3FK>QO> CO>JBTLOH. ) >J MOBQQV PROB )gA PQFII ?B PQORDDIFKD TFQE MRQQFKD QEB ?LLH QLDBQEBO TBII FKQL 2013 FC FQ TBOBKgQ CLO 2RKBgP ABAF@>QFLK QL PBBFKD QEB MOLGB@Q QEOLRDE QL QEB BKA. 3QBSB +IFPB @LKQOF?RQBA @LRKQIBPP ?RD CFUBP >KA BKDFKBBOBA QEB PVPQBJ QE>Q >IILTP RP QL OBPQVIB @LAB @LJJBKQP QL QEB PFAB LC QEB @LAB ?IL@HP QEBJPBISBP. -FDRBI "BOJRABW, %S>K %JLIL, >KA ,RFP> 0BOBFO> (LOP @LKQOF?RQBA FK J>KV T>VP, IB>OKFKD QEB FKP >KA LRQP LC !3#))$/# >P TBII >P #33 0>DBA -BAF>. )40 OBPB>O@EBO 'OBD "LOBKPQBFK MOLSFABA > QOBJBKALRP >JLRKQ LC >ASF@B >KA PRMMLOQ >ILKD QEB T>V OBD>OAFKD QEB >OB>P LC MR?IFPEFKD CLO QEB 7B? >KA MOFKQ. 0OFK@B (EQQM://MOFK@BUJI.@LJ) FP QEB BKDFKB QEB ->DF@ "LLH RPBP QL DBKBO>QB > 0$& COLJ >K (4-, AL@RJBKQ, >KA )gA IFHB QL QE>KH -F@E>BI $>V, #%/ LC 0OFK@B8-,, TEL >KPTBOBA J>KV LC LRO NRBPQFLKP (>Q IFDEQKFKD PMBBA) >ILKD QEB T>V.

SF

!@HKLTIBADJBKQP

&FK>IIV )gA IFHB QL QE>KH JV C>JFIV: JV TFCB, !IFHF #>ILVBO>P, TEL PRMMLOQBA QEFP MOLGB@Q QEOLRDELRQ TEFIB E>SFKD EBO LTK DF>KQ QLJB QL TOFQB, >KA JV @EFIAOBK, %IF>P >KA /IVJMF>, JLQFS>QFLK CLO CFKFPEFKD QEFP RM PL QE>Q ) @LRIA PMBKA JLOB QFJB E>KDFKD LRQ TFQE QEBJ. )gA >IPL IFHB QL QE>KH JV C>QEBO, "BOK>OA 3EFCCJ>K, TEL DBKBOLRPIV IBKQ EFP J>QEBJ>QF@>I BUMBOQFPB >KA MOLSFABA CBBA?>@H >ILKD QEB T>V, >P TBII >P JV JLQEBO, $LOFP 9>CCB 3EFCCJ>K, >KA ?OLQEBO, *LK>QE>K 3EFCCJ>K, TEL TBOB >IT>VP QOBJBKALRPIV PRMMLOQFSB FK >PHFKD QEB NRBPQFLK: h(LT FP QEB ?LLH @LJFKD >ILKD?i

A.2 Kickstarter
4EBOB FP >KLQEBO LOD>KFW>QFLK >KA @LJJRKFQV QE>Q E>P J>AB QEFP ?LLH MLPPF?IB: +F@HPQ>OQBO. )K 2008, ) @LJMIBQBA TLOH LK JV CFOPQ ?LLH, (95FB=B; ,FC79GG=B; , MR?IFPEBA ?V -LOD>K +>RCJ>KK/%IPBSFBO. (95FB=B; ,FC79GG=B; QLLH >IJLPQ QEOBB VB>OP QL CFKFPE. ) AFAKgQ Q>HB > ILQ LC @>OB FK @ELLPFKD > MR?IFPEBO LO QEFKHFKD >?LRQ QEB QBOJP. ) GRPQ QELRDEQ e h2B>IIV? 9LR T>KQ QL MR?IFPE > ?LLH ?V JB? /+, )gII AL FQ.i 5KCLOQRK>QBIV, JV BUMBOFBK@B T>P KLQ BKQFOBIV MLPFQFSB. ) E>A CFSB AFCCBOBKQ BAFQLOP >PPFDKBA QL JB QEOLRDELRQ QEB MOL@BPP, >KA ) OB@BFSBA IFQQIB QL KL CBBA?>@H LK QEB @LKQBKQ FQPBIC. 4EB MR?IFPEBO LRQPLRO@BA QEB QVMBPBQQFKD, TEF@E OBPRIQBA FK > DOB>Q AB>I LC JFPQ>HBP >KA FK@LKPFPQBK@FBP FK MOLAR@QFLK. )K >AAFQFLK, ) CLRKA QEB MOF@FKD LC QEB ?LLH QL ?B LCC QEB J>OH. -V DL>I T>P QL TOFQB > COFBKAIV, FKBUMBKPFSB (?I>@H >KA TEFQB), M>MBO?>@H FKQOLAR@QFLK QL MOLDO>JJFKD FK 0OL@BPPFKD, >KA QEB ?LLH BKABA RM OBQ>FIFKD CLO > "QBUQ?LLH" MOF@B LC $50. .LT, ) T>KQ QL BJME>PFWB QE>Q JV MR?IFPEBO E>A DLLA FKQBKQFLKP. 4EBV ELKBPQIV T>KQBA QL MOLAR@B QEB ?BPQ ?LLH MLPPF?IB, LKB QE>Q ) TLRIA ?B E>MMV TFQE, QE>Q QEBV TLRIA ?B E>MMV TFQE, >KA QE>Q OB>ABOP TLRIA BKGLV. !KA QEBV TLOHBA E>OA QL J>HB QEFP E>MMBK. 5KCLOQRK>QBIV, QEBV E>A QL TLOH TFQEFK > SBOV QFDEQ ?RADBQ, >KA >P > OBPRIQ TBOB PQOBQ@EBA BUQOBJBIV QEFK. )K >AAFQFLK, ) ALKgQ QEFKH QEBV TBOB QBOOF?IV C>JFIF>O TFQE QEB TLOIA LC LMBKPLRO@B h@OB>QFSBi @LAFKD BKSFOLKJBKQP IFHB 0OL@BPPFKD; QEBFO TLOIA FP @LJMRQBO P@FBK@B QBUQ?LLHP. !P > OBPRIQ, CLO QEFP .>QROB LC #LAB ?LLH, ) CBIQ FQ T>P FJMLOQ>KQ QL QOV PBIC-MR?IFPEFKD. 3FK@B ) AFAKgQ DBQ BAFQFKD PRMMLOQ COLJ QEB MR?IFPEBO, TEV KLQ EFOB >K BAFQLO? ) T>PKgQ E>MMV TFQE QEB MOF@FKD, PL TEV KLQ PBQ QEB MOF@B JVPBIC (LO, FK QEB @>PB LC QEB 0$&, IBQ QEB ?RVBO PBQ QEB MOF@B)? 4EBK QEBOBgP QEB NRBPQFLK LC J>OHBQFKD e ALBP > MR?IFPEBO >AA S>IRB >KA EBIM VLR OB>@E >K >RAFBK@B? )K PLJB @>PBP, QEB >KPTBO FP VBP. 4EB /g2BFIIV h->HBi PBOFBP, CLO BU>JMIB, ALBP > TLKABOCRI GL? LC @OB>QFKD > @LJJRKFQV >OLRKA QEBFO ?LLHP >KA MOLAR@QP. 3QFII, FK QEB @>PB LC IB>OKFKD QL MOLDO>J FK 0OL@BPPFKD, OB>@EFKD QEB >RAFBK@B FP >P PFJMIB >P LKB 52, e MOL@BPPFKD.LOD. 5KCLOQRK>QBIV, ) NRF@HIV AFP@LSBOBA QE>Q QEBOB FP LKB QEFKD > MR?IFPEBO LCCBOP QE>Q ) T>P KLQ DBQQFKD COLJ JV PBIC-MR?IFPEFKD M>QE. /KB SBOV FJMLOQ>KQ, EFDEIV @OR@F>I ABQ>FI e > AB>AIFKB. /K JV LTK, ) CILRKABOBA CLO QTL VB>OP, P>VFKD ) T>P DLFKD QL TOFQB QEB .>QROB LC #LAB ?LLH ?RQ LKIV AO>CQFKD > IFQQIB ?FQ EBOB >KA QEBOB. /K JV IFPQ LC QEFKDP ) KBBABA QL AL, FQ T>P >IT>VP >Q QEB ?LQQLJ. 4EBK >ILKD @>JB +F@HPQ>OQBO, >KA TFQE >K >RAFBK@B PFQQFKD >KA T>FQFKD (>KA SFF

4EB .>QROB LC #LAB (S1.0)

E>SFKD PMBKQ @>PE JLKBV), ) IFSBA FK CB>O LC KLQ JBBQFKD JV AB>AIFKB. !KA QEB C>@Q QE>Q VLR >OB OB>AFKD QEFP KLT FP >K FKAF@>QFLK QE>Q FQ TLOHBA. -LPQ FJMLOQ>KQIV, PBIC-MR?IFPEFKD QEB ?LLH E>P >IILTBA JB > DOB>Q AB>I LC CIBUF?FIFQV FK ELT ) MOF@B >KA AFPQOF?RQB QEB @LKQBKQ. /K %IPBSFBOgP TB?PFQB, VLR @>K MRO@E>PB (95FB=B; ,FC79GG=B; >P >K B-?LLH CLO $53.95. 4E>QgP OFDEQ, CFCQV-QEOBB ALII>OP >KA KFKBQV-CFSB @BKQP. )K@FABKQ>IIV, CLO B>@E B-?LLH PLIA ) DBQ > OLV>IQV LC 5%, TEF@E FP $2.70. 4E>QgP OFDEQ, QTL ALII>OP >KA PBSBKQV @BKQP. )C ) PBIC-MR?IFPE, ) @>K J>HB QEB ?LLH J>PPFSBIV @EB>MBO. 3BIIFKD > AFDFQ>I @LMV CLO $10, )gJ OBAR@FKD QEB @LPQ QL QEB OB>ABO ?V LSBO BFDEQV MBO@BKQ >KA QOFMIFKD QEB JLKBV M>FA QL JB. )gJ Q>HFKD QEFP BSBK CROQEBO TFQE QEB 0$& >KA >IILTFKD ?RVBOP QL PBQ QEB MOF@B QEBJPBISBP. )K >AAFQFLK, ?V LTKFKD >II QEB @LKQBKQ, ) >J >?IB QL OBIB>PB QEB BKQFOB ?LLH LKIFKB CLO COBB >P TBII >P BUMBOFJBKQ TFQE KBT AFDFQ>I CLOJ>QP. 4EB O>T QBUQ LC QEB ?LLH, >P TBII >P >II QEB @LAB >KA FIIRPQO>QFLKP, FP IF@BKPBA RKABO > #OB>QFSB #LJJLKP !QQOF?RQFLK-.LK#LJJBO@F>I IF@BKPB >KA FP >S>FI>?IB LK 'FQ(R?, TEBOB OB>ABOP @>K PR?JFQ FPPRBP (KLQ QL JBKQFLK MRII OBNRBPQP!) TFQE @LOOB@QFLKP >KA @LJJBKQP. &FK>IIV, ?V RPFKD JLOB CIBUF?IB MOFKQ-LK-ABJ>KA PBOSF@BP, ) @>K JLOB B>PFIV J>HB @E>KDBP >KA HBBM QEB ?LLH @ROOBKQ, OBIB>PFKD KBT BAFQFLKP >P LCQBK >P ) IFHB. (! LKB-QFJB MRO@E>PB LC > AFDFQ>I @LMV LC QEB ?LLH FK@IRABP IFCBQFJB RMDO>ABP CLO COBB.) 3L QE>KH VLR QL +F@HPQ>OQBO, ?LQE QEB @LJM>KV (BPMB@F>IIV &OBA "BKBKPLK, TEL @LKSFK@BA JB QL Q>HB QEB MIRKDB FK QEB CFOPQ MI>@B >KA >ASFPBA JB LK ELT QL IF@BKPB QEB ?LLH) >P TBII >P >II QEB ?>@HBOP TEL QLLH > @E>K@B LK QEFP ?LLH. 3LJB LC QEBPB ?>@HBOP, QEOLRDE DBKBOLPFQV ?BVLKA QEB @>II LC ARQV, B>OKBA >K BUQO> QE>KH-VLR >P M>OQ LC QEBFO OBT>OA: j !IBU>KAOB ". j 2L?BOQ (LADFK j *LL9LRK 0>BH j !KDBI> -@.>JBB ("LVE>K) j "L? )MMLIFQL !II LC QEB ?>@HBOP AFOB@QIV @LKQOF?RQBA QL QEB CFKFPEFKD LC QEFP ?LLH. *RPQ QEB PEBBO >@Q LC PFDKFKD RM QL @LKQOF?RQB JLKBV CLO AO>CQ >KA CFK>I SBOPFLKP IFQ > CFOB FK JB QL CFKFPE, KLQ QL JBKQFLK MOLSFABA JB TFQE QEB OBPLRO@BP QL M>V CLO ABPFDK >KA BAFQFKD TLOH (>KA PLJB ?>?VPFQQFKD AROFKD 3>QROA>V JLOKFKD TOFQFKD PBPPFLKP). )K >AAFQFLK QL @LKQOF?RQFKD CRKAP, +F@HPQ>OQBO ?>@HBOP OB>A MOB-OBIB>PB SBOPFLKP LC QEB @E>MQBOP >KA MOLSFABA QLKP LC CBBA?>@H, @>Q@EFKD J>KV BOOLOP >KA MLFKQFKD LRQ @LKCRPFKD PB@QFLKP LC QEB ?LLH. 4TL PR@E OB>ABOP QE>Q )gA IFHB QL QE>KH >OB &OBABOFH 6>KELRQQB >KA (>KP AB 7LIC, TELPB BUMBOQ HKLTIBADB LC .BTQLKF>K MEVPF@P T>P BKLOJLRPIV EBIMCRI FK QEB OBSFPFKD LC #E>MQBOP 2 >KA 3.

SFFF

0OBC>@B

Preface
P.1 What is this book?
!Q )40 (EQQM://FQM.KVR.BAR), ) QB>@E > @LROPB BKQFQIBA )KQOLAR@QFLK QL #LJMRQ>QFLK>I -BAF>. )K QEFP @LROPB, QEB PQRABKQP IB>OK QEB ?>PF@P LC MOLDO>JJFKD (S>OF>?IBP, @LKAFQFLK>IP, ILLMP, L?GB@QP, >OO>VP) >P TBII >P > PROSBV LC >MMIF@>QFLKP OBI>QBA QL J>HFKD FKQBO>@QFSB MOLGB@QP (FJ>DBP, MFUBIP, @LJMRQBO SFPFLK, KBQTLOHFKD, A>Q>, 3$). 4EB @LROPB JLPQIV CLIILTP QEB J>QBOF>I CLRKA FK JV FKQOL ?LLH (95FB=B; ,FC79GG=B;; FK J>KV T>VP, /<9 *5HIF9 C: CC89 PBOSBP >P > CLIILT-RM. /K@B VLRgSB IB>OKBA QEB ?>PF@P >KA PBBK >K >OO>V LC >MMIF@>QFLKP, VLRO KBUQ PQBM JFDEQ ?B QL ABISB ABBMIV FKQL > M>OQF@RI>O >OB>. &LO BU>JMIB, VLR @LRIA CL@RP LK @LJMRQBO SFPFLK (>KA OB>A > ?LLH IFHB 'OBD "LOBKPQBFKgP )5?=B; /<=B;G .99 ). )K QEB JLPQ ?>PF@ PBKPB, QEFP ?LLH FP LKB MLPPF?IB KBUQ PQBM FK > TLOIA LC J>KV. )Q MF@HP RM BU>@QIV TEBOB (95FB=B; ,FC79GG=B; IB>SBP LCC, ABJLKPQO>QFKD JLOB >AS>K@BA MOLDO>JJFKD QB@EKFNRBP TFQE 0OL@BPPFKD QE>Q CL@RP LK >IDLOFQEJP >KA PFJRI>QFLK. 4EB DL>I LC QEFP ?LLH FP PFJMIB. 7B T>KQ QL Q>HB > ILLH >Q PLJBQEFKD QE>Q K>QRO>IIV L@@ROP FK LRO MEVPF@>I TLOIA, QEBK ABQBOJFKB ELT TB @>K TOFQB @LAB QL PFJRI>QB QE>Q L@@ROOBK@B. 3L QEBK TE>Q FP QEFP ?LLH BU>@QIV? )P FQ > P@FBK@B ?LLH? 4EB >KPTBO FP > OBPLRKAFKD KL. 4ORB, TB JFDEQ BU>JFKB QLMF@P QE>Q @LJB COLJ MEVPF@P LO ?FLILDV, ?RQ FQ TLKgQ ?B LRO GL? QL FKSBPQFD>QB QEBPB QLMF@P TFQE > M>OQF@RI>OIV EFDE IBSBI LC >@>ABJF@ OFDLO. )KPQB>A, TBgOB DLFKD QL DI>K@B >Q P@FBKQFCF@ @LK@BMQP >KA DO>? QEB M>OQP QE>Q TB KBBA FK QEB PBOSF@B LC ?RFIAFKD > M>OQF@RI>O PLCQT>OB BU>JMIB. )P QEFP >K >OQ LO ABPFDK ?LLH? ) TLRIA >IPL P>V KL; >CQBO >II, TB >OB DLFKD QL CL@RP LK >IDLOFQEJP >KA QEBFO >CCFIF>QBA MOLDO>JJFKD QB@EKFNRBP. 3ROB, QEB OBPRIQP TFII >II ?B SFPR>I FK K>QROB (J>KFCBPQBA >P >KFJ>QBA 0OL@BPPFKD PHBQ@EBP), ?RQ QEBV TFII BUFPQ JLOB >P ABJLKPQO>QFLKP LC QEB >IDLOFQEJP >KA MOLDO>JJFKD QB@EKFNRBP QEBJPBISBP, AO>TK LKIV TFQE PFJMIB PE>MBP >KA DO>VP@>IB. )Q FP JV ELMB, ELTBSBO, QE>Q ABPFDKBOP >KA >OQFPQP @>K FK@LOMLO>QB >II LC QEB J>QBOF>I EBOB FKQL QEBFO MO>@QF@B QL J>HB KBT, BKD>DFKD TLOH. FU

4EB .>QROB LC #LAB (S1.0)

)K QEB BKA, FC QEFP ?LLH FP >KVQEFKD, FQ FP OB>IIV GRPQ > DLLA LIA-C>PEFLKBA MOLDO>JJFKD ?LLH. 7EFIB > P@FBKQFCF@ QLMF@ J>V PBBA > @E>MQBO (.BTQLKF>K MEVPF@P, @BIIRI>O DOLTQE, BSLIRQFLK) LO QEB OBPRIQP JFDEQ FKPMFOB >K >OQFPQF@ MOLGB@Q, QEB @LKQBKQ FQPBIC TFII >IT>VP ?LFI ALTK QL QEB @LAB FJMIBJBKQ>QFLK, TFQE > M>OQF@RI>O CL@RP LK L?GB@Q-LOFBKQBA MOLDO>JJFKD.

P.2 A word about Processing


) >J RPFKD 0OL@BPPFKD FK QEFP ?LLH CLO > KRJ?BO LC OB>PLKP. &LO LKB, FQgP QEB I>KDR>DB >KA BKSFOLKJBKQ TFQE TEF@E ) >J JLPQ @LJCLOQ>?IB, >KA FQgP TE>Q ) BKGLV RPFKD CLO JV MBOPLK>I TLOH. 4TL, FQgP COBB, LMBK-PLRO@B, >KA TBII PRFQBA QL ?BDFKKBOP. 4EBOB FP >K >@QFSB, BKBODBQF@ @LJJRKFQV LC MBLMIB TEL MOLDO>J TFQE 0OL@BPPFKD; CLO J>KV, FQgP QEB CFOPQ MOLDO>JJFKD I>KDR>DB QEBVgSB IB>OKBA. )K QEFP PBKPB, ) ELMB QE>Q ) @>K OB>@E > TFAB >RAFBK@B >KA ABJLKPQO>QB QEB @LK@BMQP FK > COFBKAIV J>KKBO ?V RPFKD 0OL@BPPFKD. !II QE>Q P>FA, QEBOB FP KLQEFKD QE>Q QFBP TE>Q TB >OB ALFKD FK QEFP ?LLH PQOF@QIV QL 0OL@BPPFKD. 4EFP ?LLH @LRIA E>SB ?BBK TOFQQBK RPFKD !@QFLK3@OFMQ, *>S>3@OFMQ, *>S> (TFQELRQ 0OL@BPPFKD), LO >KV KRJ?BO LC LQEBO LMBK-PLRO@B h@OB>QFSB @LAFKDi BKSFOLKJBKQP IFHB LMBK&O>JBTLOHP, #FKABO, LO QEB KBTIV OBIB>PBA ML@LAB. )Q FP JV ELMB QE>Q >CQBO )gSB @LJMIBQBA QEFP ?LLH, )gII ?B >?IB QL OBIB>PB SBOPFLKP LC QEB BU>JMIBP QE>Q ORK FK LQEBO BKSFOLKJBKQP. )C >KVLKB FP FKQBOBPQBA FK EBIMFKD QL MLOQ QEB BU>JMIBP, MIB>PB CBBI COBB QL @LKQ>@Q JB (A>KFBI PEFCCJ>K.KBQ). !II LC QEB BU>JMIBP FK QEFP ?LLH E>SB ?BBK QBPQBA TFQE 0OL@BPPFKD 2.0?6, ?RQ CLO QEB JLPQ M>OQ, QEBV PELRIA >IPL TLOH TFQE B>OIFBO SBOPFLKP LC 0OL@BPPFKD. )gII ?B HBBMFKD QEBJ RM-QL-A>QB TFQE TE>QBSBO QEB I>QBPQ SBOPFLK FP. 4EB JLPQ OB@BKQ @LAB @>K >IT>VP ?B CLRKA LK 'FQ(R? (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB-%U>JMIBP).

P.3 What do you need to know?


4EB MOBOBNRFPFQB CLO RKABOPQ>KAFKD QEB J>QBOF>I FK QEFP ?LLH @LRIA ?B PQ>QBA >P: hLKB PBJBPQBO LC MOLDO>JJFKD FKPQOR@QFLK TFQE 0OL@BPPFKD (FK@IRAFKD C>JFIF>OFQV TFQE L?GB@Q-LOFBKQBA MOLDO>JJFKD).i 4E>Q P>FA, QEBOBgP KL OB>PLK TEV VLR @LRIAKgQ OB>A QEFP ?LLH E>SFKD IB>OKBA MOLDO>JJFKD RPFKD > AFCCBOBKQ I>KDR>DB LO ABSBILMJBKQ BKSFOLKJBKQ. 4EB HBV EBOB FP QE>Q VLR E>SB BUMBOFBK@B TFQE MOLDO>JJFKD. )C VLRgSB KBSBO TOFQQBK >KV @LAB ?BCLOB, VLR >OB DLFKD QL PQORDDIB, ?B@>RPB QEFP ?LLH >PPRJBP HKLTIBADB LC >II QEB ?>PF@P. ) TLRIA PRDDBPQ MF@HFKD RM >K FKQOLAR@QLOV ?LLH LK 0OL@BPPFKD, > KRJ?BO LC TEF@E >OB IFPQBA LK QEB 0OL@BPPFKD TB?PFQB (EQQM://MOL@BPPFKD.LOD/ IB>OKFKD/?LLHP/). )C VLR >OB >K BUMBOFBK@BA MOLDO>JJBO, ?RQ E>SBKgQ TLOHBA TFQE 0OL@BPPFKD, VLR @>K MOL?>?IV MF@H FQ RM ?V ALTKIL>AFKD 0OL@BPPFKD (EQQM://MOL@BPPFKD.LOD/ALTKIL>A/), MLHFKD QEOLRDE QEB

0OBC>@B

BU>JMIBP, >KA OB>AFKD QEOLRDE QEB 'BQQFKD 3Q>OQBA (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/ DBQQFKDPQ>OQBA/) M>DB. ) PELRIA >IPL MLFKQ LRQ QE>Q BUMBOFBK@B TFQE L?GB@Q-LOFBKQBA MOLDO>JJFKD FP @OR@F>I. 7BgII OBSFBT PLJB LC QEB ?>PF@P FK QEB ?LLHgP FKQOLAR@QFLK, ?RQ ) TLRIA PRDDBPQ OB>AFKD QEB 0OL@BPPFKD QRQLOF>I LK L?GB@QP (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP) CFOPQ.

P.4 What are you using to read this book?


!OB VLR OB>AFKD QEFP ?LLH LK > +FKAIB? 0OFKQBA M>MBO? /K VLRO I>MQLM FK 0$& CLOJ? /K > Q>?IBQ PELTFKD >K >KFJ>QBA (4-,5 SBOPFLK? !OB VLR PQO>MMBA QL > @E>FO, >?PLO?FKD QEB @LKQBKQ AFOB@QIV FKQL VLRO ?O>FK SF> > PBOFBP LC BIB@QOLABP, QR?BP, >KA @>OQOFADBP? 4EB ?LLH VLR >OB OB>AFKD OFDEQ KLT T>P DBKBO>QBA TFQE QEB ->DF@ "LLH MOLGB@Q (EQQM://TTT.J>DF@?LLHMOLGB@Q.@LJ). 4EB ->DF@ "LLH FP >K LMBK-PLRO@B CO>JBTLOH CLO PBICMR?IFPEFKD ABSBILMBA >Q )40 (EQQM://FQM.KVR.BAR). 4EB FAB> EBOB FP QE>Q VLR LKIV KBBA QL TOFQB QEB ?LLH LK@B >P > PFJMIB QBUQ CFIB. /K@B VLRgSB TOFQQBK VLRO @LKQBKQ, VLR MOBPP > J>DF@ ?RQQLK, >KA LRQ @LJBP VLRO ?LLH FK > S>OFBQV LC CLOJ>QPe0$&, (4-,5, MOFKQBA E>OA@LMV, +FKAIB, BQ@. %SBOVQEFKD FP ABPFDKBA >KA PQVIBA RPFKD #33. !P LC QEB CFOPQ OBIB>PB, QEB LKIV SBOPFLKP >S>FI>?IB TFII ?B AFDFQ>I 0$&, MOFKQBA E>OA@LMV, >KA (4-,5 (TEF@E TFII FK@IRAB >KFJ>QBA SBOPFLKP LC QEB BU>JMIBP RPFKD 0OL@BPPFKD.GP). (LMBCRIIV LSBO QEB @LROPB LC QEB KBUQ VB>O, QEB ?LLH TFII ?B >S>FI>?IB FK >AAFQFLK>I CLOJ>QP. )C VLRgA IFHB QL EBIM TFQE QEFP, MIB>PB @LKQ>@Q JB (A>KFBI PEFCCJ>K.KBQ).

P.5 The story of this book


)C VLR DI>K@B LSBO QEB ?LLHgP Q>?IB LC @LKQBKQP, VLRgII KLQF@B QEBOB >OB QBK @E>MQBOP, B>@E LKB @LSBOFKD > AFCCBOBKQ QLMF@. !KA FK LKB PBKPB, QEFP ?LLH FP GRPQ QE>Qe> PROSBV LC QBK @LK@BMQP >KA >PPL@F>QBA @LAB BU>JMIBP. .BSBOQEBIBPP, FK MRQQFKD QLDBQEBO QEB J>QBOF>I, ) E>A >IT>VP FJ>DFKBA PLJBQEFKD LC > IFKB>O K>OO>QFSB. "BCLOB VLR ?BDFK OB>AFKD QEB @E>MQBOP, )gA IFHB QL T>IH VLR QEOLRDE QEFP PQLOV.

Part I: Inanimate objects


! PL@@BO ?>II IFBP FK QEB DO>PP. ! HF@H I>RK@EBP FQ FKQL QEB >FO. 'O>SFQV MRIIP FQ ?>@H ALTK. ! EB>SV DRPQ LC TFKA HBBMP FQ >CIL>Q > JLJBKQ ILKDBO RKQFI FQ C>IIP >KA ?LRK@BP LCC QEB EB>A LC > GRJMFKD MI>VBO. 4EB PL@@BO ?>II FP KLQ >IFSB; FQ J>HBP KL @ELF@BP >P QL ELT FQ TFII JLSB QEOLRDELRQ QEB TLOIA. 2>QEBO, FQ FP >K FK>KFJ>QB L?GB@Q T>FQFKD QL ?B MRPEBA >KA MRIIBA ?V QEB CLO@BP LC FQP BKSFOLKJBKQ.

UF

4EB .>QROB LC #LAB (S1.0)

(LT TLRIA TB JLABI > PL@@BO ?>II JLSFKD FK 0OL@BPPFKD? )C VLRgSB BSBO MOLDO>JJBA > @FO@IB JLSFKD >@OLPP > TFKALT, QEBK VLRgSB MOL?>?IV TOFQQBK QEB CLIILTFKD IFKB LC @LAB.
R = R + 1;

9LR AO>T PLJB PE>MB >Q IL@>QFLK R . 7FQE B>@E CO>JB LC >KFJ>QFLK, VLR FK@OBJBKQ QEB S>IRB LC R , OBAO>T QEB PE>MB >KA SLFI>eQEB FIIRPFLK LC JLQFLK! ->V?B VLR QLLH FQ > PQBM LO QTL CROQEBO, >KA FK@IRABA > S IL@>QFLK, >P TBII >P S>OF>?IBP CLO PMBBA >ILKD QEB L >KA M >UBP.
R = R + RMJ??>; S = S + SMJ??>;

0>OQ ) LC QEFP PQLOV TFII Q>HB RP LKB PQBM CROQEBO. 7BgOB DLFKD QL Q>HB QEBPB S>OF>?IBP RMJ??> >KA SMJ??> >KA IB>OK ELT QLDBQEBO QEBV CLOJ > SB@QLO (Chapter 1 ), QEB ?RFIAFKD ?IL@H LC JLQFLK. 7B TLKgQ DBQ >KV KBT CRK@QFLK>IFQV LRQ LC QEFP, ?RQ FQ TFII ?RFIA > PLIFA CLRKA>QFLK CLO QEB OBPQ LC QEB ?LLH. /K@B TB HKLT > IFQQIB PLJBQEFKD >?LRQ SB@QLOP, TBgOB DLFKD QL NRF@HIV OB>IFWB QE>Q > CLO@B (Chapter 2 ) FP > SB@QLO. +F@H > PL@@BO ?>II >KA VLR >OB >MMIVFKD > CLO@B. 7E>Q ALBP > CLO@B @>RPB >K L?GB@Q QL AL? !@@LOAFKD QL )P>>@ .BTQLK, CLO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK. 4E>Q CLO@B @>RPBP >K L?GB@Q QL >@@BIBO>QB. -LABIFKD CLO@BP TFII >IILT RP QL @OB>QB PVPQBJP TFQE AVK>JF@ JLQFLK TEBOB L?GB@QP JLSB >@@LOAFKD QL > S>OFBQV LC ORIBP. .LT, QE>Q PL@@BO ?>II QL TEF@E VLR >MMIFBA > CLO@B JFDEQ E>SB >IPL ?BBK PMFKKFKD. )C >K L?GB@Q JLSBP >@@LOAFKD QL FQP >@@BIBO>QFLK, FQ @>K PMFK >@@LOAFKD QL FQP >KDRI>O >@@BIBO>QFLK ( Chapter 3). 5KABOPQ>KAFKD QEB ?>PF@P LC >KDIBP >KA QOFDLKLJBQOV TFII >IILT RP QL JLABI OLQ>QFKD L?GB@QP >P TBII >P DO>PM QEB MOFK@FMIBP ?BEFKA LP@FII>QFKD JLQFLK, IFHB > MBKARIRJ PTFKDFKD LO > PMOFKD ?LRK@FKD. /K@B TBgSB Q>@HIBA QEB ?>PF@P LC JLQFLK >KA CLO@BP CLO >K FKAFSFAR>I FK>KFJ>QB L?GB@Q, TBgII IB>OK ELT QL J>HB QELRP>KAP RMLK QELRP>KAP LC QELPB L?GB@QP >KA J>K>DB QEBJ FK > PFKDIB PVPQBJ @>IIBA > M>OQF@IB PVPQBJ (Chapter 4). 0>OQF@IB PVPQBJP TFII >IILT RP QL ILLH >Q PLJB >AS>K@BA CB>QROBP LC L?GB@Q-LOFBKQBA MOLDO>JJFKD, K>JBIV FKEBOFQ>K@B >KA MLIVJLOMEFPJ. )K #E>MQBOP 1 QEOLRDE 4, >II LC QEB BU>JMIBP TFII ?B TOFQQBK COLJ hP@O>Q@EieJB>KFKD QEB @LAB CLO QEB >IDLOFQEJP AOFSFKD QEB JLQFLK LC QEB L?GB@QP TFII ?B TOFQQBK AFOB@QIV FK 0OL@BPPFKD. 7BgOB @BOQ>FKIV KLQ QEB CFOPQ MOLDO>JJBOP BSBO QL @LKPFABO QEB FAB> LC PFJRI>QFKD MEVPF@P FK >KFJ>QFLK, PL KBUQ TBgII BU>JFKB ELT MEVPF@P IF?O>OFBP (Chapter 5 ) @>K ?B RPBA QL JLABI JLOB >AS>K@BA >KA PLMEFPQF@>QBA ?BE>SFLOP. 7BgII ILLH >Q "LU2$ (EQQM://TTT.?LU2A.LOD) >KA QLUF@IF?P' 6BOIBQ 0EVPF@P M>@H>DB (EQQM://QLUF@IF?P.LOD/).

Part II: Its alive!


7E>Q ALBP FQ JB>K QL JLABI IFCB? .LQ >K B>PV NRBPQFLK QL >KPTBO, ?RQ TB @>K ?BDFK ?V ?RFIAFKD L?GB@QP QE>Q E>SB >K >?FIFQV QL MBO@BFSB QEBFO BKSFOLKJBKQ. ,BQgP QEFKH >?LRQ QEFP CLO >

UFF

0OBC>@B

JLJBKQ. ! ?IL@H QE>Q C>IIP LCC > Q>?IB JLSBP >@@LOAFKD QL CLO@BP, >P ALBP > ALIMEFK PTFJJFKD QEOLRDE QEB T>QBO. "RQ QEBOB FP > HBV AFCCBOBK@B. 4EB ?IL@H @>KKLQ AB@FAB QL IB>M LCC QE>Q Q>?IB. 4EB ALIMEFK @>K AB@FAB QL IB>M LRQ LC QEB T>QBO. 4EB ALIMEFK @>K E>SB AOB>JP >KA ABPFOBP. )Q @>K CBBI ERKDBO LO CB>O, >KA QELPB CBBIFKDP @>K FKCLOJ FQP JLSBJBKQP. "V BU>JFKFKD QB@EKFNRBP ?BEFKA JLABIFKD >RQLKLJLRP >DBKQP ( Chapter 6 ), TB TFII ?OB>QEB IFCB FKQL LRO FK>KFJ>QB L?GB@QP, >IILTFKD QEBJ QL J>HB AB@FPFLKP >?LRQ QEBFO JLSBJBKQP >@@LOAFKD QL QEBFO RKABOPQ>KAFKD LC QEBFO BKSFOLKJBKQ. 4EOLRDE @LJ?FKFKD QEB @LK@BMQ LC >RQLKLJLRP >DBKQP TFQE TE>Q TB IB>OKBA >?LRQ JLABIFKD PVPQBJP FK #E>MQBO 4, TBgII ILLH >Q JLABIP LC DOLRM ?BE>SFLO QE>Q BUEF?FQ QEB MOLMBOQFBP LC @LJMIBUFQV. ! @LJMIBU PVPQBJ FP QVMF@>IIV ABCFKBA >P > PVPQBJ QE>Q FP hJLOB QE>K QEB PRJ LC FQP M>OQP.i 7EFIB QEB FKAFSFAR>I BIBJBKQP LC QEB PVPQBJ J>V ?B FK@OBAF?IV PFJMIB >KA B>PFIV RKABOPQLLA, QEB ?BE>SFLO LC QEB PVPQBJ >P > TELIB @>K ?B EFDEIV @LJMIBU, FKQBIIFDBKQ, >KA AFCCF@RIQ QL MOBAF@Q. 4EFP TFII IB>A RP >T>V COLJ QEFKHFKD MROBIV >?LRQ JLABIFKD JLQFLK >KA FKQL QEB OB>IJ LC ORIB-?>PBA PVPQBJP. 7E>Q @>K TB JLABI TFQE @BIIRI>O >RQLJ>Q> (Chapter 7), > PVPQBJ LC @BIIP IFSFKD LK > DOFA? 7E>Q QVMBP LC M>QQBOKP @>K TB DBKBO>QB TFQE CO>@Q>IP (Chapter 8), QEB DBLJBQOV LC K>QROB?

Part III: Intelligence


7B J>AB QEFKDP JLSB. 4EBK TB D>SB QELPB QEFKDP ELMBP >KA AOB>JP >KA CB>OP, >ILKD TFQE ORIBP QL IFSB ?V. 4EB I>PQ PQBM FK QEFP ?LLH TFII ?B QL J>HB LRO @OB>QFLKP BSBK PJ>OQBO. #>K TB >MMIV QEB ?FLILDF@>I MOL@BPP LC BSLIRQFLK QL @LJMRQ>QFLK>I PVPQBJP ( Chapter 9 ) FK LOABO QL BSLISB LRO L?GB@QP? 4>HFKD FKPMFO>QFLK COLJ QEB ERJ>K ?O>FK, @>K TB MOLDO>J >K >OQFCF@F>I KBRO>I KBQTLOH (Chapter 10) QE>Q @>K IB>OK COLJ FQP JFPQ>HBP >KA >IILT LRO L?GB@QP QL >A>MQ QL QEBFO BKSFOLKJBKQ?

P.6 This book as a syllabus


7EFIB QEB @LKQBKQ FK QEFP ?LLH @BOQ>FKIV J>HBP CLO >K FKQBKPB >KA EFDEIV @LJMOBPPBA PBJBPQBO, ) E>SB ABPFDKBA FQ QL CFQ FKQL > CLROQBBK-TBBH @LROPB. .BSBOQEBIBPP, FQgP TLOQE JBKQFLKFKD QE>Q ) CFKA QE>Q QEB ?LLH @E>MQBOP PLJBQFJBP TLOH ?BQQBO BUM>KABA >@OLPP JRIQFMIB TBBHP. &LO BU>JMIB, QEB PVII>?RP CLO JV @LROPB DBKBO>IIV TLOHP LRQ >P CLIILTP:

UFFF

4EB .>QROB LC #LAB (S1.0)

7BBH 1 7BBH 2 7BBH 3 7BBH 4 7BBH 5 7BBH 6 7BBH 7 7BBH 8 7BBH 9 7BBH 10 7BBH 11 7BBHP 12-13 7BBH 14

)KQOLAR@QFLK >KA 6B@QLOP (#E>MQBO 1) &LO@BP (#E>MQBO 2) /P@FII>QFLKP (#E>MQBO 3) 0>OQF@IB 3VPQBJP (#E>MQBO 4) 0EVPF@P ,F?O>OFBP 0>OQ ) (#E>MQBO 5) 0EVPF@P ,F?O>OFBP 0>OQ )) & 3QBBOFKD (#E>MQBOP 5-6) 0OBPBKQ JFAQBOJ MOLGB@QP >?LRQ JLQFLK #LJMIBU 3VPQBJP: &IL@HFKD >KA 1$ #BIIRI>O !RQLJ>Q> (#E>MQBOP 6-7) #LJMIBU 3VPQBJP: 2$ #BIIRI>O !RQLJ>Q> >KA &O>@Q>IP (#E>MQBOP 7-8) 'BKBQF@ !IDLOFQEJP (#E>MQBO 9) .BRO>I .BQTLOHP (#E>MQBO 10) &FK>I MOLGB@Q TLOHPELM &FK>I MOLGB@Q MOBPBKQ>QFLK

)C VLR >OB @LKPFABOFKD RPFKD QEFP QBUQ CLO > @LROPB LO TLOHPELM, MIB>PB CBBI COBB QL @LKQ>@Q JB. ) ELMB QL BSBKQR>IIV OBIB>PB > @LJM>KFLK PBQ LC SFABLP >KA PIFAB MOBPBKQ>QFLKP >P PRMMIBJBKQ>OV BAR@>QFLK>I J>QBOF>IP.

P.7 The Ecosystem Project


!P JR@E >P )gA IFHB QL MOBQBKA VLR @LRIA IB>OK BSBOVQEFKD ?V @ROIFKD RM FK > @LJCV @E>FO >KA OB>AFKD PLJB MOLPB >?LRQ MOLDO>JJFKD, QL IB>OK MOLDO>JJFKD, VLRgOB OB>IIV DLFKD QL E>SB QL AL PLJB MOLDO>JJFKD. 9LR JFDEQ CFKA FQ EBIMCRI QL HBBM FK JFKA > MOLGB@Q FAB> (LO QTL) QL ABSBILM >P > PBQ LC BUBO@FPBP TEFIB DLFKD COLJ @E>MQBO QL @E>MQBO. )K C>@Q, TEBK QB>@EFKD QEB .>QROB LC #LAB @LROPB >Q )40, ) E>SB LCQBK CLRKA QE>Q PQRABKQP BKGLV ?RFIAFKD > PFKDIB MOLGB@Q, PQBM ?V PQBM, TBBH ?V TBBH, LSBO QEB @LROPB LC > PBJBPQBO. !Q QEB BKA LC B>@E @E>MQBO, VLRgII CFKA > PBOFBP LC BUBO@FPBP CLO LKB PR@E MOLGB@QeBUBO@FPBP QE>Q ?RFIA LK B>@E LQEBO, LKB QLMF@ >Q > QFJB. #LKPFABO QEB CLIILTFKD P@BK>OFL. 9LRgSB ?BBK >PHBA ?V > P@FBK@B JRPBRJ QL ABSBILM QEB PLCQT>OB CLO > KBT BUEF?FQe4EB $FDFQ>I %@LPVPQBJ, > TLOIA LC >KFJ>QBA, MOL@BARO>I @OB>QROBP QE>Q IFSB LK > MOLGB@QFLK P@OBBK CLO SFPFQLOP QL BKGLV >P QEBV BKQBO QEB JRPBRJ. ) ALKgQ JB>K QL PRDDBPQ QE>Q QEFP FP > M>OQF@RI>OIV FKKLS>QFSB LO @OB>QFSB @LK@BMQ. 2>QEBO, TBgII RPB QEFP BU>JMIB MOLGB@Q FAB> >P > IFQBO>I OBMOBPBKQ>QFLK LC QEB @LKQBKQ FK QEB ?LLH, ABJLKPQO>QFKD ELT QEB BIBJBKQP CFQ QLDBQEBO FK > PFKDIB PLCQT>OB MOLGB@Q. ) BK@LRO>DB VLR QL ABSBILM VLRO LTK FAB>, LKB QE>Q FP JLOB >?PQO>@Q >KA @OB>QFSB FK FQP QEFKHFKD.

UFS

0OBC>@B

P.8 Where do I find the code online and submit feedback?


&LO >II QEFKDP ?LLH-OBI>QBA, MIB>PB SFPFQ QEB .>QROB LC #LAB TB?PFQB (EQQM://TTT.K>QROBLC@LAB.@LJ). 4EB O>T PLRO@B QBUQ LC QEB ?LLH >KA >II LC QEB FIIRPQO>QFLKP >OB LK 'FQ(R? (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB). 0IB>PB IB>SB CBBA?>@H >KA PR?JFQ @LOOB@QFLKP RPFKD 'FQ(R? FPPRBP. 4EB PLRO@B @LAB CLO >II LC QEB BU>JMIBP (>KA BUBO@FPBP) FP >IPL >S>FI>?IB LK 'FQ(R? (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB-%U>JMIBP). 4EB @E>MQBOP QEBJPBISBP FK@IRAB @LAB PKFMMBQP FK-IFKB TFQE QEB QBUQ. (LTBSBO, ) T>KQ QL JBKQFLK QE>Q FK J>KV @>PBP, ) E>SB PELOQBKBA LO PFJMIFCFBA QEB @LAB PKFMMBQP FK LOABO QL FIIRPQO>QB > PMB@FCF@ MLFKQ. )K >II @>PBP, QEB CRII @LAB TFQE @LJJBKQP @>K ?B CLRKA SF> 'FQ(R?. )C VLR E>SB NRBPQFLKP >?LRQ QEB @LAB FQPBIC, ) TLRIA PRDDBPQ MLPQFKD QEBJ LK QEB 0OL@BPPFKD CLORJ (EQQM://CLORJ.MOL@BPPFKD.LOD).

US

4EB .>QROB LC #LAB (S1.0)

Table of Contents
Acknowledgments
!.1 ! IFQQIB ?FQ LC EFPQLOV !.2 +F@HPQ>OQBO

iv
S SFF

Preface
0.1 7E>Q FP QEFP ?LLH? 0.2 ! TLOA >?LRQ 0OL@BPPFKD 0.3 7E>Q AL VLR KBBA QL HKLT? 0.4 7E>Q >OB VLR RPFKD QL OB>A QEFP ?LLH? 0.5 4EB hPQLOVi LC QEFP ?LLH 0.6 4EFP ?LLH >P > PVII>?RP 0.7 4EB %@LPVPQBJ 0OLGB@Q 0.8 7EBOB AL ) CFKA QEB @LAB LKIFKB >KA PR?JFQ CBBA?>@H?

ix
FU U U UF UF UFFF UFS US

Introduction
).1 2>KALJ 7>IHP ).2 4EB 2>KALJ 7>IHBO #I>PP ).3 0OL?>?FIFQV >KA .LK-5KFCLOJ $FPQOF?RQFLKP

1
1 2 7

USF

4>?IB LC #LKQBKQP

).4 ! .LOJ>I $FPQOF?RQFLK LC 2>KALJ .RJ?BOP ).5 ! #RPQLJ $FPQOF?RQFLK LC 2>KALJ .RJ?BOP ).6 0BOIFK .LFPB (! 3JLLQEBO !MMOL>@E) ).7 /KT>OA

11 14 17 26

Chapter 1. Vectors
1.1 6B@QLOP, 9LR #LJMIBQB -B 1.2 6B@QLOP CLO 0OL@BPPFKD 0OLDO>JJBOP 1.3 6B@QLO !AAFQFLK 1.4 -LOB 6B@QLO ->QE 1.5 6B@QLO ->DKFQRAB 1.6 .LOJ>IFWFKD 6B@QLOP 1.7 6B@QLO -LQFLK: 6BIL@FQV 1.8 6B@QLO -LQFLK: !@@BIBO>QFLK 1.9 3Q>QF@ SP. .LK-3Q>QF@ &RK@QFLKP 1.10 )KQBO>@QFSFQV TFQE !@@BIBO>QFLK

27
28 30 33 37 42 43 45 49 54 57

Chapter 2. Forces
2.1 &LO@BP >KA .BTQLKgP ,>TP LC -LQFLK 2.2 &LO@BP >KA 0OL@BPPFKDe.BTQLKgP 3B@LKA ,>T >P > &RK@QFLK 2.3 &LO@B !@@RJRI>QFLK 2.4 $B>IFKD TFQE ->PP 2.5 #OB>QFKD &LO@BP 2.6 'O>SFQV LK %>OQE >KA -LABIFKD > &LO@B 2.7 &OF@QFLK 2.8 !FO >KA &IRFA 2BPFPQ>K@B 2.9 'O>SFQ>QFLK>I !QQO>@QFLK 2.10 %SBOVQEFKD !QQO>@QP (LO 2BMBIP) %SBOVQEFKD

63
63 67 68 70 73 77 80 83 88 97

Chapter 3. Oscillation

101
USFF

4EB .>QROB LC #LAB (S1.0)

3.1 !KDIBP 3.2 !KDRI>O -LQFLK 3.3 4OFDLKLJBQOV 3.4 0LFKQFKD FK QEB $FOB@QFLK LC -LSBJBKQ 3.5 0LI>O SP. #>OQBPF>K #LLOAFK>QBP 3.6 /P@FII>QFLK !JMIFQRAB >KA 0BOFLA 3.7 /P@FII>QFLK TFQE !KDRI>O 6BIL@FQV 3.8 7>SBP 3.9 4OFDLKLJBQOV >KA &LO@BP: 4EB 0BKARIRJ 3.10 3MOFKD &LO@BP

101 104 108 109 112 116 119 122 127 134

Chapter 4. Particle Systems


4.1 7EV 7B .BBA 0>OQF@IB 3VPQBJP 4.2 ! 3FKDIB 0>OQF@IB 4.3 4EB !OO>V,FPQ 4.4 4EB 0>OQF@IB 3VPQBJ #I>PP 4.5 ! 3VPQBJ LC 3VPQBJP 4.6 )KEBOFQ>K@B >KA 0LIVJLOMEFPJ: !K )KQOLAR@QFLK 4.7 )KEBOFQ>K@B ">PF@P 4.8 0>OQF@IBP TFQE )KEBOFQ>K@B 4.9 0LIVJLOMEFPJ ">PF@P 4.10 0>OQF@IB 3VPQBJP TFQE 0LIVJLOMEFPJ 4.11 0>OQF@IB 3VPQBJP TFQE &LO@BP 4.12 0>OQF@IB 3VPQBJP TFQE 2BMBIIBOP 4.13 )J>DB 4BUQROBP >KA !AAFQFSB "IBKAFKD

143
144 145 149 155 157 160 162 166 168 170 173 178 183

Chapter 5. Physics Libraries


5.1 7E>Q )P "LU2$ >KA 7EBK )P )Q 5PBCRI? 5.2 'BQQFKD "LU2$ FK 0OL@BPPFKD 5.3 "LU2$ ">PF@P
USFFF

189
190 192 192

4>?IB LC #LKQBKQP

5.4 ,FSFKD FK > "LU2$ 7LOIA 5.5 "RFIAFKD > "LU2$ "LAV 5.6 4EOBBgP #LJM>KV: "LAFBP >KA 3E>MBP >KA &FUQROBP 5.7 "LU2$ >KA 0OL@BPPFKD: 2BRKFQBA >KA )Q &BBIP 3L 'LLA 5.8 &FUBA "LU2$ /?GB@QP 5.9 ! #ROSV "LRKA>OV 5.10 #LJMIBU &LOJP 5.11 &BBIFKD !QQ>@EBAe"LU2$ *LFKQP 5.12 "OFKDFKD )Q !II ">@H (LJB QL &LO@BP 5.13 #LIIFPFLK %SBKQP 5.14 ! "OFBC )KQBOIRABe)KQBDO>QFLK -BQELAP 5.15 6BOIBQ 0EVPF@P TFQE QLUF@IF?P 5.16 0>OQF@IBP >KA 3MOFKDP FK QLUF@IF?P 5.17 0RQQFKD )Q !II 4LDBQEBO: ! 3FJMIB )KQBO>@QFSB 3MOFKD 5.18 #LKKB@QBA 3VPQBJP, 0>OQ ): 3QOFKD 5.19 #LKKB@QBA 3VPQBJP, 0>OQ )): &LO@B-$FOB@QBA 'O>ME 5.20 !QQO>@QFLK >KA 2BMRIPFLK "BE>SFLOP

196 198 200 203 209 211 215 222 232 234 238 241 244 247 249 253 256

Chapter 6. Autonomous Agents


6.1 &LO@BP COLJ 7FQEFK 6.2 6BEF@IBP >KA 3QBBOFKD 6.3 4EB 3QBBOFKD &LO@B 6.4 !OOFSFKD "BE>SFLO 6.5 9LRO /TK $BPFOBP: $BPFOBA 6BIL@FQV 6.6 &ILT &FBIAP 6.7 4EB $LQ 0OLAR@Q 6.8 0>QE &LIILTFKD 6.9 0>QE &LIILTFKD TFQE -RIQFMIB 3BDJBKQP 6.10 #LJMIBU 3VPQBJP 6.11 'OLRM "BE>SFLOP (LO: ,BQgP KLQ ORK FKQL B>@E LQEBO)

260
260 262 263 270 274 276 282 286 294 298 300

UFU

4EB .>QROB LC #LAB (S1.0)

6.12 #LJ?FK>QFLKP 6.13 &IL@HFKD 6.14 !IDLOFQEJF@ %CCF@FBK@V (LO: 7EV ALBP JV $ (*%! ORK PL PILTIV?) 6.15 ! &BT ,>PQ .LQBP: /MQFJFW>QFLK 4OF@HP

306 308 315 317

Chapter 7. Cellular Automata


7.1 7E>Q )P > #BIIRI>O !RQLJ>QLK? 7.2 %IBJBKQ>OV #BIIRI>O !RQLJ>Q> 7.3 (LT QL 0OLDO>J >K %IBJBKQ>OV #! 7.4 $O>TFKD >K %IBJBKQ>OV #! 7.5 7LICO>J #I>PPFCF@>QFLK 7.6 4EB '>JB LC ,FCB 7.7 0OLDO>JJFKD QEB '>JB LC ,FCB 7.8 /?GB@Q-/OFBKQBA #BIIP 7.9 6>OF>QFLKP LC 4O>AFQFLK>I #!

323
324 325 330 336 340 342 345 349 351

Chapter 8. Fractals
8.1 7E>Q )P > &O>@Q>I? 8.2 2B@ROPFLK 8.3 4EB #>KQLO 3BQ TFQE > 2B@ROPFSB &RK@QFLK 8.4 4EB +L@E #ROSB >KA QEB !OO>V,FPQ 4B@EKFNRB 8.5 4OBBP 8.6 ,-PVPQBJP

355
356 358 363 366 374 382

Chapter 9. The Evolution of Code


9.1 'BKBQF@ !IDLOFQEJP: )KPMFOBA ?V !@QR>I %SBKQP 9.2 7EV 5PB 'BKBQF@ !IDLOFQEJP? 9.3 $>OTFKF>K .>QRO>I 3BIB@QFLK 9.4 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ ): #OB>QFKD > 0LMRI>QFLK

390
391 392 394 395

UU

4>?IB LC #LKQBKQP

9.5 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ )): 3BIB@QFLK 9.6 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ ))): 2BMOLAR@QFLK 9.7 #LAB CLO #OB>QFKD QEB 0LMRI>QFLK 9.8 'BKBQF@ !IDLOFQEJP: 0RQQFKD )Q !II 4LDBQEBO 9.9 'BKBQF@ !IDLOFQEJP: ->HB 4EBJ 9LRO /TK 9.10 %SLISFKD &LO@BP: 3J>OQ 2L@HBQP 9.11 3J>OQ 2L@HBQP: 0RQQFKD )Q !II 4LDBQEBO 9.12 )KQBO>@QFSB 3BIB@QFLK 9.13 %@LPVPQBJ 3FJRI>QFLK

397 399 402 409 413 420 425 431 435

Chapter 10. Neural Networks


10.1 !OQFCF@F>I .BRO>I .BQTLOHP: )KQOLAR@QFLK >KA !MMIF@>QFLK 10.2 4EB 0BO@BMQOLK 10.3 3FJMIB 0>QQBOK 2B@LDKFQFLK 5PFKD > 0BO@BMQOLK 10.4 #LAFKD QEB 0BO@BMQOLK 10.5 ! 3QBBOFKD 0BO@BMQOLK 10.6 )QgP > h.BQTLOH,i 2BJBJ?BO? 10.7 .BRO>I .BQTLOH $F>DO>JP 10.8 !KFJ>QFKD &BBA &LOT>OA

444
445 448 450 452 460 466 468 473

Further Reading
"LLHP 0>MBOP >KA !OQF@IBP

481
481 482

Index

484

UUF

4EB .>QROB LC #LAB (S1.0)

Introduction
I! .: AD< D6A5 ;.AB?2.J Woody Allen
(BOB TB >OB: QEB ?BDFKKFKD. 7BII, >IJLPQ QEB ?BDFKKFKD. )C FQgP ?BBK > TEFIB PFK@B VLRgSB ALKB >KV MOLDO>JJFKD FK 0OL@BPPFKD (LO >KV J>QE, CLO QE>Q J>QQBO), QEFP FKQOLAR@QFLK TFII DBQ VLRO JFKA ?>@H FKQL @LJMRQ>QFLK>I QEFKHFKD ?BCLOB TB >MMOL>@E PLJB LC QEB JLOB AFCCF@RIQ >KA @LJMIBU J>QBOF>I. )K #E>MQBO 1, TBgOB DLFKD QL Q>IH >?LRQ QEB @LK@BMQ LC > SB@QLO >KA ELT FQ TFII PBOSB >P QEB ?RFIAFKD ?IL@H CLO PFJRI>QFKD JLQFLK QEOLRDELRQ QEFP ?LLH. "RQ ?BCLOB TB Q>HB QE>Q PQBM, IBQgP QEFKH >?LRQ TE>Q FQ JB>KP CLO PLJBQEFKD QL PFJMIV JLSB >OLRKA QEB P@OBBK. ,BQgP ?BDFK TFQE LKB LC QEB ?BPQ-HKLTK >KA PFJMIBPQ PFJRI>QFLKP LC JLQFLKeQEB O>KALJ T>IH.

I.1 Random Walks


)J>DFKB VLR >OB PQ>KAFKD FK QEB JFAAIB LC > ?>I>K@B ?B>J. %SBOV QBK PB@LKAP, VLR CIFM > @LFK. (B>AP, Q>HB > PQBM CLOT>OA. 4>FIP, Q>HB > PQBM ?>@HT>OA. 4EFP FP > O>KALJ T>IHe> M>QE ABCFKBA >P > PBOFBP LC O>KALJ PQBMP. 3QBMMFKD LCC QE>Q ?>I>K@B ?B>J >KA LKQL QEB CILLO, VLR @LRIA MBOCLOJ > O>KALJ T>IH FK QTL AFJBKPFLKP ?V CIFMMFKD QE>Q P>JB @LFK QTF@B TFQE QEB CLIILTFKD OBPRIQP:

)KQOLAR@QFLK

Flip 1 (B>AP (B>AP 4>FIP 4>FIP

Flip 2 (B>AP 4>FIP (B>AP 4>FIP

Result 3QBM CLOT>OA. 3QBM OFDEQ. 3QBM IBCQ. 3QBM ?>@HT>OA.

9BP, QEFP J>V PBBJ IFHB > M>OQF@RI>OIV RKPLMEFPQF@>QBA >IDLOFQEJ. .BSBOQEBIBPP, O>KALJ T>IHP @>K ?B RPBA QL JLABI MEBKLJBK> QE>Q L@@RO FK QEB OB>I TLOIA, COLJ QEB JLSBJBKQP LC JLIB@RIBP FK > D>P QL QEB ?BE>SFLO LC > D>J?IBO PMBKAFKD > A>V >Q QEB @>PFKL. !P CLO RP, TB ?BDFK QEFP ?LLH PQRAVFKD > O>KALJ T>IH TFQE QEOBB DL>IP FK JFKA. 1. 7B KBBA QL OBSFBT > MOLDO>JJFKD @LK@BMQ @BKQO>I QL QEFP ?LLHeL?GB@Q-LOFBKQBA MOLDO>JJFKD. 4EB O>KALJ T>IHBO TFII PBOSB >P > QBJMI>QB CLO ELT TB TFII RPB L?GB@Q-LOFBKQBA ABPFDK QL J>HB QEFKDP QE>Q JLSB >OLRKA > 0OL@BPPFKD TFKALT. 2. 4EB O>KALJ T>IH FKPQFD>QBP QEB QTL NRBPQFLKP QE>Q TB TFII >PH LSBO >KA LSBO >D>FK QEOLRDELRQ QEFP ?LLH: h(LT AL TB ABCFKB QEB ORIBP QE>Q DLSBOK QEB ?BE>SFLO LC LRO L?GB@QP?i >KA QEBK, h(LT AL TB FJMIBJBKQ QEBPB ORIBP FK 0OL@BPPFKD?i 3. 4EOLRDELRQ QEB ?LLH, TBgII MBOFLAF@>IIV KBBA > ?>PF@ RKABOPQ>KAFKD LC O>KALJKBPP, MOL?>?FIFQV, >KA 0BOIFK KLFPB. 4EB O>KALJ T>IH TFII >IILT RP QL ABJLKPQO>QB > CBT HBV MLFKQP QE>Q TFII @LJB FK E>KAV I>QBO.

I.2 The Random Walker Class


,BQgP OBSFBT > ?FQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKD (//0) CFOPQ ?V ?RFIAFKD > 4;FE?L L?GB@Q. 4EFP TFII ?B LKIV > @ROPLOV OBSFBT. )C VLR E>SB KBSBO TLOHBA TFQE //0 ?BCLOB, VLR J>V T>KQ PLJBQEFKD JLOB @LJMOBEBKPFSB; )gA PRDDBPQ PQLMMFKD EBOB >KA OBSFBTFKD QEB ?>PF@P LK QEB 0OL@BPPFKD TB?PFQB (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP/) ?BCLOB @LKQFKRFKD. !K :-50.? FK 0OL@BPPFKD FP >K BKQFQV QE>Q E>P ?LQE A>Q> >KA CRK@QFLK>IFQV. 7B >OB ILLHFKD QL ABPFDK > 4;FE?L L?GB@Q QE>Q ?LQE HBBMP QO>@H LC FQP A>Q> (TEBOB FQ BUFPQP LK QEB P@OBBK) >KA E>P QEB @>M>?FIFQV QL MBOCLOJ @BOQ>FK >@QFLKP (PR@E >P AO>T FQPBIC LO Q>HB > PQBM). ! .7,>> FP QEB QBJMI>QB CLO ?RFIAFKD >@QR>I FKPQ>K@BP LC L?GB@QP. 4EFKH LC > @I>PP >P QEB @LLHFB @RQQBO; QEB L?GB@QP >OB QEB @LLHFBP QEBJPBISBP. ,BQgP ?BDFK ?V ABCFKFKD QEB 4;FE?L @I>PPeTE>Q FQ JB>KP QL ?B > 4;FE?L L?GB@Q. 4EB 4;FE?L LKIV KBBAP QTL MFB@BP LC A>Q>e> KRJ?BO CLO FQP U-IL@>QFLK >KA LKB CLO FQP V-IL@>QFLK.
=F;MM 4;FE?L U

4EB .>QROB LC #LAB (S1.0)

CHN R; CHN S;

Objects have data.

%SBOV @I>PP JRPQ E>SB > @LKPQOR@QLO, > PMB@F>I CRK@QFLK QE>Q FP @>IIBA TEBK QEB L?GB@Q FP CFOPQ @OB>QBA. 9LR @>K QEFKH LC FQ >P QEB L?GB@QgP M?NOJ() . 4EBOB, TBgII FKFQF>IFWB QEB 4;FE?L gP PQ>OQFKD IL@>QFLK (FK QEFP @>PB, QEB @BKQBO LC QEB TFKALT).
4;FE?L() U R = QC>NB/2; S = B?CABN/2; W Objects have a constructor where they are initialized.

&FK>IIV, FK >AAFQFLK QL A>Q>, @I>PPBP @>K ?B ABCFKBA TFQE CRK@QFLK>IFQV. )K QEFP BU>JMIB, > 4;FE?L E>P QTL CRK@QFLKP. 7B CFOPQ TOFQB > CRK@QFLK QE>Q >IILTP QEB L?GB@Q QL AFPMI>V FQPBIC (>P > TEFQB ALQ).
PIC> >CMJF;S() U MNLIE?(0); JICHN(R,S); W Objects have functions.

4EB PB@LKA CRK@QFLK AFOB@QP QEB 4;FE?L L?GB@Q QL Q>HB > PQBM. .LT, QEFP FP TEBOB QEFKDP DBQ > ?FQ JLOB FKQBOBPQFKD. 2BJBJ?BO QE>Q CILLO LK TEF@E TB TBOB Q>HFKD O>KALJ PQBMP? 7BII, KLT TB @>K RPB > 0OL@BPPFKD TFKALT FK QE>Q P>JB @>M>@FQV. 4EBOB >OB CLRO MLPPF?IB PQBMP. ! PQBM QL QEB OFDEQ @>K ?B PFJRI>QBA ?V FK@OBJBKQFKD R (R ++); QL QEB IBCQ ?V AB@OBJBKQFKD R (R-- ); CLOT>OA ?V DLFKD ALTK > MFUBI (S++ ); >KA ?>@HT>OA ?V DLFKD RM > MFUBI ( S-- ). (LT AL TB MF@H COLJ QEBPB CLRO @ELF@BP? %>OIFBO TB PQ>QBA QE>Q TB @LRIA CIFM QTL @LFKP. )K 0OL@BPPFKD, ELTBSBO, TEBK TB T>KQ QL O>KALJIV @ELLPB COLJ > IFPQ LC LMQFLKP, TB @>K MF@H > O>KALJ KRJ?BO RPFKD L;H>IG() .
PIC> MN?J() U CHN =BIC=? = CHN(L;H>IG(4)); 0, 1, 2, or 3

4EB >?LSB IFKB LC @LAB MF@HP > O>KALJ CIL>QFKD MLFKQ KRJ?BO ?BQTBBK 0 >KA 4 >KA @LKSBOQP FQ QL >K FKQBDBO, TFQE > OBPRIQ LC 0, 1, 2, LO 3. 4B@EKF@>IIV PMB>HFKD, QEB EFDEBPQ KRJ?BO TFII KBSBO ?B 4.0, ?RQ O>QEBO 3.999999999 (TFQE >P J>KV 9P >P QEBOB >OB AB@FJ>I MI>@BP); PFK@B QEB MOL@BPP LC @LKSBOQFKD QL >K FKQBDBO ILMP LCC QEB AB@FJ>I MI>@B, QEB EFDEBPQ CHN TB @>K DBQ FP 3. .BUQ, TB Q>HB QEB >MMOLMOF>QB PQBM (IBCQ, OFDEQ, RM, LO ALTK) ABMBKAFKD LK TEF@E O>KALJ KRJ?BO T>P MF@HBA.

)KQOLAR@QFLK

C@ (=BIC=? == 0) U R++; W ?FM? C@ (=BIC=? == 1) U R--; W ?FM? C@ (=BIC=? == 2) U S++; W ?FM? U S--; W W W

The random choice determines our step.

.LT QE>Q TBgSB TOFQQBK QEB @I>PP, FQgP QFJB QL J>HB >K >@QR>I 4;FE?L L?GB@Q FK QEB J>FK M>OQ LC LRO PHBQ@EeM?NOJ() >KA >L;Q() . !PPRJFKD TB >OB ILLHFKD QL JLABI > PFKDIB O>KALJ T>IH, TB AB@I>OB LKB DIL?>I S>OF>?IB LC QVMB 4;FE?L .
4;FE?L Q; A Walker object

4EBK TB @OB>QB QEB L?GB@Q FK M?NOJ() ?V @>IIFKD QEB @LKPQOR@QLO TFQE QEB H?Q LMBO>QLO. Example I.1: Traditional random walk !57< H=A9 MCI G99 H<9 56CJ9 !L5AD@9 <958=B; =B H<=G 6CC?, =H A95BG H<9F9 =G 5 7CFF9GDCB8=B; 7C89 9L5AD@9 5J5=@56@9 CB #=H$I6 (<HHD://;=H<I6.7CA/G<=::A5B//<9-*5HIF9C:-CC89-!L5AD@9G).
PIC> M?NOJ() U MCT?(640,360); M = D;M 17BA;H(); <;=EALIOH>(255); W Create the Walker.

&FK>IIV, AROFKD B>@E @V@IB QEOLRDE >L;Q() , TB >PH QEB 4;FE?L QL Q>HB > PQBM >KA AO>T > ALQ.
PIC> >L;Q() U M.IJ;F(); M.:?IFB7O(); W Call functions on the Walker.

3FK@B TB LKIV AO>T QEB ?>@HDOLRKA LK@B FK M?NOJ() , O>QEBO QE>K @IB>OFKD FQ @LKQFKR>IIV B>@E QFJB QEOLRDE >L;Q() , TB PBB QEB QO>FI LC QEB O>KALJ T>IH FK LRO 0OL@BPPFKD TFKALT.

4EB .>QROB LC #LAB (S1.0)

4EBOB >OB > @LRMIB FJMOLSBJBKQP TB @LRIA J>HB QL QEB O>KALJ T>IHBO. &LO LKB, QEFP 4;FE?L gP PQBM @ELF@BP >OB IFJFQBA QL CLRO LMQFLKPeRM, ALTK, IBCQ, >KA OFDEQ. "RQ >KV DFSBK MFUBI FK QEB TFKALT E>P BFDEQ MLPPF?IB KBFDE?LOP, >KA > KFKQE MLPPF?FIFQV FP QL PQ>V FK QEB P>JB MI>@B.

F64B?2 !.1

4L FJMIBJBKQ > 4;FE?L L?GB@Q QE>Q @>K PQBM QL >KV KBFDE?LOFKD MFUBI (LO PQ>V MRQ), TB @LRIA MF@H > KRJ?BO ?BQTBBK 0 >KA 8 (KFKB MLPPF?IB @ELF@BP). (LTBSBO, > JLOB BCCF@FBKQ T>V QL TOFQB QEB @LAB TLRIA ?B QL PFJMIV MF@H COLJ QEOBB MLPPF?IB PQBMP >ILKD QEB U->UFP (-1, 0, LO 1) >KA QEOBB MLPPF?IB PQBMP >ILKD QEB V->UFP.
PIC> MN?J() U CHN MN?JR = CHN(L;H>IG(3))-1; CHN MN?JS = CHN(L;H>IG(3))-1; R += MN?JR; S += MN?JS; W Yields -1, 0, or 1

4>HFKD QEFP CROQEBO, TB @LRIA RPB CIL>QFKD MLFKQ KRJ?BOP (F.B. AB@FJ>I KRJ?BOP) CLO R >KA S FKPQB>A >KA JLSB >@@LOAFKD QL >K >O?FQO>OV O>KALJ S>IRB ?BQTBBK -1 >KA 1.
PIC> MN?J() U @FI;N MN?JR = L;H>IG(-1, 1); @FI;N MN?JS = L;H>IG(-1, 1); Yields any floating point number between -1.0 and 1.0

)KQOLAR@QFLK
R += MN?JR; S += MN?JS; W

!II LC QEBPB S>OF>QFLKP LK QEB hQO>AFQFLK>Ii O>KALJ T>IH E>SB LKB QEFKD FK @LJJLK: >Q >KV JLJBKQ FK QFJB, QEB MOL?>?FIFQV QE>Q QEB 4;FE?L TFII Q>HB > PQBM FK > DFSBK AFOB@QFLK FP BNR>I QL QEB MOL?>?FIFQV QE>Q QEB 4;FE?L TFII Q>HB > PQBM FK >KV AFOB@QFLK. )K LQEBO TLOAP, FC QEBOB >OB CLRO MLPPF?IB PQBMP, QEBOB FP > 1 FK 4 (LO 25%) @E>K@B QEB 4;FE?L TFII Q>HB >KV DFSBK PQBM. 7FQE KFKB MLPPF?IB PQBMP, FQgP > 1 FK 9 (LO 11.1%) @E>K@B. #LKSBKFBKQIV, QEFP FP ELT QEB L;H>IG() CRK@QFLK TLOHP. 0OL@BPPFKDgP O>KALJ KRJ?BO DBKBO>QLO (TEF@E LMBO>QBP ?BEFKA QEB P@BKBP) MOLAR@BP TE>Q FP HKLTK >P > hRKFCLOJi AFPQOF?RQFLK LC KRJ?BOP. 7B @>K QBPQ QEFP AFPQOF?RQFLK TFQE > 0OL@BPPFKD PHBQ@E QE>Q @LRKQP B>@E QFJB > O>KALJ KRJ?BO FP MF@HBA >KA DO>MEP FQ >P QEB EBFDEQ LC > OB@Q>KDIB.

Example I.2: Random number distribution


CHN89 L;H>IG!IOHNM; PIC> M?NOJ() U MCT?(640,240); L;H>IG!IOHNM = H?Q CHN8209; W PIC> >L;Q() U <;=EALIOH>(255); CHN CH>?R = CHN(L;H>IG(L;H>IG!IOHNM.F?HANB)); L;H>IG!IOHNM8CH>?R9++; MNLIE?(0); @CFF(175); CHN Q = QC>NB/L;H>IG!IOHNM.F?HANB; Pick a random number and increase the count. An array to keep track of how often random numbers are picked

4EB .>QROB LC #LAB (S1.0)

@IL (CHN R = 0; R < L;H>IG!IOHNM.F?HANB; R++) U Graphing the results L?=N(R*Q,B?CABN-L;H>IG!IOHNM8R9,Q-1,L;H>IG!IOHNM8R9); W W

4EB >?LSB P@OBBKPELQ PELTP QEB OBPRIQ LC QEB PHBQ@E ORKKFKD CLO > CBT JFKRQBP. .LQF@B ELT B>@E ?>O LC QEB DO>ME AFCCBOP FK EBFDEQ. /RO P>JMIB PFWB (F.B. QEB KRJ?BO LC O>KALJ KRJ?BOP TBgSB MF@HBA) FP O>QEBO PJ>II >KA QEBOB >OB PLJB L@@>PFLK>I AFP@OBM>K@FBP, TEBOB @BOQ>FK KRJ?BOP >OB MF@HBA JLOB LCQBK. /SBO QFJB, TFQE > DLLA O>KALJ KRJ?BO DBKBO>QLO, QEFP TLRIA BSBK LRQ.

Pseudo-Random Numbers
4EB O>KALJ KRJ?BOP TB DBQ COLJ QEB L;H>IG() CRK@QFLK >OB KLQ QORIV O>KALJ; QEBOBCLOB QEBV >OB HKLTK >P hMPBRAL-O>KALJ.i 4EBV >OB QEB OBPRIQ LC > J>QEBJ>QF@>I CRK@QFLK QE>Q PFJRI>QBP O>KALJKBPP. 4EFP CRK@QFLK TLRIA VFBIA > M>QQBOK LSBO QFJB, ?RQ QE>Q QFJB MBOFLA FP PL ILKD QE>Q CLO RP, FQgP GRPQ >P DLLA >P MROB O>KALJKBPP!

Exercise I.1
#OB>QB > O>KALJ T>IHBO QE>Q E>P > QBKABK@V QL JLSB ALTK >KA QL QEB OFDEQ. (7BgII PBB QEB PLIRQFLK QL QEFP FK QEB KBUQ PB@QFLK.)

I.3 Probability and Non-Uniform Distributions


2BJBJ?BO TEBK VLR CFOPQ PQ>OQBA MOLDO>JJFKD FK 0OL@BPPFKD? 0BOE>MP VLR T>KQBA QL AO>T > ILQ LC @FO@IBP LK QEB P@OBBK. 3L VLR P>FA QL VLROPBIC: h/E, ) HKLT. )gII AO>T >II QEBPB @FO@IBP >Q O>KALJ IL@>QFLKP, TFQE O>KALJ PFWBP >KA O>KALJ @LILOP.i )K > @LJMRQBO DO>MEF@P PVPQBJ, FQgP LCQBK B>PFBPQ QL PBBA > PVPQBJ TFQE O>KALJKBPP. )K QEFP ?LLH, ELTBSBO, TBgOB ILLHFKD QL ?RFIA PVPQBJP JLABIBA LK TE>Q TB PBB FK K>QROB. $BC>RIQFKD QL O>KALJKBPP FP KLQ > M>OQF@RI>OIV QELRDEQCRI PLIRQFLK QL > ABPFDK MOL?IBJeFK M>OQF@RI>O, QEB HFKA LC MOL?IBJ QE>Q FKSLISBP @OB>QFKD >K LOD>KF@ LO K>QRO>I-ILLHFKD PFJRI>QFLK. 7FQE > CBT QOF@HP, TB @>K @E>KDB QEB T>V TB RPB L;H>IG() QL MOLAR@B hKLK-RKFCLOJi AFPQOF?RQFLKP LC O>KALJ KRJ?BOP. 4EFP TFII @LJB FK E>KAV QEOLRDELRQ QEB ?LLH >P TB ILLH >Q > KRJ?BO LC AFCCBOBKQ P@BK>OFLP. 7EBK TB BU>JFKB DBKBQF@ >IDLOFQEJP, CLO BU>JMIB, TBgII KBBA > JBQELALILDV CLO MBOCLOJFKD hPBIB@QFLKieTEF@E JBJ?BOP LC LRO MLMRI>QFLK PELRIA ?B PBIB@QBA QL M>PP QEBFO $.! QL QEB KBUQ DBKBO>QFLK? 2BJBJ?BO QEB @LK@BMQ LC PROSFS>I LC QEB CFQQBPQ? ,BQgP P>V TB E>SB > MLMRI>QFLK LC JLKHBVP BSLISFKD. .LQ BSBOV JLKHBV TFII E>SB >

)KQOLAR@QFLK

BNR>I @E>K@B LC OBMOLAR@FKD. 4L PFJRI>QB $>OTFKF>K BSLIRQFLK, TB @>KgQ PFJMIV MF@H QTL O>KALJ JLKHBVP QL ?B M>OBKQP. 7B KBBA QEB JLOB hCFQi LKBP QL ?B JLOB IFHBIV QL ?B @ELPBK. 7B KBBA QL ABCFKB QEB hMOL?>?FIFQV LC QEB CFQQBPQ.i &LO BU>JMIB, > M>OQF@RI>OIV C>PQ >KA PQOLKD JLKHBV JFDEQ E>SB > 90% @E>K@B LC MOL@OB>QFKD, TEFIB > TB>HBO LKB E>P LKIV > 10% @E>K@B. ,BQgP M>RPB EBOB >KA Q>HB > ILLH >Q MOL?>?FIFQVgP ?>PF@ MOFK@FMIBP. &FOPQ TBgII BU>JFKB PFKDIB BSBKQ MOL?>?FIFQV, F.B. QEB IFHBIFELLA QE>Q > DFSBK BSBKQ TFII L@@RO. )C VLR E>SB > PVPQBJ TFQE > @BOQ>FK KRJ?BO LC MLPPF?IB LRQ@LJBP, QEB MOL?>?FIFQV LC QEB L@@ROOBK@B LC > DFSBK BSBKQ BNR>IP QEB KRJ?BO LC LRQ@LJBP QE>Q NR>IFCV >P QE>Q BSBKQ AFSFABA ?V QEB QLQ>I KRJ?BO LC >II MLPPF?IB LRQ@LJBP. ! @LFK QLPP FP > PFJMIB BU>JMIBeFQ E>P LKIV QTL MLPPF?IB LRQ@LJBP, EB>AP LO Q>FIP. 4EBOB FP LKIV LKB T>V QL CIFM EB>AP. 4EB MOL?>?FIFQV QE>Q QEB @LFK TFII QROK RM EB>AP, QEBOBCLOB, FP LKB AFSFABA ?V QTL: 1/2 LO 50%. 4>HB > AB@H LC CFCQV-QTL @>OAP. 4EB MOL?>?FIFQV LC AO>TFKD >K >@B COLJ QE>Q AB@H FP:
DKC8;H E< 79;I / DKC8;H E< 97H:I = 4 / 52 = 0.077 = T 8%

4EB MOL?>?FIFQV LC AO>TFKD > AF>JLKA FP:


DKC8;H E< :?7CED:I / DKC8;H E< 97H:I = 13 / 52 = 0.25 = 25%

7B @>K >IPL @>I@RI>QB QEB MOL?>?FIFQV LC JRIQFMIB BSBKQP L@@ROOFKD FK PBNRBK@B. 4L AL QEFP, TB PFJMIV JRIQFMIV QEB FKAFSFAR>I MOL?>?FIFQFBP LC B>@E BSBKQ. 4EB MOL?>?FIFQV LC > @LFK QROKFKD RM EB>AP QEOBB QFJBP FK > OLT FP:
(1/2) * (1/2) * (1/2) = 1/8 (EH 0.125)

kJB>KFKD QE>Q > @LFK TFII QROK RM EB>AP QEOBB QFJBP FK > OLT LKB LRQ LC BFDEQ QFJBP (B>@E hQFJBi ?BFKD QEOBB QLPPBP).

Exercise I.2
7E>Q FP QEB MOL?>?FIFQV LC AO>TFKD QTL >@BP FK > OLT COLJ > AB@H LC CFCQV-QTL @>OAP? 4EBOB >OB > @LRMIB LC T>VP FK TEF@E TB @>K RPB QEB L;H>IG() CRK@QFLK TFQE MOL?>?FIFQV FK @LAB. /KB QB@EKFNRB FP QL CFII >K >OO>V TFQE > PBIB@QFLK LC KRJ?BOPePLJB LC TEF@E >OB OBMB>QBAeQEBK @ELLPB O>KALJ KRJ?BOP COLJ QE>Q >OO>V >KA DBKBO>QB BSBKQP ?>PBA LK QELPB @ELF@BP.
CHN89 MNO@@ = H?Q CHN859 MNO@@809 = 1; MNO@@819 = 1; 1 is stored in the array twice, making it more likely to be picked.

4EB .>QROB LC #LAB (S1.0)


MNO@@829 = 2; MNO@@839 = 3; MNO@@849 = 3; CHN CH>?R = CHN(L;H>IG(MNO@@.F?HANB)); Picking a random element from an array

2RKKFKD QEFP @LAB TFII MOLAR@B > 40% @E>K@B LC MOFKQFKD QEB S>IRB 1, > 20% @E>K@B LC MOFKQFKD 2, >KA > 40% @E>K@B LC MOFKQFKD 3. 7B @>K >IPL >PH CLO > O>KALJ KRJ?BO (IBQgP J>HB FQ PFJMIB >KA GRPQ @LKPFABO O>KALJ CIL>QFKD MLFKQ S>IRBP ?BQTBBK 0 >KA 1) >KA >IILT >K BSBKQ QL L@@RO LKIV FC LRO O>KALJ KRJ?BO FP TFQEFK > @BOQ>FK O>KDB. &LO BU>JMIB:
@FI;N JLI< = 0.10; @FI;N L = L;H>IG(1); A probability of 10% A random floating point value between 0 and 1 If our random number is less than 0.1, try again!

C@ (L < JLI<) U // NLS ;A;CH! W

4EFP JBQELA @>K >IPL ?B >MMIFBA QL JRIQFMIB LRQ@LJBP. ,BQgP P>V QE>Q /RQ@LJB ! E>P > 60% @E>K@B LC E>MMBKFKD, /RQ@LJB ", > 10% @E>K@B, >KA /RQ@LJB #, > 30% @E>K@B. 7B FJMIBJBKQ QEFP FK @LAB ?V MF@HFKD > O>KALJ CIL>Q >KA PBBFKD FKQL TE>Q O>KDB FQ C>IIP. j 69HK99B 0.00 5B8 0.60 (60%) Q> +IH7CA9 A j 69HK99B 0.60 5B8 0.70 (10%) Q> +IH7CA9 B j 69HK99B 0.70 5B8 1.00 (30%) Q> +IH7CA9 C
@FI;N HOG = L;H>IG(1); C@ (HOG < 0.6) U JLCHNFH("-ON=IG? A"); W ?FM? C@ (HOG < 0.7) U JLCHNFH("-ON=IG? W ?FM? U JLCHNFH("-ON=IG? !"); W "); Greater than 0.7 Between 0.6 and 0.7 If random number is less than 0.6

7B @LRIA RPB QEB >?LSB JBQELALILDV QL @OB>QB > O>KALJ T>IHBO QE>Q QBKAP QL JLSB QL QEB OFDEQ. (BOB FP >K BU>JMIB LC > 4;FE?L TFQE QEB CLIILTFKD MOL?>?FIFQFBP: j 7<5B79 C: ACJ=B; ID: 20%

)KQOLAR@QFLK

j 7<5B79 C: ACJ=B; 8CKB: 20% j 7<5B79 C: ACJ=B; @9:H: 20% j 7<5B79 C: ACJ=B; F=;<H: 40%

Example I.3: Walker that tends to move to the right


PIC> MN?J() U @FI;N L = L;H>IG(1); C@ (L < 0.4) U R++; W ?FM? C@ (L < 0.6) U R--; W ?FM? C@ (L < 0.8) U S++; W ?FM? U S--; W W A 40% chance of moving to the right!

Exercise I.3
#OB>QB > O>KALJ T>IHBO TFQE AVK>JF@ MOL?>?FIFQFBP. &LO BU>JMIB, @>K VLR DFSB FQ > 50% @E>K@B LC JLSFKD FK QEB AFOB@QFLK LC QEB JLRPB?

10

4EB .>QROB LC #LAB (S1.0)

I.4 A Normal Distribution of Random Numbers


,BQgP DL ?>@H QL QE>Q MLMRI>QFLK LC PFJRI>QBA 0OL@BPPFKD JLKHBVP. 9LRO MOLDO>J DBKBO>QBP > QELRP>KA -LKHBV L?GB@QP, B>@E TFQE > EBFDEQ S>IRB ?BQTBBK 200 >KA 300 (>P QEFP FP > TLOIA LC JLKHBVP QE>Q E>SB EBFDEQP ?BQTBBK 200 >KA 300 MFUBIP).
@FI;N B = L;H>IG(200,300);

$LBP QEFP >@@RO>QBIV ABMF@Q QEB EBFDEQP LC OB>I-TLOIA ?BFKDP? 4EFKH LC > @OLTABA PFABT>IH FK .BT 9LOH #FQV. 0F@H >KV MBOPLK LCC QEB PQOBBQ >KA FQ J>V >MMB>O QE>Q QEBFO EBFDEQ FP O>KALJ. .BSBOQEBIBPP, FQgP KLQ QEB HFKA LC O>KALJ QE>Q L;H>IG() MOLAR@BP. 0BLMIBgP EBFDEQP >OB KLQ RKFCLOJIV AFPQOF?RQBA; QEBOB >OB > DOB>Q AB>I JLOB MBLMIB LC >SBO>DB EBFDEQ QE>K QEBOB >OB SBOV Q>II LO SBOV PELOQ LKBP. 4L PFJRI>QB K>QROB, TB J>V T>KQ FQ QL ?B JLOB IFHBIV QE>Q LRO JLKHBVP >OB LC >SBO>DB EBFDEQ (250 MFUBIP), VBQ PQFII >IILT QEBJ QL ?B, LK L@@>PFLK, SBOV PELOQ LO SBOV Q>II. ! AFPQOF?RQFLK LC S>IRBP QE>Q @IRPQBO >OLRKA >K >SBO>DB (OBCBOOBA QL >P QEB hJB>Ki) FP HKLTK >P > hKLOJ>Ii AFPQOF?RQFLK. )Q FP >IPL @>IIBA QEB '>RPPF>K AFPQOF?RQFLK (K>JBA CLO J>QEBJ>QF@F>K #>OI &OFBAOF@E '>RPP) LO, FC VLR >OB &OBK@E, QEB ,>MI>@F>K AFPQOF?RQFLK (K>JBA CLO 0FBOOB-3FJLK ,>MI>@B). "LQE J>QEBJ>QF@F>KP TBOB TLOHFKD @LK@ROOBKQIV FK QEB B>OIV KFKBQBBKQE @BKQROV LK ABCFKFKD PR@E > AFPQOF?RQFLK. 7EBK VLR DO>ME QEB AFPQOF?RQFLK, VLR DBQ PLJBQEFKD QE>Q ILLHP IFHB QEB CLIILTFKD, FKCLOJ>IIV HKLTK >P > ?BII @ROSB:

F64B?2 !.2

F64B?2 !.3

4EB @ROSB FP DBKBO>QBA ?V > J>QEBJ>QF@>I CRK@QFLK QE>Q ABCFKBP QEB MOL?>?FIFQV LC >KV DFSBK S>IRB L@@ROOFKD >P > CRK@QFLK LC QEB JB>K (LCQBK TOFQQBK >P `, QEB 'OBBH IBQQBO AI ) >KA PQ>KA>OA ABSF>QFLK (c, QEB 'OBBH IBQQBO G=;A5 ). 4EB JB>K FP MOBQQV B>PV QL RKABOPQ>KA. )K QEB @>PB LC LRO EBFDEQ S>IRBP ?BQTBBK 200 >KA 300, VLR MOL?>?IV E>SB >K FKQRFQFSB PBKPB LC QEB JB>K (F.B. >SBO>DB) >P 250. (LTBSBO, TE>Q FC ) TBOB QL P>V QE>Q QEB PQ>KA>OA ABSF>QFLK FP 3 LO 15? 7E>Q ALBP QEFP JB>K CLO QEB KRJ?BOP? 4EB

11

)KQOLAR@QFLK

DO>MEP >?LSB PELRIA DFSB RP > EFKQ. 4EB DO>ME LK QEB IBCQ PELTP RP QEB AFPQOF?RQFLK TFQE > SBOV ILT PQ>KA>OA ABSF>QFLK, TEBOB QEB J>GLOFQV LC QEB S>IRBP @IRPQBO @ILPBIV >OLRKA QEB JB>K. 4EB DO>ME LK QEB OFDEQ PELTP RP > EFDEBO PQ>KA>OA ABSF>QFLK, TEBOB QEB S>IRBP >OB JLOB BSBKIV PMOB>A LRQ COLJ QEB >SBO>DB. 4EB KRJ?BOP TLOH LRQ >P CLIILTP: 'FSBK > MLMRI>QFLK, 68% LC QEB JBJ?BOP LC QE>Q MLMRI>QFLK TFII E>SB S>IRBP FK QEB O>KDB LC LKB PQ>KA>OA ABSF>QFLK COLJ QEB JB>K, 98% TFQEFK QTL PQ>KA>OA ABSF>QFLKP, >KA 99.7% TFQEFK QEOBB PQ>KA>OA ABSF>QFLKP. 'FSBK > PQ>KA>OA ABSF>QFLK LC 5 MFUBIP, LKIV 0.3% LC QEB JLKHBV EBFDEQP TFII ?B IBPP QE>K 235 MFUBIP (QEOBB PQ>KA>OA ABSF>QFLKP ?BILT QEB JB>K LC 250) LO DOB>QBO QE>K 265 MFUBIP (QEOBB PQ>KA>OA ABSF>QFLKP >?LSB QEB JB>K LC 250).

Calculating Mean and Standard Deviation


#LKPFABO > @I>PP LC QBK PQRABKQP TEL OB@BFSB QEB CLIILTFKD P@LOBP (LRQ LC 100) LK > QBPQ: 85, 82, 88, 86, 85, 93, 98, 40, 73, 83 (30 80,9 4> ?30 ,A0=,20: 81.3 4EB PQ>KA>OA ABSF>QFLK FP @>I@RI>QBA >P QEB PNR>OB OLLQ LC QEB >SBO>DB LC QEB PNR>OBP LC ABSF>QFLKP >OLRKA QEB JB>K. )K LQEBO TLOAP, Q>HB QEB AFCCBOBK@B COLJ QEB JB>K CLO B>@E MBOPLK >KA PNR>OB FQ (S>OF>K@B). #>I@RI>QB QEB >SBO>DB LC >II QEBPB S>IRBP >KA Q>HB QEB PNR>OB OLLQ >P QEB PQ>KA>OA ABSF>QFLK.

Score 85 40 BQ@.

Difference from Mean 85-81.3 = 3.7 40-81.3 = -41.3

Variance (3.7)2 = 13.69 (-41.3)2 = 1705.69

Average Variance:

254.23

(30 >?,9/,=/ /0A4,?4:9 4> ?30 ><@,=0 =::? :1 ?30 ,A0=,20 A,=4,9.0: 15.13

,R@HFIV CLO RP, QL RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP FK > 0OL@BPPFKD PHBQ@E, TB ALKgQ E>SB QL AL >KV LC QEBPB @>I@RI>QFLKP LROPBISBP. )KPQB>A, TB @>K J>HB RPB LC > @I>PP HKLTK >P /;H>IG , TEF@E TB DBQ CLO COBB >P M>OQ LC QEB ABC>RIQ *>S> IF?O>OFBP FJMLOQBA FKQL

12

4EB .>QROB LC #LAB (S1.0)

0OL@BPPFKD (PBB QEB *>S>$L@P (EQQM://AL@P.LO>@IB.@LJ/G>S>PB/6/AL@P/>MF/G>S>/RQFI/ 2>KALJ.EQJI) CLO JLOB FKCLOJ>QFLK). 4L RPB QEB /;H>IG @I>PP, TB JRPQ CFOPQ AB@I>OB > S>OF>?IB LC QVMB /;H>IG >KA @OB>QB QEB /;H>IG L?GB@Q FK M?NOJ() .
/;H>IG A?H?L;NIL; PIC> M?NOJ() U MCT?(640,360); A?H?L;NIL = H?Q /;H>IG(); W We use the variable name generator because what we have here can be thought of as a random number generator.

)C TB T>KQ QL MOLAR@B > O>KALJ KRJ?BO TFQE > KLOJ>I (LO '>RPPF>K) AFPQOF?RQFLK B>@E QFJB TB ORK QEOLRDE >L;Q() , FQgP >P B>PV >P @>IIFKD QEB CRK@QFLK H?RN%;OMMC;H() .
PIC> >L;Q() U @FI;N HOG = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H(); W Asking for a Gaussian random number. (Note nextGaussian() returns a double and must be converted to float.)

(BOBgP QEB QEFKD. 7E>Q >OB TB PRMMLPBA QL AL TFQE QEFP S>IRB? 7E>Q FC TB T>KQBA QL RPB FQ, CLO BU>JMIB, QL >PPFDK QEB U-MLPFQFLK LC > PE>MB TB AO>T LK P@OBBK? 4EB H?RN%;OMMC;H() CRK@QFLK OBQROKP > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP TFQE QEB CLIILTFKD M>O>JBQBOP: 5 A95B C: N9FC >KA 5 GH5B85F8 89J=5H=CB C: CB9 . ,BQgP P>V TB T>KQ > JB>K LC 320 (QEB @BKQBO ELOFWLKQ>I MFUBI FK > TFKALT LC TFAQE 640) >KA > PQ>KA>OA ABSF>QFLK LC 60 MFUBIP. 7B @>K >AGRPQ QEB S>IRB QL LRO M>O>JBQBOP ?V JRIQFMIVFKD FQ ?V QEB PQ>KA>OA ABSF>QFLK >KA >AAFKD QEB JB>K.

Example I.4: Gaussian distribution


PIC> >L;Q() U

13

)KQOLAR@QFLK

@FI;N HOG = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H(); @FI;N M> = 60; @FI;N G?;H = 320; @FI;N R = M> * HOG + G?;H; HI0NLIE?(); @CFF(255,10); ?FFCJM?(R,180,16,16); W

Note that nextGaussian() returns a double.

Multiply by the standard deviation and add the mean.

"V AO>TFKD QEB BIIFMPBP LK QLM LC B>@E LQEBO TFQE PLJB QO>KPM>OBK@V, TB @>K >@QR>IIV PBB QEB AFPQOF?RQFLK. 4EB ?OFDEQBPQ PMLQ FP KB>O QEB @BKQBO, TEBOB JLPQ LC QEB S>IRBP @IRPQBO, ?RQ BSBOV PL LCQBK @FO@IBP >OB AO>TK C>OQEBO QL QEB OFDEQ LO IBCQ LC QEB @BKQBO.

Exercise I.4
#LKPFABO > PFJRI>QFLK LC M>FKQ PMI>QQBO AO>TK >P > @LIIB@QFLK LC @LILOBA ALQP. -LPQ LC QEB M>FKQ @IRPQBOP >OLRKA > @BKQO>I IL@>QFLK, ?RQ PLJB ALQP AL PMI>QQBO LRQ QLT>OAP QEB BADBP. #>K VLR RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP QL DBKBO>QB QEB IL@>QFLKP LC QEB ALQP? #>K VLR >IPL RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP QL DBKBO>QB > @LILO M>IBQQB?

Exercise I.5
! '>RPPF>K O>KALJ T>IH FP ABCFKBA >P LKB FK TEF@E QEB PQBM PFWB (ELT C>O QEB L?GB@Q JLSBP FK > DFSBK AFOB@QFLK) FP DBKBO>QBA TFQE > KLOJ>I AFPQOF?RQFLK. )JMIBJBKQ QEFP S>OF>QFLK LC LRO O>KALJ T>IH.

I.5 A Custom Distribution of Random Numbers


4EBOB TFII @LJB > QFJB FK VLRO IFCB TEBK VLR AL KLQ T>KQ > RKFCLOJ AFPQOF?RQFLK LC O>KALJ S>IRBP, LO > '>RPPF>K LKB. ,BQgP FJ>DFKB CLO > JLJBKQ QE>Q VLR >OB > O>KALJ T>IHBO FK PB>O@E LC CLLA. -LSFKD O>KALJIV >OLRKA > PM>@B PBBJP IFHB > OB>PLK>?IB PQO>QBDV CLO CFKAFKD PLJBQEFKD QL B>Q. !CQBO >II, VLR ALKgQ HKLT TEBOB QEB CLLA FP, PL VLR JFDEQ >P TBII PB>O@E O>KALJIV RKQFI VLR CFKA FQ. 4EB MOL?IBJ, >P VLR J>V E>SB KLQF@BA, FP QE>Q O>KALJ T>IHBOP OBQROK QL MOBSFLRPIV SFPFQBA IL@>QFLKP J>KV QFJBP (QEFP FP HKLTK >P hLSBOP>JMIFKDi). /KB PQO>QBDV QL >SLFA PR@E > MOL?IBJ FP QL, BSBOV PL LCQBK, Q>HB > SBOV I>ODB PQBM. 4EFP >IILTP QEB T>IHBO QL CLO>DB O>KALJIV >OLRKA > PMB@FCF@ IL@>QFLK TEFIB MBOFLAF@>IIV GRJMFKD SBOV C>O >T>V QL OBAR@B QEB >JLRKQ LC LSBOP>JMIFKD. 4EFP S>OF>QFLK LK QEB O>KALJ T>IH

14

4EB .>QROB LC #LAB (S1.0)

(HKLTK >P > ,[SV CIFDEQ) OBNRFOBP > @RPQLJ PBQ LC MOL?>?FIFQFBP. 4ELRDE KLQ >K BU>@Q FJMIBJBKQ>QFLK LC > ,[SV CIFDEQ, TB @LRIA PQ>QB QEB MOL?>?FIFQV AFPQOF?RQFLK >P CLIILTP: QEB ILKDBO QEB PQBM, QEB IBPP IFHBIV FQ FP QL ?B MF@HBA; QEB PELOQBO QEB PQBM, QEB JLOB IFHBIV. %>OIFBO FK QEFP MOLILDRB, TB P>T QE>Q TB @LRIA DBKBO>QB @RPQLJ MOL?>?FIFQV AFPQOF?RQFLKP ?V CFIIFKD >K >OO>V TFQE S>IRBP (PLJB ARMIF@>QBA PL QE>Q QEBV TLRIA ?B MF@HBA JLOB COBNRBKQIV) LO ?V QBPQFKD QEB OBPRIQ LC L;H>IG() . 7B @LRIA FJMIBJBKQ > ,[SV CIFDEQ ?V P>VFKD QE>Q QEBOB FP > 1% @E>K@B LC QEB T>IHBO Q>HFKD > I>ODB PQBM.
@FI;N L = L;H>IG(1); C@ (L < 0.01) U RMN?J = L;H>IG(-100,100); SMN?J = L;H>IG(-100,100); W ?FM? U RMN?J = L;H>IG(-1,1); SMN?J = L;H>IG(-1,1); W A 1% chance of taking a large step

(LTBSBO, QEFP OBAR@BP QEB MOL?>?FIFQFBP QL > CFUBA KRJ?BO LC LMQFLKP. 7E>Q FC TB T>KQBA QL J>HB > JLOB DBKBO>I ORIBeQEB EFDEBO > KRJ?BO, QEB JLOB IFHBIV FQ FP QL ?B MF@HBA? 3.145 TLRIA ?B JLOB IFHBIV QL ?B MF@HBA QE>K 3.144, BSBK FC QE>Q IFHBIFELLA FP GRPQ > QFKV ?FQ DOB>QBO. )K LQEBO TLOAP, FC R FP QEB O>KALJ KRJ?BO, TB @LRIA J>M QEB IFHBIFELLA LK QEB V->UFP TFQE S = R .

F64B?2 !.4

)C TB @>K CFDROB LRQ ELT QL DBKBO>QB > AFPQOF?RQFLK LC O>KALJ KRJ?BOP >@@LOAFKD QL QEB >?LSB DO>ME, QEBK TB TFII ?B >?IB QL >MMIV QEB P>JB JBQELALILDV QL >KV @ROSB CLO TEF@E TB E>SB > CLOJRI>. /KB PLIRQFLK FP QL MF@H QTL O>KALJ KRJ?BOP FKPQB>A LC LKB. 4EB CFOPQ O>KALJ KRJ?BO FP GRPQ QE>Q, > O>KALJ KRJ?BO. 4EB PB@LKA LKB, ELTBSBO, FP TE>Q TBgII @>II > hNR>IFCVFKD O>KALJ S>IRB.i )Q TFII QBII RP TEBQEBO QL RPB QEB CFOPQ LKB LO QEOLT FQ >T>V >KA MF@H >KLQEBO LKB. .RJ?BOP QE>Q E>SB >K B>PFBO QFJB NR>IFCVFKD TFII ?B MF@HBA JLOB LCQBK, >KA KRJ?BOP QE>Q O>OBIV NR>IFCV TFII ?B MF@HBA FKCOBNRBKQIV. (BOB >OB QEB PQBMP (CLO KLT, IBQgP @LKPFABO LKIV O>KALJ S>IRBP ?BQTBBK 0 >KA 1):

15

)KQOLAR@QFLK

1. 0F@H > O>KALJ KRJ?BO: 21 2. #LJMRQB > MOL?>?FIFQV 0 QE>Q 21 PELRIA NR>IFCV. ,BQgP QOV: 0 = 21. 3. 0F@H >KLQEBO O>KALJ KRJ?BO: 22 4. )C 22 FP IBPP QE>K 0, QEBK TB E>SB CLRKA LRO KRJ?BOe21! 5. )C 22 FP KLQ IBPP QE>K 0, DL ?>@H QL PQBM 1 >KA PQ>OQ LSBO. (BOB TB >OB P>VFKD QE>Q QEB IFHBIFELLA QE>Q > O>KALJ S>IRB TFII NR>IFCV FP BNR>I QL QEB O>KALJ KRJ?BO FQPBIC. ,BQgP P>V TB MF@H 0.1 CLO 21. 4EFP JB>KP QE>Q 21 TFII E>SB > 10% @E>K@B LC NR>IFCVFKD. )C TB MF@H 0.83 CLO 21 QEBK FQ TFII E>SB > 83% @E>K@B LC NR>IFCVFKD. 4EB EFDEBO QEB KRJ?BO, QEB DOB>QBO QEB IFHBIFELLA QE>Q TB TFII >@QR>IIV RPB FQ. (BOB FP > CRK@QFLK (K>JBA CLO QEB -LKQB #>OIL JBQELA, TEF@E T>P K>JBA CLO QEB -LKQB #>OIL @>PFKL) QE>Q FJMIBJBKQP QEB >?LSB >IDLOFQEJ, OBQROKFKD > O>KALJ S>IRB ?BQTBBK 0 >KA 1.
@FI;N GIHN?=;LFI() U QBCF? (NLO?) U We do this forever until we find a qualifying random value. Pick a random value. Assign a probability. Pick a second random value.

@FI;N L1 = L;H>IG(1); @FI;N JLI<;<CFCNS = L1; @FI;N L2 = L;H>IG(1);

C@ (L2 < JLI<;<CFCNS) U L?NOLH L1; W W W

Does it qualify? If so, were done!

16

4EB .>QROB LC #LAB (S1.0)

Exercise I.6
5PB > @RPQLJ MOL?>?FIFQV AFPQOF?RQFLK QL S>OV QEB PFWB LC > PQBM Q>HBK ?V QEB O>KALJ T>IHBO. 4EB PQBM PFWB @>K ?B ABQBOJFKBA ?V FKCIRBK@FKD QEB O>KDB LC S>IRBP MF@HBA. #>K VLR J>M QEB MOL?>?FIFQV BUMLKBKQF>IIVeF.B. J>HFKD QEB IFHBIFELLA QE>Q > S>IRB FP MF@HBA BNR>I QL QEB S>IRB PNR>OBA?
@FI;N MN?JMCT? = L;H>IG(0,10); @FI;N MN?JR = L;H>IG(-MN?JMCT?,MN?JMCT?); @FI;N MN?JS = L;H>IG(-MN?JMCT?,MN?JMCT?); R += MN?JR; S += MN?JS; A uniform distribution of step sizes. Change this!

(,>QBO TBgII PBB ELT QL AL QEFP JLOB BCCF@FBKQIV RPFKD SB@QLOP.)

I.6 Perlin Noise (A Smoother Approach)


! DLLA O>KALJ KRJ?BO DBKBO>QLO MOLAR@BP KRJ?BOP QE>Q E>SB KL OBI>QFLKPEFM >KA PELT KL AFP@BOKF?IB M>QQBOK. !P TB >OB ?BDFKKFKD QL PBB, > IFQQIB ?FQ LC O>KALJKBPP @>K ?B > DLLA QEFKD TEBK MOLDO>JJFKD LOD>KF@, IFCBIFHB ?BE>SFLOP. (LTBSBO, O>KALJKBPP >P QEB PFKDIB DRFAFKD MOFK@FMIB FP KLQ KB@BPP>OFIV K>QRO>I. !K >IDLOFQEJ HKLTK >P h0BOIFK KLFPB,i K>JBA CLO FQP FKSBKQLO +BK 0BOIFK, Q>HBP QEFP @LK@BMQ FKQL >@@LRKQ. 0BOIFK ABSBILMBA QEB KLFPB CRK@QFLK TEFIB TLOHFKD LK QEB LOFDFK>I /FCB JLSFB FK QEB B>OIV 1980P; FQ T>P ABPFDKBA QL @OB>QB MOL@BARO>I QBUQROBP CLO @LJMRQBO-DBKBO>QBA BCCB@QP. )K 1997 0BOIFK TLK >K !@>ABJV !T>OA FK QB@EKF@>I >@EFBSBJBKQ CLO QEFP TLOH. 0BOIFK KLFPB @>K ?B RPBA QL DBKBO>QB S>OFLRP BCCB@QP TFQE K>QRO>I NR>IFQFBP, PR@E >P @ILRAP, I>KAP@>MBP, >KA M>QQBOKBA QBUQROBP IFHB J>O?IB. 0BOIFK KLFPB E>P > JLOB LOD>KF@ >MMB>O>K@B ?B@>RPB FQ MOLAR@BP > K>QRO>IIV LOABOBA (hPJLLQEi) PBNRBK@B LC MPBRAL-O>KALJ KRJ?BOP. 4EB DO>ME LK QEB IBCQ ?BILT PELTP 0BOIFK KLFPB LSBO QFJB, TFQE QEB U->UFP OBMOBPBKQFKD QFJB; KLQB QEB PJLLQEKBPP LC QEB @ROSB. 4EB DO>ME LK QEB OFDEQ PELTP MROB O>KALJ KRJ?BOP LSBO QFJB. (4EB @LAB CLO DBKBO>QFKD QEBPB DO>MEP FP >S>FI>?IB FK QEB >@@LJM>KVFKD ?LLH ALTKIL>AP.)

17

)KQOLAR@QFLK

F64B?2 !.5: %<6@2

F64B?2 !.6: (.;1<:

0OL@BPPFKD E>P > ?RFIQ-FK FJMIBJBKQ>QFLK LC QEB 0BOIFK KLFPB >IDLOFQEJ: QEB CRK@QFLK HICM?() . 4EB HICM?() CRK@QFLK Q>HBP LKB, QTL, LO QEOBB >ODRJBKQP, >P KLFPB FP @LJMRQBA FK LKB, QTL, LO QEOBB AFJBKPFLKP. ,BQgP PQ>OQ ?V ILLHFKD >Q LKB-AFJBKPFLK>I KLFPB.

Noise Detail
4EB 0OL@BPPFKD KLFPB OBCBOBK@B (EQQM://MOL@BPPFKD.LOD/OBCBOBK@B/KLFPB=.EQJI) QBIIP RP QE>Q KLFPB FP @>I@RI>QBA LSBO PBSBO>I hL@Q>SBP.i #>IIFKD QEB HICM?"?N;CF() (EQQM://MOL@BPPFKD.LOD/OBCBOBK@B/KLFPB$BQ>FI=.EQJI) CRK@QFLK TFII @E>KDB ?LQE QEB KRJ?BO LC L@Q>SBP >KA QEBFO FJMLOQ>K@B OBI>QFSB QL LKB >KLQEBO. 4EFP FK QROK @E>KDBP ELT QEB KLFPB CRK@QFLK ?BE>SBP. !K LKIFKB IB@QROB ?V +BK 0BOIFK IBQP VLR IB>OK JLOB >?LRQ ELT KLFPB TLOHP COLJ 0BOIFK EFJPBIC (EQQM://TTT.KLFPBJ>@EFKB.@LJ/Q>IH1/).

#LKPFABO AO>TFKD > @FO@IB FK LRO 0OL@BPPFKD TFKALT >Q > O>KALJ U-IL@>QFLK.
@FI;N R = L;H>IG(0,QC>NB); ?FFCJM?(R,180,16,16); A random x-location

.LT, FKPQB>A LC > O>KALJ U-IL@>QFLK, TB T>KQ > 0BOIFK KLFPB U-IL@>QFLK QE>Q FP hPJLLQEBO.i 9LR JFDEQ QEFKH QE>Q >II VLR KBBA QL AL FP OBMI>@B L;H>IG() TFQE HICM?() , F.B.
@FI;N R = HICM?(0,QC>NB); A noise x-location?

7EFIB @LK@BMQR>IIV QEFP FP BU>@QIV TE>Q TB T>KQ QL ALe@>I@RI>QB >K U-S>IRB QE>Q O>KDBP ?BQTBBK 0 >KA QEB TFAQE >@@LOAFKD QL 0BOIFK KLFPBeQEFP FP KLQ QEB @LOOB@Q FJMIBJBKQ>QFLK. 7EFIB QEB >ODRJBKQP QL QEB L;H>IG() CRK@QFLK PMB@FCV > O>KDB LC S>IRBP ?BQTBBK > JFKFJRJ >KA > J>UFJRJ, HICM?() ALBP KLQ TLOH QEFP T>V. )KPQB>A, QEB LRQMRQ O>KDB FP 18

4EB .>QROB LC #LAB (S1.0)

CFUBAeFQ >IT>VP OBQROKP > S>IRB ?BQTBBK 0 >KA 1. 7BgII PBB FK > JLJBKQ QE>Q TB @>K DBQ >OLRKA QEFP B>PFIV TFQE 0OL@BPPFKDgP G;J() CRK@QFLK, ?RQ CFOPQ TB JRPQ BU>JFKB TE>Q BU>@QIV HICM?() BUMB@QP RP QL M>PP FK >P >K >ODRJBKQ. 7B @>K QEFKH LC LKB-AFJBKPFLK>I 0BOIFK KLFPB >P > IFKB>O PBNRBK@B LC S>IRBP LSBO QFJB. &LO BU>JMIB:

Time 0 1 2 3 4

Noise Value 0.365 0.363 0.363 0.364 0.366

.LT, FK LOABO QL >@@BPP > M>OQF@RI>O KLFPB S>IRB FK 0OL@BPPFKD, TB E>SB QL M>PP > PMB@FCF@ "JLJBKQ FK QFJB" QL QEB HICM?() CRK@QFLK. &LO BU>JMIB:
@FI;N H = HICM?(3);

!@@LOAFKD QL QEB >?LSB Q>?IB, HICM?(3) TFII OBQROK 0.364 >Q QFJB BNR>IP 3. 7B @LRIA FJMOLSB QEFP ?V RPFKD > S>OF>?IB CLO QFJB >KA >PHFKD CLO > KLFPB S>IRB @LKQFKRLRPIV FK >L;Q() .
@FI;N N = 3; PIC> >L;Q() U @FI;N H = HICM?(N); JLCHNFH(H); W We need the noise value for a specific moment in time.

4EB >?LSB @LAB OBPRIQP FK QEB P>JB S>IRB MOFKQBA LSBO >KA LSBO. 4EFP E>MMBKP ?B@>RPB TB >OB >PHFKD CLO QEB OBPRIQ LC QEB HICM?() CRK@QFLK >Q QEB P>JB MLFKQ FK QFJBe3eLSBO >KA LSBO. )C TB FK@OBJBKQ QEB QFJB S>OF>?IB N , ELTBSBO, TBgII DBQ > AFCCBOBKQ OBPRIQ.
@FI;N N = 0; PIC> >L;Q() U @FI;N H = HICM?(N); JLCHNFH(H); Typically we would start at time = 0, though this is arbitrary.

19

)KQOLAR@QFLK

N += 0.01; W

Now, we move forward in time!

(LT NRF@HIV TB FK@OBJBKQ N >IPL >CCB@QP QEB PJLLQEKBPP LC QEB KLFPB. )C TB J>HB I>ODB GRJMP FK QFJB, QEBK TB >OB PHFMMFKD >EB>A >KA QEB S>IRBP TFII ?B JLOB O>KALJ.

F64B?2 !.7

4OV ORKKFKD QEB @LAB PBSBO>I QFJBP, FK@OBJBKQFKD N ?V 0.01, 0.02, 0.05, 0.1, 0.0001, >KA VLR TFII PBB AFCCBOBKQ OBPRIQP.

Mapping Noise
.LT TBgOB OB>AV QL >KPTBO QEB NRBPQFLK LC TE>Q QL AL TFQE QEB KLFPB S>IRB. /K@B TB E>SB QEB S>IRB TFQE > O>KDB ?BQTBBK 0 >KA 1, FQgP RM QL RP QL J>M QE>Q O>KDB QL TE>Q TB T>KQ. 4EB B>PFBPQ T>V QL AL QEFP FP TFQE 0OL@BPPFKDgP G;J() CRK@QFLK. 4EB G;J() CRK@QFLK Q>HBP CFSB >ODRJBKQP. &FOPQ RM FP QEB S>IRB TB T>KQ QL J>M, FK QEFP @>PB H . 4EBK TB E>SB QL DFSB FQ QEB S>IRBgP @ROOBKQ O>KDB (JFKFJRJ >KA J>UFJRJ), CLIILTBA ?V LRO ABPFOBA O>KDB.

F64B?2 !.8

)K QEFP @>PB, TB HKLT QE>Q KLFPB E>P > O>KDB ?BQTBBK 0 >KA 1, ?RQ TBgA IFHB QL AO>T LRO @FO@IB TFQE > O>KDB ?BQTBBK 0 >KA QEB TFKALTgP TFAQE.

20

4EB .>QROB LC #LAB (S1.0)


@FI;N N = 0; PIC> >L;Q() U @FI;N H = HICM?(N); @FI;N R = G;J(H,0,1,0,QC>NB); ?FFCJM?(R,180,16,16); N += 0.01; W Using map() to customize the range of Perlin noise

7B @>K >MMIV QEB BU>@Q P>JB ILDF@ QL LRO O>KALJ T>IHBO, >KA >PPFDK ?LQE FQP U- >KA V-S>IRBP >@@LOAFKD QL 0BOIFK KLFPB.

Example I.5: Perlin noise walker


=F;MM 4;FE?L U @FI;N R,S; @FI;N NR,NS; 4;FE?L() U NR = 0; NS = 10000; W PIC> MN?J() U R = G;J(HICM?(NR), 0, 1, 0, QC>NB); S = G;J(HICM?(NS), 0, 1, 0, B?CABN); x- and y-location mapped from noise

NR += 0.01; NS += 0.01; W W

Move forward through time.

21

)KQOLAR@QFLK

.LQF@B ELT QEB >?LSB BU>JMIB OBNRFOBP >K >AAFQFLK>I M>FO LC S>OF>?IBP: NR >KA NS . 4EFP FP ?B@>RPB TB KBBA QL HBBM QO>@H LC QTL QFJB S>OF>?IBP, LKB CLO QEB U-IL@>QFLK LC QEB 4;FE?L L?GB@Q >KA LKB CLO QEB V-IL@>QFLK. "RQ QEBOB FP PLJBQEFKD > ?FQ LAA >?LRQ QEBPB S>OF>?IBP. 7EV ALBP NR PQ>OQ >Q 0 >KA NS >Q 10,000? 7EFIB QEBPB KRJ?BOP >OB >O?FQO>OV @ELF@BP, TB E>SB SBOV PMB@FCF@>IIV FKFQF>IFWBA LRO QTL QFJB S>OF>?IBP TFQE AFCCBOBKQ S>IRBP. 4EFP FP ?B@>RPB QEB KLFPB CRK@QFLK FP ABQBOJFKFPQF@: FQ DFSBP VLR QEB P>JB OBPRIQ CLO > PMB@FCF@ QFJB N B>@E >KA BSBOV QFJB. )C TB >PHBA CLO QEB KLFPB S>IRB >Q QEB P>JB QFJB N CLO ?LQE R >KA S , QEBK R >KA S TLRIA >IT>VP ?B BNR>I, JB>KFKD QE>Q QEB 4;FE?L L?GB@Q TLRIA LKIV JLSB >ILKD > AF>DLK>I. )KPQB>A, TB PFJMIV RPB QTL AFCCBOBKQ M>OQP LC QEB KLFPB PM>@B, PQ>OQFKD >Q 0 CLO R >KA 10,000 CLO S PL QE>Q R >KA S @>K >MMB>O QL >@Q FKABMBKABKQIV LC B>@E LQEBO.

F64B?2 !.9

)K QORQE, QEBOB FP KL >@QR>I @LK@BMQ LC QFJB >Q MI>V EBOB. )QgP > RPBCRI JBQ>MELO QL EBIM RP RKABOPQ>KA ELT QEB KLFPB CRK@QFLK TLOHP, ?RQ OB>IIV TE>Q TB E>SB FP PM>@B, O>QEBO QE>K QFJB. 4EB DO>ME >?LSB ABMF@QP > IFKB>O PBNRBK@B LC KLFPB S>IRBP FK > LKB-AFJBKPFLK>I PM>@B, >KA TB @>K >PH CLO > S>IRB >Q > PMB@FCF@ U-IL@>QFLK TEBKBSBO TB T>KQ. )K BU>JMIBP, VLR TFII LCQBK PBB > S>OF>?IB K>JBA RI@@ QL FKAF@>QB QEB U-LCCPBQ >ILKD QEB KLFPB DO>ME, O>QEBO QE>K N CLO QFJB (>P KLQBA FK QEB AF>DO>J).

Exercise I.7
)K QEB >?LSB O>KALJ T>IHBO, QEB OBPRIQ LC QEB KLFPB CRK@QFLK FP J>MMBA AFOB@QIV QL QEB 4;FE?L gP IL@>QFLK. #OB>QB > O>KALJ T>IHBO TEBOB VLR FKPQB>A J>M QEB OBPRIQ LC QEB HICM?() CRK@QFLK QL > 4;FE?L gP PQBM PFWB.

Two-Dimensional Noise
4EFP FAB> LC KLFPB S>IRBP IFSFKD FK > LKB-AFJBKPFLK>I PM>@B FP FJMLOQ>KQ ?B@>RPB FQ IB>AP RP OFDEQ FKQL > AFP@RPPFLK LC QTL-AFJBKPFLK>I PM>@B. ,BQgP QEFKH >?LRQ QEFP CLO > JLJBKQ. 7FQE LKB-AFJBKPFLK>I KLFPB, TB E>SB > PBNRBK@B LC S>IRBP FK TEF@E >KV DFSBK S>IRB FP PFJFI>O QL FQP KBFDE?LO. "B@>RPB QEB S>IRB FP FK LKB AFJBKPFLK, FQ LKIV E>P QTL KBFDE?LOP: > S>IRB QE>Q @LJBP ?BCLOB FQ (QL QEB IBCQ LK QEB DO>ME) >KA LKB QE>Q @LJBP >CQBO FQ (QL QEB OFDEQ).

22

4EB .>QROB LC #LAB (S1.0)

F64B?2 !.10: 1D %<6@2

F64B?2 !.11: 2D %<6@2

4TL-AFJBKPFLK>I KLFPB TLOHP BU>@QIV QEB P>JB T>V @LK@BMQR>IIV. 4EB AFCCBOBK@B LC @LROPB FP QE>Q TB >OBKgQ ILLHFKD >Q S>IRBP >ILKD > IFKB>O M>QE, ?RQ S>IRBP QE>Q >OB PFQQFKD LK > DOFA. 4EFKH LC > MFB@B LC DO>ME M>MBO TFQE KRJ?BOP TOFQQBK FKQL B>@E @BII. ! DFSBK S>IRB TFII ?B PFJFI>O QL >II LC FQP KBFDE?LOP: >?LSB, ?BILT, QL QEB OFDEQ, QL QEB IBCQ, >KA >ILKD >KV AF>DLK>I. )C VLR TBOB QL SFPR>IFWB QEFP DO>ME M>MBO TFQE B>@E S>IRB J>MMBA QL QEB ?OFDEQKBPP LC > @LILO, VLR TLRIA DBQ PLJBQEFKD QE>Q ILLHP IFHB @ILRAP. 7EFQB PFQP KBUQ QL IFDEQ DO>V, TEF@E PFQP KBUQ QL DO>V, TEF@E PFQP KBUQ QL A>OH DO>V, TEF@E PFQP KBUQ QL ?I>@H, TEF@E PFQP KBUQ QL A>OH DO>V, BQ@.

4EFP FP TEV KLFPB T>P LOFDFK>IIV FKSBKQBA. 9LR QTB>H QEB M>O>JBQBOP > ?FQ LO MI>V TFQE @LILO QL J>HB QEB OBPRIQFKD FJ>DB ILLH JLOB IFHB J>O?IB LO TLLA LO >KV LQEBO LOD>KF@ QBUQROB. ,BQgP Q>HB > NRF@H ILLH >Q ELT QL FJMIBJBKQ QTL-AFJBKPFLK>I KLFPB FK 0OL@BPPFKD. )C VLR T>KQBA QL @LILO BSBOV MFUBI LC > TFKALT O>KALJIV, VLR TLRIA KBBA > KBPQBA ILLM, LKB QE>Q >@@BPPBA B>@E MFUBI >KA MF@HBA > O>KALJ ?OFDEQKBPP.
FI;>.CR?FM(); @IL (CHN R = 0; R < QC>NB; R++) U @IL (CHN S = 0; S < B?CABN; S++) U @FI;N <LCABN = L;H>IG(255); JCR?FM8R+S*QC>NB9 = =IFIL(<LCABN); W W OJ>;N?.CR?FM(); A random brightness!

23

)KQOLAR@QFLK

4L @LILO B>@E MFUBI >@@LOAFKD QL QEB HICM?() CRK@QFLK, TBgII AL BU>@QIV QEB P>JB QEFKD, LKIV FKPQB>A LC @>IIFKD L;H>IG() TBgII @>II HICM?() .
<BE7J 8H?=>J = C7F(DE?I;(N,O),0,1,0,255); A Perlin noise brightness!

4EFP FP > KF@B PQ>OQ @LK@BMQR>IIVeFQ DFSBP VLR > KLFPB S>IRB CLO BSBOV ( R ,S ) IL@>QFLK FK LRO QTL-AFJBKPFLK>I PM>@B. 4EB MOL?IBJ FP QE>Q QEFP TLKgQ E>SB QEB @ILRAV NR>IFQV TB T>KQ. *RJMFKD COLJ MFUBI 200 QL MFUBI 201 FP QLL I>ODB LC > GRJM QEOLRDE KLFPB. 2BJBJ?BO, TEBK TB TLOHBA TFQE LKB-AFJBKPFLK>I KLFPB, TB FK@OBJBKQBA LRO QFJB S>OF>?IB ?V 0.01 B>@E CO>JB, KLQ ?V 1! ! MOBQQV DLLA PLIRQFLK QL QEFP MOL?IBJ FP QL GRPQ RPB AFCCBOBKQ S>OF>?IBP CLO QEB KLFPB >ODRJBKQP. &LO BU>JMIB, TB @LRIA FK@OBJBKQ > S>OF>?IB @>IIBA RI@@ B>@E QFJB TB JLSB ELOFWLKQ>IIV, >KA > SI@@ S>OF>?IB B>@E QFJB TB JLSB SBOQF@>IIV QEOLRDE QEB KBPQBA ILLMP. Example I.6: 2D Perlin noise
<BE7J NE<< = 0.0; @IL (CHN R = 0; R < QC>NB; R++) U <BE7J OE<< = 0.0; @IL (CHN S = 0; S < B?CABN; S++) U <BE7J 8H?=>J = C7F(DE?I;(NE<<,OE<<),0,1,0,255); JCR?FM8R+S*QC>NB9 = =IFIL(<LCABN); OE<< += 0.01; W NE<< += 0.01; W Increment xoff. Use xoff and yoff for noise(). For every xoff, start yoff at 0. Start xoff at 0.

Use x and y for pixel location. Increment yoff.

Exercise I.8
0I>V TFQE @LILO, HICM?"?N;CF() , >KA QEB O>QB >Q TEF@E RI@@ >KA SI@@ >OB FK@OBJBKQBA QL >@EFBSB AFCCBOBKQ SFPR>I BCCB@QP.

24

4EB .>QROB LC #LAB (S1.0)

Exercise I.9
!AA > QEFOA >ODRJBKQ QL KLFPB QE>Q FK@OBJBKQP LK@B MBO @V@IB QEOLRDE >L;Q() QL >KFJ>QB QEB QTL-AFJBKPFLK>I KLFPB.

Exercise I.10
5PB QEB KLFPB S>IRBP >P QEB BIBS>QFLKP LC > I>KAP@>MB. 3BB QEB P@OBBKPELQ ?BILT >P > OBCBOBK@B.

7BgSB BU>JFKBA PBSBO>I QO>AFQFLK>I RPBP LC 0BOIFK KLFPB FK QEFP PB@QFLK. 7FQE LKB-AFJBKPFLK>I KLFPB, TB RPBA PJLLQE S>IRBP QL >PPFDK QEB IL@>QFLK LC >K L?GB@Q QL DFSB QEB >MMB>O>K@B LC T>KABOFKD. 7FQE QTL-AFJBKPFLK>I KLFPB, TB @OB>QBA > @ILRAV M>QQBOK TFQE PJLLQEBA S>IRBP LK > MI>KB LC MFUBIP. )QgP FJMLOQ>KQ QL OBJBJ?BO, ELTBSBO, QE>Q 0BOIFK KLFPB S>IRBP >OB GRPQ QE>QeS>IRBP. 4EBV >OBKgQ FKEBOBKQIV QFBA QL MFUBI IL@>QFLKP LO @LILO. !KV BU>JMIB FK QEFP ?LLH QE>Q E>P > S>OF>?IB @LRIA ?B @LKQOLIIBA SF> 0BOIFK KLFPB. 7EBK TB JLABI > TFKA CLO@B, FQP PQOBKDQE @LRIA ?B @LKQOLIIBA ?V 0BOIFK KLFPB. 3>JB DLBP CLO QEB >KDIBP ?BQTBBK QEB ?O>K@EBP FK > CO>@Q>I QOBB M>QQBOK, LO QEB PMBBA >KA AFOB@QFLK LC L?GB@QP JLSFKD >ILKD > DOFA FK > CILT CFBIA PFJRI>QFLK.

25

)KQOLAR@QFLK

F64B?2 !.12: *?22 D6A5 '2?96; ;<6@2

F64B?2 !.13: F9<D 36291 D6A5 '2?96; ;<6@2

I.7 Onward
7B ?BD>K QEFP @E>MQBO ?V Q>IHFKD >?LRQ ELT O>KALJKBPP @>K ?B > @ORQ@E. )K J>KV T>VP, FQgP QEB JLPQ L?SFLRP >KPTBO QL QEB HFKAP LC NRBPQFLKP TB >PH @LKQFKRLRPIVeELT PELRIA QEFP L?GB@Q JLSB? 7E>Q @LILO PELRIA FQ ?B? 4EFP L?SFLRP >KPTBO, ELTBSBO, @>K >IPL ?B > I>WV LKB. !P TB CFKFPE LCC QEB FKQOLAR@QFLK, FQgP >IPL TLOQE KLQFKD QE>Q TB @LRIA GRPQ >P B>PFIV C>II FKQL QEB QO>M LC RPFKD 0BOIFK KLFPB >P > @ORQ@E. (LT PELRIA QEFP L?GB@Q JLSB? 0BOIFK KLFPB! 7E>Q @LILO PELRIA FQ ?B? 0BOIFK KLFPB! (LT C>PQ PELRIA FQ DOLT? 0BOIFK KLFPB! 4EB MLFKQ LC >II LC QEFP FP KLQ QL P>V QE>Q VLR PELRIA LO PELRIAKgQ RPB O>KALJKBPP. /O QE>Q VLR PELRIA LO PELRIAKgQ RPB 0BOIFK KLFPB. 4EB MLFKQ FP QE>Q QEB ORIBP LC VLRO PVPQBJ >OB ABCFKBA ?V VLR, >KA QEB I>ODBO VLRO QLLI?LU, QEB JLOB @ELF@BP VLRgII E>SB >P VLR FJMIBJBKQ QELPB ORIBP. 4EB DL>I LC QEFP ?LLH FP QL CFII VLRO QLLI?LU. )C >II VLR HKLT FP O>KALJ, QEBK VLRO ABPFDK QEFKHFKD FP IFJFQBA. 3ROB, 0BOIFK KLFPB EBIMP, ?RQ VLRgII KBBA JLOB. ! ILQ JLOB. ) QEFKH TBgOB OB>AV QL ?BDFK.

26

4EB .>QROB LC #LAB (S1.0)

Chapter 1. Vectors
I(<42?, (<42?. ,5.AK@ <B? C20A<?, +60A<??J Captain Oveur (Airplane)
4EFP ?LLH FP >II >?LRQ ILLHFKD >Q QEB TLOIA >OLRKA RP >KA @LJFKD RM TFQE @IBSBO T>VP QL PFJRI>QB QE>Q TLOIA TFQE @LAB. $FSFABA FKQL QEOBB M>OQP, QEB ?LLH TFII PQ>OQ ?V ILLHFKD >Q ?>PF@ MEVPF@PeELT >K >MMIB C>IIP COLJ > QOBB, > MBKARIRJ PTFKDP FK QEB >FO, QEB B>OQE OBSLISBP >OLRKA QEB PRK, BQ@. !?PLIRQBIV BSBOVQEFKD @LKQ>FKBA TFQEFK QEB CFOPQ CFSB @E>MQBOP LC QEFP ?LLH OBNRFOBP QEB RPB LC QEB JLPQ ?>PF@ ?RFIAFKD ?IL@H CLO MOLDO>JJFKD JLQFLKeQEB A0.?:=. !KA PL QEFP FP TEBOB TB ?BDFK LRO PQLOV. .LT, QEB TLOA SB@QLO @>K JB>K > ILQ LC AFCCBOBKQ QEFKDP. 6B@QLO FP QEB K>JB LC > .BT 7>SB OL@H ?>KA CLOJBA FK 3>@O>JBKQL, #! FK QEB B>OIV 1980P. )QgP QEB K>JB LC > ?OB>HC>PQ @BOB>I J>KRC>@QROBA ?V +BIILDDgP #>K>A>. )K QEB CFBIA LC BMFABJFLILDV, > SB@QLO FP RPBA QL ABP@OF?B >K LOD>KFPJ QE>Q QO>KPJFQP FKCB@QFLK COLJ LKB ELPQ QL >KLQEBO. )K QEB #++ MOLDO>JJFKD I>KDR>DB, > SB@QLO (PQA::SB@QLO) FP >K FJMIBJBKQ>QFLK LC > AVK>JF@>IIV OBPFW>?IB >OO>V A>Q> PQOR@QROB. 7EFIB >II QEBPB ABCFKFQFLKP >OB FKQBOBPQFKD, QEBVgOB KLQ TE>Q TBgOB ILLHFKD CLO. 7E>Q TB T>KQ FP @>IIBA > E@.74/0,9 A0.?:= (K>JBA CLO QEB 'OBBH J>QEBJ>QF@F>K %R@IFA >KA >IPL HKLTK >P > DBLJBQOF@ SB@QLO). 7EBK VLR PBB QEB QBOJ hSB@QLOi FK QEFP ?LLH, VLR @>K >PPRJB FQ OBCBOP QL > %R@IFAB>K SB@QLO, ABCFKBA >P 5B 9BH=HM H<5H <5G 6CH< A5;B=HI89 5B8 8=F97H=CB . ! SB@QLO FP QVMF@>IIV AO>TK >P > >OOLT; QEB AFOB@QFLK FP FKAF@>QBA ?V TEBOB QEB >OOLT FP MLFKQFKD, >KA QEB J>DKFQRAB ?V QEB IBKDQE LC QEB >OOLT FQPBIC.

27

#E>MQBO 1. 6B@QLOP

F64B?2 1.1: A C20A<? (1?.D; .@ .; .??<D) 5.@ :.4;6AB12 (92;4A5 <3 .??<D) .;1 16?20A6<; (D5605 D.F 6A 6@ =<6;A6;4).

)K QEB >?LSB FIIRPQO>QFLK, QEB SB@QLO FP AO>TK >P >K >OOLT COLJ MLFKQ ! QL MLFKQ " >KA PBOSBP >P >K FKPQOR@QFLK CLO ELT QL QO>SBI COLJ ! QL ".

1.1 Vectors, You Complete Me


"BCLOB TB AFSB FKQL JLOB LC QEB ABQ>FIP >?LRQ SB@QLOP, IBQgP ILLH >Q > ?>PF@ 0OL@BPPFKD BU>JMIB QE>Q ABJLKPQO>QBP TEV TB PELRIA @>OB >?LRQ SB@QLOP FK QEB CFOPQ MI>@B. )C VLRgSB OB>A >KV LC QEB FKQOLAR@QLOV 0OL@BPPFKD QBUQ?LLHP LO Q>HBK > @I>PP LK MOLDO>JJFKD TFQE 0OL@BPPFKD (>KA ELMBCRIIV VLRgSB ALKB LKB LC QEBPB QEFKDP QL EBIM MOBM>OB VLR CLO QEFP ?LLH), VLR MOL?>?IV, >Q LKB MLFKQ LO >KLQEBO, IB>OKBA ELT QL TOFQB > PFJMIB ?LRK@FKD ?>II PHBQ@E.

!3 F<B .?2 ?2.16;4 A56@ /<<8 .@ . 'DF <? 6; =?6;A, A52; F<B D699 <;9F @22 @0?22;@5<A@ <3 A52 0<12. $<A6<;, <3 0<B?@2, 6@ . 82F 292:2;A <3 <B? 16@0B@@6<;, @< A< A52 2EA2;A =<@@6/92, A52 @A.A60 @0?22;@5<A@ D699 6;09B12 A?.69@ A< 46C2 . @2;@2 <3 A52 /25.C6<?. F<? :<?2 ./<BA 5<D A< 1?.D A?.69@, @22 A52 0<12 2E.:=92@ .C.69./92 3<? 1<D;9<.1.

28

4EB .>QROB LC #LAB (S1.0)

Example 1.1: Bouncing ball with no vectors


@FI;N @FI;N @FI;N @FI;N R = 100; S = 100; RMJ??> = 1; SMJ??> = 3.3; Variables for location and speed of ball.

PIC> M?NOJ() U MCT?(640,360); <;=EALIOH>(255); W PIC> >L;Q() U <;=EALIOH>(255); R = R + RMJ??>; S = S + SMJ??>;

Remember how Processing works? setup() is executed once when the sketch starts and draw() loops forever and ever (until you quit).

Move the ball according to its speed.

C@ ((R > RMJ??> W C@ ((S > SMJ??> W

QC>NB) VV (R < 0)) U = RMJ??> * -1; B?CABN) VV (S < 0)) U = SMJ??> * -1;

Check for bouncing.

MNLIE?(0); @CFF(175); ?FFCJM?(R,S,16,16); W Display the ball at the location (x,y).

)K QEB >?LSB BU>JMIB, TB E>SB > SBOV PFJMIB TLOIAe> ?I>KH @>KS>P TFQE > @FO@RI>O PE>MB (> h?>IIi) QO>SBIFKD >OLRKA. 4EFP ?>II E>P PLJB MOLMBOQFBP, TEF@E >OB OBMOBPBKQBA FK QEB @LAB >P S>OF>?IBP.

,L@>QFLK 3MBBA

L 5B8 M LGD998 5B8 MGD998

)K > JLOB >AS>K@BA PHBQ@E, TB @LRIA FJ>DFKB E>SFKD J>KV JLOB S>OF>?IBP:

29

#E>MQBO 1. 6B@QLOP

!@@BIBO>QFLK 4>ODBQ IL@>QFLK 7FKA &OF@QFLK

L5779@9F5H=CB 5B8 M5779@9F5H=CB LH5F;9H 5B8 MH5F;9H LK=B8 5B8 MK=B8 L:F=7H=CB 5B8 M:F=7H=CB

)QgP ?B@LJFKD @IB>OBO QE>Q CLO BSBOV @LK@BMQ FK QEFP TLOIA (TFKA, IL@>QFLK, >@@BIBO>QFLK, BQ@.), TBgII KBBA QTL S>OF>?IBP. !KA QEFP FP LKIV > QTL-AFJBKPFLK>I TLOIA. )K > 3$ TLOIA, TBgII KBBA R , S , T , RMJ??> , SMJ??> , TMJ??> , >KA PL LK. 7LRIAKgQ FQ ?B KF@B FC TB @LRIA PFJMIFCV LRO @LAB >KA RPB CBTBO S>OF>?IBP? )KPQB>A LC:
@FI;N @FI;N @FI;N @FI;N R; S; RMJ??>; SMJ??>;

7B @LRIA PFJMIV E>SBk


3?=NIL FI=;NCIH; 3?=NIL MJ??>;

4>HFKD QEFP CFOPQ PQBM FK RPFKD SB@QLOP TLKgQ >IILT RP QL AL >KVQEFKD KBT. *RPQ >AAFKD SB@QLOP TLKgQ J>DF@>IIV J>HB VLRO 0OL@BPPFKD PHBQ@EBP PFJRI>QB MEVPF@P. (LTBSBO, QEBV TFII PFJMIFCV VLRO @LAB >KA MOLSFAB > PBQ LC CRK@QFLKP CLO @LJJLK J>QEBJ>QF@>I LMBO>QFLKP QE>Q E>MMBK LSBO >KA LSBO >KA LSBO >D>FK TEFIB MOLDO>JJFKD JLQFLK. !P >K FKQOLAR@QFLK QL SB@QLOP, TBgOB DLFKD QL IFSB FK QTL AFJBKPFLKP CLO NRFQB PLJB QFJB (>Q IB>PQ RKQFI TB DBQ QEOLRDE QEB CFOPQ PBSBO>I @E>MQBOP). !II LC QEBPB BU>JMIBP @>K ?B C>FOIV B>PFIV BUQBKABA QL QEOBB AFJBKPFLKP (>KA QEB @I>PP TB TFII RPBe .3?=NIL e>IILTP CLO QEOBB AFJBKPFLKP.) (LTBSBO, FQgP B>PFBO QL PQ>OQ TFQE GRPQ QTL.

1.2 Vectors for Processing Programmers


/KB T>V QL QEFKH LC > SB@QLO FP QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP. #LKPFABO ELT VLR JFDEQ DL >?LRQ MOLSFAFKD FKPQOR@QFLKP QL T>IH COLJ LKB MLFKQ QL >KLQEBO. (BOB >OB PLJB SB@QLOP >KA MLPPF?IB QO>KPI>QFLKP:

30

4EB .>QROB LC #LAB (S1.0)

F64B?2 1.2

(-15, 3) (3, 4) (2, -1)

25@? :=:H99B GH9DG K9GH; HIFB 5B8 K5@? H<F99 GH9DG BCFH<. 25@? H<F99 GH9DG 95GH; HIFB 5B8 K5@? :=J9 GH9DG BCFH<. 25@? HKC GH9DG 95GH; HIFB 5B8 K5@? CB9 GH9D GCIH<.

9LRgSB MOL?>?IV ALKB QEFP ?BCLOB TEBK MOLDO>JJFKD JLQFLK. &LO BSBOV CO>JB LC >KFJ>QFLK (F.B. > PFKDIB @V@IB QEOLRDE 0OL@BPPFKDgP >L;Q() ILLM), VLR FKPQOR@Q B>@E L?GB@Q LK QEB P@OBBK QL JLSB > @BOQ>FK KRJ?BO LC MFUBIP ELOFWLKQ>IIV >KA > @BOQ>FK KRJ?BO LC MFUBIP SBOQF@>IIV.

F64B?2 1.3

&LO BSBOV CO>JB: 90B 7:.,?4:9 = A07:.4?D ,;;740/ ?: .@==09? 7:.,?4:9 )C SBIL@FQV FP > SB@QLO (QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP), TE>Q FP IL@>QFLK? )P FQ > SB@QLO QLL? 4B@EKF@>IIV, LKB JFDEQ >ODRB QE>Q IL@>QFLK FP KLQ > SB@QLO, PFK@B FQgP KLQ ABP@OF?FKD ELT QL JLSB COLJ LKB MLFKQ QL >KLQEBOeFQgP PFJMIV ABP@OF?FKD > PFKDRI>O MLFKQ FK PM>@B.

31

#E>MQBO 1. 6B@QLOP

.BSBOQEBIBPP, >KLQEBO T>V QL ABP@OF?B > IL@>QFLK FP QEB M>QE Q>HBK COLJ QEB LOFDFK QL OB>@E QE>Q IL@>QFLK. (BK@B, > IL@>QFLK @>K ?B QEB SB@QLO OBMOBPBKQFKD QEB AFCCBOBK@B ?BQTBBK IL@>QFLK >KA LOFDFK.

F64B?2 1.4

,BQgP BU>JFKB QEB RKABOIVFKD A>Q> CLO ?LQE IL@>QFLK >KA SBIL@FQV. )K QEB ?LRK@FKD ?>II BU>JMIB, TB E>A QEB CLIILTFKD:

IL@>QFLK SBIL@FQV

L,M LGD998,MGD998

.LQF@B ELT TB >OB PQLOFKD QEB P>JB A>Q> CLO ?LQEeQTL CIL>QFKD MLFKQ KRJ?BOP, >K R >KA > S . )C TB TBOB QL TOFQB > SB@QLO @I>PP LROPBISBP, TBgA PQ>OQ TFQE PLJBQEFKD O>QEBO ?>PF@:
=F;MM .3?=NIL U @FI;N R; @FI;N S; .3?=NIL(@FI;N R:, @FI;N S:) U R = R:; S = S:; W W

!Q FQP @LOB, > .3?=NIL FP GRPQ > @LKSBKFBKQ T>V QL PQLOB QTL S>IRBP (LO QEOBB, >P TBgII PBB FK 3$ BU>JMIBP). !KA PL QEFP k

32

4EB .>QROB LC #LAB (S1.0)


@FI;N @FI;N @FI;N @FI;N R = 100; S = 100; RMJ??> = 1; SMJ??> = 3.3;

?B@LJBP k
.3?=NIL FI=;NCIH = H?Q .3?=NIL(100,100); .3?=NIL P?FI=CNS = H?Q .3?=NIL(1,3.3);

.LT QE>Q TB E>SB QTL SB@QLO L?GB@QP (IL@>QFLK >KA SBIL@FQV), TBgOB OB>AV QL FJMIBJBKQ QEB >IDLOFQEJ CLO JLQFLKe7:.,?4:9 = 7:.,?4:9 + A07:.4?D. )K %U>JMIB 1.1, TFQELRQ SB@QLOP, TB E>A:
R = R + RMJ??>; S = S + SMJ??>; Add each speed to each location.

)K >K FAB>I TLOIA, TB TLRIA ?B >?IB QL OBTOFQB QEB >?LSB >P:


FI=;NCIH = FI=;NCIH + P?FI=CNS; Add the velocity vector to the location vector.

(LTBSBO, FK 0OL@BPPFKD, QEB >AAFQFLK LMBO>QLO + FP OBPBOSBA CLO MOFJFQFSB S>IRBP (FKQBDBOP, CIL>QP, BQ@.) LKIV. 0OL@BPPFKD ALBPKgQ HKLT ELT QL >AA QTL .3?=NIL L?GB@QP QLDBQEBO >KV JLOB QE>K FQ HKLTP ELT QL >AA QTL .$IHN L?GB@QP LO .'G;A? L?GB@QP. &LOQRK>QBIV CLO RP, QEB .3?=NIL @I>PP FK@IRABP CRK@QFLKP CLO @LJJLK J>QEBJ>QF@>I LMBO>QFLKP.

1.3 Vector Addition


"BCLOB TB @LKQFKRB ILLHFKD >Q QEB .3?=NIL @I>PP >KA FQP ;>>() JBQELA (MROBIV CLO QEB P>HB LC IB>OKFKD PFK@B FQgP >IOB>AV FJMIBJBKQBA CLO RP FK 0OL@BPPFKD FQPBIC), IBQgP BU>JFKB SB@QLO >AAFQFLK RPFKD QEB KLQ>QFLK CLRKA FK J>QE >KA MEVPF@P QBUQ?LLHP. 6B@QLOP >OB QVMF@>IIV TOFQQBK BFQEBO FK ?LIAC>@B QVMB LO TFQE >K >OOLT LK QLM. &LO QEB MROMLPBP LC QEFP ?LLH, QL AFPQFKDRFPE > A0.?:= COLJ > >.,7,= (G75@5F OBCBOP QL > PFKDIB S>IRB, PR@E >P >K FKQBDBO LO > CIL>QFKD MLFKQ KRJ?BO), TBgII RPB QEB >OOLT KLQ>QFLK: j 6B@QLO: u j 3@>I>O: x ,BQgP P>V ) E>SB QEB CLIILTFKD QTL SB@QLOP:

33

#E>MQBO 1. 6B@QLOP

F64B?2 1.5

%>@E SB@QLO E>P QTL @LJMLKBKQP, >K R >KA > S . 4L >AA QTL SB@QLOP QLDBQEBO, TB PFJMIV >AA ?LQE R gP >KA ?LQE S gP.

F64B?2 1.6

)K LQEBO TLOAP:

w= u + v

@>K ?B TOFQQBK >P:

wx = ux + vx wy = uy + vy
4EBK, OBMI>@FKD O >KA P TFQE QEBFO S>IRBP COLJ &FDROB 1.6, TB DBQ:

wx = 5 + 3
TEF@E JB>KP QE>Q: &FK>IIV, TB TOFQB QE>Q >P > SB@QLO:

w = (8, 6)

34

4EB .>QROB LC #LAB (S1.0)

.LT QE>Q TB RKABOPQ>KA ELT QL >AA QTL SB@QLOP QLDBQEBO, TB @>K ILLH >Q ELT >AAFQFLK FP FJMIBJBKQBA FK QEB .3?=NIL @I>PP FQPBIC. ,BQgP TOFQB > CRK@QFLK @>IIBA ;>>() QE>Q Q>HBP >KLQEBO .3?=NIL L?GB@Q >P FQP >ODRJBKQ.
=F;MM .3?=NIL U @FI;N R; @FI;N S; .3?=NIL(@FI;N R:, @FI;N S:) U R = R:; S = S:; W LE?: 7::(+0;9JEH L) Q O = O + L.O; N = N + L.N; S W New! A function to add another PVector to this PVector. Simply add the L components and the M components together.

.LT QE>Q TB PBB ELT ;>>() FP TOFQQBK FKPFAB LC .3?=NIL , TB @>K OBQROK QL LRO ?LRK@FKD ?>II BU>JMIB TFQE FQP 7:.,?4:9 + A07:.4?D >IDLOFQEJ >KA FJMIBJBKQ SB@QLO >AAFQFLK:
FI=;NCIH = FI=;NCIH + P?FI=CNS; FI=;NCIH.;>>(P?FI=CNS); Add the current velocity to the location.

!KA EBOB TB >OB, OB>AV QL OBTOFQB QEB ?LRK@FKD ?>II BU>JMIB RPFKD .3?=NIL . Example 1.2: Bouncing ball with PVectors!
+0;9JEH BE97J?ED; +0;9JEH L;BE9?JO; PIC> M?NOJ() U MCT?(640,360); BE97J?ED = D;M +0;9JEH(100,100); L;BE9?JO = D;M +0;9JEH(2.5,5); W PIC> >L;Q() U <;=EALIOH>(255); BE97J?ED.7::(L;BE9?JO); Instead of a bunch of floats, we now just have two PVector variables.

35

#E>MQBO 1. 6B@QLOP

?< ((BE97J?ED.N > M?:J>) RR (BE97J?ED.N < 0)) Q L;BE9?JO.N = L;BE9?JO.N * -1; S ?< ((BE97J?ED.O > >;?=>J) RR (BE97J?ED.O < 0)) Q L;BE9?JO.O = L;BE9?JO.O * -1; S MNLIE?(0); @CFF(175); ;BB?FI;(BE97J?ED.N,BE97J?ED.O,16,16); W

We still sometimes need to refer to the individual components of a PVector and can do so using the dot syntax: location.x, velocity.y, etc.

.LT, VLR JFDEQ CBBI PLJBTE>Q AFP>MMLFKQBA. !CQBO >II, QEFP J>V FKFQF>IIV >MMB>O QL E>SB J>AB QEB @LAB JLOB @LJMIF@>QBA QE>K QEB LOFDFK>I SBOPFLK. 7EFIB QEFP FP > MBOCB@QIV OB>PLK>?IB >KA S>IFA @OFQFNRB, FQgP FJMLOQ>KQ QL RKABOPQ>KA QE>Q TB E>SBKgQ CRIIV OB>IFWBA QEB MLTBO LC MOLDO>JJFKD TFQE SB@QLOP GRPQ VBQ. ,LLHFKD >Q > PFJMIB ?LRK@FKD ?>II >KA LKIV FJMIBJBKQFKD SB@QLO >AAFQFLK FP GRPQ QEB CFOPQ PQBM. !P TB JLSB CLOT>OA FKQL > JLOB @LJMIBU TLOIA LC JRIQFMIB L?GB@QP >KA JRIQFMIB 1:=.0> (TEF@E TBgII FKQOLAR@B FK #E>MQBO 2), QEB ?BKBCFQP LC .3?=NIL TFII ?B@LJB JLOB >MM>OBKQ. 7B PELRIA, ELTBSBO, KLQB >K FJMLOQ>KQ >PMB@Q LC QEB >?LSB QO>KPFQFLK QL MOLDO>JJFKD TFQE SB@QLOP. %SBK QELRDE TB >OB RPFKD .3?=NIL L?GB@QP QL ABP@OF?B QTL S>IRBPeQEB R >KA S LC IL@>QFLK >KA QEB R >KA S LC SBIL@FQVeTB PQFII LCQBK KBBA QL OBCBO QL QEB L >KA M @LJMLKBKQP LC B>@E .3?=NIL FKAFSFAR>IIV. 7EBK TB DL QL AO>T >K L?GB@Q FK 0OL@BPPFKD, QEBOBgP KL JB>KP CLO RP QL P>V:
?FFCJM?(FI=;NCIH,16,16);

4EB ?FFCJM?() CRK@QFLK ALBP KLQ >IILT CLO > .3?=NIL >P >K >ODRJBKQ. !K BIIFMPB @>K LKIV ?B AO>TK TFQE QTL P@>I>O S>IRBP, >K R -@LLOAFK>QB >KA > S -@LLOAFK>QB. !KA PL TB JRPQ AFD FKQL QEB .3?=NIL L?GB@Q >KA MRII LRQ QEB L >KA M @LJMLKBKQP RPFKD L?GB@Q-LOFBKQBA ALQ PVKQ>U.
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);

4EB P>JB FPPRB >OFPBP TEBK QBPQFKD FC QEB @FO@IB E>P OB>@EBA QEB BADB LC QEB TFKALT, >KA TB KBBA QL >@@BPP QEB FKAFSFAR>I @LJMLKBKQP LC ?LQE SB@QLOP: FI=;NCIH >KA P?FI=CNS .
C@ ((FI=;NCIH.R > QC>NB) VV (FI=;NCIH.R < 0)) U P?FI=CNS.R = P?FI=CNS.R * -1; W

36

4EB .>QROB LC #LAB (S1.0)

Exercise 1.1
&FKA PLJBQEFKD VLRgSB MOBSFLRPIV J>AB FK 0OL@BPPFKD RPFKD PBM>O>QB R >KA S S>OF>?IBP >KA RPB .3?=NIL P FKPQB>A.

Exercise 1.2
4>HB LKB LC QEB T>IHBO BU>JMIBP COLJ QEB FKQOLAR@QFLK >KA @LKSBOQ FQ QL RPB .3?=NIL P.

Exercise 1.3
%UQBKA QEB ?LRK@FKD ?>II TFQE SB@QLOP BU>JMIB FKQL 3$. #>K VLR DBQ > PMEBOB QL ?LRK@B >OLRKA > ?LU?

1.4 More Vector Math


!AAFQFLK T>P OB>IIV GRPQ QEB CFOPQ PQBM. 4EBOB >OB J>KV J>QEBJ>QF@>I LMBO>QFLKP QE>Q >OB @LJJLKIV RPBA TFQE SB@QLOP. "BILT FP > @LJMOBEBKPFSB IFPQ LC QEB LMBO>QFLKP >S>FI>?IB >P CRK@QFLKP FK QEB .3?=NIL @I>PP. 7BgII DL QEOLRDE > CBT LC QEB HBV LKBP KLT. !P LRO BU>JMIBP DBQ JLOB PLMEFPQF@>QBA FK I>QBO @E>MQBOP, TBgII @LKQFKRB QL OBSB>I QEB ABQ>FIP LC JLOB CRK@QFLKP. j ;>>() e >AA SB@QLOP j MO<() e PR?QO>@Q SB@QLOP j GOFN() e P@>IB QEB SB@QLO TFQE JRIQFMIF@>QFLK j >CP() e P@>IB QEB SB@QLO TFQE AFSFPFLK j G;A() e @>I@RI>QB QEB J>DKFQRAB LC > SB@QLO j M?N+;A() - PBQ QEB J>DKFQRAB LC > SB@QLO j HILG;FCT?() e KLOJ>IFWB QEB SB@QLO QL > RKFQ IBKDQE LC 1 j FCGCN() e IFJFQ QEB J>DKFQRAB LC > SB@QLO j B?;>CHA() e QEB 2$ EB>AFKD LC > SB@QLO BUMOBPPBA >P >K >KDIB j LIN;N?() e OLQ>QB > 2$ SB@QLO ?V >K >KDIB

37

#E>MQBO 1. 6B@QLOP

j F?LJ() e IFKB>O FKQBOMLI>QB QL >KLQEBO SB@QLO j >CMN() e QEB %R@IFAB>K AFPQ>K@B ?BQTBBK QTL SB@QLOP (@LKPFABOBA >P MLFKQP) j ;HAF? ?NQ??H() e CFKA QEB >KDIB ?BQTBBK QTL SB@QLOP j >IN() e QEB ALQ MOLAR@Q LC QTL SB@QLOP j =LIMM() e QEB @OLPP MOLAR@Q LC QTL SB@QLOP (LKIV OBIBS>KQ FK QEOBB AFJBKPFLKP) j L;H>IG2"() - J>HB > O>KALJ 2$ SB@QLO j L;H>IG3"() - J>HB > O>KALJ 3$ SB@QLO (>SFKD >IOB>AV @LSBOBA >AAFQFLK, IBQgP PQ>OQ TFQE PR?QO>@QFLK. 4EFP LKBgP KLQ PL ?>A; GRPQ Q>HB QEB MIRP PFDK >KA OBMI>@B FQ TFQE > JFKRP!

Vector subtraction

w= u v

@>K ?B TOFQQBK >P:

wx = ux vx wy = uy vy

F64B?2 1.7: +20A<? )B/A?.0A6<;

>KA PL QEB CRK@QFLK FKPFAB .3?=NIL ILLHP IFHB:


PIC> MO<(.3?=NIL P) U R = R - P.R; S = S - P.S; W

4EB CLIILTFKD BU>JMIB ABJLKPQO>QBP SB@QLO PR?QO>@QFLK ?V Q>HFKD QEB AFCCBOBK@B ?BQTBBK QTL MLFKQPeQEB JLRPB IL@>QFLK >KA QEB @BKQBO LC QEB TFKALT.

38

4EB .>QROB LC #LAB (S1.0)

Example 1.3: Vector subtraction


PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2); GIOM?.MO<(=?HN?L); NL;HMF;N?(QC>NB/2,B?CABN/2); FCH?(0,0,GIOM?.R,GIOM?.S); W Two PVectors, one for the mouse location and one for the center of the window PVector subtraction! Draw a line to represent the vector.

Basic Number Properties with Vectors


!AAFQFLK TFQE SB@QLOP CLIILT QEB P>JB >IDB?O>F@ ORIBP >P TFQE OB>I KRJ?BOP. (30 .:88@?,?4A0 =@70: u + v = v + u (30 ,>>:.4,?4A0 =@70: u + ( v + w) = ( u + v ) + w &>K@V QBOJFKLILDV >KA PVJ?LIP >PFAB, QEFP FP OB>IIV NRFQB > PFJMIB @LK@BMQ. 7BgOB GRPQ P>VFKD QE>Q @LJJLK PBKPB MOLMBOQFBP LC >AAFQFLK >MMIV QL SB@QLOP >P TBII.

3+2=2+3 (3 + 2) + 1 = 3 + (2 + 1)

39

#E>MQBO 1. 6B@QLOP

Vector multiplication
-LSFKD LK QL JRIQFMIF@>QFLK, TB E>SB QL QEFKH > IFQQIB ?FQ AFCCBOBKQIV. 7EBK TB Q>IH >?LRQ JRIQFMIVFKD > SB@QLO, TE>Q TB QVMF@>IIV JB>K FP >.,7492 > SB@QLO. )C TB T>KQBA QL P@>IB > SB@QLO QL QTF@B FQP PFWB LO LKB-QEFOA LC FQP PFWB (IB>SFKD FQP AFOB@QFLK QEB P>JB), TB TLRIA P>V: h-RIQFMIV QEB SB@QLO ?V 2i LO h-RIQFMIV QEB SB@QLO ?V 1/3.i .LQB QE>Q TB >OB JRIQFMIVFKD > SB@QLO ?V > P@>I>O, > PFKDIB KRJ?BO, KLQ >KLQEBO SB@QLO. 4L P@>IB > SB@QLO, TB JRIQFMIV B>@E @LJMLKBKQ (R >KA S ) ?V > P@>I>O.

w= u *n

@>K ?B TOFQQBK >P:

wx = ux * n wy = uy * n
,BQgP ILLH >Q >K BU>JMIB TFQE SB@QLO KLQ>QFLK.

u = (3, 7)

n=3

w= u *n

wx = 3 * 3 wy = 7 * 3

w = (9, 21)
F64B?2 1.8: )0.96;4 . C20A<?

4EBOBCLOB, QEB CRK@QFLK FKPFAB QEB .3?=NIL @I>PP FP TOFQQBK >P:

PIC> GOFN(@FI;N H) U R = R * H; S = S * H; W With multiplication, the components of the vector are multiplied by a number.

!KA FJMIBJBKQFKD JRIQFMIF@>QFLK FK @LAB FP >P PFJMIB >P:


.3?=NIL O = H?Q .3?=NIL(-3,7); O.GOFN(3); This PVector is now three times the size and is equal to (-9,21).

40

4EB .>QROB LC #LAB (S1.0)

Example 1.4: Multiplying a vector


PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2); GIOM?.MO<(=?HN?L); GIOM?.GOFN(0.5); NL;HMF;N?(QC>NB/2,B?CABN/2); FCH?(0,0,GIOM?.R,GIOM?.S); W Multiplying a vector! The vector is now half its original size (multiplied by 0.5).

$FSFPFLK TLOHP GRPQ IFHB JRIQFMIF@>QFLKeTB PFJMIV OBMI>@B QEB JRIQFMIF@>QFLK PFDK (>PQBOFPH) TFQE QEB AFSFPFLK PFDK (CLOT>OA PI>PE).
F64B?2 1.9

PIC> >CP(@FI;N H) U R = R / H; S = S / H; W .3?=NIL O = H?Q .3?=NIL(8,-4); O.>CP(2); Dividing a vector! The vector is now half its original size (divided by 2).

41

#E>MQBO 1. 6B@QLOP

More Number Properties with Vectors


!P TFQE >AAFQFLK, ?>PF@ >IDB?O>F@ ORIBP LC JRIQFMIF@>QFLK >MMIV QL SB@QLOP. 4EB >PPL@F>QFSB ORIB: (n * m) * v = n * (m * v ) 4EB AFPQOF?RQFSB ORIB TFQE 2 P@>I>OP, 1 SB@QLO: (n * m) * v = n * v + m * v 4EB AFPQOF?RQFSB ORIB TFQE 2 SB@QLOP, 1 P@>I>O: ( u + v ) * n = u * n + v * n

1.5 Vector Magnitude


-RIQFMIF@>QFLK >KA AFSFPFLK, >P TB GRPQ P>T, >OB JB>KP ?V TEF@E QEB IBKDQE LC QEB SB@QLO @>K ?B @E>KDBA TFQELRQ >CCB@QFKD AFOB@QFLK. 0BOE>MP VLRgOB TLKABOFKD: h/+, PL ELT AL ) HKLT TE>Q QEB IBKDQE LC > SB@QLO FP? ) HKLT QEB @LJMLKBKQP (R >KA S ), ?RQ ELT ILKD (FK MFUBIP) FP QEB >@QR>I >OOLT?i 5KABOPQ>KAFKD ELT QL @>I@RI>QB QEB IBKDQE (>IPL HKLTK >P 8,294?@/0 ) LC > SB@QLO FP FK@OBAF?IV RPBCRI >KA FJMLOQ>KQ. .LQF@B FK QEB >?LSB AF>DO>J ELT QEB SB@QLO, AO>TK >P >K >OOLT >KA QTL @LJMLKBKQP (R >KA S ), @OB>QBP > OFDEQ QOF>KDIB. 4EB PFABP >OB QEB @LJMLKBKQP >KA QEB EVMLQBKRPB FP QEB >OOLT FQPBIC. 7BgOB SBOV IR@HV QL E>SB QEFP OFDEQ QOF>KDIB, ?B@>RPB LK@B RMLK > QFJB, > 'OBBH J>QEBJ>QF@F>K K>JBA 0VQE>DLO>P ABSBILMBA > ILSBIV CLOJRI> QL ABP@OF?B QEB OBI>QFLKPEFM ?BQTBBK QEB PFABP >KA EVMLQBKRPB LC > OFDEQ QOF>KDIB. 4EB 0VQE>DLOB>K QEBLOBJ FP 5 PNR>OBA MIRP 6 PNR>OBA BNR>IP 7 PNR>OBA. !OJBA TFQE QEFP CLOJRI>, TB @>K KLT @LJMRQB QEB J>DKFQRAB LC v >P CLIILTP:

F64B?2 1.10: *52 92;4A5 <? I:.4;6AB12J <3 . C20A<? C 6@ <3A2; D?6AA2; .@: C

v = vx * vx + v y * v y
LO FK .3?=NIL :
@FI;N G;A() U L?NOLH MKLN(R*R + S*S); W

F64B?2 1.11: *52 'FA5.4<?2.; *52<?2:

42

4EB .>QROB LC #LAB (S1.0)

Example 1.5: Vector magnitude


PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2); GIOM?.MO<(=?HN?L); @FI;N G = GIOM?.G;A(); @CFF(0); L?=N(0,0,G,10); The magnitude (i.e. length) of a vector can be accessed via the mag() function. Here it is used as the width of a rectangle drawn at the top of the window.

NL;HMF;N?(QC>NB/2,B?CABN/2); FCH?(0,0,GIOM?.R,GIOM?.S); W

1.6 Normalizing Vectors


#>I@RI>QFKD QEB J>DKFQRAB LC > SB@QLO FP LKIV QEB ?BDFKKFKD. 4EB J>DKFQRAB CRK@QFLK LMBKP QEB ALLO QL J>KV MLPPF?FIFQFBP, QEB CFOPQ LC TEF@E FP 9:=8,74E,?4:9. .LOJ>IFWFKD OBCBOP QL QEB MOL@BPP LC J>HFKD PLJBQEFKD hPQ>KA>OAi LO, TBII, hKLOJ>I.i )K QEB @>PB LC SB@QLOP, IBQgP >PPRJB CLO QEB JLJBKQ QE>Q > PQ>KA>OA SB@QLO E>P > IBKDQE LC 1. 4L KLOJ>IFWB > SB@QLO, QEBOBCLOB, FP QL Q>HB > SB@QLO LC >KV IBKDQE >KA, HBBMFKD FQ MLFKQFKD FK QEB P>JB AFOB@QFLK, @E>KDB FQP IBKDQE QL 1, QROKFKD FQ FKQL TE>Q FP @>IIBA > @94? A0.?:=.

43

#E>MQBO 1. 6B@QLOP

3FK@B FQ ABP@OF?BP > SB@QLOgP AFOB@QFLK TFQELRQ OBD>OA QL FQP IBKDQE, FQgP RPBCRI QL E>SB QEB RKFQ SB@QLO OB>AFIV >@@BPPF?IB. 7BgII PBB QEFP @LJB FK E>KAV LK@B TB PQ>OQ QL TLOH TFQE CLO@BP FK #E>MQBO 2. &LO >KV DFSBK SB@QLO u , FQP RKFQ SB@QLO (TOFQQBK >P u ) FP @>I@RI>QBA >P CLIILTP:

F64B?2 1.12

u=

u u

)K LQEBO TLOAP, QL KLOJ>IFWB > SB@QLO, PFJMIV AFSFAB B>@E @LJMLKBKQ ?V FQP J>DKFQRAB. 4EFP FP MOBQQV FKQRFQFSB. 3>V > SB@QLO FP LC IBKDQE 5. 7BII, 5 AFSFABA ?V 5 FP 1. 3L, ILLHFKD >Q LRO OFDEQ QOF>KDIB, TB QEBK KBBA QL P@>IB QEB EVMLQBKRPB ALTK ?V AFSFAFKD ?V 5. )K QE>Q MOL@BPP QEB PFABP PEOFKH, AFSFABA ?V 5 >P TBII. )K QEB .3?=NIL @I>PP, TB QEBOBCLOB TOFQB LRO KLOJ>IFW>QFLK CRK@QFLK >P CLIILTP:

F64B?2 1.13

PIC> HILG;FCT?() U @FI;N G = G;A(); >CP(G); W

/C @LROPB, QEBOBgP LKB PJ>II FPPRB. 7E>Q FC QEB J>DKFQRAB LC QEB SB@QLO FP 0? 7B @>KgQ AFSFAB ?V 0! 3LJB NRF@H BOOLO @EB@HFKD TFII CFU QE>Q OFDEQ RM:
PIC> HILG;FCT?() U @FI;N G = G;A(); C@ (G != 0) U >CP(G); W W

44

4EB .>QROB LC #LAB (S1.0)

Example 1.6: Normalizing a vector


PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2); GIOM?.MO<(=?HN?L); GIOM?.HILG;FCT?(); GIOM?.GOFN(50); NL;HMF;N?(QC>NB/2,B?CABN/2); FCH?(0,0,GIOM?.R,GIOM?.S); W In this example, after the vector is normalized, it is multiplied by 50 so that it is viewable onscreen. Note that no matter where the mouse is, the vector will have the same length (50) due to the normalization process.

1.7 Vector Motion: Velocity


!II QEFP SB@QLO J>QE PQRCC PLRKAP IFHB PLJBQEFKD TB PELRIA HKLT >?LRQ, ?RQ TEV? (LT TFII FQ >@QR>IIV EBIM RP TOFQB @LAB? 4EB QORQE LC QEB J>QQBO FP QE>Q TB KBBA QL E>SB PLJB M>QFBK@B. )Q TFII Q>HB PLJB QFJB ?BCLOB QEB >TBPLJBKBPP LC RPFKD QEB .3?=NIL @I>PP CRIIV @LJBP QL IFDEQ. 4EFP FP >@QR>IIV > @LJJLK L@@ROOBK@B TEBK CFOPQ IB>OKFKD > KBT A>Q> PQOR@QROB. &LO BU>JMIB, TEBK VLR CFOPQ IB>OK >?LRQ >K >OO>V, FQ JFDEQ PBBJ IFHB JR@E JLOB TLOH QL RPB >K >OO>V QE>K QL GRPQ E>SB PBSBO>I S>OF>?IBP PQ>KA CLO JRIQFMIB QEFKDP. "RQ QE>Q MI>K NRF@HIV ?OB>HP ALTK TEBK VLR KBBA > ERKAOBA, LO > QELRP>KA, LO QBK QELRP>KA QEFKDP. 4EB P>JB @>K ?B QORB CLO .3?=NIL . 7E>Q JFDEQ PBBJ IFHB JLOB TLOH KLT TFII M>V LCC I>QBO, >KA M>V LCC NRFQB KF@BIV. !KA VLR ALKgQ E>SB QL T>FQ QLL ILKD, >P VLRO OBT>OA TFII @LJB FK QEB KBUQ @E>MQBO. &LO KLT, ELTBSBO, TB T>KQ QL CL@RP LK PFJMIF@FQV. 7E>Q ALBP FQ JB>K QL MOLDO>J JLQFLK RPFKD SB@QLOP? 7BgSB PBBK QEB ?BDFKKFKD LC QEFP FK %U>JMIB 1.2 (PBB M>DB 35): QEB ?LRK@FKD ?>II. !K L?GB@Q LK P@OBBK E>P > IL@>QFLK (TEBOB FQ FP >Q >KV DFSBK JLJBKQ) >P TBII >P > SBIL@FQV (FKPQOR@QFLKP CLO ELT FQ PELRIA JLSB COLJ LKB JLJBKQ QL QEB KBUQ). 6BIL@FQV FP >AABA QL IL@>QFLK:

45

#E>MQBO 1. 6B@QLOP
FI=;NCIH.;>>(P?FI=CNS);

!KA QEBK TB AO>T QEB L?GB@Q >Q QE>Q IL@>QFLK:


?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);

4EFP FP -LQFLK 101. 1. A// A07:.4?D ?: 7:.,?4:9 2. D=,B :-50.? ,? 7:.,?4:9 )K QEB ?LRK@FKD ?>II BU>JMIB, >II LC QEFP @LAB E>MMBKBA FK 0OL@BPPFKDgP J>FK Q>?, TFQEFK M?NOJ() >KA >L;Q() . 7E>Q TB T>KQ QL AL KLT FP JLSB QLT>OAP BK@>MPRI>QFKD >II LC QEB ILDF@ CLO JLQFLK FKPFAB LC > .7,>>. 4EFP T>V, TB @>K @OB>QB > CLRKA>QFLK CLO MOLDO>JJFKD JLSFKD L?GB@QP FK 0OL@BPPFKD. )K PB@QFLK ).2 LC QEB FKQOLAR@QFLK (PBB M>DB 2), h4EB 2>KALJ 7>IHBO #I>PP,i TB ?OFBCIV OBSFBTBA QEB ?>PF@P LC L?GB@Q-LOFBKQBA-MOLDO>JJFKD (h//0i). "BVLKA QE>Q PELOQ FKQOLAR@QFLK, QEFP ?LLH >PPRJBP BUMBOFBK@B TFQE L?GB@QP >KA @I>PPBP FK 0OL@BPPFKD. )C VLR KBBA > OBCOBPEBO, ) BK@LRO>DB VLR QL @EB@H LRQ QEB 0OL@BPPFKD L?GB@QP QRQLOF>I (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP/). )K QEFP @>PB, TBgOB DLFKD QL @OB>QB > DBKBOF@ +IP?L @I>PP QE>Q TFII ABP@OF?B > QEFKD JLSFKD >OLRKA QEB P@OBBK. !KA PL TB JRPQ @LKPFABO QEB CLIILTFKD QTL NRBPQFLKP: 1. +3,? /,?, /:0> , 8:A0= 3,A0? 2. +3,? 1@9.?4:9,74?D /:0> , 8:A0= 3,A0? /RO -LQFLK 101 >IDLOFQEJ QBIIP RP QEB >KPTBOP QL QEBPB NRBPQFLKP. ! +IP?L L?GB@Q E>P QTL MFB@BP LC A>Q>: FI=;NCIH >KA P?FI=CNS , TEF@E >OB ?LQE .3?=NIL L?GB@QP.
=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS;

)QP CRK@QFLK>IFQV FP GRPQ >?LRQ >P PFJMIB. 4EB +IP?L KBBAP QL JLSB >KA FQ KBBAP QL ?B PBBK. 7BgII FJMIBJBKQ QEBPB KBBAP >P CRK@QFLKP K>JBA OJ>;N?() >KA >CMJF;S() . 7BgII MRQ >II LC LRO JLQFLK ILDF@ @LAB FK OJ>;N?() >KA AO>T QEB L?GB@Q FK >CMJF;S() .
PIC> OJ>;N?() U FI=;NCIH.;>>(P?FI=CNS); W PIC> >CMJF;S() U MNLIE?(0); @CFF(175); The Mover moves.

46

4EB .>QROB LC #LAB (S1.0)

?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16); W W

The Mover is displayed.

7BgSB CLODLQQBK LKB @OR@F>I FQBJ, ELTBSBO: QEB L?GB@QgP .:9>?=@.?:=. 4EB @LKPQOR@QLO FP > PMB@F>I CRK@QFLK FKPFAB LC > @I>PP QE>Q @OB>QBP QEB FKPQ>K@B LC QEB L?GB@Q FQPBIC. )Q FP TEBOB VLR DFSB FKPQOR@QFLKP LK ELT QL PBQ RM QEB L?GB@Q. )Q >IT>VP E>P QEB P>JB K>JB >P QEB @I>PP >KA FP @>IIBA ?V FKSLHFKD QEB 90B LMBO>QLO:
+IP?L G = H?Q +IP?L();

)K LRO @>PB, IBQgP >O?FQO>OFIV AB@FAB QL FKFQF>IFWB LRO +IP?L L?GB@Q ?V DFSFKD FQ > O>KALJ IL@>QFLK >KA > O>KALJ SBIL@FQV.
+IP?L() U FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN)); P?FI=CNS = H?Q .3?=NIL(L;H>IG(-2,2),L;H>IG(-2,2)); W

)C L?GB@Q-LOFBKQBA MOLDO>JJFKD FP >Q >II KBT QL VLR, LKB >PMB@Q EBOB J>V PBBJ > ?FQ @LKCRPFKD. !CQBO >II, TB PMBKQ QEB ?BDFKKFKD LC QEFP @E>MQBO AFP@RPPFKD QEB .3?=NIL @I>PP. 4EB .3?=NIL @I>PP FP QEB QBJMI>QB CLO J>HFKD QEB FI=;NCIH L?GB@Q >KA QEB P?FI=CNS L?GB@Q. 3L TE>Q >OB QEBV ALFKD FKPFAB LC VBQ >KLQEBO L?GB@Q, QEB +IP?L L?GB@Q? )K C>@Q, QEFP FP GRPQ >?LRQ QEB JLPQ KLOJ>I QEFKD BSBO. !K L?GB@Q FP PFJMIV PLJBQEFKD QE>Q ELIAP A>Q> (>KA CRK@QFLK>IFQV). 4E>Q A>Q> @>K ?B KRJ?BOP (FKQBDBOP, CIL>QP, BQ@.) LO LQEBO L?GB@QP! 7BgII PBB QEFP LSBO >KA LSBO >D>FK FK QEFP ?LLH. &LO BU>JMIB, FK #E>MQBO 4 (PBB M>DB 144) TBgII TOFQB > @I>PP QL ABP@OF?B > PVPQBJ LC M>OQF@IBP. 4E>Q .;LNC=F?0SMN?G L?GB@Q TFII E>SB >P FQP A>Q> > IFPQ LC .;LNC=F? L?GB@QPk>KA B>@E .;LNC=F? L?GB@Q TFII E>SB >P FQP A>Q> PBSBO>I .3?=NIL L?GB@QP! ,BQgP CFKFPE LCC QEB +IP?L @I>PP ?V FK@LOMLO>QFKD > CRK@QFLK QL ABQBOJFKB TE>Q QEB L?GB@Q PELRIA AL TEBK FQ OB>@EBP QEB BADB LC QEB TFKALT. &LO KLT IBQgP AL PLJBQEFKD PFJMIB, >KA GRPQ E>SB FQ TO>M >OLRKA QEB BADBP.
PIC> =B?=E#>A?M() U C@ (FI=;NCIH.R > QC>NB) U FI=;NCIH.R = 0; W ?FM? C@ (FI=;NCIH.R < 0) U FI=;NCIH.R = QC>NB; W C@ (FI=;NCIH.S > B?CABN) U FI=;NCIH.S = 0; W ?FM? C@ (FI=;NCIH.S < 0) U FI=;NCIH.S = B?CABN; W When it reaches one edge, set location to the other.

47

#E>MQBO 1. 6B@QLOP

.LT QE>Q QEB +IP?L @I>PP FP CFKFPEBA, TB @>K ILLH >Q TE>Q TB KBBA QL AL FK LRO J>FK MOLDO>J. 7B CFOPQ AB@I>OB > +IP?L L?GB@Q:
+IP?L GIP?L;

4EBK FKFQF>IFWB QEB JLSBO FK M?NOJ() :


GIP?L = H?Q +IP?L();

>KA @>II QEB >MMOLMOF>QB CRK@QFLKP FK >L;Q() :


GIP?L.OJ>;N?(); GIP?L.=B?=E#>A?M(); GIP?L.>CMJF;S();

(BOB FP QEB BKQFOB BU>JMIB CLO OBCBOBK@B:

Example 1.7: Motion 101 (velocity)


+IP?L GIP?L; PIC> M?NOJ() U MCT?(640,360); GIP?L = H?Q +IP?L(); W PIC> >L;Q() U <;=EALIOH>(255); Create Mover object. Declare Mover object.

48

4EB .>QROB LC #LAB (S1.0)

GIP?L.OJ>;N?(); GIP?L.=B?=E#>A?M(); GIP?L.>CMJF;S(); W =F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS;

Call functions on Mover object.

Our object has two PVectors: location and velocity.

+IP?L() U FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN)); P?FI=CNS = H?Q .3?=NIL(L;H>IG(-2,2),L;H>IG(-2,2)); W PIC> OJ>;N?() U FI=;NCIH.;>>(P?FI=CNS); W PIC> >CMJF;S() U MNLIE?(0); @CFF(175); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16); W PIC> =B?=E#>A?M() U C@ (FI=;NCIH.R > QC>NB) U FI=;NCIH.R = 0; W ?FM? C@ (FI=;NCIH.R < 0) U FI=;NCIH.R = QC>NB; W C@ (FI=;NCIH.S > B?CABN) U FI=;NCIH.S = 0; W ?FM? C@ (FI=;NCIH.S < 0) U FI=;NCIH.S = B?CABN; W W W Motion 101: Location changes by velocity.

1.8 Vector Motion: Acceleration


/+. !Q QEFP MLFKQ, TB PELRIA CBBI @LJCLOQ>?IB TFQE QTL QEFKDP: (1) TE>Q > .3?=NIL FP >KA (2) ELT TB RPB .3?=NIL P FKPFAB LC >K L?GB@Q QL HBBM QO>@H LC FQP IL@>QFLK >KA JLSBJBKQ. 4EFP FP >K BU@BIIBKQ CFOPQ PQBM >KA ABPBOSBP > JFIA OLRKA LC >MMI>RPB. "BCLOB PQ>KAFKD LS>QFLKP >KA P@OB>JFKD C>KP, ELTBSBO, TB KBBA QL J>HB LKB JLOB, PLJBTE>Q ?FDDBO PQBM CLOT>OA. !CQBO >II, T>Q@EFKD QEB -LQFLK 101 BU>JMIB FP C>FOIV ?LOFKDeQEB @FO@IB KBSBO PMBBAP RM, KBSBO PILTP

49

#E>MQBO 1. 6B@QLOP

ALTK, >KA KBSBO QROKP. &LO JLOB FKQBOBPQFKD JLQFLK, CLO JLQFLK QE>Q >MMB>OP FK QEB OB>I TLOIA >OLRKA RP, TB KBBA QL >AA LKB JLOB .3?=NIL QL LRO @I>PPe ;==?F?L;NCIH . 4EB PQOF@Q ABCFKFQFLK LC ,..070=,?4:9 TBgOB RPFKD EBOB FP: H<9 F5H9 C: 7<5B;9 C: J9@C7=HM. ,BQgP QEFKH >?LRQ QE>Q ABCFKFQFLK CLO > JLJBKQ. )P QEFP > KBT @LK@BMQ? .LQ OB>IIV. 6BIL@FQV FP ABCFKBA >P H<9 F5H9 C: 7<5B;9 C: @C75H=CB. )K BPPBK@B, TB >OB ABSBILMFKD > hQOF@HIB-ALTKi BCCB@Q. !@@BIBO>QFLK >CCB@QP SBIL@FQV, TEF@E FK QROK >CCB@QP IL@>QFLK (CLO PLJB ?OFBC CLOBPE>ALTFKD, QEFP MLFKQ TFII ?B@LJB BSBK JLOB @OR@F>I FK QEB KBUQ @E>MQBO, TEBK TB PBB ELT CLO@BP >CCB@Q >@@BIBO>QFLK, TEF@E >CCB@QP SBIL@FQV, TEF@E >CCB@QP IL@>QFLK). )K @LAB, QEFP OB>AP:
P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS);

!P >K BUBO@FPB, COLJ QEFP MLFKQ CLOT>OA, IBQgP J>HB > ORIB CLO LROPBISBP. ,BQgP TOFQB BSBOV BU>JMIB FK QEB OBPQ LC QEFP ?LLH TFQELRQ BSBO QLR@EFKD QEB S>IRB LC SBIL@FQV >KA IL@>QFLK (BU@BMQ QL FKFQF>IFWB QEBJ). )K LQEBO TLOAP, LRO DL>I KLT CLO MOLDO>JJFKD JLQFLK FP: #LJB RM TFQE >K >IDLOFQEJ CLO ELT TB @>I@RI>QB >@@BIBO>QFLK >KA IBQ QEB QOF@HIB-ALTK BCCB@Q TLOH FQP J>DF@. ()K QORQE, VLRgII CFKA OB>PLKP QL ?OB>H QEFP ORIB, ?RQ FQgP FJMLOQ>KQ QL FIIRPQO>QB QEB MOFK@FMIBP ?BEFKA LRO JLQFLK >IDLOFQEJ.) !KA PL TB KBBA QL @LJB RM TFQE PLJB T>VP QL @>I@RI>QB >@@BIBO>QFLK:

Acceleration Algorithms!
1. A 7CBGH5BH 5779@9F5H=CB 2. A HCH5@@M F5B8CA 5779@9F5H=CB 3. A779@9F5H=CB HCK5F8G H<9 ACIG9 !IDLOFQEJ #1, 5 7CBGH5BH 5779@9F5H=CB , FP KLQ M>OQF@RI>OIV FKQBOBPQFKD, ?RQ FQ FP QEB PFJMIBPQ >KA TFII EBIM RP ?BDFK FK@LOMLO>QFKD >@@BIBO>QFLK FKQL LRO @LAB. 4EB CFOPQ QEFKD TB KBBA QL AL FP >AA >KLQEBO .3?=NIL QL QEB +IP?L @I>PP:
=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; +0;9JEH 799;B;H7J?ED; A new PVector for acceleration

!KA FK@LOMLO>QB >@@BIBO>QFLK FKQL QEB OJ>;N?() CRK@QFLK:


PIC> OJ>;N?() U L;BE9?JO.7::(799;B;H7J?ED); FI=;NCIH.;>>(P?FI=CNS); W Our motion algorithm is now two lines of code!

50

4EB .>QROB LC #LAB (S1.0)

7BgOB >IJLPQ ALKB. 4EB LKIV JFPPFKD MFB@B FP FKFQF>IFW>QFLK FK QEB @LKPQOR@QLO.
+IP?L() U

,BQgP PQ>OQ QEB +IP?L L?GB@Q FK QEB JFAAIB LC QEB TFKALTk


FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);

kTFQE >K FKFQF>I SBIL@FQV LC WBOL.


P?FI=CNS = H?Q .3?=NIL(0,0);

4EFP JB>KP QE>Q TEBK QEB PHBQ@E PQ>OQP, QEB L?GB@Q FP >Q OBPQ. 7B ALKgQ E>SB QL TLOOV >?LRQ SBIL@FQV >KVJLOB, >P TB >OB @LKQOLIIFKD QEB L?GB@QgP JLQFLK BKQFOBIV TFQE >@@BIBO>QFLK. 3MB>HFKD LC TEF@E, >@@LOAFKD QL !IDLOFQEJ #1, LRO CFOPQ PHBQ@E FKSLISBP @LKPQ>KQ >@@BIBO>QFLK. 3L IBQgP MF@H > S>IRB.
;==?F?L;NCIH = H?Q .3?=NIL(-0.001,0.01); W

->V?B VLRgOB QEFKHFKD, h'LPE, QELPB S>IRBP PBBJ >TCRIIV PJ>II!i 4E>QgP OFDEQ, QEBV >OB NRFQB QFKV. )QgP FJMLOQ>KQ QL OB>IFWB QE>Q LRO >@@BIBO>QFLK S>IRBP (JB>PROBA FK MFUBIP) >@@RJRI>QB LSBO QFJB FK QEB SBIL@FQV, >?LRQ QEFOQV QFJBP MBO PB@LKA ABMBKAFKD LK LRO PHBQ@EgP CO>JB O>QB. !KA PL QL HBBM QEB J>DKFQRAB LC QEB SBIL@FQV SB@QLO TFQEFK > OB>PLK>?IB O>KDB, LRO >@@BIBO>QFLK S>IRBP PELRIA OBJ>FK NRFQB PJ>II. 7B @>K >IPL EBIM QEFP @>RPB ?V FK@LOMLO>QFKD QEB .3?=NIL CRK@QFLK FCGCN() .
P?FI=CNS.FCGCN(10); The limit() function constrains the magnitude of a vector.

4EFP QO>KPI>QBP QL QEB CLIILTFKD: 2<5H =G H<9 A5;B=HI89 C: J9@C7=HM? %: =HSG @9GG H<5B 10, BC KCFF=9G; >IGH @95J9 =H 5G =G. %: =HSG ACF9 H<5B 10, <CK9J9F, F98I79 =H HC 10!

51

#E>MQBO 1. 6B@QLOP

Exercise 1.4
7OFQB QEB FCGCN() CRK@QFLK CLO QEB .3?=NIL @I>PP.
PIC> FCGCN(@FI;N G;R) U C@ (::::::: > :::::::) U :::::::::(); ::::(G;R); W W

,BQgP Q>HB > ILLH >Q QEB @E>KDBP QL QEB +IP?L @I>PP, @LJMIBQB TFQE ;==?F?L;NCIH >KA FCGCN() .

Example 1.8: Motion 101 (velocity and constant acceleration)


=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N NIJMJ??>; +IP?L() U FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2); P?FI=CNS = H?Q .3?=NIL(0,0); ;==?F?L;NCIH = H?Q .3?=NIL(-0.001,0.01); NIJMJ??> = 10; W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); P?FI=CNS.FCGCN(NIJMJ??>); Velocity changes by acceleration and is limited by topspeed. Acceleration is the key! The variable topspeed will limit the magnitude of velocity.

52

4EB .>QROB LC #LAB (S1.0)


FI=;NCIH.;>>(P?FI=CNS); W PIC> >CMJF;S() UW PIC> =B?=E#>A?M() UW W display() is the same. checkEdges() is the same.

Exercise 1.5
#OB>QB > PFJRI>QFLK LC > @>O (LO ORKKBO) QE>Q >@@BIBO>QBP TEBK VLR MOBPP QEB RM HBV >KA ?O>HBP TEBK VLR MOBPP QEB ALTK HBV. .LT LK QL !IDLOFQEJ #2, 5 HCH5@@M F5B8CA 5779@9F5H=CB. )K QEFP @>PB, FKPQB>A LC FKFQF>IFWFKD >@@BIBO>QFLK FK QEB L?GB@QgP @LKPQOR@QLO, TB T>KQ QL MF@H > KBT >@@BIBO>QFLK B>@E @V@IB, F.B. B>@E QFJB OJ>;N?() FP @>IIBA.

Example 1.9: Motion 101 (velocity and random acceleration)


PIC> OJ>;N?() U ;==?F?L;NCIH = .3?=NIL.L;H>IG2"(); P?FI=CNS.;>>(;==?F?L;NCIH); P?FI=CNS.FCGCN(NIJMJ??>); FI=;NCIH.;>>(P?FI=CNS); W The random2D() function will give us a PVector of length 1 pointing in a random direction.

"B@>RPB QEB O>KALJ SB@QLO FP > KLOJ>IFWBA LKB, TB @>K QOV P@>IFKD FQ: (>) P@>IFKD QEB >@@BIBO>QFLK QL > @LKPQ>KQ S>IRB
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();

53

#E>MQBO 1. 6B@QLOP

799;B;H7J?ED.CKBJ(0.5);

Constant

(?) P@>IFKD QEB >@@BIBO>QFLK QL > O>KALJ S>IRB


;==?F?L;NCIH = .3?=NIL.L;H>IG2"(); 799;B;H7J?ED.CKBJ(H7D:EC(2)); Random

7EFIB QEFP J>V PBBJ IFHB >K L?SFLRP MLFKQ, FQgP @OR@F>I QL RKABOPQ>KA QE>Q >@@BIBO>QFLK ALBP KLQ JBOBIV OBCBO QL QEB GD998=B; ID LO G@CK=B; 8CKB LC > JLSFKD L?GB@Q, ?RQ O>QEBO 5BM 7<5B;9 FK SBIL@FQV FK BFQEBO J>DKFQRAB LO AFOB@QFLK. !@@BIBO>QFLK FP RPBA QL PQBBO >K L?GB@Q, >KA TBgII PBB QEFP >D>FK >KA >D>FK FK CRQROB @E>MQBOP >P TB ?BDFK QL MOLDO>J L?GB@QP QE>Q J>HB AB@FPFLKP >?LRQ ELT QL JLSB >?LRQ QEB P@OBBK.

Exercise 1.6
2BCBOOFKD ?>@H QL QEB )KQOLAR@QFLK (PBB M>DB 17), FJMIBJBKQ >@@BIBO>QFLK >@@LOAFKD QL 0BOIFK KLFPB.

1.9 Static vs. Non-Static Functions


"BCLOB TB DBQ QL !IDLOFQEJ #3 (5779@9F5H9 HCK5F8G H<9 ACIG9 ), TB KBBA QL @LSBO LKB JLOB O>QEBO FJMLOQ>KQ >PMB@Q LC TLOHFKD TFQE SB@QLOP >KA QEB .3?=NIL @I>PP: QEB AFCCBOBK@B ?BQTBBK RPFKD >?,?4. JBQELAP >KA 9:9->?,?4. JBQELAP. &LODBQQFKD >?LRQ SB@QLOP CLO > JLJBKQ, Q>HB > ILLH >Q QEB CLIILTFKD @LAB:
@FI;N R = 0; @FI;N S = 5; R = R + S;

0OBQQV PFJMIB, OFDEQ? R E>P QEB S>IRB LC 0, TB >AA S QL FQ, >KA KLT R FP BNR>I QL 5. 7B @LRIA TOFQB QEB @LOOBPMLKAFKD @LAB MOBQQV B>PFIV ?>PBA LK TE>Q TBgSB IB>OKBA >?LRQ .3?=NIL .
.3?=NIL P = H?Q .3?=NIL(0,0); .3?=NIL O = H?Q .3?=NIL(4,5); P.;>>(O);

4EB SB@QLO P E>P QEB S>IRB LC (0,0), TB >AA O QL FQ, >KA KLT P FP BNR>I QL (4,5). %>PV, OFDEQ? ,BQgP Q>HB > ILLH >Q >KLQEBO BU>JMIB LC PLJB PFJMIB CIL>QFKD MLFKQ J>QE:

54

4EB .>QROB LC #LAB (S1.0)


@FI;N R = 0; @FI;N S = 5; @FI;N T = R + S;

R E>P QEB S>IRB LC 0, TB >AA S QL FQ, >KA PQLOB QEB OBPRIQ FK > KBT S>OF>?IB T . 4EB S>IRB LC R

ALBP KLQ @E>KDB FK QEFP BU>JMIB (KBFQEBO ALBP S )! 4EFP J>V PBBJ IFHB > QOFSF>I MLFKQ, >KA LKB QE>Q FP NRFQB FKQRFQFSB TEBK FQ @LJBP QL J>QEBJ>QF@>I LMBO>QFLKP TFQE CIL>QP. (LTBSBO, FQgP KLQ PL L?SFLRP TFQE J>QEBJ>QF@>I LMBO>QFLKP FK .3?=NIL . ,BQgP QOV QL TOFQB QEB @LAB ?>PBA LK TE>Q TB HKLT PL C>O.
.3?=NIL P = H?Q .3?=NIL(0,0); .3?=NIL O = H?Q .3?=NIL(4,5); .3?=NIL Q = P.;>>(O); Dont be fooled; this is incorrect!!!

4EB >?LSB JFDEQ PBBJ IFHB > DLLA DRBPP, ?RQ FQgP GRPQ KLQ QEB T>V QEB .3?=NIL @I>PP TLOHP. )C TB ILLH >Q QEB ABCFKFQFLK LC ;>>() . . .
PIC> ;>>(.3?=NIL P) U R = R + P.R; S = S + P.S; W

TB PBB QE>Q QEFP @LAB ALBP KLQ >@@LJMIFPE LRO DL>I. &FOPQ, FQ ALBP KLQ OBQROK > KBT .3?=NIL (QEB OBQROK QVMB FP hSLFAi) >KA PB@LKA, FQ @E>KDBP QEB S>IRB LC QEB .3?=NIL RMLK TEF@E FQ FP @>IIBA. )K LOABO QL >AA QTL .3?=NIL L?GB@QP QLDBQEBO >KA OBQROK QEB OBPRIQ >P > KBT .3?=NIL , TB JRPQ RPB QEB PQ>QF@ ;>>() CRK@QFLK. &RK@QFLKP QE>Q TB @>II COLJ QEB @I>PP K>JB FQPBIC (O>QEBO QE>K COLJ > PMB@Fl@ L?GB@Q FKPQ>K@B) >OB HKLTK >P >?,?4. 1@9.?4:9>. (BOB >OB QTL BU>JMIBP LC CRK@QFLK @>IIP QE>Q >PPRJB QTL .3?=NIL L?GB@QP, P >KA O :
.3?=NIL.;>>(P,O); P.;>>(O); Static: called from the class name. Not static: called from an object instance.

3FK@B VLR @>KgQ TOFQB PQ>QF@ CRK@QFLKP VLROPBIC FK 0OL@BPPFKD, VLR JFDEQ KLQ E>SB BK@LRKQBOBA QEBJ ?BCLOB. .3?=NIL 'P PQ>QF@ CRK@QFLKP >IILT RP QL MBOCLOJ DBKBOF@ J>QEBJ>QF@>I LMBO>QFLKP LK .3?=NIL L?GB@QP TFQELRQ E>SFKD QL >AGRPQ QEB S>IRB LC LKB LC QEB FKMRQ .3?=NIL P. ,BQgP ILLH >Q ELT TB JFDEQ TOFQB QEB PQ>QF@ SBOPFLK LC ;>>() :

55

#E>MQBO 1. 6B@QLOP

MN;NC= .3?=NIL ;>>(.3?=NIL P1, .3?=NIL P2) U

The static version of add allows us to add two PVectors together and assign the result to a new PVector while leaving the original PVectors (v and u above) intact.

.3?=NIL P3 = H?Q .3?=NIL(P1.R + P2.R, P1.S + P2.S); L?NOLH P3; W

4EBOB >OB PBSBO>I AFCCBOBK@BP EBOB: j 4EB CRK@QFLK FP I>?BIBA >P >?,?4.. j 4EB CRK@QFLK ALBP KLQ E>SB > A:4/ OBQROK QVMB, ?RQ O>QEBO OBQROKP > .3?=NIL . j 4EB CRK@QFLK @OB>QBP > KBT .3?=NIL (P3 ) >KA OBQROKP QEB PRJ LC QEB @LJMLKBKQP LC P1 >KA P2 FK QE>Q KBT .3?=NIL . 7EBK VLR @>II > PQ>QF@ CRK@QFLK, FKPQB>A LC OBCBOBK@FKD >K >@QR>I L?GB@Q FKPQ>K@B, VLR PFJMIV OBCBOBK@B QEB K>JB LC QEB @I>PP FQPBIC.
.3?=NIL .3?=NIL .3?=NIL +0;9JEH P O Q M = = = = H?Q .3?=NIL(0,0); H?Q .3?=NIL(4,5); P.;>>(O); +0;9JEH.7::(L,K);

4EB .3?=NIL @I>PP E>P PQ>QF@ SBOPFLKP LC ;>>() , MO<() , GOFN() , >KA >CP() .

Exercise 1.7
4O>KPI>QB QEB CLIILTFKD MPBRAL@LAB QL @LAB RPFKD PQ>QF@ LO KLK-PQ>QF@ CRK@QFLKP TEBOB >MMOLMOF>QB. j j j j 4EB .3?=NIL P BNR>IP (1,5). 4EB .3?=NIL O BNR>IP P JRIQFMIFBA ?V 2. 4EB .3?=NIL Q BNR>IP P JFKRP O . $FSFAB QEB .3?=NIL T ?V 3.

.3?=NIL P = H?Q .3?=NIL(1,5); .3?=NIL O = ::::::::.:::::(::,::); .3?=NIL Q = ::::::::.:::::(::,::); :::::::::::;

56

4EB .>QROB LC #LAB (S1.0)

1.10 Interactivity with Acceleration


4L CFKFPE LRQ QEFP @E>MQBO, IBQgP QOV PLJBQEFKD > ?FQ JLOB @LJMIBU >KA > DOB>Q AB>I JLOB RPBCRI. 7BgII AVK>JF@>IIV @>I@RI>QB >K L?GB@QgP >@@BIBO>QFLK >@@LOAFKD QL > ORIB PQ>QBA FK !IDLOFQEJ #3 e H<9 C6>97H 5779@9F5H9G HCK5F8G H<9 ACIG9.

F64B?2 1.14

!KVQFJB TB T>KQ QL @>I@RI>QB > SB@QLO ?>PBA LK > ORIB LO > CLOJRI>, TB KBBA QL @LJMRQB QTL QEFKDP: 8,294?@/0 >KA /4=0.?4:9. ,BQgP PQ>OQ TFQE AFOB@QFLK. 7B HKLT QEB >@@BIBO>QFLK SB@QLO PELRIA MLFKQ COLJ QEB L?GB@QgP IL@>QFLK QLT>OAP QEB JLRPB IL@>QFLK. ,BQgP P>V QEB L?GB@Q FP IL@>QBA >Q QEB MLFKQ (R , S ) >KA QEB JLRPB >Q (GIOM?5 ,GIOM?6 ). )K &FDROB 1.15, TB PBB QE>Q TB @>K DBQ > SB@QLO ( >R , >S ) ?V PR?QO>@QFKD QEB L?GB@QgP IL@>QFLK COLJ QEB JLRPBgP IL@>QFLK. j j
:N = CEKI;2 - N :O = CEKI;3 - O F64B?2 1.15

,BQgP OBTOFQB QEB >?LSB RPFKD .3?=NIL PVKQ>U. !PPRJFKD TB >OB FK QEB +IP?L @I>PP >KA QERP E>SB >@@BPP QL QEB L?GB@QgP .3?=NIL FI=;NCIH , TB QEBK E>SB:
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH); Look! Were using the static reference to sub() because we want a new PVector pointing from one point to another.

7B KLT E>SB > .3?=NIL QE>Q MLFKQP COLJ QEB JLSBOgP IL@>QFLK >II QEB T>V QL QEB JLRPB. )C QEB L?GB@Q TBOB QL >@QR>IIV >@@BIBO>QB RPFKD QE>Q SB@QLO, FQ TLRIA >MMB>O FKPQ>KQ>KBLRPIV >Q QEB JLRPB IL@>QFLK. 4EFP ALBP KLQ J>HB CLO DLLA >KFJ>QFLK, LC @LROPB, >KA TE>Q TB T>KQ QL AL KLT FP AB@FAB ELT NRF@HIV QE>Q L?GB@Q PELRIA >@@BIBO>QB QLT>OA QEB JLRPB. )K LOABO QL PBQ QEB J>DKFQRAB (TE>QBSBO FQ J>V ?B) LC LRO >@@BIBO>QFLK .3?=NIL , TB JRPQ CFOPQ 444 QE>Q AFOB@QFLK SB@QLO. 4E>QgP OFDEQ, VLR P>FA FQ. *CFA5@=N9 . )C TB @>K PEOFKH QEB SB@QLO ALTK QL FQP RKFQ SB@QLO (LC IBKDQE LKB) QEBK TB E>SB > SB@QLO QE>Q QBIIP RP QEB AFOB@QFLK >KA @>K B>PFIV ?B P@>IBA QL >KV S>IRB. /KB JRIQFMIFBA ?V >KVQEFKD BNR>IP >KVQEFKD.
@FI;N ;HSNBCHA = ????? >CL.HILG;FCT?(); >CL.GOFN(;HSNBCHA);

57

#E>MQBO 1. 6B@QLOP

4L PRJJ>OFWB, TB Q>HB QEB CLIILTFKD PQBMP: 1. #>I@RI>QB > SB@QLO QE>Q MLFKQP COLJ QEB L?GB@Q QL QEB Q>ODBQ IL@>QFLK (JLRPB) 2. .LOJ>IFWB QE>Q SB@QLO (OBAR@FKD FQP IBKDQE QL 1) 3. 3@>IB QE>Q SB@QLO QL >K >MMOLMOF>QB S>IRB (?V JRIQFMIVFKD FQ ?V PLJB S>IRB) 4. !PPFDK QE>Q SB@QLO QL >@@BIBO>QFLK !KA EBOB >OB QELPB PQBMP FK QEB OJ>;N?() CRK@QFLK FQPBIC:

Example 1.10: Accelerating towards the mouse


PIC> OJ>;N?() U .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH); Step 1: Compute direction

>CL.HILG;FCT?();

Step 2: Normalize

>CL.GOFN(0.5);

Step 3: Scale

;==?F?L;NCIH = >CL; P?FI=CNS.;>>(;==?F?L;NCIH); P?FI=CNS.FCGCN(NIJMJ??>); FI=;NCIH.;>>(P?FI=CNS); W

Step 4: Accelerate

9LR J>V ?B TLKABOFKD TEV QEB @FO@IB ALBPKgQ PQLM TEBK FQ OB>@EBP QEB Q>ODBQ. )QgP FJMLOQ>KQ QL KLQB QE>Q QEB L?GB@Q JLSFKD E>P KL HKLTIBADB >?LRQ QOVFKD QL PQLM >Q > ABPQFK>QFLK; FQ LKIV HKLTP TEBOB QEB ABPQFK>QFLK FP >KA QOFBP QL DL QEBOB >P NRF@HIV >P MLPPF?IB. 'LFKD >P

58

4EB .>QROB LC #LAB (S1.0)

NRF@HIV >P MLPPF?IB JB>KP FQ TFII FKBSFQ>?IV LSBOPELLQ QEB IL@>QFLK >KA E>SB QL QROK >OLRKA, >D>FK DLFKD >P NRF@HIV >P MLPPF?IB QLT>OAP QEB ABPQFK>QFLK, LSBOPELLQFKD FQ >D>FK, >KA PL LK >KA PL CLOQE. 3Q>V QRKBA; FK I>QBO @E>MQBOP TBgII IB>OK ELT QL MOLDO>J >K L?GB@Q QL ,==4A0 >Q > IL@>QFLK (PILT ALTK LK >MMOL>@E). 4EFP BU>JMIB FP OBJ>OH>?IV @ILPB QL QEB @LK@BMQ LC DO>SFQ>QFLK>I >QQO>@QFLK (FK TEF@E QEB L?GB@Q FP >QQO>@QBA QL QEB JLRPB IL@>QFLK). 'O>SFQ>QFLK>I >QQO>@QFLK TFII ?B @LSBOBA FK JLOB ABQ>FI FK QEB KBUQ @E>MQBO. (LTBSBO, LKB QEFKD JFPPFKD EBOB FP QE>Q QEB PQOBKDQE LC DO>SFQV (J>DKFQRAB LC >@@BIBO>QFLK) FP FKSBOPBIV MOLMLOQFLK>I QL AFPQ>K@B. 4EFP JB>KP QE>Q QEB @ILPBO QEB L?GB@Q FP QL QEB JLRPB, QEB C>PQBO FQ >@@BIBO>QBP.

Exercise 1.8
4OV FJMIBJBKQFKD QEB >?LSB BU>JMIB TFQE > S>OF>?IB J>DKFQRAB LC >@@BIBO>QFLK, PQOLKDBO TEBK FQ FP BFQEBO @ILPBO LO C>OQEBO >T>V. ,BQgP PBB TE>Q QEFP BU>JMIB TLRIA ILLH IFHB TFQE >K >OO>V LC JLSBOP (O>QEBO QE>K GRPQ LKB).

Example 1.11: Array of movers accelerating towards the mouse


+IP?L89 GIP?LM = H?Q +IP?L8209; PIC> M?NOJ() U MCT?(640,360); <;=EALIOH>(255); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U An array of objects

59

#E>MQBO 1. 6B@QLOP

GIP?LM8C9 = H?Q +IP?L(); W W PIC> >L;Q() U <;=EALIOH>(255); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9.OJ>;N?(); GIP?LM8C9.=B?=E#>A?M(); GIP?LM8C9.>CMJF;S(); W W =F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N NIJMJ??>;

Initialize each object in the array.

Calling functions on all the objects in the array

+IP?L() U FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN)); P?FI=CNS = H?Q .3?=NIL(0,0); NIJMJ??> = 4; W PIC> OJ>;N?() U Our algorithm for calculating acceleration: .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); .3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH); >CL.HILG;FCT?(); >CL.GOFN(0.5); ;==?F?L;NCIH = >CL; Find the vector pointing towards the mouse. Normalize. Scale. Set to acceleration.

P?FI=CNS.;>>(;==?F?L;NCIH); P?FI=CNS.FCGCN(NIJMJ??>); FI=;NCIH.;>>(P?FI=CNS); W

Motion 101! Velocity changes by acceleration. Location changes by velocity.

60

4EB .>QROB LC #LAB (S1.0)

PIC> >CMJF;S() U MNLIE?(0); @CFF(175); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16); W PIC> =B?=E#>A?M() U C@ (FI=;NCIH.R > QC>NB) U FI=;NCIH.R = 0; W ?FM? C@ (FI=;NCIH.R < 0) U FI=;NCIH.R = QC>NB; W C@ (FI=;NCIH.S > B?CABN) U FI=;NCIH.S = 0; W ?FM? C@ (FI=;NCIH.S < 0) U FI=;NCIH.S = B?CABN; W W W

Display the Mover

What to do at the edges

61

#E>MQBO 1. 6B@QLOP

F64B?2 1.16: *52 E0<@F@A2: '?<720A

The Ecosystem Project


AG A9BH=CB98 =B H<9 DF9:579, CB9 K5M HC IG9 H<=G 6CC? =G HC 6I=@8 5 G=B;@9 DFC>97H CJ9F H<9 7CIFG9 C: F958=B; =H, =B7CFDCF5H=B; 9@9A9BHG :FCA 957< 7<5DH9F CB9 GH9D 5H 5 H=A9. 29S@@ :C@@CK H<9 89J9@CDA9BH C: 5B 9L5AD@9 DFC>97H H<FCI;<CIH H<=G 6CC?R5 G=AI@5H=CB C: 5B 97CGMGH9A. %A5;=B9 5 DCDI@5H=CB C: 7CADIH5H=CB5@ 7F95HIF9G GK=AA=B; 5FCIB8 5 8=;=H5@ DCB8, =BH9F57H=B; K=H< 957< CH<9F 577CF8=B; HC J5F=CIG FI@9G. Step 1 Exercise: Develop a set of rules for simulating the real-world behavior of a creature, such as a nervous fly, swimming fish, hopping bunny, slithering snake, etc. Can you control the objects motion by only manipulating the acceleration? Try to give the creature a personality through its behavior (rather than through its visual design).

62

4EB .>QROB LC #LAB (S1.0)

Chapter 2. Forces
ID<;KA B;12?2@A6:.A2 A52 F<?02.J Darth Vader
)K QEB CFK>I BU>JMIB LC #E>MQBO 1, TB P>T ELT TB @LRIA @>I@RI>QB > AVK>JF@ >@@BIBO>QFLK ?>PBA LK > SB@QLO MLFKQFKD COLJ > @FO@IB LK QEB P@OBBK QL QEB JLRPB IL@>QFLK. 4EB OBPRIQFKD JLQFLK OBPBJ?IBA > J>DKBQF@ >QQO>@QFLK ?BQTBBK @FO@IB >KA JLRPB, >P FC PLJB :CF79 TBOB MRIIFKD QEB @FO@IB FK QLT>OAP QEB JLRPB. )K QEFP @E>MQBO TB TFII CLOJ>IFWB LRO RKABOPQ>KAFKD LC QEB @LK@BMQ LC > CLO@B >KA FQP OBI>QFLKPEFM QL >@@BIBO>QFLK. /RO DL>I, ?V QEB BKA LC QEFP @E>MQBO, FP QL RKABOPQ>KA ELT QL J>HB JRIQFMIB L?GB@QP JLSB >OLRKA QEB P@OBBK >KA OBPMLKA QL > S>OFBQV LC BKSFOLKJBKQ>I CLO@BP.

2.1 Forces and Newtons Laws of Motion


"BCLOB TB ?BDFK BU>JFKFKD QEB MO>@QF@>I OB>IFQFBP LC PFJRI>QFKD CLO@BP FK @LAB, IBQgP Q>HB > @LK@BMQR>I ILLH >Q TE>Q FQ JB>KP QL ?B > CLO@B FK QEB OB>I TLOIA. *RPQ IFHB QEB TLOA hSB@QLO,i hCLO@Bi FP LCQBK RPBA QL JB>K > S>OFBQV LC QEFKDP. )Q @>K FKAF@>QB > MLTBOCRI FKQBKPFQV, >P FK h3EB MRPEBA QEB ?LRIABO TFQE DOB>Q CLO@Bi LO h(B PMLHB CLO@BCRIIV.i 4EB ABCFKFQFLK LC 1:=.0 QE>Q TB @>OB >?LRQ FP JR@E JLOB CLOJ>I >KA @LJBP COLJ )P>>@ .BTQLKgP I>TP LC JLQFLK:

! CLO@B FP > SB@QLO QE>Q @>RPBP >K L?GB@Q TFQE J>PP QL >@@BIBO>QB.

63

#E>MQBO 2. &LO@BP

4EB DLLA KBTP EBOB FP QE>Q TB OB@LDKFWB QEB CFOPQ M>OQ LC QEB ABCFKFQFLK: 5 :CF79 =G 5 J97HCF . 4E>KH DLLAKBPP TB GRPQ PMBKQ > TELIB @E>MQBO IB>OKFKD TE>Q > SB@QLO FP >KA ELT QL MOLDO>J TFQE .3?=NIL P! ,BQgP ILLH >Q .BTQLKgP QEOBB I>TP LC JLQFLK FK OBI>QFLK QL QEB @LK@BMQ LC > CLO@B.

Newtons First Law


.BTQLKgP CFOPQ I>T FP @LJJLKIV PQ>QBA >P:

!K L?GB@Q >Q OBPQ PQ>VP >Q OBPQ >KA >K L?GB@Q FK JLQFLK PQ>VP FK JLQFLK.

(LTBSBO, QEFP FP JFPPFKD >K FJMLOQ>KQ BIBJBKQ OBI>QBA QL CLO@BP. 7B @LRIA BUM>KA FQ ?V PQ>QFKD:

!K L?GB@Q >Q OBPQ PQ>VP >Q OBPQ >KA >K L?GB@Q FK JLQFLK PQ>VP FK JLQFLK >Q > @LKPQ>KQ PMBBA >KA AFOB@QFLK RKIBPP >@QBA RMLK ?V >K RK?>I>K@BA CLO@B.

"V QEB QFJB .BTQLK @>JB >ILKD, QEB MOBS>FIFKD QEBLOV LC JLQFLKeCLOJRI>QBA ?V !OFPQLQIBeT>P KB>OIV QTL QELRP>KA VB>OP LIA. )Q PQ>QBA QE>Q FC >K L?GB@Q FP JLSFKD, PLJB PLOQ LC CLO@B FP OBNRFOBA QL HBBM FQ JLSFKD. 5KIBPP QE>Q JLSFKD QEFKD FP ?BFKD MRPEBA LO MRIIBA, FQ TFII PFJMIV PILT ALTK LO PQLM. 2FDEQ? 4EFP, LC @LROPB, FP KLQ QORB. )K QEB >?PBK@B LC >KV CLO@BP, KL CLO@B FP OBNRFOBA QL HBBM >K L?GB@Q JLSFKD. !K L?GB@Q (PR@E >P > ?>II) QLPPBA FK QEB B>OQEgP >QJLPMEBOB PILTP ALTK ?B@>RPB LC >FO OBPFPQ>K@B (> CLO@B). !K L?GB@QgP SBIL@FQV TFII LKIV OBJ>FK @LKPQ>KQ FK QEB >?PBK@B LC >KV CLO@BP LO FC QEB CLO@BP QE>Q >@Q LK FQ @>K@BI B>@E LQEBO LRQ, F.B. QEB KBQ CLO@B >AAP RM QL WBOL. 4EFP FP LCQBK OBCBOOBA QL >P 0<@474-=4@8. 4EB C>IIFKD ?>II TFII OB>@E > QBOJFK>I SBIL@FQV (QE>Q PQ>VP @LKPQ>KQ) LK@B QEB CLO@B LC >FO OBPFPQ>K@B BNR>IP QEB CLO@B LC DO>SFQV.

F64B?2 2.1: *52 =2;1B9B: 1<2@;'A :<C2 /20.B@2 .99 A52 3<?02@ 0.;029 2.05 <A52? <BA (.11 B= A< . ;2A 3<?02 <3 G2?<).

64

4EB .>QROB LC #LAB (S1.0)

)K LRO 0OL@BPPFKD TLOIA, TB @LRIA OBPQ>QB .BTQLKgP CFOPQ I>T >P CLIILTP:

!K L?GB@QgP 06B@QLO SBIL@FQV TFII OBJ>FK @LKPQ>KQ FC FQ FP FK > PQ>QB LC BNRFIF?OFRJ.

3HFMMFKD .BTQLKgP PB@LKA I>T (>ODR>?IV QEB JLPQ FJMLOQ>KQ I>T CLO LRO MROMLPBP) CLO > JLJBKQ, IBQgP JLSB LK QL QEB QEFOA I>T.

Newtons Third Law


4EFP I>T FP LCQBK PQ>QBA >P:

&LO BSBOV >@QFLK QEBOB FP >K BNR>I >KA LMMLPFQB OB>@QFLK.

4EFP I>T COBNRBKQIV @>RPBP PLJB @LKCRPFLK FK QEB T>V QE>Q FQ FP PQ>QBA. &LO LKB, FQ PLRKAP IFHB LKB CLO@B @>RPBP >KLQEBO. 9BP, FC VLR MRPE PLJBLKB, QE>Q PLJBLKB J>V 57H=J9@M AB@FAB QL MRPE VLR ?>@H. "RQ QEFP FP KLQ QEB >@QFLK >KA OB>@QFLK TB >OB Q>IHFKD >?LRQ TFQE .BTQLKgP QEFOA I>T. ,BQgP P>V VLR MRPE >D>FKPQ > T>II. 4EB T>II ALBPKgQ >@QFSBIV AB@FAB QL MRPE ?>@H LK VLR. 4EBOB FP KL hLOFDFKi CLO@B. 9LRO MRPE PFJMIV FK@IRABP ?LQE CLO@BP, OBCBOOBA QL >P >K h>@QFLK/OB>@QFLK M>FO.i ! ?BQQBO T>V LC PQ>QFKD QEB I>T JFDEQ ?B:

&LO@BP >IT>VP L@@RO FK M>FOP. 4EB QTL CLO@BP >OB LC BNR>I PQOBKDQE, ?RQ FK LMMLPFQB AFOB@QFLKP.

.LT, QEFP PQFII @>RPBP @LKCRPFLK ?B@>RPB FQ PLRKAP IFHB QEBPB CLO@BP TLRIA >IT>VP @>K@BI B>@E LQEBO LRQ. 4EFP FP KLQ QEB @>PB. 2BJBJ?BO, QEB CLO@BP >@Q LK AFCCBOBKQ L?GB@QP. !KA GRPQ ?B@>RPB QEB QTL CLO@BP >OB BNR>I, FQ ALBPKgQ JB>K QE>Q QEB JLSBJBKQP >OB BNR>I (LO QE>Q QEB L?GB@QP TFII PQLM JLSFKD). 4OV MRPEFKD LK > PQ>QFLK>OV QOR@H. !IQELRDE QEB QOR@H FP C>O JLOB MLTBOCRI QE>K VLR, RKIFHB > JLSFKD LKB, > PQ>QFLK>OV QOR@H TFII KBSBO LSBOMLTBO VLR >KA PBKA VLR CIVFKD ?>@HT>OAP. 4EB CLO@B VLR BUBOQ LK FQ FP BNR>I >KA LMMLPFQB QL QEB CLO@B BUBOQBA LK VLRO E>KAP. 4EB LRQ@LJB ABMBKAP LK > S>OFBQV LC LQEBO C>@QLOP. )C QEB QOR@H FP > PJ>II QOR@H LK >K F@V ALTKEFII, VLRgII

65

#E>MQBO 2. &LO@BP

MOL?>?IV ?B >?IB QL DBQ FQ QL JLSB. /K QEB LQEBO E>KA, FC FQgP > SBOV I>ODB QOR@H LK > AFOQ OL>A >KA VLR MRPE E>OA BKLRDE (J>V?B BSBK Q>HB > ORKKFKD PQ>OQ), VLR @LRIA FKGROB VLRO E>KA. !KA FC VLR >OB TB>OFKD OLIIBO PH>QBP TEBK VLR MRPE LK QE>Q QOR@H?

F64B?2 2.2

9LRgII >@@BIBO>QB >T>V COLJ QEB QOR@H, PIFAFKD >ILKD QEB OL>A TEFIB QEB QOR@H PQ>VP MRQ. 7EV AL VLR PIFAB ?RQ KLQ QEB QOR@H? &LO LKB, QEB QOR@H E>P > JR@E I>ODBO J>PP (TEF@E TBgII DBQ FKQL TFQE .BTQLKgP PB@LKA I>T). 4EBOB >OB LQEBO CLO@BP >Q TLOH QLL, K>JBIV QEB COF@QFLK LC QEB QOR@HgP QFOBP >KA VLRO OLIIBO PH>QBP >D>FKPQ QEB OL>A.

Newtons Third Law (as seen through the eyes of Processing)

)C TB @>I@RI>QB > 06B@QLO C QE>Q FP > CLO@B LC L?GB@Q ! LK L?GB@Q ", TB JRPQ >IPL >MMIV QEB CLO@Be06B@QLO.JRIQ(C,-1);eQE>Q " BUBOQP LK L?GB@Q !.

7BgII PBB QE>Q FK QEB TLOIA LC 0OL@BPPFKD MOLDO>JJFKD, TB ALKgQ >IT>VP E>SB QL PQ>V QORB QL QEB >?LSB. 3LJBQFJBP, PR@E >P FK QEB @>PB LC PBB DO>SFQ>QFLK>I >QQO>@QFLK ?BQTBBK ?LAFBP (PBB M>DB 94), TBgII T>KQ QL JLABI BNR>I >KA LMMLPFQB CLO@BP. /QEBO QFJBP, PR@E >P TEBK TBgOB PFJMIV P>VFKD, h(BV, QEBOBgP PLJB TFKA FK QEB BKSFOLKJBKQ,i TBgOB KLQ DLFKD QL ?LQEBO QL JLABI QEB CLO@B QE>Q > ?LAV BUBOQP ?>@H LK QEB >FO. )K C>@Q, TBgOB KLQ JLABIFKD QEB >FO >Q >II! 2BJBJ?BO, TB >OB PFJMIV Q>HFKD FKPMFO>QFLK COLJ QEB MEVPF@P LC QEB K>QRO>I TLOIA, KLQ PFJRI>QFKD BSBOVQEFKD TFQE MBOCB@Q MOB@FPFLK.

66

4EB .>QROB LC #LAB (S1.0)

2.2 Forces and ProcessingNewtons Second Law as a Function


!KA EBOB TB >OB >Q QEB JLPQ FJMLOQ>KQ I>T CLO QEB 0OL@BPPFKD MOLDO>JJBO.

Newtons Second Law


4EFP I>T FP PQ>QBA >P:

&LO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK.

/O:

F=MA

7EV FP QEFP QEB JLPQ FJMLOQ>KQ I>T CLO RP? 7BII, IBQgP TOFQB FQ > AFCCBOBKQ T>V.

A = F/M

!@@BIBO>QFLK FP AFOB@QIV MOLMLOQFLK>I QL CLO@B >KA FKSBOPBIV MOLMLOQFLK>I QL J>PP. 4EFP JB>KP QE>Q FC VLR DBQ MRPEBA, QEB E>OABO VLR >OB MRPEBA, QEB C>PQBO VLRgII JLSB (>@@BIBO>QB). 4EB ?FDDBO VLR >OB, QEB PILTBO VLRgII JLSB.

Weight vs. Mass


j 4EB 8,>> LC >K L?GB@Q FP > JB>PROB LC QEB >JLRKQ LC J>QQBO FK QEB L?GB@Q
(JB>PROBA FK HFILDO>JP).

j +0423?, QELRDE LCQBK JFPQ>HBK CLO J>PP, FP QB@EKF@>IIV QEB CLO@B LC DO>SFQV LK
>K L?GB@Q. &OLJ .BTQLKgP PB@LKA I>T, TB @>K @>I@RI>QB FQ >P J>PP QFJBP QEB >@@BIBO>QFLK LC DO>SFQV (Q = G * A ). 7BFDEQ FP JB>PROBA FK KBTQLKP. j D09>4?D FP ABCFKBA >P QEB >JLRKQ LC J>PP MBO RKFQ LC SLIRJB (DO>JP MBO @R?F@ @BKQFJBQBO, CLO BU>JMIB). .LQB QE>Q >K L?GB@Q QE>Q E>P > J>PP LC LKB HFILDO>J LK B>OQE TLRIA E>SB > J>PP LC LKB HFILDO>J LK QEB JLLK. (LTBSBO, FQ TLRIA TBFDE LKIV LKB-PFUQE >P JR@E.

.LT, FK QEB TLOIA LC 0OL@BPPFKD, TE>Q FP J>PP >KVT>V? !OBKgQ TB AB>IFKD TFQE MFUBIP? 4L PQ>OQ FK > PFJMIBO MI>@B, IBQgP P>V QE>Q FK LRO MOBQBKA MFUBI TLOIA, >II LC LRO L?GB@QP E>SB > J>PP BNR>I QL 1. $ / 1 = $ . !KA PL:

67

#E>MQBO 2. &LO@BP

A=F

4EB >@@BIBO>QFLK LC >K L?GB@Q FP BNR>I QL CLO@B. 4EFP FP DOB>Q KBTP. !CQBO >II, TB P>T FK #E>MQBO 1 QE>Q >@@BIBO>QFLK T>P QEB HBV QL @LKQOLIIFKD QEB JLSBJBKQ LC LRO L?GB@QP LK P@OBBK. ,L@>QFLK FP >AGRPQBA ?V SBIL@FQV, >KA SBIL@FQV ?V >@@BIBO>QFLK. !@@BIBO>QFLK T>P TEBOB FQ >II ?BD>K. .LT TB IB>OK QE>Q :CF79 FP QORIV TEBOB FQ >II ?BDFKP. ,BQgP Q>HB LRO +IP?L @I>PP, TFQE IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK.
=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; W

.LT LRO DL>I FP QL ?B >?IB QL >AA CLO@BP QL QEFP L?GB@Q, MBOE>MP P>VFKD:
GIP?L.;JJFS$IL=?(QCH>);

LO:
GIP?L.;JJFS$IL=?(AL;PCNS);

TEBOB TFKA >KA DO>SFQV >OB .3?=NIL P. !@@LOAFKD QL .BTQLKgP PB@LKA I>T, TB @LRIA FJMIBJBKQ QEFP CRK@QFLK >P CLIILTP.
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U ;==?F?L;NCIH = @IL=?; W Newtons second law at its simplest.

2.3 Force Accumulation


4EFP ILLHP MOBQQV DLLA. !CQBO >II, 5779@9F5H=CB = :CF79 FP > IFQBO>I QO>KPI>QFLK LC .BTQLKgP PB@LKA I>T (TFQELRQ J>PP). .BSBOQEBIBPP, QEBOBgP > MOBQQV ?FD MOL?IBJ EBOB. ,BQgP OBQROK QL TE>Q TB >OB QOVFKD QL >@@LJMIFPE: @OB>QFKD > JLSFKD L?GB@Q LK QEB P@OBBK QE>Q OBPMLKAP QL TFKA >KA DO>SFQV.
GIP?L.;JJFS$IL=?(QCH>); GIP?L.;JJFS$IL=?(AL;PCNS); GIP?L.OJ>;N?(); GIP?L.>CMJF;S();

/H, IBQgP 69 QEB @LJMRQBO CLO > JLJBKQ. &FOPQ, TB @>II ;JJFS$IL=?() TFQE TFKA. !KA PL QEB +IP?L L?GB@QgP >@@BIBO>QFLK FP KLT >PPFDKBA QEB .3?=NIL QCH> . 3B@LKA, TB @>II

68

4EB .>QROB LC #LAB (S1.0) ;JJFS$IL=?() TFQE DO>SFQV. .LT QEB +IP?L L?GB@QgP >@@BIBO>QFLK FP PBQ QL QEB DO>SFQV .3?=NIL .

4EFOA, TB @>II OJ>;N?() . 7E>Q E>MMBKP FK OJ>;N?() ? !@@BIBO>QFLK FP >AABA QL SBIL@FQV.


P?FI=CNS.;>>(;==?F?L;NCIH);

7BgOB KLQ DLFKD QL PBB >KV BOOLO FK 0OL@BPPFKD, ?RQ WLFKHP! 7BgSB DLQ > J>GLO MOL?IBJ. 7E>Q FP QEB S>IRB LC >@@BIBO>QFLK TEBK FQ FP >AABA QL SBIL@FQV? )Q FP BNR>I QL QEB DO>SFQV CLO@B. 7FKA E>P ?BBK IBCQ LRQ! )C TB @>II ;JJFS$IL=?() JLOB QE>K LK@B, FQ LSBOOFABP B>@E MOBSFLRP @>II. (LT >OB TB DLFKD QL E>KAIB JLOB QE>K LKB CLO@B? 4EB QORQE LC QEB J>QQBO EBOB FP QE>Q TB PQ>OQBA TFQE > PFJMIFCFBA PQ>QBJBKQ LC .BTQLKgP PB@LKA I>T. (BOBgP > JLOB >@@RO>QB T>V QL MRQ FQ:

.BQ &LO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK.

/O, >@@BIBO>QFLK FP BNR>I QL QEB GIA C: 5@@ :CF79G AFSFABA ?V J>PP. 4EFP J>HBP MBOCB@Q PBKPB. !CQBO >II, >P TB P>T FK .BTQLKgP CFOPQ I>T, FC >II QEB CLO@BP >AA RM QL WBOL, >K L?GB@Q BUMBOFBK@BP >K BNRFIF?OFRJ PQ>QB (F.B. KL >@@BIBO>QFLK). /RO FJMIBJBKQ>QFLK LC QEFP FP QEOLRDE > MOL@BPP HKLTK >P 1:=.0 ,..@8@7,?4:9. )QgP >@QR>IIV SBOV PFJMIB; >II TB KBBA QL AL FP >AA >II LC QEB CLO@BP QLDBQEBO. !Q >KV DFSBK JLJBKQ, QEBOB JFDEQ ?B 1, 2, 6, 12, LO 303 CLO@BP. !P ILKD >P LRO L?GB@Q HKLTP ELT QL >@@RJRI>QB QEBJ, FQ ALBPKgQ J>QQBO ELT J>KV CLO@BP >@Q LK FQ.
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U ;==?F?L;NCIH.;>>(@IL=?); W Newtons second law, but with force accumulation. We now add each force to acceleration, one at a time.

.LT, TBgOB KLQ CFKFPEBA GRPQ VBQ. &LO@B >@@RJRI>QFLK E>P LKB JLOB MFB@B. 3FK@B TBgOB >AAFKD >II QEB CLO@BP QLDBQEBO >Q >KV DFSBK JLJBKQ, TB E>SB QL J>HB PROB QE>Q TB @IB>O >@@BIBO>QFLK (F.B. PBQ FQ QL WBOL) ?BCLOB B>@E QFJB OJ>;N?() FP @>IIBA. ,BQgP QEFKH >?LRQ TFKA CLO > JLJBKQ. 3LJBQFJBP QEB TFKA FP SBOV PQOLKD, PLJBQFJBP FQgP TB>H, >KA PLJBQFJBP QEBOBgP KL TFKA >Q >II. !Q >KV DFSBK JLJBKQ, QEBOB JFDEQ ?B > ERDB DRPQ LC TFKA, P>V, TEBK QEB RPBO ELIAP ALTK QEB JLRPB.
C@ (GIOM?.L?MM?>) U .3?=NIL QCH> = H?Q .3?=NIL(0.5,0); GIP?L.;JJFS$IL=?(QCH>); W

7EBK QEB RPBO OBIB>PBP QEB JLRPB, QEB TFKA TFII PQLM, >KA >@@LOAFKD QL .BTQLKgP CFOPQ I>T, QEB L?GB@Q TFII @LKQFKRB QL JLSB >Q > @LKPQ>KQ SBIL@FQV. (LTBSBO, FC TB E>A CLODLQQBK QL OBPBQ >@@BIBO>QFLK QL WBOL, QEB DRPQ LC TFKA TLRIA PQFII ?B FK BCCB@Q. %SBK TLOPB, FQ TLRIA >AA LKQL FQPBIC COLJ QEB MOBSFLRP CO>JB, PFK@B TB >OB >@@RJRI>QFKD CLO@BP! !@@BIBO>QFLK, FK LRO PFJRI>QFLK, E>P KL JBJLOV; FQ FP PFJMIV @>I@RI>QBA ?>PBA LK QEB BKSFOLKJBKQ>I CLO@BP MOBPBKQ

69

#E>MQBO 2. &LO@BP

>Q > JLJBKQ FK QFJB. 4EFP FP AFCCBOBKQ QE>K, P>V, IL@>QFLK, TEF@E JRPQ OBJBJ?BO TEBOB QEB L?GB@Q T>P FK QEB MOBSFLRP CO>JB FK LOABO QL JLSB MOLMBOIV QL QEB KBUQ. 4EB B>PFBPQ T>V QL FJMIBJBKQ @IB>OFKD QEB >@@BIBO>QFLK CLO B>@E CO>JB FP QL JRIQFMIV QEB .3?=NIL ?V 0 >Q QEB BKA LC OJ>;N?() .
PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); ;==?F?L;NCIH.GOFN(0); W

Exercise 2.1
5PFKD CLO@BP, PFJRI>QB > EBIFRJ-CFIIBA ?>IILLK CIL>QFKD RMT>OA >KA ?LRK@FKD LCC QEB QLM LC > TFKALT. #>K VLR >AA > TFKA CLO@B QE>Q @E>KDBP LSBO QFJB, MBOE>MP >@@LOAFKD QL 0BOIFK KLFPB?

2.4 Dealing with Mass


/+. 7BgSB DLQ LKB QFKV IFQQIB >AAFQFLK QL J>HB ?BCLOB TB >OB ALKB TFQE FKQBDO>QFKD CLO@BP FKQL LRO +IP?L @I>PP >KA >OB OB>AV QL ILLH >Q BU>JMIBP. !CQBO >II, .BTQLKgP PB@LKA I>T FP OB>IIV F = M A, KLQ A = F . )K@LOMLO>QFKD J>PP FP >P B>PV >P >AAFKD >K FKPQ>K@B S>OF>?IB QL LRO @I>PP, ?RQ TB KBBA QL PMBKA > IFQQIB JLOB QFJB EBOB ?B@>RPB > PIFDEQ @LJMIF@>QFLK TFII BJBODB. &FOPQ TB GRPQ KBBA QL >AA J>PP.
=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N G;MM; Adding mass as a float

70

4EB .>QROB LC #LAB (S1.0)

Units of Measurement
.LT QE>Q TB >OB FKQOLAR@FKD J>PP, FQgP FJMLOQ>KQ QL J>HB > NRF@H KLQB >?LRQ RKFQP LC JB>PROBJBKQ. )K QEB OB>I TLOIA, QEFKDP >OB JB>PROBA FK PMB@FCF@ RKFQP. 7B P>V QE>Q QTL L?GB@QP >OB 3 JBQBOP >M>OQ, QEB ?>PB?>II FP JLSFKD >Q > O>QB LC 90 JFIBP MBO ELRO, LO QEFP ?LTIFKD ?>II E>P > J>PP LC 6 HFILDO>JP. !P TBgII PBB I>QBO FK QEFP ?LLH, PLJBQFJBP TB TFII T>KQ QL Q>HB OB>I-TLOIA RKFQP FKQL @LKPFABO>QFLK. (LTBSBO, FK QEFP @E>MQBO, TBgOB DLFKD QL FDKLOB QEBJ CLO QEB JLPQ M>OQ. /RO RKFQP LC JB>PROBJBKQ >OB FK MFUBIP (h4EBPB QTL @FO@IBP >OB 100 MFUBIP >M>OQi) >KA CO>JBP LC >KFJ>QFLK (h4EFP @FO@IB FP JLSFKD >Q > O>QB LC 2 MFUBIP MBO CO>JBi). )K QEB @>PB LC J>PP, QEBOB FPKgQ >KV RKFQ LC JB>PROBJBKQ CLO RP QL RPB. 7BgOB GRPQ DLFKD QL J>HB PLJBQEFKD RM. )K QEFP BU>JMIB, TBgOB >O?FQO>OFIV MF@HFKD QEB KRJ?BO 10. 4EBOB FP KL RKFQ LC JB>PROBJBKQ, QELRDE VLR JFDEQ BKGLV FKSBKQFKD > RKFQ LC VLRO LTK, IFHB h1 JLLDi LO h1 VROHIB.i )Q PELRIA >IPL ?B KLQBA QE>Q, CLO ABJLKPQO>QFLK MROMLPBP, TBgII QFB J>PP QL MFUBIP (AO>TFKD, P>V, > @FO@IB TFQE > O>AFRP LC 10). 4EFP TFII >IILT RP QL SFPR>IFWB QEB J>PP LC >K L?GB@Q. )K QEB OB>I TLOIA, ELTBSBO, PFWB ALBP KLQ ABCFKFQBIV FKAF@>QB J>PP. ! PJ>II JBQ>I ?>II @LRIA E>SB > JR@E EFDEBO J>PP QE>K > I>ODB ?>IILLK ARB QL FQP EFDEBO ABKPFQV.

->PP FP > P@>I>O (CIL>Q), KLQ > SB@QLO, >P FQgP GRPQ LKB KRJ?BO ABP@OF?FKD QEB >JLRKQ LC J>QQBO FK >K L?GB@Q. 7B @LRIA ?B C>K@V >?LRQ QEFKDP >KA @LJMRQB QEB >OB> LC > PE>MB >P FQP J>PP, ?RQ FQgP PFJMIBO QL ?BDFK ?V P>VFKD, h(BV, QEB J>PP LC QEFP L?GB@Q FPkRJ, ) ARKKLkELT >?LRQ 10?i
+IP?L() U FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN)); P?FI=CNS = H?Q .3?=NIL(0,0); ;==?F?L;NCIH = H?Q .3?=NIL(0,0); G;MM = 10.0; W

4EFP FPKgQ PL DOB>Q PFK@B QEFKDP LKIV ?B@LJB FKQBOBPQFKD LK@B TB E>SB L?GB@QP TFQE S>OVFKD J>PP, ?RQ FQgII DBQ RP PQ>OQBA. 7EBOB ALBP J>PP @LJB FK? 7B RPB FQ TEFIB >MMIVFKD .BTQLKgP PB@LKA I>T QL LRO L?GB@Q.
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U @IL=?.>CP(G;MM); ;==?F?L;NCIH.;>>(@IL=?); W Newtons second law (with force accumulation and mass)

9BQ >D>FK, BSBK QELRDE LRO @LAB ILLHP NRFQB OB>PLK>?IB, TB E>SB > C>FOIV J>GLO MOL?IBJ EBOB. #LKPFABO QEB CLIILTFKD P@BK>OFL TFQE QTL +IP?L L?GB@QP, ?LQE ?BFKD ?ILTK >T>V ?V > TFKA CLO@B.

71

#E>MQBO 2. &LO@BP
+IP?L G1 = H?Q +IP?L(); +IP?L G2 = H?Q +IP?L(); .3?=NIL QCH> = H?Q .3?=NIL(1,0); G1.;JJFS$IL=?(QCH>); G2.;JJFS$IL=?(QCH>);

!D>FK, IBQgP 69 QEB @LJMRQBO. /?GB@Q G1 OB@BFSBP QEB TFKA CLO@Be(1,0)eAFSFABP FQ ?V J>PP (10), >KA >AAP FQ QL >@@BIBO>QFLK.
C1 ;GK7BI M?D: <EH9;: ?L?:;: 8O C7II E< 10: (1,0) (0.1,0)

/+. -LSFKD LK QL L?GB@Q G2 . )Q >IPL OB@BFSBP QEB TFKA CLO@Be(1,0). 7>FQ. (LIA LK > PB@LKA. 7E>Q FP QEB S>IRB LC QEB TFKA CLO@B? 4>HFKD > @ILPBO ILLH, QEB TFKA CLO@B FP >@QR>IIV KLTe(0.1,0)!! $L VLR OBJBJ?BO QEFP IFQQIB QFA?FQ >?LRQ TLOHFKD TFQE L?GB@QP? 7EBK VLR M>PP >K L?GB@Q (FK QEFP @>PB > .3?=NIL ) FKQL > CRK@QFLK, VLR >OB M>PPFKD > OBCBOBK@B QL QE>Q L?GB@Q. )QgP KLQ > @LMV! 3L FC > CRK@QFLK J>HBP > @E>KDB QL QE>Q L?GB@Q (TEF@E, FK QEFP @>PB, FQ ALBP ?V AFSFAFKD ?V J>PP) QEBK QE>Q L?GB@Q FP MBOJ>KBKQIV @E>KDBA! "RQ TB ALKgQ T>KQ G2 QL OB@BFSB > CLO@B AFSFABA ?V QEB J>PP LC L?GB@Q G1 . 7B T>KQ FQ QL OB@BFSB QE>Q CLO@B FK FQP LOFDFK>I PQ>QBe(1,0). !KA PL TB JRPQ MOLQB@Q LROPBISBP >KA J>HB > @LMV LC QEB .3?=NIL C ?BCLOB AFSFAFKD FQ ?V J>PP. &LOQRK>QBIV, QEB .3?=NIL @I>PP E>P > @LKSBKFBKQ JBQELA CLO J>HFKD > @LMVeA?N() . A?N() OBQROKP > KBT .3?=NIL L?GB@Q TFQE QEB P>JB A>Q>. !KA PL TB @>K OBSFPB ;JJFS$IL=?() >P CLIILTP:
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U .3?=NIL @ = @IL=?.A?N(); @.>CP(G;MM); ;==?F?L;NCIH.;>>(@); W Making a copy of the PVector before using it!

4EBOBgP >KLQEBO T>V TB @LRIA TOFQB QEB >?LSB CRK@QFLK, RPFKD QEB PQ>QF@ JBQELA >CP() . &LO EBIM TFQE QEFP BUBO@FPB, OBSFBT PQ>QF@ JBQELAP FK #E>MQBO 1 (PBB M>DB 54).

Exercise 2.2
2BTOFQB QEB ;JJFS$IL=?() JBQELA RPFKD QEB PQ>QF@ JBQELA >CP() FKPQB>A LC A?N() .
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U .3?=NIL @ = :::::::.:::(:::::,::::); ;==?F?L;NCIH.;>>(@); W

72

4EB .>QROB LC #LAB (S1.0)

2.5 Creating Forces


,BQgP Q>HB > JLJBKQ QL OBJFKA LROPBISBP TEBOB TB >OB. 7B HKLT TE>Q > CLO@B FP (> SB@QLO), >KA TB HKLT ELT QL >MMIV > CLO@B QL >K L?GB@Q (AFSFAB FQ ?V J>PP >KA >AA FQ QL QEB L?GB@QgP >@@BIBO>QFLK SB@QLO). 7E>Q >OB TB JFPPFKD? 7BII, TB E>SB VBQ QL CFDROB LRQ ELT TB DBQ > CLO@B FK QEB CFOPQ MI>@B. 7EBOB AL CLO@BP @LJB COLJ? )K QEFP @E>MQBO, TBgII ILLH >Q QTL JBQELAP CLO @OB>QFKD CLO@BP FK LRO 0OL@BPPFKD TLOIA. 1. Make up a force! !CQBO >II, VLR >OB QEB MOLDO>JJBO, QEB @OB>QLO LC VLRO TLOIA. 4EBOBgP KL OB>PLK TEV VLR @>KgQ GRPQ J>HB RM > CLO@B >KA >MMIV FQ. 2. Model a force! 9BP, CLO@BP BUFPQ FK QEB OB>I TLOIA. !KA MEVPF@P QBUQ?LLHP LCQBK @LKQ>FK CLOJRI>P CLO QEBPB CLO@BP. 7B @>K Q>HB QEBPB CLOJRI>P, QO>KPI>QB QEBJ FKQL PLRO@B @LAB, >KA JLABI OB>I-TLOIA CLO@BP FK 0OL@BPPFKD. 4EB B>PFBPQ T>V QL J>HB RM > CLO@B FP QL GRPQ MF@H > KRJ?BO. ,BQgP PQ>OQ TFQE QEB FAB> LC PFJRI>QFKD TFKA. (LT >?LRQ > TFKA CLO@B QE>Q MLFKQP QL QEB OFDEQ >KA FP C>FOIV TB>H? !PPRJFKD > +IP?L L?GB@Q J, LRO @LAB TLRIA ILLH IFHB:
.3?=NIL QCH> = H?Q .3?=NIL(0.01,0); G.;JJFS$IL=?(QCH>);

4EB OBPRIQ FPKgQ QBOOF?IV FKQBOBPQFKD, ?RQ FQ FP > DLLA MI>@B QL PQ>OQ. 7B @OB>QB > .3?=NIL L?GB@Q, FKFQF>IFWB FQ, >KA M>PP FQ FKQL >K L?GB@Q (TEF@E FK QROK TFII >MMIV FQ QL FQP LTK >@@BIBO>QFLK). )C TB T>KQBA QL E>SB QTL CLO@BP, MBOE>MP TFKA >KA DO>SFQV (> ?FQ PQOLKDBO, MLFKQFKD ALTK), TB JFDEQ TOFQB QEB CLIILTFKD:

Example 2.1: Forces


.3?=NIL QCH> = H?Q .3?=NIL(0.01,0); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1); G.;JJFS$IL=?(QCH>); G.;JJFS$IL=?(AL;PCNS);

73

#E>MQBO 2. &LO@BP

.LT TB E>SB QTL CLO@BP, MLFKQFKD FK AFCCBOBKQ AFOB@QFLKP TFQE AFCCBOBKQ J>DKFQRABP, ?LQE >MMIFBA QL L?GB@Q G . 7BgOB ?BDFKKFKD QL DBQ PLJBTEBOB. 7BgSB KLT ?RFIQ > TLOIA CLO LRO L?GB@QP FK 0OL@BPPFKD, >K BKSFOLKJBKQ QL TEF@E QEBV @>K >@QR>IIV OBPMLKA. ,BQgP ILLH >Q ELT TB @LRIA J>HB QEFP BU>JMIB > ?FQ JLOB BU@FQFKD TFQE J>KV L?GB@QP LC S>OVFKD J>PP. 4L AL QEFP, TBgII KBBA > NRF@H OBSFBT LC L?GB@Q-LOFBKQBA MOLDO>JJFKD. !D>FK, TBgOB KLQ @LSBOFKD >II QEB ?>PF@P LC MOLDO>JJFKD EBOB (CLO QE>Q VLR @>K @EB@H LRQ >KV LC QEB FKQOL 0OL@BPPFKD ?LLHP IFPQBA FK QEB FKQOLAR@QFLK). (LTBSBO, PFK@B QEB FAB> LC @OB>QFKD > TLOIA CFIIBA TFQE L?GB@QP FP MOBQQV CRKA>JBKQ>I QL >II QEB BU>JMIBP FK QEFP ?LLH, FQgP TLOQE Q>HFKD > JLJBKQ QL T>IH QEOLRDE QEB PQBMP LC DLFKD COLJ LKB L?GB@Q QL J>KV. 4EFP FP TEBOB TB >OB TFQE QEB +IP?L @I>PP >P > TELIB. .LQF@B ELT FQ FP FABKQF@>I QL QEB +IP?L @I>PP @OB>QBA FK #E>MQBO 1, TFQE QTL >AAFQFLKPeG;MM >KA > KBT ;JJFS$IL=?() CRK@QFLK.
=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N G;MM; +IP?L() U G;MM = 1; FI=;NCIH = H?Q .3?=NIL(30,30); P?FI=CNS = H?Q .3?=NIL(0,0); ;==?F?L;NCIH = H?Q .3?=NIL(0,0); W PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U .3?=NIL @ = .3?=NIL.>CP(@IL=?,G;MM); ;==?F?L;NCIH.;>>(@); W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); ;==?F?L;NCIH.GOFN(0); W PIC> >CMJF;S() U MNLIE?(0); @CFF(175); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,G;MM*16,G;MM*16); W Scaling the size according to mass. Motion 101 from Chapter 1 Newtons second law. Receive a force, divide by mass, and add to acceleration. And for now, well just set the mass equal to 1 for simplicity. The object now has mass!

Now add clearing the acceleration each time!

74

4EB .>QROB LC #LAB (S1.0)

PIC> =B?=E#>A?M() U C@ (FI=;NCIH.R > QC>NB) U FI=;NCIH.R = QC>NB; P?FI=CNS.R *= -1; W ?FM? C@ (FI=;NCIH.R < 0) U P?FI=CNS.R *= -1; FI=;NCIH.R = 0; W C@ (FI=;NCIH.S > B?CABN) U P?FI=CNS.S *= -1; FI=;NCIH.S = B?CABN; W W W

Somewhat arbitrarily, we are deciding that an object bounces when it hits the edges of a window.

Even though we said we shouldn't touch location and velocity directly, there are some exceptions. Here we are doing so as a quick and easy way to reverse the direction of our object when it reaches the edge.

.LT QE>Q LRO @I>PP FP PBQ, TB @>K @ELLPB QL @OB>QB, P>V, LKB ERKAOBA +IP?L L?GB@QP TFQE >K >OO>V.
+IP?L89 GIP?LM = H?Q +IP?L81009;

!KA QEBK TB @>K FKFQF>IFWB >II LC QELPB +IP?L L?GB@QP FK M?NOJ() TFQE > ILLM.
PIC> M?NOJ() U @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9 = H?Q +IP?L(); W W

"RQ KLT TB E>SB > PJ>II FPPRB. )C TB OBCBO ?>@H QL QEB +IP?L L?GB@QgP @LKPQOR@QLOk
+IP?L() U G;MM = 1; FI=;NCIH = H?Q .3?=NIL(30,30); P?FI=CNS = H?Q .3?=NIL(0,0); ;==?F?L;NCIH = H?Q .3?=NIL(0,0); W Every object has a mass of 1 and a location of (30,30).

kTB AFP@LSBO QE>Q BSBOV +IP?L L?GB@Q FP J>AB BU>@QIV QEB P>JB T>V. 7E>Q TB T>KQ >OB +IP?L L?GB@QP LC S>OVFKD J>PP QE>Q PQ>OQ >Q S>OVFKD IL@>QFLKP. (BOB FP TEBOB TB KBBA QL FK@OB>PB QEB PLMEFPQF@>QFLK LC LRO @LKPQOR@QLO ?V >AAFKD >ODRJBKQP.
+IP?L(@FI;N G, @FI;N R , @FI;N S) U G;MM = G; FI=;NCIH = H?Q .3?=NIL(R,S); Now setting these variables with arguments

75

#E>MQBO 2. &LO@BP
P?FI=CNS = H?Q .3?=NIL(0,0); ;==?F?L;NCIH = H?Q .3?=NIL(0,0); W

.LQF@B ELT QEB J>PP >KA IL@>QFLK >OB KL ILKDBO PBQ QL E>OA@LABA KRJ?BOP, ?RQ O>QEBO FKFQF>IFWBA SF> >ODRJBKQP M>PPBA QEOLRDE QEB @LKPQOR@QLO. 4EFP JB>KP TB @>K @OB>QB > S>OFBQV LC +IP?L L?GB@QP: ?FD LKBP, PJ>II LKBP, LKBP QE>Q PQ>OQ LK QEB IBCQ PFAB LC QEB P@OBBK, LKBP QE>Q PQ>OQ LK QEB OFDEQ, BQ@.
+IP?L G1 = H?Q +IP?L(10,0,B?CABN/2); +IP?L G1 = H?Q +IP?L(0.1,QC>NB,B?CABN/2); A big Mover on the left side of the window A small Mover on the right side of the window

7FQE >K >OO>V, ELTBSBO, TB T>KQ QL FKFQF>IFWB >II LC QEB L?GB@QP TFQE > ILLM.
PIC> M?NOJ() U @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,5),0,0); W W Initializing many Mover objects, all with random mass (and all starting at 0,0)

&LO B>@E JLSBO @OB>QBA, QEB J>PP FP PBQ QL > O>KALJ S>IRB ?BQTBBK 0.1 >KA 5, QEB PQ>OQFKD U-IL@>QFLK FP PBQ QL 0, >KA QEB PQ>OQFKD V-IL@>QFLK FP PBQ QL 0. #BOQ>FKIV, QEBOB >OB >II PLOQP LC T>VP TB JFDEQ @ELLPB QL FKFQF>IFWB QEB L?GB@QP; QEFP FP GRPQ > ABJLKPQO>QFLK LC LKB MLPPF?FIFQV. /K@B QEB >OO>V LC L?GB@QP FP AB@I>OBA, @OB>QBA, >KA FKFQF>IFWBA, QEB OBPQ LC QEB @LAB FP PFJMIB. 7B ORK QEOLRDE BSBOV L?GB@Q, E>KA QEBJ B>@E QEB CLO@BP FK QEB BKSFOLKJBKQ, >KA BKGLV QEB PELT.

76

4EB .>QROB LC #LAB (S1.0)

Example 2.2: Forces acting on many objects


PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL QCH> = H?Q .3?=NIL(0.01,0); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1); Make up two forces.

@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9.;JJFS$IL=?(QCH>); GIP?LM8C9.;JJFS$IL=?(AL;PCNS); GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); GIP?LM8C9.=B?=E#>A?M(); W W

Loop through all objects and apply both forces to each object.

.LQB ELT FK QEB >?LSB FJ>DB, QEB PJ>IIBO @FO@IBP OB>@E QEB OFDEQ LC QEB TFKALT C>PQBO QE>K QEB I>ODBO LKBP. 4EFP FP ?B@>RPB LC LRO CLOJRI>: 5779@9F5H=CB = :CF79 8=J=898 6M A5GG . 4EB I>ODBO QEB J>PP, QEB PJ>IIBO QEB >@@BIBO>QFLK.

Exercise 2.3
)KPQB>A LC L?GB@QP ?LRK@FKD LCC QEB BADB LC QEB T>II, @OB>QB >K BU>JMIB FK TEF@E >K FKSFPF?IB CLO@B MRPEBP ?>@H LK QEB L?GB@QP QL HBBM QEBJ FK QEB TFKALT. #>K VLR TBFDEQ QEB CLO@B >@@LOAFKD QL ELT C>O QEB L?GB@Q FP COLJ >K BADBeF.B., QEB @ILPBO FQ FP, QEB PQOLKDBO QEB CLO@B?

2.6 Gravity on Earth and Modeling a Force


9LR J>V E>SB KLQF@BA PLJBQEFKD TLBCRIIV FK>@@RO>QB >?LRQ QEFP I>PQ BU>JMIB. 4EB PJ>IIBO QEB @FO@IB, QEB C>PQBO FQ C>IIP. 4EBOB FP > ILDF@ QL QEFP; >CQBO >II, TB GRPQ PQ>QBA (>@@LOAFKD QL .BTQLKgP PB@LKA I>T) QE>Q QEB PJ>IIBO QEB J>PP, QEB EFDEBO QEB >@@BIBO>QFLK. "RQ QEFP FP KLQ TE>Q E>MMBKP FK QEB OB>I TLOIA. )C VLR TBOB QL @IFJ? QL QEB QLM LC QEB ,B>KFKD 4LTBO LC 0FP> >KA AOLM QTL ?>IIP LC AFCCBOBKQ J>PPBP, TEF@E LKB TFII EFQ QEB DOLRKA CFOPQ? !@@LOAFKD QL IBDBKA, '>IFIBL MBOCLOJBA QEFP BU>@Q QBPQ FK 1589, AFP@LSBOFKD QE>Q QEBV CBII TFQE QEB P>JB >@@BIBO>QFLK, EFQQFKD QEB DOLRKA >Q QEB P>JB QFJB. 7EV FP QEFP? !P TB TFII PBB I>QBO FK QEFP @E>MQBO, QEB CLO@B LC DO>SFQV FP @>I@RI>QBA OBI>QFSB QL >K L?GB@QgP J>PP. 4EB ?FDDBO QEB L?GB@Q, QEB PQOLKDBO QEB CLO@B. 3L FC QEB CLO@B FP P@>IBA >@@LOAFKD QL J>PP, FQ FP @>K@BIBA LRQ TEBK >@@BIBO>QFLK FP AFSFABA ?V J>PP. 7B @>K FJMIBJBKQ QEFP FK LRO PHBQ@E O>QEBO B>PFIV ?V JRIQFMIVFKD LRO J>AB-RM DO>SFQV CLO@B ?V J>PP.

77

#E>MQBO 2. &LO@BP

Example 2.3: Gravity scaled by mass


@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U .3?=NIL QCH> = H?Q .3?=NIL(0.001,0); @FI;N G = GIP?LM8C9.G;MM; .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1*G); GIP?LM8C9.;JJFS$IL=?(QCH>); GIP?LM8C9.;JJFS$IL=?(AL;PCNS); GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); GIP?LM8C9.=B?=E#>A?M(); W Scaling gravity by mass to be more accurate

7EFIB QEB L?GB@QP KLT C>II >Q QEB P>JB O>QB, ?B@>RPB QEB PQOBKDQE LC QEB TFKA CLO@B FP FKABMBKABKQ LC J>PP, QEB PJ>IIBO L?GB@QP PQFII >@@BIBO>QB QL QEB OFDEQ JLOB NRF@HIV. ->HFKD RM CLO@BP TFII >@QR>IIV DBQ RP NRFQB C>O. 4EB TLOIA LC 0OL@BPPFKD FP > MOBQBKA TLOIA LC MFUBIP >KA VLR >OB FQP J>PQBO. 3L TE>QBSBO VLR ABBJ >MMOLMOF>QB QL ?B > CLO@B, TBII ?V DLIIV, QE>QgP QEB CLO@B FQ PELRIA ?B. .BSBOQEBIBPP, QEBOB J>V @LJB > QFJB TEBOB VLR CFKA VLROPBIC TLKABOFKD: h"RQ ELT ALBP FQ OB>IIV >II TLOH?i /MBK RM >KV EFDE P@ELLI MEVPF@P QBUQ?LLH >KA VLR TFII CFKA PLJB AF>DO>JP >KA CLOJRI>P ABP@OF?FKD J>KV AFCCBOBKQ CLO@BPeDO>SFQV, BIB@QOLJ>DKBQFPJ, COF@QFLK, QBKPFLK, BI>PQF@FQV, >KA JLOB. )K QEFP @E>MQBO TBgOB DLFKD QL ILLH >Q QTL CLO@BPeCOF@QFLK >KA DO>SFQV. 4EB MLFKQ TBgOB J>HFKD EBOB FP KLQ QE>Q COF@QFLK >KA DO>SFQV >OB CRKA>JBKQ>I CLO@BP QE>Q VLR >IT>VP KBBA QL E>SB FK VLRO 0OL@BPPFKD PHBQ@EBP. 2>QEBO, TB T>KQ QL BS>IR>QB QEBPB QTL CLO@BP >P @>PB PQRAFBP CLO QEB CLIILTFKD MOL@BPP: j 5KABOPQ>KAFKD QEB @LK@BMQ ?BEFKA > CLO@B j $B@LKPQOR@QFKD QEB CLO@BgP CLOJRI> FKQL QTL M>OQP: (LT AL TB @LJMRQB QEB CLO@BgP AFOB@QFLK?

78

4EB .>QROB LC #LAB (S1.0)

(LT AL TB @LJMRQB QEB CLO@BgP J>DKFQRAB? j 4O>KPI>QFKD QE>Q CLOJRI> FKQL 0OL@BPPFKD @LAB QE>Q @>I@RI>QBP > .3?=NIL QL ?B PBKQ QEOLRDE LRO +IP?L 'P ;JJFS$IL=?() CRK@QFLK )C TB @>K CLIILT QEB >?LSB PQBMP TFQE QTL CLO@BP, QEBK ELMBCRIIV FC VLR BSBO CFKA VLROPBIC 'LLDIFKD h>QLJF@ KR@IBF TB>H KR@IB>O CLO@Bi >Q 3 >.J., VLR TFII E>SB QEB PHFIIP QL Q>HB TE>Q VLR CFKA >KA >A>MQ FQ CLO 0OL@BPPFKD.

Dealing with formulae


/+, FK > JLJBKQ TBgOB DLFKD QL TOFQB LRQ QEB CLOJRI> CLO COF@QFLK. 4EFP FPKgQ QEB CFOPQ QFJB TBgSB PBBK > CLOJRI> FK QEFP ?LLH; TB GRPQ CFKFPEBA RM LRO AFP@RPPFLK LC .BTQLKgP PB@LKA I>T, F = M A (LO CLO@B = J>PP * >@@BIBO>QFLK). 7B AFAKgQ PMBKA > ILQ LC QFJB TLOOVFKD >?LRQ QEFP CLOJRI> ?B@>RPB FQgP > KF@B >KA PFJMIB LKB. .BSBOQEBIBPP, FQgP > P@>OV TLOIA LRQ QEBOB. *RPQ Q>HB > ILLH >Q QEB BNR>QFLK CLO > hKLOJ>Ii AFPQOF?RQFLK, TEF@E TB @LSBOBA (TFQELRQ ILLHFKD >Q QEB CLOJRI>) FK QEB )KQOLAR@QFLK (PBB M>DB 10).

f ( x; , 2) =

1 e 2

( x )2 2 2

7E>Q TBgOB PBBFKD EBOB FP QE>Q CLOJRI>P IFHB QL RPB > ILQ LC PVJ?LIP (NRFQB LCQBK IBQQBOP COLJ QEB 'OBBH >IME>?BQ). ,BQgP Q>HB > ILLH >Q QEB CLOJRI> CLO COF@QFLK.

Friction = N v

)C FQgP ?BBK > TEFIB PFK@B VLRgSB ILLHBA >Q > CLOJRI> COLJ > J>QE LO MEVPF@P QBUQ?LLH, QEBOB >OB QEOBB HBV MLFKQP QE>Q >OB FJMLOQ>KQ QL @LSBO ?BCLOB TB JLSB LK.

j EA,7@,?0 ?30 =423? >4/0, ,>>429 ?: ?30 701? >4/0. 4EFP FP GRPQ IFHB FK @LAB! 7E>Q
TBgOB ALFKD EBOB FP BS>IR>QFKD QEB OFDEQ PFAB LC QEB BNR>QFLK >KA >PPFDKFKD FQ QL QEB IBCQ. )K QEB @>PB >?LSB, TB T>KQ QL @>I@RI>QB QEB CLO@B LC COF@QFLKeQEB IBCQ PFAB QBIIP RP TE>Q TB T>KQ QL @>I@RI>QB >KA QEB OFDEQ PFAB QBIIP RP ELT QL AL FQ. j A=0 B0 ?,76492 ,-:@? , A0.?:= := , >.,7,=? )QgP FJMLOQ>KQ CLO RP QL OB>IFWB QE>Q FK PLJB @>PBP, TBgII ?B ILLHFKD >Q > SB@QLO; FK LQEBOP, > P@>I>O. &LO BU>JMIB, FK QEFP @>PB QEB CLO@B LC COF@QFLK FP > SB@QLO. 7B @>K PBB QE>Q ?V QEB >OOLT >?LSB QEB TLOA hCOF@QFLK.i )Q E>P > J>DKFQRAB >KA AFOB@QFLK. 4EB OFDEQ PFAB LC QEB BNR>QFLK >IPL E>P > SB@QLO, >P FKAF@>QBA ?V QEB PVJ?LI v , TEF@E FK QEFP @>PB PQ>KAP CLO QEB SBIL@FQV RKFQ SB@QLO. j +309 >D8-:7> ,=0 ;7,.0/ 90C? ?: 0,.3 :?30=, B0 80,9 1:= ?308 ?: -0 8@7?4;740/. 4EB CLOJRI> >?LSB >@QR>IIV E>P CLRO BIBJBKQP: -1, P, *, >KA v . 7B T>KQ QL JRIQFMIV QEBJ QLDBQEBO >KA OB>A QEB CLOJRI> >P: Friction = 1 * * N * v

79

#E>MQBO 2. &LO@BP

2.7 Friction
,BQgP ?BDFK TFQE COF@QFLK >KA CLIILT LRO PQBMP. &OF@QFLK FP > /4>>4;,?4A0 1:=.0 . ! AFPPFM>QFSB CLO@B FP LKB FK TEF@E QEB QLQ>I BKBODV LC > PVPQBJ AB@OB>PBP TEBK >K L?GB@Q FP FK JLQFLK. ,BQgP P>V VLR >OB AOFSFKD > @>O. 7EBK VLR MOBPP VLRO CLLQ ALTK LK QEB ?O>HB MBA>I, QEB @>OgP ?O>HBP RPB COF@QFLK QL PILT ALTK QEB JLQFLK LC QEB QFOBP. +FKBQF@ BKBODV (JLQFLK) FP @LKSBOQBA FKQL QEBOJ>I BKBODV (EB>Q). 7EBKBSBO QTL PROC>@BP @LJB FKQL @LKQ>@Q, QEBV BUMBOFBK@B COF@QFLK. ! @LJMIBQB JLABI LC COF@QFLK TLRIA FK@IRAB PBM>O>QB @>PBP CLO PQ>QF@ COF@QFLK (> ?LAV >Q OBPQ >D>FKPQ > PROC>@B) >KA HFKBQF@ COF@QFLK (> ?LAV FK JLQFLK >D>FKPQ > PROC>@B), ?RQ CLO LRO MROMLPBP, TB >OB LKIV DLFKD QL ILLH >Q QEB HFKBQF@ @>PB. (BOBgP QEB CLOJRI> CLO COF@QFLK:

F64B?2 2.3

)QgP KLT RM QL RP QL PBM>O>QB QEFP CLOJRI> FKQL QTL @LJMLKBKQP QE>Q ABQBOJFKB QEB AFOB@QFLK LC COF@QFLK >P TBII >P QEB J>DKFQRAB. ">PBA LK QEB AF>DO>J >?LSB, TB @>K PBB QE>Q :F=7H=CB DC=BHG =B H<9 CDDCG=H9 8=F97H=CB C: J9@C7=HM. )K C>@Q, QE>QgP QEB M>OQ LC QEB CLOJRI> QE>Q P>VP -1 * v , LO -1 QFJBP QEB SBIL@FQV RKFQ SB@QLO. )K 0OL@BPPFKD, QEFP TLRIA JB>K Q>HFKD QEB SBIL@FQV SB@QLO, KLOJ>IFWFKD FQ, >KA JRIQFMIVFKD ?V -1.
.3?=NIL @LC=NCIH = P?FI=CNS.A?N(); @LC=NCIH.HILG;FCT?(); @LC=NCIH.GOFN(-1); Lets figure out the direction of the friction force (a unit vector in the opposite direction of velocity).

.LQF@B QTL >AAFQFLK>I PQBMP EBOB. &FOPQ, FQgP FJMLOQ>KQ QL J>HB > @LMV LC QEB SBIL@FQV SB@QLO, >P TB ALKgQ T>KQ QL OBSBOPB QEB L?GB@QgP AFOB@QFLK ?V >@@FABKQ. 3B@LKA, TB KLOJ>IFWB QEB SB@QLO. 4EFP FP ?B@>RPB QEB J>DKFQRAB LC COF@QFLK FP KLQ >PPL@F>QBA TFQE ELT C>PQ FQ FP JLSFKD, >KA TB T>KQ QL PQ>OQ TFQE > COF@QFLK SB@QLO LC J>DKFQRAB 1 PL QE>Q FQ @>K B>PFIV ?B P@>IBA.

80

4EB .>QROB LC #LAB (S1.0)

!@@LOAFKD QL QEB CLOJRI>, QEB J>DKFQRAB FP X * , . X , QEB 'OBBH IBQQBO AI (MOLKLRK@BA hJBTi), FP RPBA EBOB QL ABP@OF?B QEB .:0114.409? :1 1=4.?4:9. 4EB @LBCCF@FBKQ LC COF@QFLK BPQ>?IFPEBP QEB PQOBKDQE LC > COF@QFLK CLO@B CLO > M>OQF@RI>O PROC>@B. 4EB EFDEBO FQ FP, QEB PQOLKDBO QEB COF@QFLK; QEB ILTBO, QEB TB>HBO. ! ?IL@H LC F@B, CLO BU>JMIB, TFII E>SB > JR@E ILTBO @LBCCF@FBKQ LC COF@QFLK QE>K, P>V, P>KAM>MBO. 3FK@B TBgOB FK > MOBQBKA 0OL@BPPFKD TLOIA, TB @>K >O?FQO>OFIV PBQ QEB @LBCCF@FBKQ ?>PBA LK ELT JR@E COF@QFLK TB T>KQ QL PFJRI>QB.
@FI;N = = 0.01;

.LT CLO QEB PB@LKA M>OQ: , . , OBCBOP QL QEB 9:=8,7 1:=.0, QEB CLO@B MBOMBKAF@RI>O QL QEB L?GB@QgP JLQFLK >ILKD > PROC>@B. 4EFKH LC > SBEF@IB AOFSFKD >ILKD > OL>A. 4EB SBEF@IB MRPEBP ALTK >D>FKPQ QEB OL>A TFQE DO>SFQV, >KA .BTQLKgP QEFOA I>T QBIIP RP QE>Q QEB OL>A FK QROK MRPEBP ?>@H >D>FKPQ QEB SBEF@IB. 4E>QgP QEB KLOJ>I CLO@B. 4EB DOB>QBO QEB DO>SFQ>QFLK>I CLO@B, QEB DOB>QBO QEB KLOJ>I CLO@B. !P TBgII PBB FK QEB KBUQ PB@QFLK, DO>SFQV FP >PPL@F>QBA TFQE J>PP, >KA PL > IFDEQTBFDEQ PMLOQP @>O TLRIA BUMBOFBK@B IBPP COF@QFLK QE>K > J>PPFSB QO>@QLO QO>FIBO QOR@H. 7FQE QEB AF>DO>J >?LSB, ELTBSBO, TEBOB QEB L?GB@Q FP JLSFKD >ILKD > PROC>@B >Q >K >KDIB, @LJMRQFKD QEB KLOJ>I CLO@B FP > ?FQ JLOB @LJMIF@>QBA ?B@>RPB FQ ALBPKgQ MLFKQ FK QEB P>JB AFOB@QFLK >P DO>SFQV. 7BgII KBBA QL HKLT PLJBQEFKD >?LRQ >KDIBP >KA QOFDLKLJBQOV. !II LC QEBPB PMB@FCF@P >OB FJMLOQ>KQ; ELTBSBO, FK 0OL@BPPFKD, > hDLLA BKLRDEi PFJRI>QFLK @>K ?B >@EFBSBA TFQELRQ QEBJ. 7B @>K, CLO BU>JMIB, J>HB COF@QFLK TLOH TFQE QEB >PPRJMQFLK QE>Q QEB KLOJ>I CLO@B TFII >IT>VP E>SB > J>DKFQRAB LC 1. 7EBK TB DBQ FKQL QOFDLKLJBQOV FK QEB KBUQ @E>MQBO, TBgII OBJBJ?BO QL OBQROK QL QEFP NRBPQFLK >KA J>HB LRO COF@QFLK BU>JMIB > ?FQ JLOB PLMEFPQF@>QBA. 4EBOBCLOB:
@FI;N HILG;F = 1;

.LT QE>Q TB E>SB ?LQE QEB J>DKFQRAB >KA AFOB@QFLK CLO COF@QFLK, TB @>K MRQ FQ >II QLDBQEBOk
@FI;N = = 0.01; @FI;N HILG;F = 1; @FI;N @LC=NCIH+;A = =*HILG;F; .3?=NIL @LC=NCIH = P?FI=CNS.A?N(); @LC=NCIH.GOFN(-1); @LC=NCIH.HILG;FCT?(); @LC=NCIH.GOFN(@LC=NCIH+;A); Take the unit vector and multiply it by magnitude and we have our force vector! Lets figure out the magnitude of friction (really just an arbitrary constant).

k>KA >AA FQ QL LRO hCLO@BPi BU>JMIB, TEBOB J>KV L?GB@QP BUMBOFBK@B TFKA, DO>SFQV, >KA KLT COF@QFLK:

81

#E>MQBO 2. &LO@BP

%< 3?60A6<;

,6A5 3?60A6<;

Example 2.4: Including friction


PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL QCH> = H?Q .3?=NIL(0.001,0); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U <BE7J 9 = 0.01; +0;9JEH <H?9J?ED = CEL;HI4?5.L;BE9?JO.=;J(); <H?9J?ED.CKBJ(-1); <H?9J?ED.DEHC7B?P;(); <H?9J?ED.CKBJ(9); CEL;HI4?5.7FFBO"EH9;(<H?9J?ED); GIP?LM8C9.;JJFS$IL=?(QCH>); GIP?LM8C9.;JJFS$IL=?(AL;PCNS); GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); GIP?LM8C9.=B?=E#>A?M(); W W Apply the friction force vector to the object. We could scale by mass to be more accurate.

2RKKFKD QEFP BU>JMIB, VLRgII KLQF@B QE>Q QEB @FO@IBP ALKgQ BSBK J>HB FQ QL QEB OFDEQ PFAB LC QEB TFKALT. 3FK@B COF@QFLK @LKQFKRLRPIV MRPEBP >D>FKPQ QEB L?GB@Q FK QEB LMMLPFQB AFOB@QFLK LC FQP JLSBJBKQ, QEB L?GB@Q @LKQFKRLRPIV PILTP ALTK. 4EFP @>K ?B > RPBCRI QB@EKFNRB LO > MOL?IBJ ABMBKAFKD LK QEB DL>IP LC VLRO SFPR>IFW>QFLK.

82

4EB .>QROB LC #LAB (S1.0)

Exercise 2.4
#OB>QB ML@HBQP LC COF@QFLK FK > 0OL@BPPFKD PHBQ@E PL QE>Q L?GB@QP LKIV BUMBOFBK@B COF@QFLK TEBK @OLPPFKD LSBO QELPB ML@HBQP. 7E>Q FC VLR S>OV QEB PQOBKDQE (COF@QFLK @LBCCF@FBKQ) LC B>@E >OB>? 7E>Q FC VLR J>HB PLJB ML@HBQP CB>QROB QEB LMMLPFQB LC COF@QFLKeF.B., TEBK VLR BKQBO > DFSBK ML@HBQ VLR >@QR>IIV PMBBA RM FKPQB>A LC PILTFKD ALTK?

2.8 Air and Fluid Resistance

F64B?2 2.4

&OF@QFLK >IPL L@@ROP TEBK > ?LAV M>PPBP QEOLRDE > IFNRFA LO D>P. 4EFP CLO@B E>P J>KV AFCCBOBKQ K>JBP, >II OB>IIV JB>KFKD QEB P>JB QEFKD: J=G7CIG :CF79, 8F5; :CF79, :@I=8 F9G=GH5B79. 7EFIB QEB OBPRIQ FP RIQFJ>QBIV QEB P>JB >P LRO MOBSFLRP COF@QFLK BU>JMIBP (QEB L?GB@Q PILTP ALTK), QEB T>V FK TEF@E TB @>I@RI>QB > AO>D CLO@B TFII ?B PIFDEQIV AFCCBOBKQ. ,BQgP ILLH >Q QEB CLOJRI>:
2 Fd = 1 2 v AC d v

.LT IBQgP ?OB>H QEFP ALTK >KA PBB TE>Q TB OB>IIV KBBA CLO >K BCCB@QFSB PFJRI>QFLK FK 0OL@BPPFKD, J>HFKD LROPBISBP > JR@E PFJMIBO CLOJRI> FK QEB MOL@BPP. j F d OBCBOP QL 8F5; :CF79, QEB SB@QLO TB RIQFJ>QBIV T>KQ QL @LJMRQB >KA M>PP FKQL LRO ;JJFS$IL=?() CRK@QFLK. j - 1/2 FP > @LKPQ>KQ: -0.5. 4EFP FP C>FOIV FOOBIBS>KQ FK QBOJP LC LRO 0OL@BPPFKD TLOIA, >P TB TFII ?B J>HFKD RM S>IRBP CLO LQEBO @LKPQ>KQP >KVT>V. (LTBSBO, QEB C>@Q QE>Q FQ FP KBD>QFSB FP FJMLOQ>KQ, >P FQ QBIIP RP QE>Q QEB CLO@B FP FK QEB LMMLPFQB AFOB@QFLK LC SBIL@FQV (GRPQ >P TFQE COF@QFLK).

83

#E>MQBO 2. &LO@BP

j FP QEB 'OBBH IBQQBO F<C , >KA OBCBOP QL QEB ABKPFQV LC QEB IFNRFA, PLJBQEFKD TB ALKgQ KBBA QL TLOOV >?LRQ. 7B @>K PFJMIFCV QEB MOL?IBJ >KA @LKPFABO QEFP QL E>SB > @LKPQ>KQ S>IRB LC 1. j v OBCBOP QL QEB PMBBA LC QEB L?GB@Q JLSFKD. /+, TBgSB DLQ QEFP LKB! 4EB L?GB@QgP PMBBA FP QEB J>DKFQRAB LC QEB SBIL@FQV SB@QLO: P?FI=CNS.G;AHCNO>?() . !KA v2 GRPQ JB>KP v PNR>OBA LO v * v. j A OBCBOP QL QEB COLKQ>I >OB> LC QEB L?GB@Q QE>Q FP MRPEFKD QEOLRDE QEB IFNRFA (LO D>P). !K >BOLAVK>JF@ ,>J?LODEFKF, CLO BU>JMIB, TFII BUMBOFBK@B IBPP >FO OBPFPQ>K@B QE>K > ?LUV 6LISL. .BSBOQEBIBPP, CLO > ?>PF@ PFJRI>QFLK, TB @>K @LKPFABO LRO L?GB@Q QL ?B PMEBOF@>I >KA FDKLOB QEFP BIBJBKQ. j C d FP QEB @LBCCF@FBKQ LC AO>D, BU>@QIV QEB P>JB >P QEB @LBCCF@FBKQ LC COF@QFLK ( ). 4EFP FP > @LKPQ>KQ TBgII ABQBOJFKB ?>PBA LK TEBQEBO TB T>KQ QEB AO>D CLO@B QL ?B PQOLKD LO TB>H. j v ,LLH C>JFIF>O? )Q PELRIA. 4EFP OBCBOP QL QEB SBIL@FQV RKFQ SB@QLO, F.B. P?FI=CNS.HILG;FCT?() . *RPQ IFHB TFQE COF@QFLK, AO>D FP > CLO@B QE>Q MLFKQP FK QEB LMMLPFQB AFOB@QFLK LC SBIL@FQV. .LT QE>Q TBgSB >K>IVWBA B>@E LC QEBPB @LJMLKBKQP >KA ABQBOJFKBA TE>Q TB KBBA CLO > PFJMIB PFJRI>QFLK, TB @>K OBAR@B LRO CLOJRI> QL:

F64B?2 2.5: &B? @6:=963621 1?.4 3<?02 3<?:B9.

LO:
@FI;N = = 0.1; @FI;N MJ??> = P.G;A(); @FI;N >L;A+;AHCNO>? = = * MJ??> * MJ??>; .3?=NIL >L;A = P?FI=CNS.A?N(); >L;A.GOFN(-1); >L;A.HILG;FCT?(); >L;A.GOFN(>L;A+;AHCNO>?); Part 2 of our formula (direction): -1 * velocity Magnitude and direction together! Part 1 of our formula (magnitude): Cd * v2

,BQgP FJMIBJBKQ QEFP CLO@B FK LRO +IP?L @I>PP BU>JMIB TFQE LKB >AAFQFLK. 7EBK TB TOLQB LRO COF@QFLK BU>JMIB, QEB CLO@B LC COF@QFLK T>P >IT>VP MOBPBKQ. 7EBKBSBO >K L?GB@Q T>P JLSFKD, COF@QFLK TLRIA PILT FQ ALTK. (BOB, IBQgP FKQOLAR@B >K BIBJBKQ QL QEB BKSFOLKJBKQe> hIFNRFAi QE>Q QEB +IP?L L?GB@QP M>PP QEOLRDE. 4EB *CKOC> L?GB@Q TFII ?B > OB@Q>KDIB >KA TFII HKLT 84

4EB .>QROB LC #LAB (S1.0)

>?LRQ FQP IL@>QFLK, TFAQE, EBFDEQ, >KA h@LBCCF@FBKQ LC AO>DieF.B., FP FQ B>PV CLO L?GB@QP QL JLSB QEOLRDE FQ (IFHB >FO) LO AFCCF@RIQ (IFHB JLI>PPBP)? )K >AAFQFLK, FQ PELRIA FK@IRAB > CRK@QFLK QL AO>T FQPBIC LK QEB P@OBBK (>KA QTL JLOB CRK@QFLKP, TEF@E TBgII PBB FK > JLJBKQ).
=F;MM *CKOC> U @FI;N R,S,Q,B; @FI;N =; The liquid object includes a variable defining its coefficient of drag.

*CKOC>(@FI;N R:, @FI;N S:, @FI;N Q:, @FI;N B:, @FI;N =:) U R = R:; S = S:; Q = Q:; B = B:; = = =:; W PIC> >CMJF;S() U HI0NLIE?(); @CFF(175); L?=N(R,S,Q,B); W W

4EB J>FK MOLDO>J TFII KLT FK@IRAB > *CKOC> L?GB@Q OBCBOBK@B >P TBII >P > IFKB LC @LAB QE>Q FKFQF>IFWBP QE>Q L?GB@Q.
*CKOC> FCKOC>; PIC> M?NOJ() U Initialize a Liquid object. Note the coefficient is low (0.1), otherwise the object would come to a halt fairly quickly (which may someday be the effect you want).

FCKOC> = H?Q *CKOC>(0, B?CABN/2, QC>NB, B?CABN/2, 0.1); W

.LT @LJBP >K FKQBOBPQFKD NRBPQFLK: ELT AL TB DBQ QEB +IP?L L?GB@Q QL Q>IH QL QEB *CKOC> L?GB@Q? )K LQEBO TLOAP, TB T>KQ QL BUB@RQB QEB CLIILTFKD: 2<9B 5 ACJ9F D5GG9G H<FCI;< 5 @=EI=8 =H 9LD9F=9B79G 5 8F5; :CF79. kLO FK L?GB@Q-LOFBKQBA PMB>H (>PPRJFKD TB >OB ILLMFKD QEOLRDE >K >OO>V LC +IP?L L?GB@QP TFQE FKABU F):
C@ (GIP?LM8C9.CM'HMC>?(FCKOC>)) U GIP?LM8C9.>L;A(FCKOC>); W If a Mover is inside a Liquid, apply the drag force.

85

#E>MQBO 2. &LO@BP

4EB >?LSB @LAB QBIIP RP QE>Q TB KBBA QL >AA QTL CRK@QFLKP QL QEB +IP?L @I>PP: (1) > CRK@QFLK QE>Q ABQBOJFKBP FC > +IP?L L?GB@Q FP FKPFAB QEB *CKOC> L?GB@Q, >KA (2) > CRK@QFLK QE>Q @LJMRQBP >KA >MMIFBP > AO>D CLO@B LK QEB +IP?L L?GB@Q. 4EB CFOPQ FP B>PV; TB @>K PFJMIV RPB > @LKAFQFLK>I PQ>QBJBKQ QL ABQBOJFKB FC QEB IL@>QFLK SB@QLO OBPQP FKPFAB QEB OB@Q>KDIB ABCFKBA ?V QEB IFNRFA.
<IIF?;H CM'HMC>?(*CKOC> F) U C@ (FI=;NCIH.R>F.R && FI=;NCIH.R<F.R+F.Q && FI=;NCIH.S>F.S && FI=;NCIH.S<F.S+F.B) U L?NOLH NLO?; W ?FM? U L?NOLH @;FM?; W W This conditional statement determines if the PVector location is inside the rectangle defined by the Liquid class.

4EB >L;A() CRK@QFLK FP > ?FQ JLOB @LJMIF@>QBA; ELTBSBO, TBgSB TOFQQBK QEB @LAB CLO FQ >IOB>AV. 4EFP FP PFJMIV >K FJMIBJBKQ>QFLK LC LRO CLOJRI>. 4EB AO>D CLO@B FP BNR>I QL H<9 7C9::=7=9BH C: 8F5; AI@H=D@=98 6M H<9 GD998 C: H<9 +IP?L GEI5F98 =B H<9 CDDCG=H9 8=F97H=CB C: J9@C7=HM!
PIC> >L;A(*CKOC> F) U @FI;N MJ??> = P?FI=CNS.G;A(); @FI;N >L;A+;AHCNO>? = F.= * MJ??> * MJ??>; .3?=NIL >L;A = P?FI=CNS.A?N(); >L;A.GOFN(-1); >L;A.HILG;FCT?(); The force's direction: -1 * velocity The forces magnitude: Cd * v~2~

>L;A.GOFN(>L;A+;AHCNO>?);

Finalize the force: magnitude and direction together. Apply the force.

;JJFS$IL=?(>L;A); W

!KA TFQE QEBPB QTL CRK@QFLKP >AABA QL QEB +IP?L @I>PP, TBgOB OB>AV QL MRQ FQ >II QLDBQEBO FK QEB J>FK Q>?:

86

4EB .>QROB LC #LAB (S1.0)

Example 2.5: Fluid Resistance


+IP?L89 GIP?LM = H?Q +IP?L81009; *CKOC> FCKOC>; PIC> M?NOJ() U MCT?(360, 640); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,5),0,0); W FCKOC> = H?Q *CKOC>(0, B?CABN/2, QC>NB, B?CABN/2, 0.1); W PIC> >L;Q() U <;=EALIOH>(255); FCKOC>.>CMJF;S(); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U C@ (GIP?LM8C9.CM'HMC>?(FCKOC>)) U GIP?LM8C9.>L;A(FCKOC>); W @FI;N G = 0.1*GIP?LM8C9.G;MM; .3?=NIL AL;PCNS = H?Q .3?=NIL(0, G); GIP?LM8C9.;JJFS$IL=?(AL;PCNS); GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); GIP?LM8C9.=B?=E#>A?M(); W W Note that we are scaling gravity according to mass.

2RKKFKD QEB BU>JMIB, VLR PELRIA KLQF@B QE>Q TB >OB PFJRI>QFKD ?>IIP C>IIFKD FKQL T>QBO. 4EB L?GB@QP LKIV PILT ALTK TEBK @OLPPFKD QEOLRDE QEB DO>V >OB> >Q QEB ?LQQLJ LC QEB TFKALT (OBMOBPBKQFKD QEB IFNRFA). 9LRgII >IPL KLQF@B QE>Q QEB PJ>IIBO L?GB@QP PILT ALTK > DOB>Q AB>I JLOB QE>K QEB I>ODBO L?GB@QP. 2BJBJ?BO .BTQLKgP PB@LKA I>T? A = $ / + . !@@BIBO>QFLK BNR>IP

87

#E>MQBO 2. &LO@BP

CLO@B 8=J=898 ?V J>PP. ! J>PPFSB L?GB@Q TFII >@@BIBO>QB IBPP. ! PJ>IIBO L?GB@Q TFII >@@BIBO>QB JLOB. )K QEFP @>PB, QEB >@@BIBO>QFLK TBgOB Q>IHFKD >?LRQ FP QEB hPILTFKD ALTKi ARB QL AO>D. 4EB PJ>IIBO L?GB@QP TFII PILT ALTK >Q > DOB>QBO O>QB QE>K QEB I>ODBO LKBP.

Exercise 2.5
4>HB > ILLH >Q LRO CLOJRI> CLO AO>D >D>FK: /=,2 1:=.0 = .:0114.409? * >;00/ * >;00/. 4EB C>PQBO >K L?GB@Q JLSBP, QEB DOB>QBO QEB AO>D CLO@B >D>FKPQ FQ. )K C>@Q, >K L?GB@Q KLQ JLSFKD FK T>QBO BUMBOFBK@BP KL AO>D >Q >II. %UM>KA QEB BU>JMIB QL AOLM QEB ?>IIP COLJ AFCCBOBKQ EBFDEQP. (LT ALBP QEFP >CCB@Q QEB AO>D >P QEBV EFQ QEB T>QBO?

Exercise 2.6
4EB CLOJRI> CLO AO>D >IPL FK@IRABA PROC>@B >OB>. #>K VLR @OB>QB > PFJRI>QFLK LC ?LUBP C>IIFKD FKQL T>QBO TFQE > AO>D CLO@B ABMBKABKQ LK QEB IBKDQE LC QEB PFAB EFQQFKD QEB T>QBO?

Exercise 2.7
&IRFA OBPFPQ>K@B ALBP KLQ LKIV TLOH LMMLPFQB QL QEB SBIL@FQV SB@QLO, ?RQ >IPL MBOMBKAF@RI>O QL FQ. 4EFP FP HKLTK >P hIFCQ-FKAR@BA AO>Di >KA TFII @>RPB >K >FOMI>KB TFQE >K >KDIBA TFKD QL OFPB FK >IQFQRAB. 4OV @OB>QFKD > PFJRI>QFLK LC IFCQ.

2.9 Gravitational Attraction


0OL?>?IV QEB JLPQ C>JLRP CLO@B LC >II FP DO>SFQV. 7B ERJ>KP LK B>OQE QEFKH LC DO>SFQV >P >K >MMIB EFQQFKD )P>>@ .BTQLK LK QEB EB>A. 'O>SFQV JB>KP QE>Q PQRCC C>IIP ALTK. "RQ QEFP FP LKIV CIF BUMBOFBK@B LC DO>SFQV. )K QORQE, GRPQ >P QEB B>OQE MRIIP QEB >MMIB QLT>OAP FQ ARB QL > DO>SFQ>QFLK>I CLO@B, QEB >MMIB MRIIP QEB B>OQE >P TBII. 4EB QEFKD FP, QEB B>OQE FP GRPQ PL COB>HFKD ?FD QE>Q FQ LSBOTEBIJP >II QEB LQEBO DO>SFQV F64B?2 2.6 FKQBO>@QFLKP. %SBOV L?GB@Q TFQE J>PP BUBOQP > DO>SFQ>QFLK>I CLO@B LK BSBOV LQEBO L?GB@Q. !KA QEBOB FP > CLOJRI> CLO @>I@RI>QFKD QEB PQOBKDQEP LC QEBPB CLO@BP, >P ABMF@QBA FK &FDROB 2.6.

88

4EB .>QROB LC #LAB (S1.0)

,BQgP BU>JFKB QEFP CLOJRI> > ?FQ JLOB @ILPBIV. j $ OBCBOP QL QEB DO>SFQ>QFLK>I CLO@B, QEB SB@QLO TB RIQFJ>QBIV T>KQ QL @LJMRQB >KA M>PP FKQL LRO ;JJFS$IL=?() CRK@QFLK. j % FP QEB IB=J9FG5@ ;F5J=H5H=CB5@ 7CBGH5BH, TEF@E FK LRO TLOIA BNR>IP 6.67428 U 10 -11 JBQBOP @R?BA MBO HFILDO>J MBO PB@LKA PNR>OBA. 4EFP FP > MOBQQV FJMLOQ>KQ KRJ?BO FC VLRO K>JB FP )P>>@ .BTQLK LO !I?BOQ %FKPQBFK. )QgP KLQ >K FJMLOQ>KQ KRJ?BO FC VLR >OB > 0OL@BPPFKD MOLDO>JJBO. !D>FK, FQgP > @LKPQ>KQ QE>Q TB @>K RPB QL J>HB QEB CLO@BP FK LRO TLOIA TB>HBO LO PQOLKDBO. *RPQ J>HFKD FQ BNR>I QL LKB >KA FDKLOFKD FQ FPKgQ PR@E > QBOOF?IB @ELF@B BFQEBO. j G 1 >KA G 2 >OB QEB J>PPBP LC L?GB@QP 1 >KA 2. !P TB P>T TFQE .BTQLKgP PB@LKA I>T ( F = M A), J>PP FP >IPL PLJBQEFKD TB @LRIA @ELLPB QL FDKLOB. !CQBO >II, PE>MBP AO>TK LK QEB P@OBBK ALKgQ >@QR>IIV E>SB > MEVPF@>I J>PP. (LTBSBO, FC TB HBBM QEBPB S>IRBP, TB @>K @OB>QB JLOB FKQBOBPQFKD PFJRI>QFLKP FK TEF@E h?FDDBOi L?GB@QP BUBOQ > PQOLKDBO DO>SFQ>QFLK>I CLO@B QE>K PJ>IIBO LKBP. j r OBCBOP QL QEB RKFQ SB@QLO MLFKQFKD COLJ L?GB@Q 1 QL L?GB@Q 2. !P TBgII PBB FK > JLJBKQ, TB @>K @LJMRQB QEFP AFOB@QFLK SB@QLO ?V PR?QO>@QFKD QEB IL@>QFLK LC LKB L?GB@Q COLJ QEB LQEBO. j L 2 OBCBOP QL QEB AFPQ>K@B ?BQTBBK QEB QTL L?GB@QP PNR>OBA. ,BQgP Q>HB > JLJBKQ QL QEFKH >?LRQ QEFP > ?FQ JLOB. 7FQE BSBOVQEFKD LK QEB QLM LC QEB CLOJRI>e % , G 1 , G 2 eQEB ?FDDBO FQP S>IRB, QEB PQOLKDBO QEB CLO@B. "FD J>PP, ?FD CLO@B. "FD % , ?FD CLO@B. .LT, TEBK TB AFSFAB ?V PLJBQEFKD, TB E>SB QEB LMMLPFQB. 4EB PQOBKDQE LC QEB CLO@B FP FKSBOPBIV MOLMLOQFLK>I QL QEB AFPQ>K@B PNR>OBA. 4EB :5FH<9F 5K5M >K L?GB@Q FP, QEB K95?9F QEB CLO@B; QEB 7@CG9F , QEB GHFCB;9F . (LMBCRIIV ?V KLT QEB CLOJRI> J>HBP PLJB PBKPB QL RP. 7BgSB ILLHBA >Q > AF>DO>J >KA AFPPB@QBA QEB FKAFSFAR>I @LJMLKBKQP LC QEB CLOJRI>. .LT FQgP QFJB QL CFDROB LRQ ELT TB QO>KPI>QB QEB J>QE FKQL 0OL@BPPFKD @LAB. ,BQgP J>HB QEB CLIILTFKD >PPRJMQFLKP. 7B E>SB QTL L?GB@QP, >KA: 1. %>@E L?GB@Q E>P > IL@>QFLK: .3?=NIL FI=;NCIH1 >KA .3?=NIL FI=;NCIH2 . 2. %>@E L?GB@Q E>P > J>PP: @FI;N G;MM1 >KA @FI;N G;MM2 . 3. 4EBOB FP > S>OF>?IB @FI;N % CLO QEB RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ. 'FSBK QEBPB >PPRJMQFLKP, TB T>KQ QL @LJMRQB .3?=NIL @IL=? , QEB CLO@B LC DO>SFQV. 7BgII AL FQ FK QTL M>OQP. &FOPQ, TBgII @LJMRQB QEB AFOB@QFLK LC QEB CLO@B r FK QEB CLOJRI> >?LSB. 3B@LKA, TBgII @>I@RI>QB QEB PQOBKDQE LC QEB CLO@B >@@LOAFKD QL QEB J>PPBP >KA AFPQ>K@B.

89

#E>MQBO 2. &LO@BP

2BJBJ?BO FK #E>MQBO 1 (PBB M>DB 56), TEBK TB CFDROBA LRQ ELT QL E>SB >K L?GB@Q >@@BIBO>QB QLT>OAP QEB JLRPB? (3BB &FDROB 2.7.) ! SB@QLO FP QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP. 4L J>HB > SB@QLO QE>Q MLFKQP COLJ QEB @FO@IB QL QEB JLRPB, TB PFJMIV PR?QO>@Q LKB MLFKQ COLJ >KLQEBO:
.3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);

F64B?2 2.7

)K LRO @>PB, QEB AFOB@QFLK LC QEB >QQO>@QFLK CLO@B QE>Q L?GB@Q 1 BUBOQP LK L?GB@Q 2 FP BNR>I QL:
.3?=NIL >CL = .3?=NIL.MO<(FI=;NCIH1,FI=;NCIH2); >CL.HILG;FCT?();

$LKgQ CLODBQ QE>Q PFK@B TB T>KQ > RKFQ SB@QLO, > SB@QLO QE>Q QBIIP RP >?LRQ AFOB@QFLK LKIV, TBgII KBBA QL BCFA5@=N9 QEB SB@QLO >CQBO PR?QO>@QFKD QEB IL@>QFLKP. /+, TBgSB DLQ QEB AFOB@QFLK LC QEB CLO@B. .LT TB GRPQ KBBA QL @LJMRQB QEB J>DKFQRAB >KA P@>IB QEB SB@QLO >@@LOAFKDIV.
@FI;N G = (% * G;MM1 * G;MM2) / (>CMN;H=? * >CMN;H=?); >CL.GOFN(G);

4EB LKIV MOL?IBJ FP QE>Q TB ALKgQ HKLT QEB AFPQ>K@B. % , G;MM1 , >KA G;MM2 TBOB >II DFSBKP, ?RQ TBgII KBBA QL >@QR>IIV @LJMRQB AFPQ>K@B ?BCLOB QEB >?LSB @LAB TFII TLOH. $FAKgQ TB GRPQ J>HB > SB@QLO QE>Q MLFKQP >II QEB T>V COLJ LKB IL@>QFLK QL >KLQEBO? 7LRIAKgQ QEB IBKDQE LC QE>Q SB@QLO ?B QEB AFPQ>K@B ?BQTBBK QTL L?GB@QP? 7BII, FC TB >AA GRPQ LKB IFKB LC @LAB >KA DO>? QEB J>DKFQRAB LC QE>Q SB@QLO ?BCLOB KLOJ>IFWFKD FQ, QEBK TBgII E>SB QEB AFPQ>K@B.
F64B?2 2.8

.3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH1,FI=;NCIH2);

The vector that points from one object to another The length (magnitude) of that vector is the distance between the two objects. Use the formula for gravity to compute the strength of the force.

@FI;N >CMN;H=? = @IL=?.G;AHCNO>?();

@FI;N G = (% * G;MM1 * G;MM2) / (>CMN;H=? * >CMN;H=?);

90

4EB .>QROB LC #LAB (S1.0)

@IL=?.HILG;FCT?(); @IL=?.GOFN(G);

Normalize and scale the force vector to the appropriate magnitude.

.LQB QE>Q ) >IPL OBK>JBA QEB .3?=NIL hAFOi >P hCLO@B.i !CQBO >II, TEBK TBgOB CFKFPEBA TFQE QEB @>I@RI>QFLKP, QEB .3?=NIL TB PQ>OQBA TFQE BKAP RM ?BFKD QEB >@QR>I CLO@B SB@QLO TB T>KQBA >II >ILKD. .LT QE>Q TBgSB TLOHBA LRQ QEB J>QE >KA QEB @LAB CLO @>I@RI>QFKD >K >QQO>@QFSB CLO@B (BJRI>QFKD DO>SFQV), TB KBBA QL QROK LRO >QQBKQFLK QL >MMIVFKD QEFP QB@EKFNRB FK QEB @LKQBUQ LC >K >@QR>I 0OL@BPPFKD PHBQ@E. )K %U>JMIB 2.1, VLR J>V OB@>II ELT TB @OB>QBA > PFJMIB +IP?L L?GB@Qe> @I>PP TFQE .3?=NIL gP IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK >P TBII >P >K ;JJFS$IL=?() . ,BQgP Q>HB QEFP BU>@Q @I>PP >KA MRQ FQ FK > PHBQ@E TFQE: j ! PFKDIB +IP?L L?GB@Q. j ! PFKDIB ANNL;=NIL L?GB@Q (> KBT @I>PP QE>Q TFII E>SB > CFUBA IL@>QFLK). 4EB +IP?L L?GB@Q TFII BUMBOFBK@B > DO>SFQ>QFLK>I MRII QLT>OAP QEB ANNL;=NIL L?GB@Q, >P FIIRPQO>QBA FK &FDROB 2.9. 7B @>K PQ>OQ ?V J>HFKD QEB KBT ANNL;=NIL @I>PP SBOV PFJMIBeDFSFKD FQ > IL@>QFLK >KA > J>PP, >ILKD TFQE > CRK@QFLK QL AFPMI>V FQPBIC (QVFKD J>PP QL PFWB).
=F;MM ANNL;=NIL U @FI;N G;MM; .3?=NIL FI=;NCIH; ANNL;=NIL() U FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2); G;MM = 20; W PIC> >CMJF;S() U MNLIE?(0); @CFF(175,200); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,G;MM*2,G;MM*2); W W Our Attractor is a simple object that doesnt move. We just need a mass and a location.

F64B?2 2.9

!KA FK LRO J>FK MOLDO>J, TB @>K >AA >K FKPQ>K@B LC QEB ANNL;=NIL @I>PP.

91

#E>MQBO 2. &LO@BP
+IP?L G; ANNL;=NIL ;; PIC> M?NOJ() U MCT?(640,360); G = H?Q +IP?L(); ; = H?Q ANNL;=NIL(); W PIC> >L;Q() U <;=EALIOH>(255); ;.>CMJF;S(); G.OJ>;N?(); G.>CMJF;S(); W Display Attractor object. Initialize Attractor object.

4EFP FP > DLLA PQOR@QROB: > J>FK MOLDO>J TFQE > +IP?L >KA >K ANNL;=NIL L?GB@Q, >KA > @I>PP QL E>KAIB QEB S>OF>?IBP >KA ?BE>SFLOP LC JLSBOP >KA >QQO>@QLOP. 4EB I>PQ MFB@B LC QEB MRWWIB FP ELT QL DBQ LKB L?GB@Q QL >QQO>@Q QEB LQEBO. (LT AL TB DBQ QEBPB QTL L?GB@QP QL Q>IH QL B>@E LQEBO? 4EBOB >OB > KRJ?BO LC T>VP TB @LRIA AL QEFP. (BOB >OB GRPQ > CBT MLPPF?FIFQFBP.

Task
1. ! CRK@QFLK QE>Q OB@BFSBP ?LQE >K !QQO>@QLO >KA > -LSBO: 2. ! CRK@QFLK FK QEB !QQO>@QLO @I>PP QE>Q OB@BFSBP > -LSBO: 3. ! CRK@QFLK FK QEB -LSBO @I>PP QE>Q OB@BFSBP >K !QQO>@QLO: 4. ! CRK@QFLK FK QEB !QQO>@QLO @I>PP QE>Q OB@BFSBP > -LSBO >KA OBQROKP > 06B@QLO, TEF@E FP QEB >QQO>@QFLK CLO@B. 4E>Q >QQO>@QFLK CLO@B FP QEBK M>PPBA FKQL QEB -LSBO'P >MMIV&LO@B() CRK@QFLK:
;NNL;=NCIH(;,G); ;.;NNL;=N(G); G.;NNL;=N?>1I(;);

Function

.3?=NIL @ = ;.;NNL;=N(G); G.;JJFS$IL=?(@);

>KA PL LK. . . )QgP DLLA QL ILLH >Q > O>KDB LC LMQFLKP CLO J>HFKD L?GB@QP Q>IH QL B>@E LQEBO, >KA VLR @LRIA MOL?>?IV J>HB >ODRJBKQP CLO B>@E LC QEB >?LSB MLPPF?FIFQFBP. )gA IFHB QL >Q IB>PQ AFP@>OA QEB CFOPQ LKB, PFK@B >K L?GB@Q-LOFBKQBA >MMOL>@E FP OB>IIV > JR@E ?BQQBO @ELF@B LSBO >K >O?FQO>OV CRK@QFLK KLQ QFBA QL BFQEBO QEB +IP?L LO ANNL;=NIL @I>PP. 7EBQEBO VLR MF@H LMQFLK 2 LO LMQFLK 3 FP QEB AFCCBOBK@B ?BQTBBK P>VFKD h4EB >QQO>@QLO >QQO>@QP QEB JLSBOi LO h4EB JLSBO FP >QQO>@QBA QL QEB >QQO>@QLO.i .RJ?BO 4 FP OB>IIV JV C>SLOFQB, >Q IB>PQ FK QBOJP LC TEBOB TB

92

4EB .>QROB LC #LAB (S1.0)

>OB FK QEFP ?LLH. !CQBO >II, TB PMBKQ > ILQ LC QFJB TLOHFKD LRQ QEB ;JJFS$IL=?() CRK@QFLK, >KA ) QEFKH LRO BU>JMIBP TFII ?B @IB>OBO FC TB @LKQFKRB TFQE QEB P>JB JBQELALILDV. )K LQEBO TLOAP, TEBOB TB LK@B E>A:
.3?=NIL @ = H?Q .3?=NIL(0.1,0); G.;JJFS$IL=?(@); Made-up force

7B KLT E>SB:
+0;9JEH < = 7.7JJH79J(C); G.;JJFS$IL=?(@); Attraction force between two objects

!KA PL LRO >L;Q() CRK@QFLK @>K KLT ?B TOFQQBK >P:


PIC> >L;Q() U <;=EALIOH>(255); +0;9JEH < = 7.7JJH79J(C); C.7FFBO"EH9;(<); G.OJ>;N?(); ;.>CMJF;S(); G.>CMJF;S(); W Calculate attraction force and apply it.

7BgOB >IJLPQ QEBOB. 3FK@B TB AB@FABA QL MRQ QEB ;NNL;=N() CRK@QFLK FKPFAB LC QEB ANNL;=NIL @I>PP, TBgII KBBA QL >@QR>IIV TOFQB QE>Q CRK@QFLK. 4EB CRK@QFLK KBBAP QL OB@BFSB > +IP?L L?GB@Q >KA OBQROK > .3?=NIL , F.B.:
.3?=NIL ;NNL;=N(+IP?L G) U W

!KA TE>Q DLBP FKPFAB QE>Q CRK@QFLK? !II LC QE>Q KF@B J>QE TB TLOHBA LRQ CLO DO>SFQ>QFLK>I >QQO>@QFLK!
.3?=NIL ;NNL;=N(+IP?L G) U .3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH); @FI;N >CMN;H=? = @IL=?.G;A(); @IL=?.HILG;FCT?(); Whats the forces direction?

93

#E>MQBO 2. &LO@BP

@FI;N MNL?HANB = (% * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?); @IL=?.GOFN(MNL?HANB); L?NOLH @IL=?; W Whats the forces magnitude? Return the force so that it can be applied!

!KA TBgOB ALKB. 3LOQ LC. !IJLPQ. 4EBOBgP LKB PJ>II HFKH TB KBBA QL TLOH LRQ. ,BQgP ILLH >Q QEB >?LSB @LAB >D>FK. 3BB QE>Q PVJ?LI CLO AFSFAB, QEB PI>PE? 7EBKBSBO TB E>SB LKB LC QEBPB, TB KBBA QL >PH LROPBISBP QEB NRBPQFLK: 7E>Q TLRIA E>MMBK FC QEB AFPQ>K@B E>MMBKBA QL ?B > OB>IIV, OB>IIV PJ>II KRJ?BO LO (BSBK TLOPB!) WBOL??! 7BII, TB HKLT TB @>KgQ AFSFAB > KRJ?BO ?V 0, >KA FC TB TBOB QL AFSFAB > KRJ?BO ?V PLJBQEFKD IFHB 0.0001, QE>Q FP QEB BNRFS>IBKQ LC JRIQFMIVFKD QE>Q KRJ?BO ?V 10,000! 9BP, QEFP FP QEB OB>I-TLOIA CLOJRI> CLO QEB PQOBKDQE LC DO>SFQV, ?RQ TB ALKgQ IFSB FK QEB OB>I TLOIA. 7B IFSB FK QEB ,FC79GG=B; TLOIA. !KA FK QEB 0OL@BPPFKD TLOIA, QEB JLSBO @LRIA BKA RM ?BFKD SBOV, SBOV @ILPB QL QEB >QQO>@QLO >KA QEB CLO@B @LRIA ?B@LJB PL PQOLKD QEB JLSBO TLRIA GRPQ CIV T>V LCC QEB P@OBBK. !KA PL TFQE QEFP CLOJRI>, FQgP DLLA CLO RP QL ?B MO>@QF@>I >KA @LKPQO>FK QEB O>KDB LC TE>Q >CMN;H=? @>K >@QR>IIV ?B. ->V?B, KL J>QQBO TEBOB QEB +IP?L >@QR>IIV FP, TB PELRIA KBSBO @LKPFABO FQ IBPP QE>K 5 MFUBIP LO JLOB QE>K 25 MFUBIP >T>V COLJ QEB >QQO>@QLO.
>CMN;H=? = =IHMNL;CH(>CMN;H=?,5,25);

&LO QEB P>JB OB>PLK QE>Q TB KBBA QL @LKPQO>FK QEB JFKFJRJ AFPQ>K@B, FQgP RPBCRI CLO RP QL AL QEB P>JB TFQE QEB J>UFJRJ. !CQBO >II, FC QEB JLSBO TBOB QL ?B, P>V, 500 MFUBIP COLJ QEB >QQO>@QLO (KLQ RKOB>PLK>?IB), TBgA ?B AFSFAFKD QEB CLO@B ?V 250,000. 4E>Q CLO@B JFDEQ BKA RM ?BFKD PL TB>H QE>Q FQgP >IJLPQ >P FC TBgOB KLQ >MMIVFKD FQ >Q >II. .LT, FQgP OB>IIV RM QL VLR QL AB@FAB TE>Q ?BE>SFLOP VLR T>KQ. "RQ FK QEB @>PB LC, h) T>KQ OB>PLK>?IB-ILLHFKD >QQO>@QFLK QE>Q FP KBSBO >?PROAIV TB>H LO PQOLKD,i QEBK @LKPQO>FKFKD QEB AFPQ>K@B FP > DLLA QB@EKFNRB. /RO +IP?L @I>PP E>PKgQ @E>KDBA >Q >II, PL IBQgP GRPQ ILLH >Q QEB J>FK MOLDO>J >KA QEB ANNL;=NIL @I>PP >P > TELIB, >AAFKD > S>OF>?IB % CLO QEB RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ. (/K QEB TB?PFQB, VLRgII CFKA QE>Q QEFP BU>JMIB >IPL E>P @LAB QE>Q >IILTP VLR QL JLSB QEB ANNL;=NIL L?GB@Q TFQE QEB JLRPB.)

94

4EB .>QROB LC #LAB (S1.0)

Example 2.6: Attraction


+IP?L G; ANNL;=NIL ;; PIC> M?NOJ() U MCT?(640,360); G = H?Q +IP?L(); ; = H?Q ANNL;=NIL(); W PIC> >L;Q() U <;=EALIOH>(255); .3?=NIL @IL=? = ;.;NNL;=N(G); G.;JJFS$IL=?(@IL=?); G.OJ>;N?(); ;.>CMJF;S(); G.>CMJF;S(); W =F;MM ANNL;=NIL U @FI;N G;MM; .3?=NIL FI=;NCIH; @FI;N %; ANNL;=NIL() U FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2); G;MM = 20; % = 0.4; W .3?=NIL ;NNL;=N(+IP?L G) U .3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH); @FI;N >CMN;H=? = @IL=?.G;A(); >CMN;H=? = =IHMNL;CH(>CMN;H=?,5.0,25.0); Remember, we need to constrain the distance so that our circle doesnt spin out of control. Apply the attraction force from the Attractor on the Mover. A Mover and an Attractor

@IL=?.HILG;FCT?(); @FI;N MNL?HANB = (% * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?); @IL=?.GOFN(MNL?HANB); L?NOLH @IL=?; W PIC> >CMJF;S() U MNLIE?(0); @CFF(175,200); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,G;MM*2,G;MM*2); W W

95

#E>MQBO 2. &LO@BP

!KA TB @LRIA, LC @LROPB, BUM>KA QEFP BU>JMIB RPFKD >K >OO>V QL FK@IRAB J>KV +IP?L L?GB@QP, GRPQ >P TB AFA TFQE COF@QFLK >KA AO>D:

Example 2.7: Attraction with many Movers


+IP?L89 GIP?LM = H?Q +IP?L8109; ANNL;=NIL ;; PIC> M?NOJ() U MCT?(400,400); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,2),L;H>IG(QC>NB),L;H>IG(B?CABN)); W ; = H?Q ANNL;=NIL(); W PIC> >L;Q() U <;=EALIOH>(255); ;.>CMJF;S(); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U .3?=NIL @IL=? = ;.;NNL;=N(GIP?LM8C9); GIP?LM8C9.;JJFS$IL=?(@IL=?); GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); W W We calculate an attraction force for each Mover object. Each Mover is initialized randomly. Now we have 10 Movers!

96

4EB .>QROB LC #LAB (S1.0)

Exercise 2.8
)K QEB BU>JMIB >?LSB, TB E>SB > PVPQBJ (F.B. >OO>V) LC +IP?L L?GB@QP >KA LKB ANNL;=NIL L?GB@Q. "RFIA >K BU>JMIB QE>Q E>P PVPQBJP LC ?LQE JLSBOP >KA >QQO>@QLOP. 7E>Q FC VLR J>HB QEB >QQO>@QLOP FKSFPF?IB? #>K VLR @OB>QB > M>QQBOK/ABPFDK COLJ QEB QO>FIP LC L?GB@QP JLSFKD >OLRKA >QQO>@QLOP? 3BB QEB -BQOLMLM $BKFJ MOLGB@Q ?V #I>VQLK #R?FQQ >KA 4LJ #>OABK (EQQM://MOL@BPPFKD.LOD/BUEF?FQFLK/TLOHP/JBQOLMLM/) CLO >K BU>JMIB.

Exercise 2.9
)QgP TLOQE KLQFKD QE>Q DO>SFQ>QFLK>I >QQO>@QFLK FP > JLABI TB @>K CLIILT QL ABSBILM LRO LTK CLO@BP. 4EFP @E>MQBO FPKgQ PRDDBPQFKD QE>Q VLR PELRIA BU@IRPFSBIV @OB>QB PHBQ@EBP QE>Q RPB DO>SFQ>QFLK>I >QQO>@QFLK. 2>QEBO, VLR PELRIA ?B QEFKHFKD @OB>QFSBIV >?LRQ ELT QL ABPFDK VLRO LTK ORIBP QL AOFSB QEB ?BE>SFLO LC L?GB@QP. &LO BU>JMIB, TE>Q E>MMBKP FC VLR ABPFDK > CLO@B QE>Q FP TB>HBO QEB @ILPBO FQ DBQP >KA PQOLKDBO QEB C>OQEBO FQ DBQP? /O TE>Q FC VLR ABPFDK VLRO >QQO>@QLO QL >QQO>@Q C>O>T>V L?GB@QP, ?RQ OBMBI @ILPB LKBP?

2.10 Everything Attracts (or Repels) Everything


(LMBCRIIV, VLR CLRKA FQ EBIMCRI QE>Q TB PQ>OQBA TFQE > PFJMIB P@BK>OFLe CB9 C6>97H 5HHF57HG 5BCH<9F C6>97H e>KA JLSBA LK QL CB9 C6>97H 5HHF57HG A5BM C6>97HG . (LTBSBO, FQgP IFHBIV QE>Q VLR >OB DLFKD QL CFKA VLROPBIC FK > PIFDEQIV JLOB @LJMIBU PFQR>QFLK: A5BM C6>97HG 5HHF57H 957< CH<9F. )K LQEBO TLOAP, BSBOV L?GB@Q FK > DFSBK PVPQBJ >QQO>@QP BSBOV LQEBO L?GB@Q FK QE>Q PVPQBJ (BU@BMQ CLO FQPBIC). 7BgSB OB>IIV ALKB >IJLPQ >II LC QEB TLOH CLO QEFP >IOB>AV. ,BQgP @LKPFABO > 0OL@BPPFKD PHBQ@E TFQE >K >OO>V LC +IP?L L?GB@QP:

97

#E>MQBO 2. &LO@BP
+IP?L89 GIP?LM = H?Q +IP?L8109; PIC> M?NOJ() U MCT?(400,400); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,2),L;H>IG(QC>NB),L;H>IG(B?CABN)); W W PIC> >L;Q() U <;=EALIOH>(255); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); W W

4EB >L;Q() CRK@QFLK FP TEBOB TB KBBA QL TLOH PLJB J>DF@. #ROOBKQIV, TBgOB P>VFKD: hCLO BSBOV JLSBO C , RMA>QB >KA AFPMI>V VLROPBIC.i .LT TE>Q TB KBBA QL P>V FP: hCLO BSBOV JLSBO C , ?B >QQO>@QBA QL BSBOV LQEBO JLSBO D , >KA RMA>QB >KA AFPMI>V VLROPBIC.i 4L AL QEFP, TB KBBA QL KBPQ > PB@LKA ILLM.
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U @IL (CHN D = 0; D < GIP?LM.F?HANB; D++) U .3?=NIL @IL=? = GIP?LM8D9.;NNL;=N(GIP?LM8C9); GIP?LM8C9.;JJFS$IL=?(@IL=?); W GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); W For every Mover, check every Mover!

)K QEB MOBSFLRP BU>JMIB, TB E>A >K ANNL;=NIL L?GB@Q TFQE > CRK@QFLK K>JBA ;NNL;=N() . .LT, PFK@B TB E>SB JLSBOP >QQO>@QFKD JLSBOP, >II TB KBBA QL AL FP @LMV QEB ;NNL;=N() CRK@QFLK FKQL QEB +IP?L @I>PP.
=F;MM +IP?L U // AFF NB? INB?L MNO@@ Q? B;> <?@IL? JFOM. . .

98

4EB .>QROB LC #LAB (S1.0)

.3?=NIL ;NNL;=N(+IP?L G) U .3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH); @FI;N >CMN;H=? = @IL=?.G;A(); >CMN;H=? = =IHMNL;CH(>CMN;H=?,5.0,25.0); @IL=?.HILG;FCT?();

The Mover now knows how to attract another Mover.

@FI;N MNL?HANB = (% * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?); @IL=?.GOFN(MNL?HANB); L?NOLH @IL=?; W W

/C @LROPB, QEBOBgP LKB PJ>II MOL?IBJ. 7EBK TB >OB ILLHFKD >Q BSBOV JLSBO C >KA BSBOV JLSBO D , >OB TB /+ TFQE QEB QFJBP QE>Q C BNR>IP D ? &LO BU>JMIB, PELRIA JLSBO #3 >QQO>@Q JLSBO #3? 4EB >KPTBO, LC @LROPB, FP KL. )C QEBOB >OB CFSB L?GB@QP, TB LKIV T>KQ JLSBO #3 QL >QQO>@Q 0, 1, 2, >KA 4, PHFMMFKD FQPBIC. !KA PL, TB CFKFPE QEFP BU>JMIB ?V >AAFKD > PFJMIB @LKAFQFLK>I PQ>QBJBKQ QL PHFM >MMIVFKD QEB CLO@B TEBK F BNR>IP G.

Example 2.8: Mutual attraction


+IP?L89 GIP?LM = H?Q +IP?L8209; @FI;N A = 0.4; PIC> M?NOJ() U MCT?(400,400); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,2),L;H>IG(QC>NB),L;H>IG(B?CABN)); W W PIC> >L;Q() U <;=EALIOH>(255); @IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U @IL (CHN D = 0; D < GIP?LM.F?HANB; D++) U

99

#E>MQBO 2. &LO@BP

C@ (C != D) U

Dont attract yourself!

.3?=NIL @IL=? = GIP?LM8D9.;NNL;=N(GIP?LM8C9); GIP?LM8C9.;JJFS$IL=?(@IL=?); W W GIP?LM8C9.OJ>;N?(); GIP?LM8C9.>CMJF;S(); W W

Exercise 2.10
#E>KDB QEB >QQO>@QFLK CLO@B FK %U>JMIB 2.8 QL > OBMRIPFLK CLO@B. #>K VLR @OB>QB >K BU>JMIB FK TEF@E >II LC QEB +IP?L L?GB@QP >OB >QQO>@QBA QL QEB JLRPB, ?RQ OBMBI B>@E LQEBO? 4EFKH >?LRQ ELT VLR KBBA QL ?>I>K@B QEB OBI>QFSB PQOBKDQE LC QEB CLO@BP >KA ELT QL JLPQ BCCB@QFSBIV RPB AFPQ>K@B FK VLRO CLO@B @>I@RI>QFLKP.

The Ecosystem Project


Step 2 Exercise: Incorporate the concept of forces into your ecosystem. Try introducing other elements into the environment (food, a predator) for the creature to interact with. Does the creature experience attraction or repulsion to things in its world? Can you think more abstractly and design forces based on the creatures desires or goals?

100

4EB .>QROB LC #LAB (S1.0)

Chapter 3. Oscillation
I*?64<;<:2A?F 6@ . @6;2 <3 A52 A6:2@.J Anonymous
)K #E>MQBOP 1 >KA 2, TB @>OBCRIIV TLOHBA LRQ >K L?GB@Q-LOFBKQBA PQOR@QROB QL J>HB PLJBQEFKD JLSB LK QEB P@OBBK, RPFKD QEB @LK@BMQ LC > SB@QLO QL OBMOBPBKQ IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK AOFSBK ?V CLO@BP FK QEB BKSFOLKJBKQ. 7B @LRIA JLSB PQO>FDEQ COLJ EBOB FKQL QLMF@P PR@E >P M>OQF@IB PVPQBJP, PQBBOFKD CLO@BP, DOLRM ?BE>SFLOP, BQ@. )C TB AFA QE>Q, ELTBSBO, TBgA PHFM >K FJMLOQ>KQ >OB> LC J>QEBJ>QF@P QE>Q TBgOB DLFKD QL KBBA: ?=42:9:80?=D, LO QEB J>QEBJ>QF@P LC QOF>KDIBP, PMB@FCF@>IIV OFDEQ QOF>KDIBP. 4OFDLKLJBQOV FP DLFKD QL DFSB RP > ILQ LC QLLIP. 7BgII DBQ QL QEFKH >?LRQ >KDIBP >KA >KDRI>O SBIL@FQV >KA >@@BIBO>QFLK. 4OFD TFII QB>@E RP >?LRQ QEB PFKB >KA @LPFKB CRK@QFLKP, TEF@E TEBK RPBA MOLMBOIV @>K VFBIA >K KF@B B>PB-FK, B>PB-LRQ T>SB M>QQBOK. )QgP DLFKD QL >IILT RP QL @>I@RI>QB JLOB @LJMIBU CLO@BP FK >K BKSFOLKJBKQ QE>Q FKSLISBP >KDIBP, PR@E >P > MBKARIRJ PTFKDFKD LO > ?LU PIFAFKD ALTK >K FK@IFKB. 3L QEFP @E>MQBO FP > ?FQ LC > JFPEJ>PE. 7BgII PQ>OQ TFQE QEB ?>PF@P LC >KDIBP FK 0OL@BPPFKD >KA @LSBO J>KV QOFDLKLJBQOF@ QLMF@P, QVFKD FQ >II FKQL CLO@BP >Q QEB BKA. !KA ?V Q>HFKD QEFP ?OB>H KLT, TBgII >IPL M>SB QEB T>V CLO JLOB >AS>K@BA BU>JMIBP QE>Q OBNRFOB QOFD I>QBO FK QEFP ?LLH.

3.1 Angles
/+. "BCLOB TB @>K AL >KV LC QEFP PQRCC, TB KBBA QL J>HB PROB TB RKABOPQ>KA TE>Q FQ JB>KP QL ?B >K >KDIB FK 0OL@BPPFKD. )C VLR E>SB BUMBOFBK@B TFQE 0OL@BPPFKD, VLRgSB RKALR?QBAIV BK@LRKQBOBA QEFP FPPRB TEFIB RPFKD QEB LIN;N?() CRK@QFLK QL OLQ>QB >KA PMFK L?GB@QP.

101

#E>MQBO 3. /P@FII>QFLK

4EB CFOPQ LOABO LC ?RPFKBPP FP QL @LSBO =,/4,9> >KA /02=00>. 9LRgOB MOL?>?IV C>JFIF>O TFQE QEB @LK@BMQ LC >K >KDIB FK /02=00>. ! CRII OLQ>QFLK DLBP COLJ 0 QL 360 ABDOBBP. 90 ABDOBBP (> OFDEQ >KDIB) FP 1/4QE LC 360, PELTK ?BILT >P QTL MBOMBKAF@RI>O IFKBP.

F64B?2 3.1

)QgP C>FOIV FKQRFQFSB CLO RP QL QEFKH LC >KDIBP FK QBOJP LC ABDOBBP. &LO BU>JMIB, QEB PNR>OB FK &FDROB 3.2 FP OLQ>QBA 45 ABDOBBP >OLRKA FQP @BKQBO.

F64B?2 3.3

0OL@BPPFKD, ELTBSBO, OBNRFOBP >KDIBP QL ?B PMB@FCFBA FK =,/4,9>. ! O>AF>K FP > RKFQ LC JB>PROBJBKQ CLO >KDIBP ABCFKBA ?V QEB O>QFL LC QEB IBKDQE LC QEB >O@ LC > @FO@IB QL QEB O>AFRP LC QE>Q @FO@IB. /KB O>AF>K FP QEB >KDIB >Q TEF@E QE>Q O>QFL BNR>IP LKB (PBB &FDROB 3.1). 180 ABDOBBP = 0) O>AF>KP, 360 ABDOBBP = 2*0) O>AF>KP, 90 ABDOBBP = 0)/2 O>AF>KP, BQ@.

102

4EB .>QROB LC #LAB (S1.0)

F64B?2 3.3

4EB CLOJRI> QL @LKSBOQ COLJ ABDOBBP QL O>AF>KP FP: O>AF>KP = 2 * 0) * (ABDOBBP / 360) 4E>KHCRIIV, FC TB MOBCBO QL QEFKH FK ABDOBBP ?RQ @LAB TFQE O>AF>KP, 0OL@BPPFKD J>HBP QEFP B>PV. 4EB L;>C;HM() CRK@QFLK TFII >RQLJ>QF@>IIV @LKSBOQ S>IRBP COLJ ABDOBBP QL O>AF>KP, >KA QEB @LKPQ>KQP .' >KA 14-:.' MOLSFAB @LKSBKFBKQ >@@BPP QL QEBPB @LJJLKIV RPBA KRJ?BOP (BNRFS>IBKQ QL 180 >KA 360 ABDOBBP, OBPMB@QFSBIV). 4EB CLIILTFKD @LAB, CLO BU>JMIB, TFII OLQ>QB PE>MBP ?V 60 ABDOBBP.
@FI;N ;HAF? = L;>C;HM(60); LIN;N?(;HAF?);

)C VLR >OB KLQ C>JFIF>O TFQE ELT OLQ>QFLK FP FJMIBJBKQBA FK 0OL@BPPFKD, ) TLRIA PRDDBPQ QEFP QRQLOF>I: 0OL@BPPFKD - 4O>KPCLOJ 2$ (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/QO>KPCLOJ2A/).

What is PI?
4EB J>QEBJ>QF@>I @LKPQ>KQ MF (LO a) FP > OB>I KRJ?BO ABCFKBA >P QEB O>QFL LC > @FO@IBgP @FO@RJCBOBK@B (QEB AFPQ>K@B >OLRKA QEB MBOFJBQBO) QL FQP AF>JBQBO (> PQO>FDEQ IFKB QE>Q M>PPBP QEOLRDE QEB @FO@IBgP @BKQBO). )Q FP BNR>I QL >MMOLUFJ>QBIV 3.14159 >KA @>K ?B >@@BPPBA FK 0OL@BPPFKD TFQE QEB ?RFIQ-FK S>OF>?IB .' .

103

#E>MQBO 3. /P@FII>QFLK

Exercise 3.1
2LQ>QB > ?>QLK-IFHB L?GB@Q (PBB ?BILT) >OLRKA FQP @BKQBO RPFKD NL;HMF;N?() >KA LIN;N?() .

3.2 Angular Motion


2BJBJ?BO >II QEFP PQRCC?
BE97J?ED = BE97J?ED + L;BE9?JO L;BE9?JO = L;BE9?JO + 799;B;H7J?ED

4EB PQRCC TB ABAF@>QBA >IJLPQ >II LC #E>MQBOP 1 >KA 2 QL? 7BII, TB @>K >MMIV BU>@QIV QEB P>JB ILDF@ QL > OLQ>QFKD L?GB@Q.
7D=B; = 7D=B; + 7D=KB7H L;BE9?JO 7D=KB7H L;BE9?JO = 7D=KB7H L;BE9?JO + 7D=KB7H 799;B;H7J?ED

)K C>@Q, QEB >?LSB FP >@QR>IIV PFJMIBO QE>K TE>Q TB PQ>OQBA TFQE ?B@>RPB >K >KDIB FP > G75@5F NR>KQFQVe> PFKDIB KRJ?BO, KLQ > SB@QLO! 5PFKD QEB >KPTBO COLJ %UBO@FPB 3.1 >?LSB, IBQgP P>V TB T>KQBA QL OLQ>QB > ?>QLK FK 0OL@BPPFKD ?V PLJB >KDIB. 7B TLRIA E>SB @LAB IFHB:
NL;HMF;N?(QC>NB/2,B?CABN/2); LIN;N?(;HAF?); FCH?(-50,0,50,0); ?FFCJM?(50,0,8,8); ?FFCJM?(-50,0,8,8);

!AAFKD FK LRO MOFK@FMIBP LC JLQFLK ?OFKDP RP QL QEB CLIILTFKD BU>JMIB.

104

4EB .>QROB LC #LAB (S1.0)

Example 3.1: Angular motion using rotate()


@FI;N ;HAF? = 0; @FI;N ;3?FI=CNS = 0; @FI;N ;A==?F?L;NCIH = 0.001; PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); @CFF(175); MNLIE?(0); L?=N+I>?(!#,1#/); NL;HMF;N?(QC>NB/2,B?CABN/2); LIN;N?(;HAF?); FCH?(-50,0,50,0); ?FFCJM?(50,0,8,8); ?FFCJM?(-50,0,8,8); ;3?FI=CNS += ;A==?F?L;NCIH; Angular equivalent of velocity.add(acceleration); Angular equivalent of location.add(velocity); Location Velocity Acceleration

;HAF? += ;3?FI=CNS; W

4EB ?>QLK PQ>OQP LKP@OBBK TFQE KL OLQ>QFLK >KA QEBK PMFKP C>PQBO >KA C>PQBO >P QEB >KDIB LC OLQ>QFLK >@@BIBO>QBP. 4EFP FAB> @>K ?B FK@LOMLO>QBA FKQL LRO +IP?L L?GB@Q. &LO BU>JMIB, TB @>K >AA QEB S>OF>?IBP OBI>QBA QL >KDRI>O JLQFLK QL LRO +IP?L .

105

#E>MQBO 3. /P@FII>QFLK
=F;MM +IP?L U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N G;MM; @FI;N ;HAF? = 0; @FI;N ;3?FI=CNS = 0; @FI;N ;A==?F?L;NCIH = 0;

!KA QEBK FK OJ>;N?() , TB RMA>QB ?LQE IL@>QFLK >KA >KDIB >@@LOAFKD QL QEB P>JB >IDLOFQEJ!
PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); Regular old-fashioned motion

;3?FI=CNS += ;A==?F?L;NCIH; ;HAF? += ;3?FI=CNS; ;==?F?L;NCIH.GOFN(0); W

Newfangled angular motion

/C @LROPB, CLO >KV LC QEFP QL J>QQBO, TB >IPL TLRIA KBBA QL OLQ>QB QEB L?GB@Q TEBK AFPMI>VFKD FQ.
PIC> >CMJF;S() U MNLIE?(0); @CFF(175,200); L?=N+I>?(!#,1#/); JOMB+;NLCR(); pushMatrix() and popMatrix() are necessary so that the rotation of this shape doesnt affect the rest of our world. Set the origin at the shapes location. Rotate by the angle.

NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S); LIN;N?(;HAF?); L?=N(0,0,G;MM*16,G;MM*16); JIJ+;NLCR(); W

.LT, FC TB TBOB QL >@QR>IIV DL >EB>A >KA ORK QEB >?LSB @LAB, TB TLRIAKgQ PBB >KVQEFKD KBT. 4EFP FP ?B@>RPB QEB >KDRI>O >@@BIBO>QFLK (@FI;N ;A==?F?L;NCIH = 0; ) FP FKFQF>IFWBA QL WBOL. &LO QEB L?GB@Q QL OLQ>QB, TB KBBA QL DFSB FQ >K >@@BIBO>QFLK! #BOQ>FKIV, TB @LRIA E>OA@LAB FK > AFCCBOBKQ KRJ?BO.
@FI;N ;A==?F?L;NCIH = 0.01;

106

4EB .>QROB LC #LAB (S1.0)

(LTBSBO, TB @>K MOLAR@B > JLOB FKQBOBPQFKD OBPRIQ ?V AVK>JF@>IIV >PPFDKFKD >K >KDRI>O >@@BIBO>QFLK >@@LOAFKD QL CLO@BP FK QEB BKSFOLKJBKQ. .LT, TB @LRIA EB>A C>O ALTK QEFP OL>A, QOVFKD QL JLABI QEB MEVPF@P LC >KDRI>O >@@BIBO>QFLK RPFKD QEB @LK@BMQP LC QLONRB (EQQM://BK.TFHFMBAF>.LOD/TFHF/4LONRB) >KA JLJBKQ LC FKBOQF> (EQQM://BK.TFHFMBAF>.LOD/TFHF/ -LJBKQ=LC=FKBOQF>). .BSBOQEBIBPP, QEFP IBSBI LC PFJRI>QFLK FP ?BVLKA QEB P@LMB LC QEFP ?LLH. (7B TFII PBB JLOB >?LRQ JLABIFKD >KDRI>O >@@BIBO>QFLK TFQE > MBKARIRJ I>QBO FK QEFP @E>MQBO, >P TBII >P ILLH >Q ELT "LU2$ OB>IFPQF@>IIV JLABIP OLQ>QFLK>I JLQFLK FK #E>MQBO 5.) &LO KLT, > NRF@H >KA AFOQV PLIRQFLK TFII AL. 7B @>K MOLAR@B OB>PLK>?IB OBPRIQP ?V PFJMIV @>I@RI>QFKD >KDRI>O >@@BIBO>QFLK >P > CRK@QFLK LC QEB L?GB@QgP >@@BIBO>QFLK SB@QLO. (BOBgP LKB PR@E BU>JMIB:
;A==?F?L;NCIH = ;==?F?L;NCIH.R;

9BP, QEFP FP @LJMIBQBIV >O?FQO>OV. "RQ FQ ALBP AL PLJBQEFKD. )C QEB L?GB@Q FP >@@BIBO>QFKD QL QEB OFDEQ, FQP >KDRI>O OLQ>QFLK >@@BIBO>QBP FK > @IL@HTFPB AFOB@QFLK; >@@BIBO>QFLK QL QEB IBCQ OBPRIQP FK > @LRKQBO@IL@HTFPB OLQ>QFLK. /C @LROPB, FQgP FJMLOQ>KQ QL QEFKH >?LRQ P@>IB FK QEFP @>PB. 4EB L @LJMLKBKQ LC QEB >@@BIBO>QFLK SB@QLO JFDEQ ?B > NR>KQFQV QE>QgP QLL I>ODB, @>RPFKD QEB L?GB@Q QL PMFK FK > T>V QE>Q ILLHP OFAF@RILRP LO RKOB>IFPQF@. 3L AFSFAFKD QEB L @LJMLKBKQ ?V PLJB S>IRB, LO MBOE>MP @LKPQO>FKFKD QEB >KDRI>O SBIL@FQV QL > OB>PLK>?IB O>KDB, @LRIA OB>IIV EBIM. (BOBgP QEB BKQFOB OJ>;N?() CRK@QFLK TFQE QEBPB QTB>HP >AABA.

Example 3.2: Forces with (arbitrary) angular motion


PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); ;A==?F?L;NCIH = ;==?F?L;NCIH.R / 10.0; ;3?FI=CNS += ;A==?F?L;NCIH; Calculate angular acceleration according to accelerations horizontal direction and magnitude.

107

#E>MQBO 3. /P@FII>QFLK

;3?FI=CNS = =IHMNL;CH(;3?FI=CNS,-0.1,0.1); ;HAF? += ;3?FI=CNS; ;==?F?L;NCIH.GOFN(0); W

Use constrain() to ensure that angular velocity doesnt spin out of control.

Exercise 3.2
3QBM 1: #OB>QB > PFJRI>QFLK TEBOB L?GB@QP >OB PELQ LRQ LC > @>KKLK. %>@E L?GB@Q PELRIA BUMBOFBK@B > PRAABK CLO@B TEBK PELQ (GRPQ LK@B) >P TBII >P DO>SFQV (>IT>VP MOBPBKQ). 3QBM 2: !AA OLQ>QFLK QL QEB L?GB@Q QL JLABI FQP PMFK >P FQ FP PELQ COLJ QEB @>KKLK. (LT OB>IFPQF@ @>K VLR J>HB FQ ILLH?

3.3 Trigonometry
) QEFKH FQ J>V ?B QFJB. 7BgSB ILLHBA >Q >KDIBP, TBgSB PMRK >K L?GB@Q. )QgP QFJB CLO: GC<75<HC5. 9BP, GC<75<HC5. 4EFP PBBJFKDIV KLKPBKPF@>I TLOA FP >@QR>IIV QEB CLRKA>QFLK CLO > ILQ LC @LJMRQBO DO>MEF@P TLOH. ! ?>PF@ RKABOPQ>KAFKD LC QOFDLKLJBQOV FP BPPBKQF>I FC VLR T>KQ QL @>I@RI>QB >K >KDIB, CFDROB LRQ QEB AFPQ>K@B ?BQTBBK MLFKQP, TLOH TFQE @FO@IBP, >O@P, LO IFKBP. !KA GC<75<HC5 FP > JKBJLKF@ ABSF@B (>I?BFQ > PLJBTE>Q >?PROA LKB) CLO TE>Q QEB QOFDLKLJBQOF@ CRK@QFLKP PFKB, @LPFKB, >KA Q>KDBKQ JB>K.

F64B?2 3.4

j >:3 : PFKB = LMMLPFQB / EVMLQBKRPB j .,3: @LPFKB = >AG>@BKQ / EVMLQBKRPB j ?:,: Q>KDBKQ = LMMLPFQB / >AG>@BKQ

108

4EB .>QROB LC #LAB (S1.0)

4>HB > ILLH >Q &FDROB 3.4 >D>FK. 4EBOBgP KL KBBA QL JBJLOFWB FQ, ?RQ J>HB PROB VLR CBBI @LJCLOQ>?IB TFQE FQ. $O>T FQ >D>FK VLROPBIC. .LT IBQgP AO>T FQ > PIFDEQIV AFCCBOBKQ T>V (&FDROB 3.5). 3BB ELT TB @OB>QB > OFDEQ QOF>KDIB LRQ LC > SB@QLO? 4EB SB@QLO >OOLT FQPBIC FP QEB EVMLQBKRPB >KA QEB @LJMLKBKQP LC QEB SB@QLO ( R >KA S ) >OB QEB PFABP LC QEB QOF>KDIB. 4EB >KDIB FP >K >AAFQFLK>I JB>KP CLO PMB@FCVFKD QEB SB@QLOgP AFOB@QFLK (LO hEB>AFKDi).

F64B?2 3.5

"B@>RPB QEB QOFDLKLJBQOF@ CRK@QFLKP >IILT RP QL BPQ>?IFPE > OBI>QFLKPEFM ?BQTBBK QEB @LJMLKBKQP LC > SB@QLO >KA FQP AFOB@QFLK + J>DKFQRAB, QEBV TFII MOLSB SBOV RPBCRI QEOLRDELRQ QEFP ?LLH. 7BgII ?BDFK ?V ILLHFKD >Q >K BU>JMIB QE>Q OBNRFOBP QEB Q>KDBKQ CRK@QFLK.

3.4 Pointing in the Direction of Movement


,BQgP DL >II QEB T>V ?>@H QL %U>JMIB 1.10, TEF@E CB>QROBP > +IP?L L?GB@Q >@@BIBO>QFKD QLT>OAP QEB JLRPB.

9LR JFDEQ KLQF@B QE>Q >IJLPQ >II LC QEB PE>MBP TBgSB ?BBK AO>TFKD PL C>O >OB @FO@IBP. 4EFP FP @LKSBKFBKQ CLO > KRJ?BO LC OB>PLKP, LKB LC TEF@E FP QE>Q TB ALKgQ E>SB QL @LKPFABO QEB NRBPQFLK LC OLQ>QFLK. 2LQ>QB > @FO@IB >KA, TBII, FQ ILLHP BU>@QIV QEB P>JB. (LTBSBO, QEBOB @LJBP > QFJB FK >II JLQFLK MOLDO>JJBOPg IFSBP TEBK QEBV T>KQ QL AO>T PLJBQEFKD LK QEB P@OBBK QE>Q MLFKQP FK QEB AFOB@QFLK LC JLSBJBKQ. 0BOE>MP VLR >OB AO>TFKD >K >KQ, LO > @>O, LO > PM>@BPEFM. !KA TEBK TB P>V "MLFKQ FK QEB AFOB@QFLK LC JLSBJBKQ," TE>Q TB >OB OB>IIV P>VFKD FP hOLQ>QB >@@LOAFKD QL QEB SBIL@FQV SB@QLO.i 6BIL@FQV FP > SB@QLO, TFQE >K R >KA > S @LJMLKBKQ, ?RQ QL OLQ>QB FK 0OL@BPPFKD TB KBBA >K >KDIB, FK O>AF>KP. ,BQgP AO>T LRO QOFDLKLJBQOV AF>DO>J LKB JLOB QFJB, TFQE >K L?GB@QgP SBIL@FQV SB@QLO (&FDROB 3.6).

109

#E>MQBO 3. /P@FII>QFLK

/+. 7B HKLT QE>Q QEB ABCFKFQFLK LC Q>KDBKQ FP:

velocity tangent(angle) = velocity y x


4EB MOL?IBJ TFQE QEB >?LSB FP QE>Q TB HKLT SBIL@FQV, ?RQ TB ALKgQ HKLT QEB >KDIB. 7B E>SB QL PLISB CLO QEB >KDIB. 4EFP FP TEBOB > PMB@F>I CRK@QFLK HKLTK >P =BJ9FG9 H5B;9BH @LJBP FK, PLJBQFJBP OBCBOOBA QL >P 5F7H5B;9BH LO H5B -1. (4EBOB FP >IPL >K =BJ9FG9 G=B9 >KA >K =BJ9FG9 7CG=B9.)
F64B?2 3.6

)C QEB Q>KDBKQ LC PLJB S>IRB ; BNR>IP PLJB S>IRB < , QEBK QEB FKSBOPB Q>KDBKQ LC < BNR>IP ; . &LO BU>JMIB: =: H<9B H5B;9BH(5) = 6 5 = 5F7H5B;9BH(6)

3BB ELT QE>Q FP QEB FKSBOPB? 4EB >?LSB KLT >IILTP RP QL PLISB CLO QEB >KDIB: =: H<9B H5B;9BH(5B;@9) = J9@C7=HMM / J9@C7=HML 5B;@9 = 5F7H5B;9BH(J9@C7=HMM / J9@C7=HML)

.LT QE>Q TB E>SB QEB CLOJRI>, IBQgP PBB TEBOB FQ PELRIA DL FK LRO JLSBOgP >CMJF;S() CRK@QFLK. .LQF@B QE>Q FK 0OL@BPPFKD, QEB CRK@QFLK CLO >O@Q>KDBKQ FP @>IIBA ;N;H() .
PIC> >CMJF;S() U @FI;N ;HAF? = ;N;H(P?FI=CNS.S/P?FI=CNS.R); MNLIE?(0); @CFF(175); JOMB+;NLCR(); L?=N+I>?(!#,1#/); NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S); LIN;N?(;HAF?); L?=N(0,0,30,10); JIJ+;NLCR(); W Rotate according to that angle. Solve for angle by using atan().

.LT QEB >?LSB @LAB FP MOBQQV A>OK @ILPB, >KA >IJLPQ TLOHP. 7B PQFII E>SB > ?FD MOL?IBJ, QELRDE. ,BQgP @LKPFABO QEB QTL SBIL@FQV SB@QLOP ABMF@QBA ?BILT.

110

4EB .>QROB LC #LAB (S1.0)

F64B?2 3.7

4ELRDE PRMBOCF@F>IIV PFJFI>O, QEB QTL SB@QLOP MLFKQ FK NRFQB AFCCBOBKQ AFOB@QFLKPeLMMLPFQB AFOB@QFLKP, FK C>@Q! (LTBSBO, FC TB TBOB QL >MMIV LRO CLOJRI> QL PLISB CLO QEB >KDIB QL B>@E SB@QLOk
01 Y 7D=B; = 7J7D(-4/3) = 7J7D(-1.25) = -0.9272952 H7:?7DI = -53 :;=H;;I 02 Y 7D=B; = 7J7D(4/-3) = 7J7D(-1.25) = -0.9272952 H7:?7DI = -53 :;=H;;I

kTB DBQ QEB P>JB >KDIB CLO B>@E SB@QLO. 4EFP @>KgQ ?B OFDEQ CLO ?LQE; QEB SB@QLOP MLFKQ FK LMMLPFQB AFOB@QFLKP! 4EB QEFKD FP, QEFP FP > MOBQQV @LJJLK MOL?IBJ FK @LJMRQBO DO>MEF@P. 2>QEBO QE>K PFJMIV RPFKD ;N;H() >ILKD TFQE > ?RK@E LC @LKAFQFLK>I PQ>QBJBKQP QL >@@LRKQ CLO MLPFQFSB/KBD>QFSB P@BK>OFLP, 0OL@BPPFKD (>ILKD TFQE MOBQQV JR@E >II MOLDO>JJFKD BKSFOLKJBKQP) E>P > KF@B CRK@QFLK @>IIBA ;N;H2() QE>Q ALBP FQ CLO VLR.

Example 3.3: Pointing in the direction of motion


PIC> >CMJF;S() U @FI;N ;HAF? = ;N;H2(P?FI=CNS.S,P?FI=CNS.R); MNLIE?(0); @CFF(175); JOMB+;NLCR(); L?=N+I>?(!#,1#/); NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S); Using atan2() to account for all possible directions

111

#E>MQBO 3. /P@FII>QFLK

LIN;N?(;HAF?); L?=N(0,0,30,10); JIJ+;NLCR(); W

Rotate according to that angle.

4L PFJMIFCV QEFP BSBK CROQEBO, QEB .3?=NIL @I>PP FQPBIC MOLSFABP > CRK@QFLK @>IIBA B?;>CHA() , TEF@E Q>HBP @>OB LC @>IIFKD ;N;H2() CLO VLR PL VLR @>K DBQ QEB 2$ AFOB@QFLK >KDIB, FK O>AF>KP, CLO >KV 0OL@BPPFKD .3?=NIL .
@FI;N ;HAF? = P?FI=CNS.B?;>CHA(); The easiest way to do this!

Exercise 3.3
#OB>QB > PFJRI>QFLK LC > SBEF@IB QE>Q VLR @>K AOFSB >OLRKA QEB P@OBBK RPFKD QEB >OOLT HBVP: IBCQ >OOLT >@@BIBO>QBP QEB @>O QL QEB IBCQ, OFDEQ QL QEB OFDEQ. 4EB @>O PELRIA MLFKQ FK QEB AFOB@QFLK FK TEF@E FQ FP @ROOBKQIV JLSFKD.

3.5 Polar vs. Cartesian Coordinates


!KV QFJB TB AFPMI>V > PE>MB FK 0OL@BPPFKD, TB E>SB QL PMB@FCV > MFUBI IL@>QFLK, > PBQ LC R >KA S @LLOAFK>QBP. 4EBPB @LLOAFK>QBP >OB HKLTK >P C,=?0>4,9 .::=/49,?0>, K>JBA CLO 2BK[ $BP@>OQBP, QEB &OBK@E J>QEBJ>QF@F>K TEL ABSBILMBA QEB FAB>P ?BEFKA #>OQBPF>K PM>@B. !KLQEBO RPBCRI @LLOAFK>QB PVPQBJ HKLTK >P ;:7,= .::=/49,?0> ABP@OF?BP > MLFKQ FK PM>@B >P >K >KDIB LC OLQ>QFLK >OLRKA QEB LOFDFK >KA > O>AFRP COLJ QEB LOFDFK. 4EFKHFKD >?LRQ QEFP FK QBOJP LC > SB@QLO: #>OQBPF>K @LLOAFK>QBeQEB L, M @LJMLKBKQP LC > SB@QLO 0LI>O @LLOAFK>QBeQEB J>DKFQRAB (IBKDQE) >KA AFOB@QFLK (>KDIB) LC > SB@QLO 0OL@BPPFKDgP AO>TFKD CRK@QFLKP, ELTBSBO, ALKgQ RKABOPQ>KA MLI>O @LLOAFK>QBP. 7EBKBSBO TB T>KQ QL AFPMI>V PLJBQEFKD FK 0OL@BPPFKD, TB E>SB QL PMB@FCV IL@>QFLKP >P (L,M) #>OQBPF>K @LLOAFK>QBP. (LTBSBO, PLJBQFJBP FQ FP > DOB>Q AB>I JLOB @LKSBKFBKQ CLO RP QL QEFKH FK MLI>O @LLOAFK>QBP TEBK ABPFDKFKD. (>MMFIV CLO RP, TFQE QOFDLKLJBQOV TB @>K @LKSBOQ ?>@H >KA CLOQE ?BQTBBK MLI>O >KA #>OQBPF>K, TEF@E >IILTP RP QL ABPFDK TFQE TE>QBSBO @LLOAFK>QB PVPQBJ TB E>SB FK JFKA ?RQ >IT>VP AO>T TFQE #>OQBPF>K @LLOAFK>QBP.

112

4EB .>QROB LC #LAB (S1.0)

F64B?2 3.8: *52 G?228 92AA2? L (A52A.) 6@ <3A2; B@21 A< 12;<A2 .; .;492. )6;02 . =<9.? 0<<?16;.A2 6@ 0<;C2;A6<;.99F ?232??21 A< .@ (?, L), D2K99 B@2 A52A. .@ . C.?6./92 ;.:2 D52; ?232??6;4 A< .; .;492. I?D;(J>;J7) = O/H X X O = H * I?D;(J>;J7) N = H * 9EI?D;(J>;J7)

9EI?D;(J>;J7) = N/H

&LO BU>JMIB, FC L FP 75 >KA NB?N; FP 45 ABDOBBP (LO 0)/4 O>AF>KP), TB @>K @>I@RI>QB R >KA S >P ?BILT. 4EB CRK@QFLKP CLO PFKB >KA @LPFKB FK 0OL@BPPFKD >OB MCH() >KA =IM() , OBPMB@QFSBIV. 4EBV B>@E Q>HB LKB >ODRJBKQ, >K >KDIB JB>PROBA FK O>AF>KP.
@FI;N L = 75; @FI;N NB?N; = .' / 4; @FI;N R = L * =IM(NB?N;); @FI;N S = L * MCH(NB?N;); Converting from polar (r,theta) to Cartesian (x,y)

4EFP QVMB LC @LKSBOPFLK @>K ?B RPBCRI FK @BOQ>FK >MMIF@>QFLKP. &LO BU>JMIB, QL JLSB > PE>MB >ILKD > @FO@RI>O M>QE RPFKD #>OQBPF>K @LLOAFK>QBP FP KLQ PL B>PV. 7FQE MLI>O @LLOAFK>QBP, LK QEB LQEBO E>KA, FQgP PFJMIB: FK@OBJBKQ QEB >KDIB! (BOBgP ELT FQ FP ALKB TFQE DIL?>I S>OF>?IBP L >KA NB?N; .

113

#E>MQBO 3. /P@FII>QFLK

Example 3.4: Polar to Cartesian


@FI;N L = 75; @FI;N NB?N; = 0; PIC> M?NOJ() U MCT?(640,360); <;=EALIOH>(255); W PIC> >L;Q() U @FI;N R = L * =IM(NB?N;); @FI;N S = L * MCH(NB?N;); Polar coordinates (r,theta) are converted to Cartesian (x,y) for use in the ellipse() function.

HI0NLIE?(); @CFF(0); ?FFCJM?(R+QC>NB/2, S+B?CABN/2, 16, 16); NB?N; += 0.01; W

114

4EB .>QROB LC #LAB (S1.0)

Exercise 3.4
5PFKD %U>JMIB 3.4 >P > ?>PFP, AO>T > PMFO>I M>QE. 3Q>OQ FK QEB @BKQBO >KA JLSB LRQT>OAP. .LQB QE>Q QEFP @>K ?B ALKB ?V LKIV @E>KDFKD LKB IFKB LC @LAB >KA >AAFKD LKB IFKB LC @LAB!

Exercise 3.5
3FJRI>QB QEB PM>@BPEFM FK QEB D>JB !PQBOLFAP. )K @>PB VLR >OBKgQ C>JFIF>O TFQE !PQBOLFAP, EBOB FP > ?OFBC ABP@OFMQFLK: ! PM>@BPEFM (OBMOBPBKQBA >P > QOF>KDIB) CIL>QP FK QTL AFJBKPFLK>I PM>@B. 4EB IBCQ >OOLT HBV QROKP QEB PM>@BPEFM @LRKQBO@IL@HTFPB, QEB OFDEQ >OOLT HBV, @IL@HTFPB. 4EB N HBV >MMIFBP > hQEORPQi CLO@B FK QEB AFOB@QFLK QEB PM>@BPEFM FP MLFKQFKD.

115

#E>MQBO 3. /P@FII>QFLK

3.6 Oscillation Amplitude and Period


!OB VLR >J>WBA VBQ? 7BgSB PBBK PLJB MOBQQV DOB>Q RPBP LC Q>KDBKQ (CLO CFKAFKD QEB >KDIB LC > SB@QLO) >KA PFKB >KA @LPFKB (CLO @LKSBOQFKD COLJ MLI>O QL #>OQBPF>K @LLOAFK>QBP). 7B @LRIA PQLM OFDEQ EBOB >KA ?B P>QFPCFBA. "RQ TBgOB KLQ DLFKD QL. 4EFP FP LKIV QEB ?BDFKKFKD. 7E>Q PFKB >KA @LPFKB @>K AL CLO VLR DLBP ?BVLKA J>QEBJ>QF@>I CLOJRI>P >KA OFDEQ QOF>KDIBP. ,BQgP Q>HB > ILLH >Q > DO>ME LC QEB PFKB CRK@QFLK, TEBOB M = G=B9(L).

F64B?2 3.9: F = @6;2(E)

9LRgII KLQF@B QE>Q QEB LRQMRQ LC QEB PFKB CRK@QFLK FP > PJLLQE @ROSB >IQBOK>QFKD ?BQTBBK d1 >KA 1. 4EFP QVMB LC > ?BE>SFLO FP HKLTK >P :>.477,?4:9, > MBOFLAF@ JLSBJBKQ ?BQTBBK QTL MLFKQP. 0IR@HFKD > DRFQ>O PQOFKD, PTFKDFKD > MBKARIRJ, ?LRK@FKD LK > MLDL PQF@HeQEBPB >OB >II BU>JMIBP LC LP@FII>QFKD JLQFLK. !KA PL TB E>MMFIV AFP@LSBO QE>Q TB @>K PFJRI>QB LP@FII>QFLK FK > 0OL@BPPFKD PHBQ@E ?V >PPFDKFKD QEB LRQMRQ LC QEB PFKB CRK@QFLK QL >K L?GB@QgP IL@>QFLK. .LQB QE>Q QEFP TFII CLIILT QEB P>JB JBQELALILDV TB >MMIFBA QL 0BOIFK KLFPB FK QEB )KQOLAR@QFLK (PBB M>DB 17). ,BQgP ?BDFK TFQE > OB>IIV ?>PF@ P@BK>OFL. 7B T>KQ > @FO@IB QL LP@FII>QB COLJ QEB IBCQ PFAB QL QEB OFDEQ PFAB LC > 0OL@BPPFKD TFKALT.

116

4EB .>QROB LC #LAB (S1.0)

4EFP FP TE>Q FP HKLTK >P >48;70 3,=8:94. 8:?4:9 (LO, QL ?B C>K@FBO, hQEB MBOFLAF@ PFKRPLFA>I LP@FII>QFLK LC >K L?GB@Qi). )QgP DLFKD QL ?B > PFJMIB MOLDO>J QL TOFQB, ?RQ ?BCLOB TB DBQ FKQL QEB @LAB, IBQgP C>JFIF>OFWB LROPBISBP TFQE PLJB LC QEB QBOJFKLILDV LC LP@FII>QFLK (>KA T>SBP). 3FJMIB E>OJLKF@ JLQFLK @>K ?B BUMOBPPBA >P >KV IL@>QFLK (FK LRO @>PB, QEB R IL@>QFLK) >P > CRK@QFLK LC QFJB, TFQE QEB CLIILTFKD QTL BIBJBKQP: j A8;74?@/0: 4EB AFPQ>K@B COLJ QEB @BKQBO LC JLQFLK QL BFQEBO BUQOBJB j %0=4:/: 4EB >JLRKQ LC QFJB FQ Q>HBP CLO LKB @LJMIBQB @V@IB LC JLQFLK ,LLHFKD >Q QEB DO>ME LC PFKB (&FDROB 3.9), TB @>K PBB QE>Q QEB >JMIFQRAB FP 1 >KA QEB MBOFLA FP 14-:.' ; QEB LRQMRQ LC PFKB KBSBO OFPBP >?LSB 1 LO ?BILT -1; >KA BSBOV 14-:.' O>AF>KP (LO 360 ABDOBBP) QEB T>SB M>QQBOK OBMB>QP. .LT, FK QEB 0OL@BPPFKD TLOIA TB IFSB FK, TE>Q FP >JMIFQRAB >KA TE>Q FP MBOFLA? !JMIFQRAB @>K ?B JB>PROBA O>QEBO B>PFIV FK MFUBIP. )K QEB @>PB LC > TFKALT 200 MFUBIP TFAB, TB TLRIA LP@FII>QB COLJ QEB @BKQBO 100 MFUBIP QL QEB OFDEQ >KA 100 MFUBIP QL QEB IBCQ. 4EBOBCLOB:
@FI;N ;GJFCNO>? = 100; Our amplitude is measured in pixels.

,9F=C8 FP QEB >JLRKQ LC QFJB FQ Q>HBP CLO LKB @V@IB, ?RQ TE>Q FP QFJB FK LRO 0OL@BPPFKD TLOIA? ) JB>K, @BOQ>FKIV TB @LRIA P>V TB T>KQ QEB @FO@IB QL LP@FII>QB BSBOV QEOBB PB@LKAP. !KA TB @LRIA QO>@H QEB JFIIFPB@LKAPeRPFKD GCFFCM() eFK 0OL@BPPFKD >KA @LJB RM TFQE >K BI>?LO>QB >IDLOFQEJ CLO LP@FII>QFKD >K L?GB@Q >@@LOAFKD QL OB>I-TLOIA QFJB. "RQ CLO RP, OB>I-TLOIA QFJB ALBPKgQ OB>IIV J>QQBO. 4EB OB>I JB>PROB LC QFJB FK 0OL@BPPFKD FP FK CO>JBP. 4EB LP@FII>QFKD JLQFLK PELRIA OBMB>Q BSBOV 30 CO>JBP, LO 50 CO>JBP, LO 1000 CO>JBP, BQ@.
@FI;N J?LCI> = 120; Our period is measured in frames (our unit of time for animation).

/K@B TB E>SB QEB >JMIFQRAB >KA MBOFLA, FQgP QFJB QL TOFQB > CLOJRI> QL @>I@RI>QB U >P > CRK@QFLK LC QFJB, TEF@E TB KLT HKLT FP QEB @ROOBKQ CO>JB @LRKQ.
@FI;N R = ;GJFCNO>? * =IM(14-:.' * @L;G?!IOHN / J?LCI>);

117

#E>MQBO 3. /P@FII>QFLK

,BQgP AFPPB@Q QEB CLOJRI> > ?FQ JLOB >KA QOV QL RKABOPQ>KA B>@E @LJMLKBKQ. 4EB CFOPQ FP MOL?>?IV QEB B>PFBPQ. 7E>QBSBO @LJBP LRQ LC QEB @LPFKB CRK@QFLK TB JRIQFMIV ?V >JMIFQRAB. 7B HKLT QE>Q @LPFKB TFII LP@FII>QB ?BQTBBK -1 >KA 1. )C TB Q>HB QE>Q S>IRB >KA JRIQFMIV FQ ?V >JMIFQRAB QEBK TBgII DBQ QEB ABPFOBA OBPRIQ: > S>IRB LP@FII>QFKD ?BQTBBK ->JMIFQRAB >KA >JMIFQRAB. (.LQB: QEFP FP >IPL > MI>@B TEBOB TB @LRIA RPB 0OL@BPPFKDgP G;J() CRK@QFLK QL J>M QEB LRQMRQ LC @LPFKB QL > @RPQLJ O>KDB.) .LT, IBQgP ILLH >Q TE>Q FP FKPFAB QEB @LPFKB CRK@QFLK:
.1*6+% * <H7C;CEKDJ / F;H?E:

7E>QgP DLFKD LK EBOB? ,BQgP PQ>OQ TFQE TE>Q TB HKLT. 7B HKLT QE>Q @LPFKB TFII OBMB>Q BSBOV 2*0) O>AF>KPeF.B. FQ TFII PQ>OQ >Q 0 >KA OBMB>Q >Q 2*0), 4*0), 6*0), BQ@. )C QEB MBOFLA FP 120, QEBK TB T>KQ QEB LP@FII>QFKD JLQFLK QL OBMB>Q TEBK QEB @L;G?!IOHN FP >Q 120 CO>JBP, 240 CO>JBP, 360 CO>JBP, BQ@. @L;G?!IOHN FP OB>IIV QEB LKIV S>OF>?IB; FQ PQ>OQP >Q 0 >KA @LRKQP RMT>OA. ,BQgP Q>HB > ILLH >Q TE>Q QEB CLOJRI> VFBIAP TFQE QELPB S>IRBP.

frameCount 0 60 120 240 BQ@.

frameCount / period 0 0.5 1 2

TWO_PI * frameCount / period 0 0) 47/=0) 2 * 47/=0) (LO 4* 0))

@L;G?!IOHN AFSFABA ?V J?LCI> QBIIP RP ELT J>KV @V@IBP TBgSB @LJMIBQBAe>OB TB E>ICT>V

QEOLRDE QEB CFOPQ @V@IB? (>SB TB @LJMIBQBA QTL @V@IBP? "V JRIQFMIVFKD QE>Q KRJ?BO ?V 14-:.' , TB DBQ QEB OBPRIQ TB T>KQ, PFK@B 14-:.' FP QEB KRJ?BO LC O>AF>KP OBNRFOBA CLO LKB @LPFKB (LO PFKB) QL @LJMIBQB LKB @V@IB. 7O>MMFKD QEFP >II RM, EBOBgP QEB 0OL@BPPFKD BU>JMIB QE>Q LP@FII>QBP QEB R IL@>QFLK LC > @FO@IB TFQE >K >JMIFQRAB LC 100 MFUBIP >KA > MBOFLA LC 120 CO>JBP.

118

4EB .>QROB LC #LAB (S1.0)

Example 3.5: Simple Harmonic Motion


PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); @FI;N J?LCI> = 120; @FI;N ;GJFCNO>? = 100; @FI;N R = ;GJFCNO>? * =IM(14-:.' * @L;G?!IOHN / J?LCI>); MNLIE?(0); @CFF(175); NL;HMF;N?(QC>NB/2,B?CABN/2); FCH?(0,0,R,0); ?FFCJM?(R,0,20,20); W Calculating horizontal location according to the formula for simple harmonic motion

)QgP >IPL TLOQE JBKQFLKFKD QEB QBOJ 1=0<@09.D : QEB KRJ?BO LC @V@IBP MBO QFJB RKFQ. &OBNRBK@V FP BNR>I QL 1 AFSFABA ?V J?LCI> . )C QEB MBOFLA FP 120 CO>JBP, QEBK LKIV 1/120QE LC > @V@IB FP @LJMIBQBA FK LKB CO>JB, >KA PL COBNRBK@V = 1/120. )K QEB >?LSB BU>JMIB, TB PFJMIV @ELPB QL ABCFKB QEB O>QB LC LP@FII>QFLK FK QBOJP LC MBOFLA >KA QEBOBCLOB AFA KLQ KBBA > S>OF>?IB CLO COBNRBK@V.

Exercise 3.6
5PFKD QEB PFKB CRK@QFLK, @OB>QB > PFJRI>QFLK LC > TBFDEQ (PLJBQFJBP OBCBOOBA QL >P > h?L?i) QE>Q E>KDP COLJ > PMOFKD COLJ QEB QLM LC QEB TFKALT. 5PB QEB G;J() CRK@QFLK QL @>I@RI>QB QEB SBOQF@>I IL@>QFLK LC QEB ?L?. ,>QBO FK QEFP @E>MQBO, TBgII PBB ELT QL OB@OB>QB QEFP P>JB PFJRI>QFLK ?V JLABIFKD QEB CLO@BP LC > PMOFKD >@@LOAFKD QL (LLHBgP I>T.

3.7 Oscillation with Angular Velocity


!K RKABOPQ>KAFKD LC QEB @LK@BMQP LC LP@FII>QFLK, >JMIFQRAB, >KA COBNRBK@V/MBOFLA FP LCQBK OBNRFOBA FK QEB @LROPB LC PFJRI>QFKD OB>I-TLOIA ?BE>SFLOP. (LTBSBO, QEBOB FP > PIFDEQIV B>PFBO T>V QL OBTOFQB QEB >?LSB BU>JMIB TFQE QEB P>JB OBPRIQ. ,BQgP Q>HB LKB JLOB ILLH >Q LRO LP@FII>QFLK CLOJRI>:
@FI;N R = ;GJFCNO>? * =IM(14-:.' * @L;G?!IOHN / J?LCI>);

!KA IBQgP OBTOFQB FQ > PIFDEQIV AFCCBOBKQ T>V:

119

#E>MQBO 3. /P@FII>QFLK
@FI;N R = ;GJFCNO>? * =IM ( MIG? P;FO? NB;N CH=L?G?HNM MFIQFS );

)C TB @>OB >?LRQ MOB@FPBIV ABCFKFKD QEB MBOFLA LC LP@FII>QFLK FK QBOJP LC CO>JBP LC >KFJ>QFLK, TB JFDEQ KBBA QEB CLOJRI> QEB T>V TB CFOPQ TOLQB FQ, ?RQ TB @>K GRPQ >P B>PFIV OBTOFQB LRO BU>JMIB RPFKD QEB @LK@BMQ LC >KDRI>O SBIL@FQV (>KA >@@BIBO>QFLK) COLJ PB@QFLK 3.2 (PBB M>DB 104). !PPRJFKD:
@FI;N ;HAF? = 0; @FI;N ;3?FI=CNS = 0.05;

FK >L;Q() , TB @>K PFJMIV P>V:


;HAF? += ;3?FI=CNS; @FI;N R = ;GJFCNO>? * =IM(;HAF?);

;HAF? FP LRO hPLJB S>IRB QE>Q FK@OBJBKQP PILTIV.i

Example 3.6: Simple Harmonic Motion II


@FI;N ;HAF? = 0; @FI;N ;3?FI=CNS = 0.05; PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); @FI;N ;GJFCNO>? = 100; @FI;N R = ;GJFCNO>? * =IM(;HAF?); ;HAF? += ;3?FI=CNS; ?FFCJM?+I>?(!#,1#/); MNLIE?(0); @CFF(175); NL;HMF;N?(QC>NB/2,B?CABN/2); FCH?(0,0,R,0); ?FFCJM?(R,0,20,20); W Using the concept of angular velocity to increment an angle variable

*RPQ ?B@>RPB TBgOB KLQ OBCBOBK@FKD FQ AFOB@QIV ALBPKgQ JB>K QE>Q TBgSB BIFJFK>QBA QEB @LK@BMQ LC J?LCI> . !CQBO >II, QEB DOB>QBO QEB >KDRI>O SBIL@FQV, QEB C>PQBO QEB @FO@IB TFII LP@FII>QB (QEBOBCLOB ILTBOFKD QEB MBOFLA). )K C>@Q, QEB KRJ?BO LC QFJBP FQ Q>HBP QL >AA RM QEB >KDRI>O SBIL@FQV QL DBQ QL 14-:.' FP QEB MBOFLA LO:
F;H?E: = .1*6+% / 7D=KB7H L;BE9?JO

120

4EB .>QROB LC #LAB (S1.0)

,BQgP BUM>KA QEFP BU>JMIB > ?FQ JLOB >KA @OB>QB >K -M=CFF;NIL @I>PP. !KA IBQgP >PPRJB TB T>KQ QEB LP@FII>QFLK QL E>MMBK >ILKD ?LQE QEB U->UFP (>P >?LSB) >KA QEB V->UFP. 4L AL QEFP, TBgII KBBA QTL >KDIBP, QTL >KDRI>O SBIL@FQFBP, >KA QTL >JMIFQRABP (LKB CLO B>@E >UFP). !KLQEBO MBOCB@Q LMMLOQRKFQV CLO .3?=NIL !

Example 3.7: Oscillator objects


=F;MM -M=CFF;NIL .3?=NIL ;HAF?; .3?=NIL P?FI=CNS; .3?=NIL ;GJFCNO>?; -M=CFF;NIL() U ;HAF? = H?Q .3?=NIL(); P?FI=CNS = H?Q .3?=NIL(L;H>IG(-0.05,0.05),L;H>IG(-0.05,0.05)); ;GJFCNO>? = H?Q .3?=NIL(L;H>IG(QC>NB/2),L;H>IG(B?CABN/2)); W PIC> IM=CFF;N?() U ;HAF?.;>>(P?FI=CNS); W PIC> >CMJF;S() U Oscillating on the x-axis Oscillating on the y-axis Random velocities and amplitudes U Using a PVector to track two angles!

@FI;N R = MCH(;HAF?.R)*;GJFCNO>?.R; @FI;N S = MCH(;HAF?.S)*;GJFCNO>?.S; JOMB+;NLCR(); NL;HMF;N?(QC>NB/2,B?CABN/2); MNLIE?(0); @CFF(175);

121

#E>MQBO 3. /P@FII>QFLK

FCH?(0,0,R,S); ?FFCJM?(R,S,16,16); JIJ+;NLCR(); W W

Drawing the Oscillator as a line connecting a circle

Exercise 3.7
4OV FKFQF>IFWFKD B>@E -M=CFF;NIL L?GB@Q TFQE SBIL@FQFBP >KA >JMIFQRABP QE>Q >OB KLQ O>KALJ QL @OB>QB PLJB PLOQ LC OBDRI>O M>QQBOK. #>K VLR J>HB QEB LP@FII>QLOP >MMB>O QL ?B QEB IBDP LC > FKPB@Q-IFHB @OB>QROB?

Exercise 3.8
)K@LOMLO>QB >KDRI>O >@@BIBO>QFLK FKQL QEB -M=CFF;NIL L?GB@Q.

3.8 Waves
)C VLRgOB P>VFKD QL VLROPBIC, h5J, QEFP FP >II DOB>Q >KA BSBOVQEFKD, ?RQ TE>Q ) OB>IIV T>KQ FP QL AO>T > T>SB LKP@OBBK,i TBII, QEBK, QEB QFJB E>P @LJB. 4EB QEFKD FP, TBgOB >?LRQ 90% QEBOB. 7EBK TB LP@FII>QB > PFKDIB @FO@IB RM >KA ALTK >@@LOAFKD QL QEB PFKB CRK@QFLK, TE>Q TB >OB ALFKD FP ILLHFKD >Q > PFKDIB MLFKQ >ILKD QEB U->UFP LC > T>SB M>QQBOK. 7FQE > IFQQIB M>K>@EB >KA > @IL ILLM, TB @>K MI>@B > TELIB ?RK@E LC QEBPB LP@FII>QFKD @FO@IBP KBUQ QL B>@E LQEBO.

4EFP T>SV M>QQBOK @LRIA ?B RPBA FK QEB ABPFDK LC QEB ?LAV LO >MMBKA>DBP LC > @OB>QROB, >P TBII >P QL PFJRI>QB > PLCQ PROC>@B (PR@E >P T>QBO). (BOB, TBgOB DLFKD QL BK@LRKQBO QEB P>JB NRBPQFLKP LC >JMIFQRAB (EBFDEQ LC M>QQBOK) >KA MBOFLA. )KPQB>A LC MBOFLA OBCBOOFKD QL QFJB, ELTBSBO, PFK@B TBgOB ILLHFKD >Q QEB CRII T>SB, TB

122

4EB .>QROB LC #LAB (S1.0)

@>K Q>IH >?LRQ MBOFLA >P QEB TFAQE (FK MFUBIP) LC > CRII T>SB @V@IB. !KA GRPQ >P TFQE PFJMIB LP@FII>QFLK, TB E>SB QEB LMQFLK LC @LJMRQFKD QEB T>SB M>QQBOK >@@LOAFKD QL > MOB@FPB MBOFLA LO PFJMIV CLIILTFKD QEB JLABI LC >KDRI>O SBIL@FQV. ,BQgP DL TFQE QEB PFJMIBO @>PB, >KDRI>O SBIL@FQV. 7B HKLT TB KBBA QL PQ>OQ TFQE >K >KDIB, >K >KDRI>O SBIL@FQV, >KA >K >JMIFQRAB:
@FI;N ;HAF? = 0; @FI;N ;HAF?3?F = 0.2; @FI;N ;GJFCNO>? = 100;

4EBK TBgOB DLFKD QL ILLM QEOLRDE >II LC QEB R S>IRBP TEBOB TB T>KQ QL AO>T > MLFKQ LC QEB T>SB. ,BQgP P>V BSBOV 24 MFUBIP CLO KLT. )K QE>Q ILLM, TBgOB DLFKD QL T>KQ QL AL QEOBB QEFKDP: 1. #>I@RI>QB QEB V IL@>QFLK >@@LOAFKD QL >JMIFQRAB >KA PFKB LC QEB >KDIB. 2. $O>T > @FO@IB >Q QEB (L,M) IL@>QFLK. 3. )K@OBJBKQ QEB >KDIB >@@LOAFKD QL >KDRI>O SBIL@FQV.
@IL (CHN R = 0; R <= QC>NB; R += 24) U @FI;N S = ;GJFCNO>?*MCH(;HAF?); 1) Calculate the y location according to amplitude and sine of the angle. 2) Draw a circle at the (x,y) location.

?FFCJM?(R,S+B?CABN/2,48,48);

;HAF? += ;HAF?3?F; W

3) Increment the angle according to angular velocity.

,BQgP ILLH >Q QEB OBPRIQP TFQE AFCCBOBKQ S>IRBP CLO ;HAF?3?F :

.;492+29 = 0.05

.;492+29 = 0.2

.;492+29 = 0.4

123

#E>MQBO 3. /P@FII>QFLK

.LQF@B ELT, >IQELRDE TBgOB KLQ MOB@FPBIV @LJMRQFKD QEB MBOFLA LC QEB T>SB, QEB EFDEBO QEB >KDRI>O SBIL@FQV, QEB PELOQBO QEB MBOFLA. )QgP >IPL TLOQE KLQFKD QE>Q >P QEB MBOFLA ?B@LJBP PELOQBO, FQ ?B@LJBP JLOB >KA JLOB AFCCF@RIQ QL J>HB LRQ QEB T>SB FQPBIC >P QEB AFPQ>K@B ?BQTBBK QEB FKAFSFAR>I MLFKQP FK@OB>PBP. /KB LMQFLK TB E>SB FP QL RPB <?ACH0B;J?() >KA ?H>0B;J?() QL @LKKB@Q QEB MLFKQP TFQE > IFKB.

Example 3.8: Static wave drawn as a continuous line


@FI;N ;HAF? = 0; @FI;N ;HAF?3?F = 0.2; @FI;N ;GJFCNO>? = 100; MCT?(400,200); <;=EALIOH>(255); MNLIE?(0); MNLIE?4?CABN(2); HI$CFF(); <?ACH0B;J?(); @IL (CHN R = 0; R <= QC>NB; R += 5) U @FI;N S = G;J(MCH(;HAF?),-1,1,0,B?CABN); Heres an example of using the map() function instead. With beginShape() and endShape(), you call vertex() to set all the vertices of your shape.

P?LN?R(R,S); ;HAF? +=;HAF?3?F; W ?H>0B;J?();

9LR J>V E>SB KLQF@BA QE>Q QEB >?LSB BU>JMIB FP PQ>QF@. 4EB T>SB KBSBO @E>KDBP, KBSBO RKARI>QBP. 4EFP >AAFQFLK>I PQBM FP > ?FQ QOF@HV. 9LRO CFOPQ FKPQFK@Q JFDEQ ?B QL P>V: h(BV, KL MOL?IBJ, TBgII GRPQ IBQ QEBQ> ?B > DIL?>I S>OF>?IB >KA IBQ FQ FK@OBJBKQ COLJ LKB @V@IB QEOLRDE >L;Q() QL >KLQEBO.i 7EFIB FQgP > KF@B QELRDEQ, FQ ALBPKgQ TLOH. )C VLR ILLH >Q QEB T>SB, QEB OFDEQE>KA BADB ALBPKgQ J>Q@E QEB IBCQE>KA; TEBOB FQ BKAP FK LKB @V@IB LC >L;Q() @>KgQ ?B TEBOB FQ PQ>OQP FK QEB KBUQ. )KPQB>A, TE>Q TB KBBA QL AL FP E>SB > S>OF>?IB ABAF@>QBA BKQFOBIV QL QO>@HFKD TE>Q S>IRB LC 124

4EB .>QROB LC #LAB (S1.0)

>KDIB QEB T>SB PELRIA PQ>OQ TFQE. 4EFP >KDIB (TEF@E TBgII @>II MN;LNAHAF? ) FK@OBJBKQP TFQE FQP LTK >KDRI>O SBIL@FQV.

Example 3.9: The Wave


@FI;N MN;LNAHAF? = 0; @FI;N ;HAF?3?F = 0.1; PIC> M?NOJ() U MCT?(400,200); W PIC> >L;Q() U <;=EALIOH>(255); @FI;N ;HAF? = MN;LNAHAF?; In order to move the wave, we start at a different theta value each frame. startAngle += 0.02;

@IL (CHN R = 0; R <= QC>NB; R += 24) U @FI;N S = G;J(MCH(;HAF?),-1,1,0,B?CABN); MNLIE?(0); @CFF(0,50); ?FFCJM?(R,S,48,48); ;HAF? += ;HAF?3?F; W W

125

#E>MQBO 3. /P@FII>QFLK

Exercise 3.9
4OV RPFKD QEB 0BOIFK KLFPB CRK@QFLK FKPQB>A LC PFKB LO @LPFKB TFQE QEB >?LSB BU>JMIB.

Exercise 3.10
%K@>MPRI>QB QEB >?LSB BU>JMIBP FKQL > 4;P? @I>PP >KA @OB>QB > PHBQ@E QE>Q AFPMI>VP QTL T>SBP (TFQE AFCCBOBKQ >JMIFQRABP/MBOFLAP) >P FK QEB P@OBBKPELQ ?BILT. -LSB ?BVLKA MI>FK @FO@IBP >KA IFKBP >KA QOV SFPR>IFWFKD QEB T>SB FK > JLOB @OB>QFSB T>V.

Exercise 3.11
-LOB @LJMIBU T>SBP @>K ?B MOLAR@BA ?V QEB S>IRBP LC JRIQFMIB T>SBP QLDBQEBO. #OB>QB > PHBQ@E QE>Q FJMIBJBKQP QEFP, >P FK QEB P@OBBKPELQ ?BILT.

126

4EB .>QROB LC #LAB (S1.0)

3.9 Trigonometry and Forces: The Pendulum


$L VLR JFPP .BTQLKgP I>TP LC JLQFLK? ) HKLT ) PROB AL. 7BII, IR@HV CLO VLR, FQgP QFJB QL ?OFKD FQ >II ?>@H ELJB. !CQBO >II, FQgP ?BBK KF@B IB>OKFKD >?LRQ QOF>KDIBP >KA Q>KDBKQP >KA T>SBP, ?RQ OB>IIV, QEB @LOB LC QEFP ?LLH FP >?LRQ PFJRI>QFKD QEB MEVPF@P LC JLSFKD ?LAFBP. ,BQgP Q>HB > ILLH >Q ELT QOFDLKLJBQOV @>K EBIM RP TFQE QEFP MROPRFQ.

F64B?2 3.10

F64B?2 3.11

! MBKARIRJ FP > ?L? PRPMBKABA COLJ > MFSLQ. /?SFLRPIV > OB>I-TLOIA MBKARIRJ TLRIA IFSB FK > 3$ PM>@B, ?RQ TBgOB DLFKD QL ILLH >Q > PFJMIBO P@BK>OFL, > MBKARIRJ FK > 2$ PM>@Be> 0OL@BPPFKD TFKALT (PBB &FDROB 3.10). )K #E>MQBO 2, TB IB>OKBA ELT > CLO@B (PR@E >P QEB CLO@B LC DO>SFQV FK &FDROB 3.11) @>RPBP >K L?GB@Q QL >@@BIBO>QB. " = ( * A LO A = " / ( . )K QEFP @>PB, ELTBSBO, QEB MBKARIRJ ?L? ALBPKgQ PFJMIV C>II QL QEB DOLRKA ?B@>RPB FQ FP >QQ>@EBA ?V >K >OJ QL QEB MFSLQ MLFKQ. !KA PL, FK LOABO QL ABQBOJFKB FQP 5B;I@5F >@@BIBO>QFLK, TB KLQ LKIV KBBA QL ILLH >Q QEB CLO@B LC DO>SFQV, ?RQ >IPL QEB CLO@B >Q QEB >KDIB LC QEB MBKARIRJgP >OJ (OBI>QFSB QL > MBKARIRJ >Q OBPQ TFQE >K >KDIB LC 0). )K QEB >?LSB @>PB, PFK@B QEB MBKARIRJgP >OJ FP LC CFUBA IBKDQE, QEB LKIV S>OF>?IB FK QEB P@BK>OFL FP QEB >KDIB. 7B >OB DLFKD QL PFJRI>QB QEB MBKARIRJgP JLQFLK QEOLRDE QEB RPB LC >KDRI>O SBIL@FQV >KA >@@BIBO>QFLK. 4EB >KDRI>O >@@BIBO>QFLK TFII ?B @>I@RI>QBA RPFKD .BTQLKgP PB@LKA I>T TFQE > IFQQIB QOFDLKLJBQOV QTFPQ. ,BQgP WLLJ FK LK QEB OFDEQ QOF>KDIB COLJ QEB MBKARIRJ AF>DO>J.

127

#E>MQBO 3. /P@FII>QFLK

7B @>K PBB QE>Q QEB CLO@B LC QEB MBKARIRJ ($ J ) PELRIA MLFKQ MBOMBKAF@RI>O QL QEB >OJ LC QEB MBKARIRJ FK QEB AFOB@QFLK QE>Q QEB MBKARIRJ FP PTFKDFKD. !CQBO >II, FC QEBOB TBOB KL >OJ, QEB ?L? TLRIA GRPQ C>II PQO>FDEQ ALTK. )QgP QEB QBKPFLK CLO@B LC QEB >OJ QE>Q HBBMP QEB ?L? >@@BIBO>QFKD QLT>OAP QEB MBKARIRJgP OBPQ PQ>QB. 3FK@B QEB CLO@B LC DO>SFQV ($ J ) MLFKQP ALTKT>OA, ?V J>HFKD > OFDEQ QOF>KDIB LRQ LC QEBPB QTL SB@QLOP, TBgSB >@@LJMIFPEBA PLJBQEFKD NRFQB J>DKFCF@BKQ. 7BgSB J>AB QEB CLO@B LC DO>SFQV QEB EVMLQBKRPB LC > OFDEQ QOF>KDIB >KA PBM>O>QBA QEB SB@QLO FKQL QTL @LJMLKBKQP, LKB LC TEF@E OBMOBPBKQP QEB CLO@B LC QEB MBKARIRJ. 3FK@B PFKB BNR>IP LMMLPFQB LSBO EVMLQBKRPB, TB E>SB:
I?D;(V) = " F / " =

F64B?2 3.12

4EBOBCLOB:
" F = " = * I?D;(V)

,BPQ TB CLODBQ, TBgSB ?BBK ALFKD >II LC QEFP TFQE > PFKDIB NRBPQFLK FK JFKA: 7E>Q FP QEB >KDRI>O >@@BIBO>QFLK LC QEB MBKARIRJ? /K@B TB E>SB QEB >KDRI>O >@@BIBO>QFLK, TBgII ?B >?IB QL >MMIV LRO ORIBP LC JLQFLK QL CFKA QEB KBT >KDIB CLO QEB MBKARIRJ.
7D=KB7H L;BE9?JO = 7D=KB7H L;BE9?JO + 7D=KB7H 799;B;H7J?ED 7D=B; = 7D=B; + 7D=KB7H L;BE9?JO

4EB DLLA KBTP FP QE>Q TFQE .BTQLKgP PB@LKA I>T, TB HKLT QE>Q QEBOB FP > OBI>QFLKPEFM ?BQTBBK CLO@B >KA >@@BIBO>QFLK, K>JBIV & = - * !, LO ! = & / -. 3L FC QEB CLO@B LC QEB MBKARIRJ FP BNR>I QL QEB CLO@B LC DO>SFQV QFJBP PFKB LC QEB >KDIB, QEBK:
F;D:KBKC 7D=KB7H 799;B;H7J?ED = 799;B;H7J?ED :K; JE =H7L?JO * I?D; (V)

4EFP FP > DLLA QFJB QL OBJFKA LROPBISBP QE>Q TBgOB 0OL@BPPFKD MOLDO>JJBOP >KA KLQ MEVPF@FPQP. 9BP, TB HKLT QE>Q QEB >@@BIBO>QFLK ARB QL DO>SFQV LK B>OQE FP 9.8 JBQBOP MBO PB@LKA PNR>OBA. "RQ QEFP KRJ?BO FPKgQ OBIBS>KQ QL RP. 7E>Q TB E>SB EBOB FP GRPQ >K >O?FQO>OV @LKPQ>KQ (TBgII @>II FQ AL;PCNS ), LKB QE>Q TB @>K RPB QL P@>IB QEB >@@BIBO>QFLK QL PLJBQEFKD QE>Q CBBIP OFDEQ.
7D=KB7H 799;B;H7J?ED = =H7L?JO * I?D;(V)

!J>WFKD. !CQBO >II QE>Q, QEB CLOJRI> FP PL PFJMIB. 9LR JFDEQ ?B TLKABOFKD, TEV ?LQEBO DLFKD QEOLRDE QEB ABOFS>QFLK >Q >II? ) JB>K, IB>OKFKD FP DOB>Q >KA >II, ?RQ TB @LRIA E>SB B>PFIV GRPQ

128

4EB .>QROB LC #LAB (S1.0)

P>FA, "(BV, QEB >KDRI>O >@@BIBO>QFLK LC > MBKARIRJ FP PLJB @LKPQ>KQ QFJBP QEB PFKB LC QEB >KDIB." 4EFP FP GRPQ >KLQEBO JLJBKQ FK TEF@E TB OBJFKA LROPBISBP QE>Q QEB MROMLPB LC QEB ?LLH FP KLQ QL IB>OK ELT MBKARIRJP PTFKD LO DO>SFQV TLOHP. 4EB MLFKQ FP QL QEFKH @OB>QFSBIV >?LRQ ELT QEFKDP @>K JLSB >?LRQ QEB P@OBBK FK > @LJMRQ>QFLK>IIV ?>PBA DO>MEF@P PVPQBJ. 4EB MBKARIRJ FP GRPQ > @>PB PQRAV. )C VLR @>K RKABOPQ>KA QEB >MMOL>@E QL MOLDO>JJFKD > MBKARIRJ, QEBK ELTBSBO VLR @ELLPB QL ABPFDK VLRO LKP@OBBK TLOIA, VLR @>K >MMIV QEB P>JB QB@EKFNRBP. /C @LROPB, TBgOB KLQ CFKFPEBA VBQ. 7B J>V ?B E>MMV TFQE LRO PFJMIB, BIBD>KQ CLOJRI>, ?RQ TB PQFII E>SB QL >MMIV FQ FK @LAB. 4EFP FP JLPQ ABCFKFQBIV > DLLA QFJB QL MO>@QF@B LRO L?GB@Q-LOFBKQBA MOLDO>JJFKD PHFIIP >KA @OB>QB > .?H>OFOG @I>PP. ,BQgP QEFKH >?LRQ >II QEB MOLMBOQFBP TBgSB BK@LRKQBOBA FK LRO MBKARIRJ AFP@RPPFLK QE>Q QEB @I>PP TFII KBBA: j >OJ IBKDQE j >KDIB j >KDRI>O SBIL@FQV j >KDRI>O >@@BIBO>QFLK
=F;MM .?H>OFOG @FI;N L; @FI;N ;HAF?; @FI;N ;3?FI=CNS; @FI;N ;A==?F?L;NCIH; U Length of arm Pendulum arm angle Angular velocity Angular acceleration

7BgII >IPL KBBA QL TOFQB > CRK@QFLK OJ>;N?() QL RMA>QB QEB MBKARIRJgP >KDIB >@@LOAFKD QL LRO CLOJRI>k
PIC> OJ>;N?() U @FI;N AL;PCNS = 0.4; ;A==?F?L;NCIH = -1 * AL;PCNS * MCH(;HAF?); Arbitrary constant Calculate acceleration according to our formula. Increment velocity. Increment angle.

;3?FI=CNS += ;A==?F?L;NCIH; ;HAF? += ;3?FI=CNS; W

129

#E>MQBO 3. /P@FII>QFLK

k>P TBII >P > CRK@QFLK >CMJF;S() QL AO>T QEB MBKARIRJ FK QEB TFKALT. 4EFP ?BDP QEB NRBPQFLK: h5J, TEBOB AL TB AO>T QEB MBKARIRJ?i 7B HKLT QEB >KDIB >KA QEB >OJ IBKDQE, ?RQ ELT AL TB HKLT QEB L,M (#>OQBPF>K!) @LLOAFK>QBP CLO ?LQE QEB MBKARIRJgP MFSLQ MLFKQ (IBQgP @>II FQ LOFDFK) >KA ?L? IL@>QFLK (IBQgP @>II FQ IL@>QFLK)? 4EFP J>V ?B DBQQFKD > IFQQIB QFOFKD, ?RQ QEB >KPTBO, VBQ >D>FK, FP QOFDLKLJBQOV. 4EB LOFDFK FP GRPQ PLJBQEFKD TB J>HB RM, >P FP QEB >OJ IBKDQE. ,BQgP P>V:

F64B?2 3.13

.3?=NIL ILCACH = H?Q .3?=NIL(100,10); @FI;N L = 125;

7BgSB DLQ QEB @ROOBKQ >KDIB PQLOBA FK LRO S>OF>?IB ;HAF? . 3L OBI>QFSB QL QEB LOFDFK, QEB MBKARIRJgP IL@>QFLK FP > MLI>O @LLOAFK>QB: (F,5B;@9) . !KA TB KBBA FQ QL ?B #>OQBPF>K. ,R@HFIV CLO RP, TB GRPQ PMBKQ PLJB QFJB (PB@QFLK 3.5) ABOFSFKD QEB CLOJRI> CLO @LKSBOQFKD COLJ MLI>O QL #>OQBPF>K. !KA PL:
.3?=NIL FI=;NCIH = H?Q .3?=NIL(L*MCH(;HAF?),L*=IM(;HAF?));

3FK@B QEB IL@>QFLK FP OBI>QFSB QL TEBOBSBO QEB LOFDFK E>MMBKP QL ?B, TB @>K GRPQ >AA LOFDFK QL QEB IL@>QFLK .3?=NIL :
FI=;NCIH.;>>(ILCACH);

!KA >II QE>Q OBJ>FKP FP QEB IFQQIB J>QQBO LC AO>TFKD > IFKB >KA BIIFMPB (VLR PELRIA ?B JLOB @OB>QFSB, LC @LROPB).
MNLIE?(0); @CFF(175); FCH?(ILCACH.R,ILCACH.S,FI=;NCIH.R,FI=;NCIH.S); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);

"BCLOB TB MRQ BSBOVQEFKD QLDBQEBO, QEBOBgP LKB I>PQ IFQQIB ABQ>FI ) KBDIB@QBA QL JBKQFLK. ,BQgP QEFKH >?LRQ QEB MBKARIRJ >OJ CLO > JLJBKQ. )P FQ > JBQ>I OLA? ! PQOFKD? ! OR??BO ?>KA? (LT FP FQ >QQ>@EBA QL QEB MFSLQ MLFKQ? (LT ILKD FP FQ? 7E>Q FP FQP J>PP? )P FQ > TFKAV A>V? 4EBOB >OB > ILQ LC NRBPQFLKP QE>Q TB @LRIA @LKQFKRB QL >PH QE>Q TLRIA >CCB@Q QEB PFJRI>QFLK. 7BgOB 130

4EB .>QROB LC #LAB (S1.0)

IFSFKD, LC @LROPB, FK > C>KQ>PV TLOIA, LKB TEBOB QEB MBKARIRJgP >OJ FP PLJB FAB>IFWBA OLA QE>Q KBSBO ?BKAP >KA QEB J>PP LC QEB ?L? FP @LK@BKQO>QBA FK > PFKDIB, FKCFKFQBPFJ>IIV PJ>II MLFKQ. .BSBOQEBIBPP, BSBK QELRDE TB ALKgQ T>KQ QL TLOOV LROPBISBP TFQE >II LC QEB NRBPQFLKP, TB PELRIA >AA LKB JLOB S>OF>?IB QL LRO @>I@RI>QFLK LC >KDRI>O >@@BIBO>QFLK. 4L HBBM QEFKDP PFJMIB, FK LRO ABOFS>QFLK LC QEB MBKARIRJgP >@@BIBO>QFLK, TB >PPRJBA QE>Q QEB IBKDQE LC QEB MBKARIRJgP >OJ FP 1. )K C>@Q, QEB IBKDQE LC QEB MBKARIRJgP >OJ >CCB@QP QEB >@@BIBO>QFLK DOB>QIV: QEB ILKDBO QEB >OJ, QEB PILTBO QEB >@@BIBO>QFLK. 4L PFJRI>QB > MBKARIRJ JLOB >@@RO>QBIV, TB AFSFAB ?V QE>Q IBKDQE, FK QEFP @>PB L . &LO > JLOB FKSLISBA BUMI>K>QFLK, SFPFQ 4EB 3FJMIB 0BKARIRJ TB?PFQB (EQQM://@>I@RIRPI>?.ABIQ>@LIIBDB.BAR//$%/7-!-2/7-!-2-E.EQJI).
;A==?F?L;NCIH = (-1 * % * MCH(;HAF?)) / L;

&FK>IIV, > OB>I-TLOIA MBKARIRJ FP DLFKD QL BUMBOFBK@B PLJB >JLRKQ LC COF@QFLK (>Q QEB MFSLQ MLFKQ) >KA >FO OBPFPQ>K@B. 7FQE LRO @LAB >P FP, QEB MBKARIRJ TLRIA PTFKD CLOBSBO, PL QL J>HB FQ JLOB OB>IFPQF@ TB @>K RPB > hA>JMFKDi QOF@H. ) P>V HF=7? ?B@>RPB O>QEBO QE>K JLABI QEB OBPFPQ>K@B CLO@BP TFQE PLJB ABDOBB LC >@@RO>@V (>P TB AFA FK #E>MQBO 2), TB @>K >@EFBSB > PFJFI>O OBPRIQ ?V PFJMIV OBAR@FKD QEB >KDRI>O SBIL@FQV AROFKD B>@E @V@IB. 4EB CLIILTFKD @LAB OBAR@BP QEB SBIL@FQV ?V 1% (LO JRIQFMIFBP FQ ?V 99%) AROFKD B>@E CO>JB LC >KFJ>QFLK:
;3?FI=CNS *= 0.99;

0RQQFKD BSBOVQEFKD QLDBQEBO, TB E>SB QEB CLIILTFKD BU>JMIB (TFQE QEB MBKARIRJ ?BDFKKFKD >Q > 45-ABDOBB >KDIB).

Example 3.10: Swinging pendulum


.?H>OFOG J; PIC> M?NOJ() U MCT?(640,360);

131

#E>MQBO 3. /P@FII>QFLK

J = H?Q .?H>OFOG(H?Q .3?=NIL(QC>NB/2,10),125); W PIC> >L;Q() U <;=EALIOH>(255); J.AI(); W =F;MM .?H>OFOG U // // // // // // // *I=;NCIH I@ <I< *I=;NCIH I@ ;LG ILCACH *?HANB I@ ;LG .?H>OFOG ;LG ;HAF? AHAF? P?FI=CNS AHAF? ;==?F?L;NCIH AL<CNL;LS >;GJCHA ;GIOHN

We make a new Pendulum object with an origin location and arm length.

.3?=NIL FI=;NCIH; .3?=NIL ILCACH; @FI;N L; @FI;N ;HAF?; @FI;N ;3?FI=CNS; @FI;N ;A==?F?L;NCIH; @FI;N >;GJCHA;

Many, many variables to keep track of the Pendulums various properties

.?H>OFOG(.3?=NIL ILCACH:, @FI;N L:) U ILCACH = ILCACH:.A?N(); FI=;NCIH = H?Q .3?=NIL(); L = L:; ;HAF? = .'/4; ;3?FI=CNS = 0.0; ;A==?F?L;NCIH = 0.0; >;GJCHA = 0.995; W PIC> AI() U OJ>;N?(); >CMJF;S(); W PIC> OJ>;N?() U @FI;N AL;PCNS = 0.4; ;A==?F?L;NCIH = (-1 * AL;PCNS / L) * MCH(;HAF?); Formula we worked out for angular acceleration An arbitrary damping so that the Pendulum slows over time

;3?FI=CNS += ;A==?F?L;NCIH; ;HAF? += ;3?FI=CNS;

Standard angular motion algorithm

;3?FI=CNS *= >;GJCHA; W PIC> >CMJF;S() U FI=;NCIH.M?N(L*MCH(;HAF?),L*=IM(;HAF?),0); FI=;NCIH.;>>(ILCACH); MNLIE?(0);

Apply some damping.

Where is the bob relative to the origin? Polar to Cartesian coordinates will tell us!

132

4EB .>QROB LC #LAB (S1.0)

FCH?(ILCACH.R,ILCACH.S,FI=;NCIH.R,FI=;NCIH.S); @CFF(175); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16); W W

The arm

The bob

(*CH9 H<5H H<9 J9FG=CB C: H<9 9L5AD@9 DCGH98 CB H<9 K96G=H9 <5G 588=H=CB5@ 7C89 HC 5@@CK H<9 IG9F HC ;F56 H<9 D9B8I@IA 5B8 GK=B; =H K=H< H<9 ACIG9.)

Exercise 3.12
3QOFKD QLDBQEBO > PBOFBP LC MBKARIRJP PL QE>Q QEB BKAMLFKQ LC LKB FP QEB LOFDFK MLFKQ LC >KLQEBO. .LQB QE>Q ALFKD QEFP J>V MOLAR@B FKQOFDRFKD OBPRIQP ?RQ TFII ?B TFIAIV FK>@@RO>QB MEVPF@>IIV. 3FJRI>QFKD >K >@QR>I ALR?IB MBKARIRJ FKSLISBP PLMEFPQF@>QBA BNR>QFLKP, TEF@E VLR @>K OB>A >?LRQ EBOB: EQQM://P@FBK@BTLOIA.TLICO>J.@LJ/MEVPF@P/ $LR?IB0BKARIRJ.EQJI (EQQM://P@FBK@BTLOIA.TLICO>J.@LJ/MEVPF@P/$LR?IB0BKARIRJ.EQJI).

Exercise 3.13
5PFKD QOFDLKLJBQOV, TE>Q FP QEB J>DKFQRAB LC QEB KLOJ>I CLO@B FK QEB FIIRPQO>QFLK LK QEB OFDEQ (QEB CLO@B MBOMBKAF@RI>O QL QEB FK@IFKB LK TEF@E QEB PIBA OBPQP)? .LQB QE>Q, >P FKAF@>QBA, QEB hKLOJ>Ii CLO@B FP > @LJMLKBKQ LC QEB CLO@B LC DO>SFQV.

Exercise 3.14
#OB>QB >K BU>JMIB QE>Q PFJRI>QBP > ?LU PIFAFKD ALTK QEB FK@IFKB TFQE COF@QFLK. .LQB QE>Q QEB J>DKFQRAB LC QEB COF@QFLK CLO@B FP BNR>I QL QEB KLOJ>I CLO@B.

133

#E>MQBO 3. /P@FII>QFLK

3.10 Spring Forces


)K PB@QFLK 3.6 (PBB M>DB 115), TB ILLHBA >Q JLABIFKD PFJMIB E>OJLKF@ JLQFLK ?V J>MMFKD QEB PFKB T>SB QL > MFUBI O>KDB. %UBO@FPB 3.6 (PBB M>DB 119) >PHBA VLR QL RPB QEFP QB@EKFNRB QL @OB>QB > PFJRI>QFLK LC > ?L? E>KDFKD COLJ > PMOFKD. 7EFIB RPFKD QEB MCH() CRK@QFLK FP > NRF@H->KA-AFOQV, LKB-IFKB-LC-@LAB T>V LC DBQQFKD PLJBQEFKD RM >KA ORKKFKD, FQ TLKgQ AL FC TE>Q TB OB>IIV T>KQ FP QL E>SB > ?L? E>KDFKD COLJ > PMOFKD FK > QTL-AFJBKPFLK>I PM>@B QE>Q OBPMLKAP QL LQEBO CLO@BP FK QEB BKSFOLKJBKQ (TFKA, DO>SFQV, BQ@.) 4L >@@LJMIFPE > PFJRI>QFLK IFHB QEFP (LKB QE>Q FP FABKQF@>I QL QEB MBKARIRJ BU>JMIB, LKIV KLT QEB >OJ FP > PMOFKDV @LKKB@QFLK), TB KBBA QL JLABI QEB CLO@BP LC > PMOFKD RPFKD .3?=NIL .

F64B?2 3.14

4EB CLO@B LC > PMOFKD FP @>I@RI>QBA >@@LOAFKD QL (LLHBgP I>T, K>JBA CLO 2L?BOQ (LLHB, > "OFQFPE MEVPF@FPQ TEL ABSBILMBA QEB CLOJRI> FK 1660. (LLHB LOFDFK>IIV PQ>QBA QEB I>T FK ,>QFK: "0H H9BG=C, G=7 J=G," LO h!P QEB BUQBKPFLK, PL QEB CLO@B.i ,BQgP QEFKH LC FQ QEFP T>V:

4EB CLO@B LC QEB PMOFKD FP AFOB@QIV MOLMLOQFLK>I QL QEB BUQBKPFLK LC QEB PMOFKD.

134

4EB .>QROB LC #LAB (S1.0)

)K LQEBO TLOAP, FC VLR MRII LK QEB ?L? > ILQ, QEB CLO@B TFII ?B PQOLKD; FC VLR MRII LK QEB ?L? > IFQQIB, QEB CLO@B TFII ?B TB>H. ->QEBJ>QF@>IIV, QEB I>T FP PQ>QBA >P CLIILTP:
" IFH?D= = - A * N

j E FP @LKPQ>KQ >KA FQP S>IRB TFII RIQFJ>QBIV P@>IB QEB CLO@B. )P QEB PMOFKD EFDEIV BI>PQF@ LO NRFQB OFDFA? j R OBCBOP QL QEB AFPMI>@BJBKQ LC QEB PMOFKD, F.B. QEB AFCCBOBK@B ?BQTBBK QEB @ROOBKQ IBKDQE >KA QEB OBPQ IBKDQE. 4EB OBPQ IBKDQE FP ABCFKBA >P QEB IBKDQE LC QEB PMOFKD FK > PQ>QB LC BNRFIF?OFRJ.
F64B?2 3.15: E = 0B??2;A 92;4A5 - ?2@A 92;4A5 .LT OBJBJ?BO, CLO@B FP > SB@QLO, PL TB KBBA QL @>I@RI>QB ?LQE J>DKFQRAB >KA AFOB@QFLK. ,BQgP ILLH >Q LKB JLOB AF>DO>J LC QEB PMOFKD >KA I>?BI >II QEB DFSBKP TB JFDEQ E>SB FK > 0OL@BPPFKD PHBQ@E.

F64B?2 3.16

,BQgP BPQ>?IFPE QEB CLIILTFKD QEOBB S>OF>?IBP >P PELTK FK &FDROB 3.16.
.3?=NIL ;H=BIL; .3?=NIL FI=;NCIH; @FI;N L?MN*?HANB;

135

#E>MQBO 3. /P@FII>QFLK

&FOPQ, IBQgP RPB (LLHBgP I>T QL @>I@RI>QB QEB J>DKFQRAB LC QEB CLO@B. 7B KBBA QL HKLT E >KA R . E FP B>PV; FQgP GRPQ > @LKPQ>KQ, PL IBQgP J>HB PLJBQEFKD RM.
@FI;N E = 0.1;

R FP MBOE>MP > ?FQ JLOB AFCCF@RIQ. 7B KBBA QL HKLT QEB hAFCCBOBK@B ?BQTBBK QEB @ROOBKQ

IBKDQE >KA QEB OBPQ IBKDQE.i 4EB OBPQ IBKDQE FP ABCFKBA >P QEB S>OF>?IB L?MN*?HANB . 7E>QgP QEB @ROOBKQ IBKDQE? 4EB AFPQ>K@B ?BQTBBK QEB >K@ELO >KA QEB ?L?. !KA ELT @>K TB @>I@RI>QB QE>Q AFPQ>K@B? (LT >?LRQ QEB J>DKFQRAB LC > SB@QLO QE>Q MLFKQP COLJ QEB >K@ELO QL QEB ?L?? (.LQB QE>Q QEFP FP BU>@QIV QEB P>JB MOL@BPP TB BJMILVBA TEBK @>I@RI>QFKD AFPQ>K@B FK %U>JMIB 2.9: DO>SFQ>QFLK>I >QQO>@QFLK.)
.3?=NIL >CL = .3?=NIL.MO<(<I<,;H=BIL); @FI;N =OLL?HN*?HANB = >CL.G;A(); @FI;N R = L?MN*?HANB - =OLL?HN*?HANB; A vector pointing from anchor to bob gives us the current length of the spring.

.LT QE>Q TBgSB PLOQBA LRQ QEB BIBJBKQP KB@BPP>OV CLO QEB J>DKFQRAB LC QEB CLO@B (-1 * E * R ), TB KBBA QL CFDROB LRQ QEB AFOB@QFLK, > RKFQ SB@QLO MLFKQFKD FK QEB AFOB@QFLK LC QEB CLO@B. 4EB DLLA KBTP FP QE>Q TB >IOB>AV E>SB QEFP SB@QLO. 2FDEQ? *RPQ > JLJBKQ >DL TB QELRDEQ QL LROPBISBP: h(LT TB @>K @>I@RI>QB QE>Q AFPQ>K@B? (LT >?LRQ QEB J>DKFQRAB LC > SB@QLO QE>Q MLFKQP COLJ QEB >K@ELO QL QEB ?L??i 7BII, QE>Q FP QEB AFOB@QFLK LC QEB CLO@B!

F64B?2 3.17

136

4EB .>QROB LC #LAB (S1.0)

)K &FDROB 3.17, TB @>K PBB QE>Q FC TB PQOBQ@E QEB PMOFKD ?BVLKA FQP OBPQ IBKDQE, QEBOB PELRIA ?B > CLO@B MRIIFKD FQ ?>@H QLT>OAP QEB >K@ELO. !KA FC FQ PEOFKHP ?BILT FQP OBPQ IBKDQE, QEB CLO@B PELRIA MRPE FQ >T>V COLJ QEB >K@ELO. 4EFP OBSBOP>I LC AFOB@QFLK FP >@@LRKQBA CLO FK QEB CLOJRI> TFQE QEB -1. !KA PL >II TB KBBA QL AL FP KLOJ>IFWB QEB .3?=NIL TB RPBA CLO QEB AFPQ>K@B @>I@RI>QFLK! ,BQgP Q>HB > ILLH >Q QEB @LAB >KA OBK>JB QE>Q .3?=NIL S>OF>?IB >P hCLO@B.i
@FI;N E = 0.1; .3?=NIL @IL=? = .3?=NIL.MO<(<I<,;H=BIL); @FI;N =OLL?HN*?HANB = >CL.G;A(); @FI;N R = L?MN*?HANB - =OLL?HN*?HANB; @IL=?.HILG;FCT?(); Magnitude of spring force according to Hookes law

Direction of spring force (unit vector)

@IL=?.GOFN(-1 * E * R);

Putting it together: direction and magnitude!

.LT QE>Q TB E>SB QEB >IDLOFQEJ TLOHBA LRQ CLO @LJMRQFKD QEB PMOFKD CLO@B SB@QLO, QEB NRBPQFLK OBJ>FKP: TE>Q L?GB@Q-LOFBKQBA MOLDO>JJFKD PQOR@QROB PELRIA TB RPB? 4EFP, >D>FK, FP LKB LC QELPB PFQR>QFLKP FK TEF@E QEBOB FP KL h@LOOB@Qi >KPTBO. 4EBOB >OB PBSBO>I MLPPF?FIFQFBP; TEF@E LKB TB @ELLPB ABMBKAP LK QEB MOLDO>JgP DL>IP >KA LKBgP LTK MBOPLK>I @LAFKD PQVIB. 3QFII, PFK@B TBgSB ?BBK TLOHFKD >II >ILKD TFQE > +IP?L @I>PP, IBQgP HBBM DLFKD TFQE QEFP P>JB CO>JBTLOH. ,BQgP QEFKH LC LRO +IP?L @I>PP >P QEB PMOFKDgP h?L?.i 4EB ?L? KBBAP FI=;NCIH , P?FI=CNS , >KA ;==?F?L;NCIH SB@QLOP QL JLSB >?LRQ QEB P@OBBK. 0BOCB@QeTBgSB DLQ QE>Q >IOB>AV! !KA MBOE>MP QEB ?L? BUMBOFBK@BP > DO>SFQV CLO@B SF> QEB ;JJFS$IL=?() CRK@QFLK. *RPQ LKB JLOB PQBMeTB KBBA QL >MMIV QEB PMOFKD CLO@B:
I< <I<; PIC> M?NOJ() U <I< = H?Q I<(); W PIC> >L;Q() U Our Chapter 2 make-up-a-gravity force

.3?=NIL AL;PCNS = H?Q .3?=NIL(0,1); <I<.;JJFS$IL=?(AL;PCNS); +0;9JEH IFH?D="EH9; = 666666666666666???? 8E8.7FFBO"EH9;(IFH?D=);

We need to also calculate and apply a spring force!

<I<.OJ>;N?(); <I<.>CMJF;S(); W

Our standard update() and display() functions

137

#E>MQBO 3. /P@FII>QFLK

F64B?2 3.18

/KB LMQFLK TLRIA ?B QL TOFQB LRQ >II LC QEB PMOFKD CLO@B @LAB FK QEB J>FK >L;Q() ILLM. "RQ QEFKHFKD >EB>A QL TEBK VLR JFDEQ E>SB JRIQFMIB ?L?P >KA JRIQFMIB PMOFKD @LKKB@QFLKP, FQ J>HBP > DLLA AB>I LC PBKPB QL TOFQB >K >AAFQFLK>I @I>PP, > 0JLCHA @I>PP. !P PELTK FK &FDROB 3.18, QEB I< @I>PP HBBMP QO>@H LC QEB JLSBJBKQP LC QEB ?L?; QEB 0JLCHA @I>PP HBBMP QO>@H LC QEB PMOFKDgP >K@ELO >KA FQP OBPQ IBKDQE >KA @>I@RI>QBP QEB PMOFKD CLO@B LK QEB ?L?. 4EFP >IILTP RP QL TOFQB > ILSBIV J>FK MOLDO>J >P CLIILTP:
I< <I<; -FH?D= IFH?D=; PIC> M?NOJ() U <I< = H?Q I<(); MJLCHA = H?Q 0JLCHA(); W PIC> >L;Q() U .3?=NIL AL;PCNS = H?Q .3?=NIL(0,1); <I<.;JJFS$IL=?(AL;PCNS); IFH?D=.9EDD;9J(8E8); <I<.OJ>;N?(); <I<.>CMJF;S(); MJLCHA.>CMJF;S(); W This new function in the Spring class will take care of computing the force of the spring on the bob. Adding a Spring object

138

4EB .>QROB LC #LAB (S1.0)

9LR J>V KLQF@B EBOB QE>Q QEFP FP NRFQB PFJFI>O QL TE>Q TB AFA FK %U>JMIB 2.6 (PBB M>DB 94) TFQE >K >QQO>@QLO. 4EBOB, TB P>FA PLJBQEFKD IFHB:
.3?=NIL @IL=? = ;NNL;=NIL.;NNL;=N(GIP?L); GIP?L.;JJFS$IL=?(@IL=?);

4EB >K>ILDLRP PFQR>QFLK EBOB TFQE > PMOFKD TLRIA ?B:


.3?=NIL @IL=? = MJLCHA.=IHH?=N(<I<); <I<.;JJFS$IL=?(@IL=?);

.BSBOQEBIBPP, FK QEFP BU>JMIB >II TB P>FA T>P:


MJLCHA.=IHH?=N(<I<);

7E>Q DFSBP? 7EV ALKgQ TB KBBA QL @>II ;JJFS$IL=?() LK QEB ?L?? 4EB >KPTBO FP, LC @LROPB, QE>Q TB AL KBBA QL @>II ;JJFS$IL=?() LK QEB ?L?. /KIV FKPQB>A LC ALFKD FQ FK >L;Q() , TBgOB GRPQ ABJLKPQO>QFKD QE>Q > MBOCB@QIV OB>PLK>?IB (>KA PLJBQFJBP MOBCBO>?IB) >IQBOK>QFSB FP QL >PH QEB =IHH?=N() CRK@QFLK QL FKQBOK>IIV E>KAIB @>IIFKD ;JJFS$IL=?() LK QEB ?L?.
PIC> =IHH?=N( I< <) U .3?=NIL @IL=? = MIG? @;H=S =;F=OF;NCIHM <.;JJFS$IL=?(@IL=?); W The function connect() takes care of calling applyForce() and therefore doesnt have to return a vector to the calling area.

7EV AL FQ LKB T>V TFQE QEB ANNL;=NIL @I>PP >KA >KLQEBO T>V TFQE QEB 0JLCHA @I>PP? 7EBK TB TBOB CFOPQ IB>OKFKD >?LRQ CLO@BP, FQ T>P > ?FQ @IB>OBO QL PELT >II QEB CLO@BP ?BFKD >MMIFBA FK QEB J>FK >L;Q() ILLM, >KA ELMBCRIIV QEFP EBIMBA VLR IB>OK >?LRQ CLO@B >@@RJRI>QFLK. .LT QE>Q TBgOB JLOB @LJCLOQ>?IB TFQE QE>Q, MBOE>MP FQgP PFJMIBO QL BJ?BA PLJB LC QEB ABQ>FIP FKPFAB QEB L?GB@QP QEBJPBISBP. ,BQgP Q>HB > ILLH >Q QEB OBPQ LC QEB BIBJBKQP FK QEB 0JLCHA @I>PP.

139

#E>MQBO 3. /P@FII>QFLK

Example 3.11: A Spring connection


=F;MM 0JLCHA U .3?=NIL ;H=BIL; We need to keep track of the springs anchor location. Rest length and spring constant variables

@FI;N F?H; @FI;N E = 0.1; 0JLCHA(@FI;N R, @FI;N S, CHN F) U ;H=BIL = H?Q .3?=NIL(R,S); F?H = F; W

The constructor initializes the anchor point and rest length.

PIC> =IHH?=N( I< <) U

Calculate spring forceour implementation of Hookes Law. Get a vector pointing from anchor to Bob location.

+0;9JEH <EH9; = +0;9JEH.IK8(8.BE97J?ED,7D9>EH); <BE7J : = <EH9;.C7=(); <BE7J IJH;J9> = : - B;D;

Calculate the displacement between distance and rest length. Direction and magnitude together!

<EH9;.DEHC7B?P;(); <EH9;.CKBJ(-1 * A * IJH;J9>);

<.;JJFS$IL=?(@IL=?); W PIC> >CMJF;S() U @CFF(100); L?=N+I>?(!#,1#/); L?=N(;H=BIL.R,;H=BIL.S,10,10); W

Call applyForce() right here!

Draw the anchor.

PIC> >CMJF;S*CH?( I< <) U Draw the spring connection between Bob MNLIE?(255); location and anchor. FCH?(<.FI=;NCIH.R,<.FI=;NCIH.S,;H=BIL.R,;H=BIL.S); W W

4EB CRII @LAB CLO QEFP BU>JMIB FP FK@IRABA LK QEB ?LLH TB?PFQB, >KA QEB 7B? SBOPFLK >IPL FK@LOMLO>QBP QTL >AAFQFLK>I CB>QROBP: (1) QEB I< @I>PP FK@IRABP CRK@QFLKP CLO JLRPB

140

4EB .>QROB LC #LAB (S1.0)

FKQBO>@QFSFQV PL QE>Q QEB ?L? @>K ?B AO>DDBA >OLRKA QEB TFKALT, >KA (2) QEB 0JLCHA L?GB@Q FK@IRABP > CRK@QFLK QL @LKPQO>FK QEB @LKKB@QFLKgP IBKDQE ?BQTBBK > JFKFJRJ >KA > J>UFJRJ.

Exercise 3.15
"BCLOB ORKKFKD QL PBB QEB BU>JMIB LKIFKB, Q>HB > ILLH >Q QEFP @LKPQO>FK CRK@QFLK >KA PBB FC VLR @>K CFII FK QEB ?I>KHP.
PIC> =IHMNL;CH*?HANB( I< <, @FI;N GCHF?H, @FI;N G;RF?H) U .3?=NIL >CL = .3?=NIL.MO<(::::::,::::::); @FI;N > = >CL.G;A(); C@ (> < GCHF?H) U >CL.HILG;FCT?(); >CL.GOFN(::::::::); <.FI=;NCIH = .3?=NIL.;>>(::::::,::::::); <.P?FI=CNS.GOFN(0); W ?FM? C@ (::::::::::::) U >CL.HILG;FCT?(); >CL.GOFN(:::::::::); <.FI=;NCIH = .3?=NIL.;>>(::::::,::::::); <.P?FI=CNS.GOFN(0); W W Keep location within constraint. Is it too long? Keep location within constraint. Is it too short? Vector pointing from Bob to Anchor

Exercise 3.16
#OB>QB > PVPQBJ LC JRIQFMIB ?L?P >KA PMOFKD @LKKB@QFLKP. (LT TLRIA VLR E>SB > ?L? @LKKB@QBA QL > ?L? TFQE KL CFUBA >K@ELO?

141

#E>MQBO 3. /P@FII>QFLK

The Ecosystem Project


Step 3 Exercise: Take one of your creatures and incorporate oscillation into its motion. You can use the -M=CFF;NIL class from Example 3.7 as a model. The -M=CFF;NIL object, however, oscillates around a single point (the middle of the window). Try oscillating around a moving point. In other words, design a creature that moves around the screen according to location, velocity, and acceleration. But that creature isnt just a static shape, its an oscillating body. Consider tying the speed of oscillation to the speed of motion. Think of a butterflys flapping wings or the legs of an insect. Can you make it appear that the creatures internal mechanics (oscillation) drive its locomotion? For a sample, check out the AttractionArrayWithOscillation example with the code download.

142

4EB .>QROB LC #LAB (S1.0)

Chapter 4. Particle Systems


I*5.A 6@ D6@2. ,2?2 ! A< 6;C<82 9<460, 5<D2C2?, 9<460 092.?9F 160A.A2@ A5.A A52 ;221@ <3 A52 :.;F <BAD2645 A52 ;221@ <3 A52 32D.J Spock

)K 1982, 7FIIF>J 4. 2BBSBP, > OBPB>O@EBO >Q ,R@>PCFIJ ,QA., T>P TLOHFKD LK QEB CFIJ .H5F /F9? %%: /<9 2F5H< C: '<5B. -R@E LC QEB JLSFB OBSLISBP >OLRKA QEB 'BKBPFP $BSF@B, > QLOMBAL QE>Q TEBK PELQ >Q > ?>OOBK, IFCBIBPP MI>KBQ E>P QEB >?FIFQV QL OBLOD>KFWB J>QQBO >KA @OB>QB > E>?FQ>?IB TLOIA CLO @LILKFW>QFLK. $ROFKD QEB PBNRBK@B, > T>II LC CFOB OFMMIBP LSBO QEB MI>KBQ TEFIB FQ FP ?BFKD hQBOO>CLOJBA.i 4EB QBOJ ;,=?4.70 >D>?08, >K FK@OBAF?IV @LJJLK >KA RPBCRI QB@EKFNRB FK @LJMRQBO DO>MEF@P, T>P @LFKBA FK QEB @OB>QFLK LC QEFP M>OQF@RI>O BCCB@Q.
IA =.?A6092 @F@A2: 6@ . 0<9920A6<; <3 :.;F :.;F :6;BA2 =.?A6092@ A5.A A<42A52? ?2=?2@2;A . 3BGGF </720A. &C2? . =2?6<1 <3 A6:2, =.?A6092@ .?2 42;2?.A21 6;A< . @F@A2:, :<C2 .;1 05.;42 3?<: D6A56; A52 @F@A2:, .;1 162 3?<: A52 @F@A2:.J H,6996.: (22C2@, "'.?A6092 )F@A2:@HA *205;6>B2 3<? $<1296;4 . C9.@@ <3 FBGGF &/720A@," ACM Transactions on Graphics 2:2 (A=?69 1983), 92.

3FK@B QEB B>OIV 1980P, M>OQF@IB PVPQBJP E>SB ?BBK RPBA FK @LRKQIBPP SFABL D>JBP, >KFJ>QFLKP, AFDFQ>I >OQ MFB@BP, >KA FKPQ>II>QFLKP QL JLABI S>OFLRP FOOBDRI>O QVMBP LC K>QRO>I MEBKLJBK>, PR@E >P CFOB, PJLHB, T>QBOC>IIP, CLD, DO>PP, ?R??IBP, >KA PL LK. 4EFP @E>MQBO TFII ?B ABAF@>QBA QL ILLHFKD >Q FJMIBJBKQ>QFLK PQO>QBDFBP CLO @LAFKD > M>OQF@IB PVPQBJ. (LT AL TB LOD>KFWB LRO @LAB? 7EBOB AL TB PQLOB FKCLOJ>QFLK OBI>QBA QL FKAFSFAR>I M>OQF@IBP SBOPRP FKCLOJ>QFLK OBI>QBA QL QEB PVPQBJ >P > TELIB? 4EB BU>JMIBP TBgII ILLH >Q TFII 143

#E>MQBO 4. 0>OQF@IB 3VPQBJP

CL@RP LK J>K>DFKD QEB A>Q> >PPL@F>QBA TFQE > M>OQF@IB PVPQBJ. 4EBVgII RPB PFJMIB PE>MBP CLO QEB M>OQF@IBP >KA >MMIV LKIV QEB JLPQ ?>PF@ ?BE>SFLOP (PR@E >P DO>SFQV). (LTBSBO, ?V RPFKD QEFP CO>JBTLOH >KA ?RFIAFKD FK JLOB FKQBOBPQFKD T>VP QL OBKABO QEB M>OQF@IBP >KA @LJMRQB ?BE>SFLOP, VLR @>K >@EFBSB > S>OFBQV LC BCCB@QP.

4.1 Why We Need Particle Systems


7BgSB ABCFKBA > M>OQF@IB PVPQBJ QL ?B > @LIIB@QFLK LC FKABMBKABKQ L?GB@QP, LCQBK OBMOBPBKQBA ?V > PFJMIB PE>MB LO ALQ. 7EV ALBP QEFP J>QQBO? #BOQ>FKIV, QEB MOLPMB@Q LC JLABIFKD PLJB LC QEB MEBKLJBK> TB IFPQBA (BUMILPFLKP!) FP >QQO>@QFSB >KA MLQBKQF>IIV RPBCRI. "RQ OB>IIV, QEBOBgP >K BSBK ?BQQBO OB>PLK CLO RP QL @LK@BOK LROPBISBP TFQE M>OQF@IB PVPQBJP. )C TB T>KQ QL DBQ >KVTEBOB FK QEFP K>QROB LC @LAB IFCB, TBgOB DLFKD QL KBBA QL TLOH TFQE PVPQBJP LC A5BM QEFKDP. 7BgOB DLFKD QL T>KQ QL ILLH >Q ?>IIP ?LRK@FKD, ?FOAP CIL@HFKD, B@LPVPQBJP BSLISFKD, >II PLOQP LC QEFKDP FK MIRO>I. *RPQ >?LRQ BSBOV @E>MQBO >CQBO QEFP LKB FP DLFKD QL KBBA QL AB>I TFQE > IFPQ LC L?GB@QP. 9BP, TBgSB ALKB QEFP TFQE >K >OO>V FK PLJB LC LRO CFOPQ SB@QLO >KA CLO@BP BU>JMIBP. "RQ TB KBBA QL DL TEBOB KL >OO>V E>P DLKB ?BCLOB. &FOPQ, TBgOB DLFKD QL T>KQ QL AB>I TFQE CIBUF?IB NR>KQFQFBP LC BIBJBKQP. 3LJBQFJBP TBgII E>SB WBOL QEFKDP, PLJBQFJBP LKB QEFKD, PLJBQFJBP QBK QEFKDP, >KA PLJBQFJBP QBK QELRP>KA QEFKDP. 3B@LKA, TBgOB DLFKD QL T>KQ QL Q>HB > JLOB PLMEFPQF@>QBA L?GB@Q-LOFBKQBA >MMOL>@E. )KPQB>A LC PFJMIV TOFQFKD > @I>PP QL ABP@OF?B > PFKDIB M>OQF@IB, TBgOB >IPL DLFKD QL T>KQ QL TOFQB > @I>PP QE>Q ABP@OF?BP QEB @LIIB@QFLK LC M>OQF@IBPeQEB M>OQF@IB PVPQBJ FQPBIC. 4EB DL>I EBOB FP QL ?B >?IB QL TOFQB > J>FK MOLDO>J QE>Q ILLHP IFHB QEB CLIILTFKD:
.;LNC=F?0SMN?G JM; PIC> M?NOJ() U MCT?(640,360); JM = H?Q .;LNC=F?0SMN?G(); W PIC> >L;Q() U <;=EALIOH>(255); JM.LOH(); W Ah, isnt this main program so simple and lovely?

.L PFKDIB M>OQF@IB FP BSBO OBCBOBK@BA FK QEB >?LSB @LAB, VBQ QEB OBPRIQ TFII ?B CRII LC M>OQF@IBP CIVFKD >II LSBO QEB P@OBBK. 'BQQFKD RPBA QL TOFQFKD 0OL@BPPFKD PHBQ@EBP TFQE JRIQFMIB @I>PPBP, >KA @I>PPBP QE>Q HBBM IFPQP LC FKPQ>K@BP LC LQEBO @I>PPBP, TFII MOLSB SBOV RPBCRI >P TB DBQ QL JLOB >AS>K@BA @E>MQBOP FK QEFP ?LLH. &FK>IIV, TLOHFKD TFQE M>OQF@IB PVPQBJP FP >IPL > DLLA BU@RPB CLO RP QL Q>@HIB QTL LQEBO >AS>K@BA L?GB@Q-LOFBKQBA MOLDO>JJFKD QB@EKFNRBP: FKEBOFQ>K@B >KA MLIVJLOMEFPJ. 7FQE QEB

144

4EB .>QROB LC #LAB (S1.0)

BU>JMIBP TBgSB PBBK RM RKQFI KLT, TBgSB >IT>VP E>A >K >OO>V LC > PFKDIB QVMB LC L?GB@Q, IFHB "JLSBOP" LO hLP@FII>QLOP.i 7FQE FKEBOFQ>K@B (>KA MLIVJLOMEFPJ), TBgII IB>OK > @LKSBKFBKQ T>V QL PQLOB > PFKDIB IFPQ QE>Q @LKQ>FKP L?GB@QP LC AFCCBOBKQ QVMBP. 4EFP T>V, > M>OQF@IB PVPQBJ KBBA KLQ LKIV ?B > PVPQBJ LC > PFKDIB QVMB LC M>OQF@IB. 4ELRDE FQ J>V PBBJ L?SFLRP QL VLR, )gA >IPL IFHB QL MLFKQ LRQ QE>Q QEBOB >OB QVMF@>I FJMIBJBKQ>QFLKP LC M>OQF@IB PVPQBJP, >KA QE>QgP TEBOB TB TFII ?BDFK FK QEFP @E>MQBO. (LTBSBO, QEB C>@Q QE>Q QEB M>OQF@IBP FK QEFP @E>MQBO ILLH LO ?BE>SB > @BOQ>FK T>V PELRIA KLQ IFJFQ VLRO FJ>DFK>QFLK. *RPQ ?B@>RPB M>OQF@IB PVPQBJP QBKA QL ILLH PM>OHIV, CIV CLOT>OA, >KA C>II TFQE DO>SFQV ALBPKgQ JB>K QE>Q QELPB >OB QEB @E>O>@QBOFPQF@P VLROP PELRIA E>SB. 4EB CL@RP EBOB FP OB>IIV GRPQ ELT QL HBBM QO>@H LC > PVPQBJ LC J>KV BIBJBKQP. 7E>Q QELPB BIBJBKQP AL >KA ELT QELPB BIBJBKQP ILLH FP RM QL VLR.

4.2 A Single Particle


"BCLOB TB @>K DBQ OLIIFKD LK QEB PVPQBJ FQPBIC, TB E>SB QL TOFQB QEB @I>PP QE>Q TFII ABP@OF?B > PFKDIB M>OQF@IB. 4EB DLLA KBTP: TBgSB ALKB QEFP >IOB>AV. /RO +IP?L @I>PP COLJ #E>MQBO 2 PBOSBP >P QEB MBOCB@Q QBJMI>QB. &LO RP, > M>OQF@IB FP >K FKABMBKABKQ ?LAV QE>Q JLSBP >?LRQ QEB P@OBBK. )Q E>P FI=;NCIH , P?FI=CNS , >KA ;==?F?L;NCIH , > @LKPQOR@QLO QL FKFQF>IFWB QELPB S>OF>?IBP, >KA CRK@QFLKP QL >CMJF;S() FQPBIC >KA OJ>;N?() FQP IL@>QFLK.
=F;MM .;LNC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; .;LNC=F?(.3?=NIL F) U FI=;NCIH = F.A?N(); ;==?F?L;NCIH = H?Q .3?=NIL(); P?FI=CNS = H?Q .3?=NIL(); W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); W PIC> >CMJF;S() U MNLIE?(0); @CFF(175); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8); W W A Particle object is just another name for our Mover. It has location, velocity, and acceleration.

4EFP FP >?LRQ >P PFJMIB >P > M>OQF@IB @>K DBQ. &OLJ EBOB, TB @LRIA Q>HB LRO M>OQF@IB FK PBSBO>I AFOB@QFLKP. 7B @LRIA >AA >K ;JJFS$IL=?() CRK@QFLK QL >CCB@Q QEB M>OQF@IBgP ?BE>SFLO (TBgII AL

145

#E>MQBO 4. 0>OQF@IB 3VPQBJP

MOB@FPBIV QEFP FK > CRQROB BU>JMIB). 7B @LRIA >AA S>OF>?IBP QL ABP@OF?B @LILO >KA PE>MB, LO OBCBOBK@B > .'G;A? QL AO>T QEB M>OQF@IB. &LO KLT, ELTBSBO, IBQgP CL@RP LK >AAFKD GRPQ LKB >AAFQFLK>I ABQ>FI: 7410>;,9 . 4VMF@>I M>OQF@IB PVPQBJP FKSLISB PLJBQEFKD @>IIBA >K 084??0=. 4EB BJFQQBO FP QEB PLRO@B LC QEB M>OQF@IBP >KA @LKQOLIP QEB FKFQF>I PBQQFKDP CLO QEB M>OQF@IBP, IL@>QFLK, SBIL@FQV, BQ@. !K BJFQQBO JFDEQ BJFQ > PFKDIB ?ROPQ LC M>OQF@IBP, LO > @LKQFKRLRP PQOB>J LC M>OQF@IBP, LO ?LQE. 4EB MLFKQ FP QE>Q CLO > QVMF@>I FJMIBJBKQ>QFLK PR@E >P QEFP, > M>OQF@IB FP ?LOK >Q QEB BJFQQBO ?RQ ALBP KLQ IFSB CLOBSBO. )C FQ TBOB QL IFSB CLOBSBO, LRO 0OL@BPPFKD PHBQ@E TLRIA BSBKQR>IIV DOFKA QL > E>IQ >P QEB KRJ?BO LC M>OQF@IBP FK@OB>PBP QL >K RKTFBIAV KRJ?BO LSBO QFJB. !P KBT M>OQF@IBP >OB ?LOK, TB KBBA LIA M>OQF@IBP QL AFB. 4EFP @OB>QBP QEB FIIRPFLK LC >K FKCFKFQB PQOB>J LC M>OQF@IBP, >KA QEB MBOCLOJ>K@B LC LRO MOLDO>J ALBP KLQ PRCCBO. 4EBOB >OB J>KV AFCCBOBKQ T>VP QL AB@FAB TEBK > M>OQF@IB AFBP. &LO BU>JMIB, FQ @LRIA @LJB FKQL @LKQ>@Q TFQE >KLQEBO L?GB@Q, LO FQ @LRIA PFJMIV IB>SB QEB P@OBBK. &LO LRO CFOPQ .;LNC=F? @I>PP, ELTBSBO, TBgOB PFJMIV DLFKD QL >AA > FC@?MJ;H S>OF>?IB. 4EB QFJBO TFII PQ>OQ >Q 255 >KA @LRKQ ALTK QL 0, TEBK QEB M>OQF@IB TFII ?B @LKPFABOBA hAB>A.i !KA PL TB BUM>KA QEB .;LNC=F? @I>PP >P CLIILTP:
=F;MM .;LNC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; <BE7J B?<;IF7D; .;LNC=F?(.3?=NIL F) U FI=;NCIH = F.A?N(); ;==?F?L;NCIH = H?Q .3?=NIL(); P?FI=CNS = H?Q .3?=NIL(); B?<;IF7D = 255; W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); B?<;IF7D -= 2.0; W PIC> >CMJF;S() U IJHEA;(0,B?<;IF7D); <?BB(175,B?<;IF7D); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8); W W Since our life ranges from 255 to 0 we can use it for alpha Lifespan decreases We start at 255 and count down for convenience A new variable to keep track of how long the particle has been alive

4EB OB>PLK TB @ELPB QL PQ>OQ QEB IFCBPM>K >Q 255 >KA @LRKQ ALTK QL 0 FP CLO @LKSBKFBK@B. 7FQE QELPB S>IRBP, TB @>K >PPFDK FC@?MJ;H QL >@Q >P QEB >IME> QO>KPM>OBK@V CLO QEB BIIFMPB >P TBII. 7EBK QEB M>OQF@IB FP hAB>Ai FQ TFII >IPL E>SB C>ABA >T>V LKP@OBBK. 146

4EB .>QROB LC #LAB (S1.0)

7FQE QEB >AAFQFLK LC QEB FC@?MJ;H S>OF>?IB, TBgII >IPL KBBA LKB >AAFQFLK>I CRK@QFLKe> CRK@QFLK QE>Q @>K ?B NRBOFBA (CLO > QORB LO C>IPB >KPTBO) >P QL TEBQEBO QEB M>OQF@IB FP >IFSB LO AB>A. 4EFP TFII @LJB FK E>KAV TEBK TB >OB TOFQFKD QEB .;LNC=F?0SMN?G @I>PP, TELPB Q>PH TFII ?B QL J>K>DB QEB IFPQ LC M>OQF@IBP QEBJPBISBP. 7OFQFKD QEFP CRK@QFLK FP MOBQQV B>PV; TB GRPQ KBBA QL @EB@H >KA PBB FC QEB S>IRB LC FC@?MJ;H FP IBPP QE>K 0. )C FQ FP TB L?NOLH NLO? , FC KLQ TB L?NOLH @;FM? .
<IIF?;H CM"?;>() U C@ (FC@?MJ;H < 0.0) U L?NOLH NLO?; W ?FM? U L?NOLH @;FM?; W W Is the particle still alive?

"BCLOB TB DBQ QL QEB KBUQ PQBM LC J>HFKD J>KV M>OQF@IBP, FQgP TLOQE Q>HFKD > JLJBKQ QL J>HB PROB LRO M>OQF@IB TLOHP @LOOB@QIV >KA @OB>QB > PHBQ@E TFQE LKB PFKDIB .;LNC=F? L?GB@Q. (BOB FP QEB CRII @LAB ?BILT, TFQE QTL PJ>II >AAFQFLKP. 7B >AA > @LKSBKFBK@B CRK@QFLK @>IIBA LOH() QE>Q PFJMIV @>IIP ?LQE OJ>;N?() >KA >CMJF;S() CLO RP. )K >AAFQFLK, TB DFSB QEB M>OQF@IB > O>KALJ FKFQF>I SBIL@FQV >P TBII >P > ALTKT>OA >@@BIBO>QFLK (QL PFJRI>QB DO>SFQV).

Example 4.1: A single particle


.;LNC=F? J; PIC> M?NOJ() U MCT?(640,360); J = H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,10)); W PIC> >L;Q() U <;=EALIOH>(255);

147

#E>MQBO 4. 0>OQF@IB 3VPQBJP

J.LOH(); C@ (J.CM"?;>()) U JLCHNFH(".;LNC=F? >?;>!"); W W =F;MM .;LNC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N FC@?MJ;H; .;LNC=F?(.3?=NIL F) U ;==?F?L;NCIH = H?Q .3?=NIL(0,0.05);

Operating the single Particle

For demonstration purposes we assign the Particle an initial velocity and constant acceleration.

P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0)); FI=;NCIH = F.A?N(); FC@?MJ;H = 255.0; W PIC> LOH() U OJ>;N?(); >CMJF;S(); W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); FC@?MJ;H -= 2.0; W PIC> >CMJF;S() U MNLIE?(0,FC@?MJ;H); @CFF(0,FC@?MJ;H); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8); W <IIF?;H CM"?;>() U C@ (FC@?MJ;H < 0.0) U L?NOLH NLO?; W ?FM? U L?NOLH @;FM?; W W W Is the Particle alive or dead? Sometimes its convenient to have a run function that calls all the other functions we need.

148

4EB .>QROB LC #LAB (S1.0)

Exercise 4.1
2BTOFQB QEB BU>JMIB PL QE>Q QEB M>OQF@IB @>K OBPMLKA QL CLO@B SB@QLOP SF> >K ;JJFS$IL=?() CRK@QFLK.

Exercise 4.2
!AA >KDRI>O SBIL@FQV (OLQ>QFLK) QL QEB M>OQF@IB. #OB>QB VLRO LTK KLK-@FO@IB M>OQF@IB ABPFDK. .LT QE>Q TB E>SB > @I>PP QL ABP@OF?B > PFKDIB M>OQF@IB, TBgOB OB>AV CLO QEB KBUQ ?FD PQBM. (LT AL TB HBBM QO>@H LC J>KV M>OQF@IBP, TEBK TB @>KgQ BKPROB BU>@QIV ELT J>KV M>OQF@IBP TB JFDEQ E>SB >Q >KV DFSBK QFJB?

4.3 The ArrayList


)K QORQE, TB @LRIA RPB > PFJMIB >OO>V QL J>K>DB LRO .;LNC=F? L?GB@QP. 3LJB M>OQF@IB PVPQBJP JFDEQ E>SB > CFUBA KRJ?BO LC M>OQF@IBP, >KA >OO>VP >OB J>DKFCF@BKQIV BCCF@FBKQ FK QELPB FKPQ>K@BP. 0OL@BPPFKD >IPL LCCBOP ?RJ;H>() , =IHNL;=N() , MO<M?N() , MJFC=?() , >KA LQEBO JBQELAP CLO OBPFWFKD >OO>VP. (LTBSBO, CLO QEBPB BU>JMIBP, TBgOB DLFKD QL Q>HB > JLOB PLMEFPQF@>QBA >MMOL>@E >KA RPB QEB *>S> @I>PP ALL;S*CMN , CLRKA FK QEB G>S>.RQFI M>@H>DB !OO>V,FPQ $L@RJBKQ>QFLK (EQQM://ALTKIL>A.LO>@IB.@LJ/G>S>PB/6/AL@P/>MF/G>S>/RQFI/ !OO>V,FPQ.EQJI). 5PFKD >K ALL;S*CMN CLIILTP QEB P>JB FAB> >P RPFKD > PQ>KA>OA >OO>V, ?RQ TFQE AFCCBOBKQ PVKQ>U. 4EB CLIILTFKD QTL @LAB BU>JMIBP (TEF@E >PPRJB QEB BUFPQBK@B LC > DBKBOF@ .;LNC=F? @I>PP) MOLAR@B QEB P>JB OBPRIQ: CFOPQ TFQE >K >OO>V, >KA PB@LKA TFQE >K ALL;S*CMN . 4EB PQ>KA>OA >OO>V T>V:
CHN NIN;F = 10; .;LNC=F?89 J;LL;S = H?Q .;LNC=F?8NIN;F9; PIC> M?NOJ() U @IL (CHN C = 0; C < J;LL;S.F?HANB; C++) U J;LL;S8C9 = H?Q .;LNC=F?(); W This is what were used to, accessing elements on the array via an index and brackets[ ].

149

#E>MQBO 4. 0>OQF@IB 3VPQBJP


W PIC> >L;Q() U @IL (CHN C = 0; C < J;LL;S.F?HANB; C++) U .;LNC=F? J = J;LL;S8C9; J.LOH(); W W Have you ever seen this syntax before? This is a new feature in Java 1.6 (called "generics") that Processing now supports. It allows us to specify in advance what type of object we intend to put in the ArrayList.

4EB KBT ALL;S*CMN T>V:


CHN NIN;F = 10;

ALL;S*CMN<.;LNC=F?> JFCMN = H?Q ALL;S*CMN<.;LNC=F?>(); PIC> M?NOJ() U @IL (CHN C = 0; C < NIN;F; C++) U JFCMN.;>>(H?Q .;LNC=F?()); W W PIC> >L;Q() U @IL (CHN C = 0; C < JFCMN.MCT?(); C++) U The size of the ArrayList is returned by size(). An object is accessed from the ArrayList with get(). Because we are using generics, we do not need to specify a type when we pull objects out of the ArrayList. An object is added to an ArrayList with add().

.;LNC=F? J = JFCMN.A?N(C); J.LOH(); W W

4EFP I>PQ @IL ILLM ILLHP MOBQQV PFJFI>O QL LRO @LAB QE>Q ILLMBA QEOLRDE > OBDRI>O >OO>V ?V >@@BPPFKD B>@E FKABU. 7B FKFQF>IFWB > S>OF>?IB @>IIBA C QL 0 >KA @LRKQ RM ?V 1, >@@BPPFKD B>@E BIBJBKQ LC QEB ALL;S*CMN RKQFI TB DBQ QL QEB BKA. (LTBSBO, QEFP FP > DLLA QFJB QL JBKQFLK QEB hBKE>K@BA @IL ILLMi >S>FI>?IB FK *>S> (>KA 0OL@BPPFKD), TEF@E FP > ?FQ JLOB @LK@FPB. 4EB BKE>K@BA ILLM TLOHP TFQE ?LQE ALL;S*CMN P >KA OBDRI>O >OO>VP >KA ILLHP IFHB QEFP:
ALL;S*CMN<.;LNC=F?> JFCMN = H?Q ALL;S*CMN<.;LNC=F?>(); @IL (.;LNC=F? J: J;LNC=F?M) U J.LOH(); W

,BQgP QO>KPI>QB QE>Q. 3>V hCLO B>@Ei FKPQB>A LC hCLOi >KA P>V hFKi FKPQB>A LC h:i. .LT VLR E>SB: h&LO B>@E 0>OQF@IB M FK M>OQF@IBP, ORK QE>Q 0>OQF@IB M!i ) HKLT. 9LR @>KKLQ @LKQ>FK VLRO BU@FQBJBKQ. ) @>KgQ. ) HKLT FQgP KLQ KB@BPP>OV, ?RQ ) GRPQ E>SB QL QVMB QE>Q >D>FK.

150

4EB .>QROB LC #LAB (S1.0)

@IL (.;LNC=F? J : J;LNC=F?M) U J.LOH(); W

This enhanced loop also works for regular arrays!

3FJMIB, BIBD>KQ, @LK@FPB, ILSBIV. 4>HB > JLJBKQ. "OB>QEB. ) E>SB PLJB ?>A KBTP. 9BP, TB ILSB QE>Q BKE>K@BA ILLM >KA TB TFII DBQ QL RPB FQ. "RQ KLQ OFDEQ KLT. /RO M>OQF@IB PVPQBJ BU>JMIBP TFII OBNRFOB > CB>QROB QE>Q J>HBP RPFKD QE>Q ILLM FJMLPPF?IB. ,BQgP @LKQFKRB. 4EB @LAB TBgSB TOFQQBK >?LSB ALBPKgQ Q>HB >AS>KQ>DB LC QEB ALL;S*CMN gP OBPFW>?FIFQV, >KA FQ RPBP > CFUBA PFWB LC 10. 7B KBBA QL ABPFDK >K BU>JMIB QE>Q CFQP TFQE LRO M>OQF@IB PVPQBJ P@BK>OFL, TEBOB TB BJFQ > @LKQFKRLRP PQOB>J LC .;LNC=F? L?GB@QP, >AAFKD LKB KBT M>OQF@IB TFQE B>@E @V@IB QEOLRDE >L;Q() . 7BgII PHFM OBE>PEFKD QEB .;LNC=F? @I>PP @LAB EBOB, >P FQ ALBPKgQ KBBA QL @E>KDB.
ALL;S*CMN<.;LNC=F?> J;LNC=F?M; PIC> M?NOJ() U MCT?(640,360); J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>(); W PIC> >L;Q() U <;=EALIOH>(255); J;LNC=F?M.;>>(H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,50))); @IL (CHN C = 0; C < J;LNC=F?M.MCT?(); C++) U .;LNC=F? J = J;LNC=F?M.A?N(C); J.LOH(); W W A new Particle object is added to the ArrayList every cycle through draw().

2RK QEB >?LSB @LAB CLO > CBT JFKRQBP >KA VLRgII PQ>OQ QL PBB QEB CO>JB O>QB PILT ALTK CROQEBO >KA CROQEBO RKQFI QEB MOLDO>J DOFKAP QL > E>IQ (JV QBPQP VFBIABA ELOOFCF@ MBOCLOJ>K@B >CQBO CFCQBBK JFKRQBP). 4EB FPPRB LC @LROPB FP QE>Q TB >OB @OB>QFKD JLOB >KA JLOB M>OQF@IBP TFQELRQ OBJLSFKD >KV. &LOQRK>QBIV, QEB ALL;S*CMN @I>PP E>P > @LKSBKFBKQ L?GIP?() CRK@QFLK QE>Q >IILTP RP QL ABIBQB > M>OQF@IB (?V OBCBOBK@FKD FQP FKABU). 4EFP FP TEV TB @>KKLQ RPB QEB KBT BKE>K@BA @IL ILLM TB GRPQ IB>OKBA; QEB BKE>K@BA ILLM MOLSFABP KL JB>KP CLO ABIBQFKD BIBJBKQP TEFIB FQBO>QFKD. (BOB, TB T>KQ QL @>II L?GIP?() TEBK QEB M>OQF@IBgP CM"?;>() CRK@QFLK OBQROKP QORB.
@IL (CHN C = 0; C < J;LNC=F?M.MCT?(); C++) U .;LNC=F? J = J;LNC=F?M.A?N(C); J.LOH(); C@ (J.CM"?;>()) U J;LNC=F?M.L?GIP?(C); W W If the particle is dead, we can go ahead and delete it from the list.

151

#E>MQBO 4. 0>OQF@IB 3VPQBJP

!IQELRDE QEB >?LSB @LAB TFII ORK GRPQ CFKB (>KA QEB MOLDO>J TFII KBSBO DOFKA QL > E>IQ), TB E>SB LMBKBA RM > JBAFRJ-PFWBA @>K LC TLOJP. 7EBKBSBO TB J>KFMRI>QB QEB @LKQBKQP LC > IFPQ TEFIB FQBO>QFKD QEOLRDE QE>Q SBOV IFPQ, TB @>K DBQ LROPBISBP FKQL QOLR?IB. 4>HB, CLO BU>JMIB, QEB CLIILTFKD @LAB.
@IL (CHN C = 0; C < J;LNC=F?M.MCT?(); C++) U .;LNC=F? J = J;LNC=F?M.A?N(C); J.LOH(); J;LNC=F?M.;>>(H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,50))); W Adding a new Particle to the list while iterating?

4EFP FP > PLJBTE>Q BUQOBJB BU>JMIB (TFQE CI>TBA ILDF@), ?RQ FQ MOLSBP QEB MLFKQ. )K QEB >?LSB @>PB, CLO B>@E M>OQF@IB FK QEB IFPQ, TB >AA > KBT M>OQF@IB QL QEB IFPQ (J>KFMRI>QFKD QEB MCT?() LC QEB ALL;S*CMN ). 4EFP TFII OBPRIQ FK >K FKCFKFQB ILLM, >P C @>K KBSBO FK@OBJBKQ M>PQ QEB PFWB LC QEB ALL;S*CMN . 7EFIB OBJLSFKD BIBJBKQP COLJ QEB ALL;S*CMN AROFKD > ILLM ALBPKgQ @>RPB QEB MOLDO>J QL @O>PE (>P FQ ALBP TFQE >AAFKD), QEB MOL?IBJ FP >IJLPQ JLOB FKPFAFLRP FK QE>Q FQ IB>SBP KL BSFABK@B. 4L AFP@LSBO QEB MOL?IBJ TB JRPQ CFOPQ BPQ>?IFPE >K FJMLOQ>KQ C>@Q. 7EBK >K L?GB@Q FP OBJLSBA COLJ QEB ALL;S*CMN , >II BIBJBKQP >OB PEFCQBA LKB PMLQ QL QEB IBCQ. .LQB QEB AF>DO>J ?BILT TEBOB M>OQF@IB # (FKABU 2) FP OBJLSBA. 0>OQF@IBP ! >KA " HBBM QEB P>JB FKABU, TEFIB M>OQF@IBP $ >KA % PEFCQ COLJ 3 >KA 4 QL 2 >KA 3, OBPMB@QFSBIV.

F64B?2 4.1

,BQgP MOBQBKA TB >OB C ILLMFKD QEOLRDE QEB ALL;S*CMN .


M>;D ? = 0 X C>;9A F7HJ?9B; A X M>;D ? = 1 X C>;9A F7HJ?9B; B X M>;D ? = 2 X C>;9A F7HJ?9B; C X -B?:; F7HJ?9B;I M>;D ? = 3 X C>;9A F7HJ?9B; ! X E DEJ :;B;J; E DEJ :;B;J; ;B;J;! 7D: ! 879A <HEC IBEJI 3 7D: 4 JE 2 7D: 3 E DEJ :;B;J;

.LQF@B QEB MOL?IBJ? 7B KBSBO @EB@HBA M>OQF@IB $! 7EBK # T>P ABIBQBA COLJ PILQ #2, $ JLSBA FKQL PILQ #2, ?RQ F E>P >IOB>AV JLSBA LK QL PILQ # 3. 4EFP FP KLQ > AFP>PQBO, PFK@B 152

4EB .>QROB LC #LAB (S1.0)

M>OQF@IB $ TFII DBQ @EB@HBA QEB KBUQ QFJB >OLRKA. 3QFII, QEB BUMB@Q>QFLK FP QE>Q TB >OB TOFQFKD @LAB QL FQBO>QB QEOLRDE BSBOV PFKDIB BIBJBKQ LC QEB ALL;S*CMN . 3HFMMFKD >K BIBJBKQ FP RK>@@BMQ>?IB. 4EBOB >OB QTL PLIRQFLKP QL QEFP MOL?IBJ. 4EB CFOPQ PLIRQFLK FP QL PFJMIV FQBO>QB QEOLRDE QEB ALL;S*CMN ?>@HT>OAP. )C VLR >OB PIFAFKD BIBJBKQP COLJ OFDEQ QL IBCQ >P BIBJBKQP >OB OBJLSBA, FQgP FJMLPPF?IB QL PHFM >K BIBJBKQ ?V >@@FABKQ. (BOBgP ELT QEB @LAB TLRIA ILLH:
<EH (?DJ ? = F7HJ?9B;I.I?P;()-1; ? >= 0; ?--) Q .;LNC=F? J = (.;LNC=F?) J;LNC=F?M.A?N(C); J.LOH(); C@ (J.CM"?;>()) U J;LNC=F?M.L?GIP?(C); W W Looping through the list backwards

4EFP FP > MBOCB@QIV CFKB PLIRQFLK FK KFKBQV-KFKB @>PBP LRQ LC > ERKAOBA. "RQ PLJBQFJBP, QEB LOABO FK TEF@E QEB BIBJBKQP >OB AO>TK @LRIA ?B FJMLOQ>KQ >KA VLR J>V KLQ T>KQ QL FQBO>QB ?>@HT>OAP. *>S> MOLSFABP > PMB@F>I @I>PPe'N?L;NIL eQE>Q Q>HBP @>OB LC >II LC QEB ABQ>FIP LC FQBO>QFLK CLO VLR. 9LR DBQ QL P>V: $9M, %S8 @=?9 HC =H9F5H9 H<FCI;< H<=G ALL;S*CMN . CCI@8 MCI 7CBH=BI9 HC ;=J9 A9 H<9 B9LH 9@9A9BH =B H<9 @=GH CB9 5H 5 H=A9 IBH=@ K9 ;9H HC H<9 9B8? AB8 =: % F9ACJ9 9@9A9BHG CF ACJ9 H<9A 5FCIB8 =B H<9 @=GH K<=@9 K9SF9 =H9F5H=B;, K=@@ MCI A5?9 GIF9 % 8CBSH @CC? 5H 5BM 9@9A9BHG HK=79 CF G?=D 5BM 6M 577=89BH? !K ALL;S*CMN @>K MOLAR@B >K 'N?L;NIL L?GB@Q CLO VLR.
'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL(); Note that with the Iterator object, we can also use the new <ClassName> generics syntax and specify the type that the Iterator will reference.

/K@B VLRgSB DLQ QEB FQBO>QLO, QEB B;M,?RN() CRK@QFLK TFII QBII RP TEBQEBO QEBOB FP > .;LNC=F? CLO RP QL ORK >KA QEB H?RN() CRK@QFLK TFII DO>? QE>Q .;LNC=F? L?GB@Q FQPBIC.
QBCF? (CN.B;M,?RN()) U .;LNC=F? J = CN.H?RN(); J.LOH(); An Iterator object doing the iterating for you

!KA FC VLR @>II QEB L?GIP?() CRK@QFLK LK QEB 'N?L;NIL L?GB@Q AROFKD QEB ILLM, FQ TFII ABIBQB QEB @ROOBKQ .;LNC=F? L?GB@Q (>KA KLQ PHFM >EB>A M>PQ QEB KBUQ LKB, >P TB P>T TFQE @LRKQFKD CLOT>OA QEOLRDE QEB ALL;S*CMN ).
C@ (J.CM"?;>()) U

153

#E>MQBO 4. 0>OQF@IB 3VPQBJP

CN.L?GIP?(); W W

An Iterator object doing the deleting for you

0RQQFKD FQ >II QLDBQEBO, TB E>SB:

Example 4.2: ArrayList of particles with Iterator


ALL;S*CMN<.;LNC=F?> J;LNC=F?M; PIC> M?NOJ() U MCT?(640,360); J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>(); W PIC> >L;Q() U <;=EALIOH>(255); J;LNC=F?M.;>>(H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,50))); 'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL(); QBCF? (CN.B;M,?RN()) U .;LNC=F? J = CN.H?RN(); J.LOH(); C@ (J.CM"?;>()) U CN.L?GIP?(); W W W Using an Iterator object instead of counting with int i

154

4EB .>QROB LC #LAB (S1.0)

4.4 The Particle System Class


/+. .LT TBgSB ALKB QTL QEFKDP. 7BgSB TOFQQBK > @I>PP QL ABP@OF?B >K FKAFSFAR>I .;LNC=F? L?GB@Q. 7BgSB @LKNRBOBA QEB ALL;S*CMN >KA RPBA FQ QL J>K>DB > IFPQ LC J>KV .;LNC=F? L?GB@QP (TFQE QEB >?FIFQV QL >AA >KA ABIBQB >Q TFII). 7B @LRIA PQLM EBOB. (LTBSBO, LKB >AAFQFLK>I PQBM TB @>K >KA PELRIA Q>HB FP QL TOFQB > @I>PP QL ABP@OF?B QEB IFPQ LC .;LNC=F? L?GB@QP FQPBICeQEB .;LNC=F?0SMN?G @I>PP. 4EFP TFII >IILT RP QL OBJLSB QEB ?RIHV ILDF@ LC ILLMFKD QEOLRDE >II M>OQF@IBP COLJ QEB J>FK Q>?, >P TBII >P LMBK RM QEB MLPPF?FIFQV LC E>SFKD JLOB QE>K LKB M>OQF@IB PVPQBJ. )C VLR OB@>II QEB DL>I TB PBQ >Q QEB ?BDFKKFKD LC QEFP @E>MQBO, TB T>KQBA LRO J>FK Q>? QL ILLH IFHB QEFP:
.;LNC=F?0SMN?G JM; PIC> M?NOJ() U MCT?(640,360); JM = H?Q .;LNC=F?0SMN?G(); W PIC> >L;Q() U <;=EALIOH>(255); JM.LOH(); W Just one wee ParticleSystem!

,BQgP Q>HB QEB @LAB COLJ %U>JMIB 4.2 >KA OBSFBT > ?FQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKD, ILLHFKD >Q ELT B>@E MFB@B COLJ QEB J>FK Q>? @>K CFQ FKQL QEB .;LNC=F?0SMN?G @I>PP.

155

#E>MQBO 4. 0>OQF@IB 3VPQBJP

ArrayList in the main tab

ArrayList in the ParticleSystem class


=F;MM .;LNC=F?0SMN?G U AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I; .;LNC=F?0SMN?G() U F7HJ?9B;I = D;M AHH7O'?IJ<+7HJ?9B;>(); W

AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I; PIC> M?NOJ() U MCT?(640,360); F7HJ?9B;I = D;M AHH7O'?IJ<+7HJ?9B;>(); W PIC> >L;Q() U <;=EALIOH>(255); F7HJ?9B;I.7::(D;M +7HJ?9B;());

PIC> ;>>.;LNC=F?() U F7HJ?9B;I.7::(D;M +7HJ?9B;()); W PIC> LOH() U %J;H7JEH<+7HJ?9B;> ?J = F7HJ?9B;I.?J;H7JEH(); M>?B; (?J.>7I);NJ()) Q +7HJ?9B; F = ?J.D;NJ(); F.HKD(); ?< (F.?I ;7:()) Q ?J.H;CEL;(); S S W W

%J;H7JEH<+7HJ?9B;> ?J = F7HJ?9B;I.?J;H7JEH(); M>?B; (?J.>7I);NJ()) Q +7HJ?9B; F = ?J.D;NJ(); F.HKD(); ?< (F.?I ;7:()) Q ?J.H;CEL;(); S S W

7B @LRIA >IPL >AA PLJB KBT CB>QROBP QL QEB M>OQF@IB PVPQBJ FQPBIC. &LO BU>JMIB, FQ JFDEQ ?B RPBCRI CLO QEB .;LNC=F?0SMN?G @I>PP QL HBBM QO>@H LC >K LOFDFK MLFKQ TEBOB M>OQF@IBP >OB J>AB. 4EFP CFQP FK TFQE QEB FAB> LC > M>OQF@IB PVPQBJ ?BFKD >K hBJFQQBO,i > MI>@B TEBOB M>OQF@IBP >OB ?LOK >KA PBKQ LRQ FKQL QEB TLOIA. 4EB LOFDFK MLFKQ PELRIA ?B FKFQF>IFWBA FK QEB @LKPQOR@QLO. Example 4.3: Simple Single Particle System
=F;MM .;LNC=F?0SMN?G U ALL;S*CMN J;LNC=F?M; +0;9JEH EH?=?D; .;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U ILCACH = FI=;NCIH.A?N(); J;LNC=F?M = H?Q ALL;S*CMN(); W PIC> ;>>.;LNC=F?() U F7HJ?9B;I.7::(D;M +7HJ?9B;(EH?=?D)); W The origin is passed to each Particle when it is added. This particular ParticleSystem implementation includes an origin point where each Particle begins.

156

4EB .>QROB LC #LAB (S1.0)

Exercise 4.3
->HB QEB LOFDFK MLFKQ JLSB AVK>JF@>IIV. (>SB QEB M>OQF@IBP BJFQ COLJ QEB JLRPB IL@>QFLK LO RPB QEB @LK@BMQP LC SBIL@FQV >KA >@@BIBO>QFLK QL J>HB QEB PVPQBJ JLSB >RQLKLJLRPIV.

Exercise 4.4
"RFIAFKD LCC #E>MQBO 3gP h!PQBOLFAPi BU>JMIB, RPB > M>OQF@IB PVPQBJ QL BJFQ M>OQF@IBP COLJ QEB PEFMgP hQEORPQBOPi TEBKBSBO > QEORPQ CLO@B FP >MMIFBA. 4EB M>OQF@IBPg FKFQF>I SBIL@FQV PELRIA ?B OBI>QBA QL QEB PEFMgP @ROOBKQ AFOB@QFLK.

4.5 A System of Systems


,BQgP OBSFBT CLO > JLJBKQ TEBOB TB >OB. 7B HKLT ELT QL Q>IH >?LRQ >K FKAFSFAR>I .;LNC=F? L?GB@Q. 7B >IPL HKLT ELT QL Q>IH >?LRQ > PVPQBJ LC .;LNC=F? L?GB@QP, >KA QEFP TB @>II > hM>OQF@IB PVPQBJ.i !KA TBgSB ABCFKBA > M>OQF@IB PVPQBJ >P > @LIIB@QFLK LC FKABMBKABKQ L?GB@QP. "RQ FPKgQ > M>OQF@IB PVPQBJ FQPBIC >K L?GB@Q? )C QE>QgP QEB @>PB (TEF@E FQ FP), QEBOBgP KL OB>PLK TEV TB @LRIAKgQ >IPL E>SB > @LIIB@QFLK LC J>KV M>OQF@IB PVPQBJP, F.B. > PVPQBJ LC PVPQBJP. 4EFP IFKB LC QEFKHFKD @LRIA LC @LROPB Q>HB RP BSBK CROQEBO, >KA VLR JFDEQ IL@H VLROPBIC FK > ?>PBJBKQ CLO A>VP PHBQ@EFKD LRQ > AF>DO>J LC > PVPQBJ LC PVPQBJP LC PVPQBJP LC PVPQBJP LC PVPQBJP LC PVPQBJP. /C PVPQBJP. !CQBO >II, QEFP FP ELT QEB TLOIA TLOHP. !K LOD>K FP > PVPQBJ LC @BIIP, > ERJ>K ?LAV FP > PVPQBJ LC LOD>KP, > KBFDE?LOELLA FP > PVPQBJ LC ERJ>K ?LAFBP, > @FQV FP > PVPQBJ LC KBFDE?LOELLAP, >KA PL LK >KA PL CLOQE. 7EFIB QEFP FP >K FKQBOBPQFKD OL>A QL QO>SBI ALTK, FQgP > ?FQ ?BVLKA TEBOB TB KBBA QL ?B OFDEQ KLT. )Q FP, ELTBSBO, NRFQB RPBCRI QL HKLT ELT QL TOFQB > 0OL@BPPFKD PHBQ@E QE>Q HBBMP QO>@H LC J>KV M>OQF@IB PVPQBJP, B>@E LC TEF@E HBBM QO>@H LC J>KV M>OQF@IBP. ,BQgP Q>HB QEB CLIILTFKD P@BK>OFL. 9LR PQ>OQ TFQE > ?I>KH P@OBBK.

157

#E>MQBO 4. 0>OQF@IB 3VPQBJP

9LR @IF@H QEB JLRPB >KA DBKBO>QB > M>OQF@IB PVPQBJ >Q QEB JLRPBgP IL@>QFLK.

%>@E QFJB VLR @IF@H QEB JLRPB, > KBT M>OQF@IB PVPQBJ FP @OB>QBA >Q QEB JLRPBgP IL@>QFLK.

)K %U>JMIB 4.3 (PBB M>DB 156), TB PQLOBA > PFKDIB OBCBOBK@B QL > .;LNC=F?0SMN?G L?GB@Q FK QEB S>OF>?IB JM .
.;LNC=F?0SMN?G JM; PIC> M?NOJ() U MCT?(640,360); JM = H?Q .;LNC=F?0SMN?G(1,H?Q .3?=NIL(QC>NB/2,50)); W PIC> >L;Q() U <;=EALIOH>(255); JM.LOH(); JM.;>>.;LNC=F?(); W

&LO QEFP KBT BU>JMIB, TE>Q TB T>KQ QL AL FKPQB>A FP @OB>QB >K ALL;S*CMN QL HBBM QO>@H LC JRIQFMIB FKPQ>K@BP LC M>OQF@IB PVPQBJP. 7EBK QEB MOLDO>J PQ>OQP, F.B. FK M?NOJ() , QEB ALL;S*CMN FP BJMQV.

158

4EB .>QROB LC #LAB (S1.0)

Example 4.4: System of systems


ALL;S*CMN<.;LNC=F?0SMN?G> MSMN?GM; PIC> M?NOJ() U MCT?(600,200); MSMN?GM = H?Q ALL;S*CMN<.;LNC=F?0SMN?G>(); W This time, the type of thing we are putting in the ArrayList is a ParticleSystem itself!

7EBKBSBO QEB JLRPB FP MOBPPBA, > KBT .;LNC=F?0SMN?G L?GB@Q FP @OB>QBA >KA MI>@BA FKQL QEB ALL;S*CMN .
PIC> GIOM?.L?MM?>() U MSMN?GM.;>>(H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(GIOM?5,GIOM?6))); W

!KA FK >L;Q() , FKPQB>A LC OBCBOBK@FKD > PFKDIB .;LNC=F?0SMN?G L?GB@Q, TB KLT ILLH QEOLRDE >II QEB PVPQBJP FK QEB ALL;S*CMN >KA @>II LOH() LK B>@E LC QEBJ.
PIC> >L;Q() U <;=EALIOH>(255); @IL (.;LNC=F?0SMN?G JM: MSMN?GM) U JM.LOH(); JM.;>>.;LNC=F?(); W W Since we arent deleting elements, we can use our enhanced loop!

Exercise 4.5
2BTOFQB %U>JMIB 4.4 PL QE>Q B>@E M>OQF@IB PVPQBJ ALBPKgQ IFSB CLOBSBO. 7EBK > M>OQF@IB PVPQBJ FP BJMQV (F.B. E>P KL M>OQF@IBP IBCQ FK FQP ALL;S*CMN ), OBJLSB FQ COLJ QEB ALL;S*CMN MSMN?GM .

Exercise 4.6
#OB>QB > PFJRI>QFLK LC >K L?GB@Q PE>QQBOFKD FKQL J>KV MFB@BP. (LT @>K VLR QROK LKB I>ODB PE>MB FKQL J>KV PJ>II M>OQF@IBP? 7E>Q FC QEBOB >OB PBSBO>I I>ODB PE>MBP LK QEB P@OBBK >KA QEBV PE>QQBO TEBK VLR @IF@H LK QEBJ?

159

#E>MQBO 4. 0>OQF@IB 3VPQBJP

4.6 Inheritance and Polymorphism: An Introduction


9LR J>V E>SB BK@LRKQBOBA QEB QBOJP =B<9F=H5B79 >KA DC@MACFD<=GA FK VLRO MOLDO>JJFKD IFCB ?BCLOB QEFP ?LLH. !CQBO >II, QEBV >OB QTL LC QEB QEOBB CRKA>JBKQ>I MOFK@FMIBP ?BEFKA QEB QEBLOV LC L?GB@Q-LOFBKQBA MOLDO>JJFKD (QEB LQEBO ?BFKD 9B75DGI@5H=CB ). )C VLRgSB OB>A LQEBO 0OL@BPPFKD LO *>S> MOLDO>JJFKD ?LLHP, @E>K@BP >OB FQgP ?BBK @LSBOBA. -V ?BDFKKBO QBUQ, (95FB=B; ,FC79GG=B;, E>P @ILPB QL >K BKQFOB @E>MQBO (#22) ABAF@>QBA QL QEBPB QTL QLMF@P. 3QFII, MBOE>MP VLRgSB LKIV IB>OKBA >?LRQ FQ FK QEB >?PQO>@Q PBKPB >KA KBSBO E>A > OB>PLK QL OB>IIV RPB FKEBOFQ>K@B >KA MLIVJLOMEFPJ. )C QEFP FP QORB, VLRgSB @LJB QL QEB OFDEQ MI>@B. 7FQELRQ QEBPB QTL QLMF@P, VLRO >?FIFQV QL MOLDO>J > S>OFBQV LC M>OQF@IBP >KA M>OQF@IB PVPQBJP FP BUQOBJBIV IFJFQBA. ()K QEB KBUQ @E>MQBO, TBgII >IPL PBB ELT RKABOPQ>KAFKD QEBPB QLMF@P TFII EBIM RP QL RPB MEVPF@P IF?O>OFBP.) )J>DFKB QEB CLIILTFKD. )QgP > 3>QROA>V JLOKFKD, VLRgSB GRPQ DLKB LRQ CLO > ILSBIV GLD, E>A > ABIF@FLRP ?LTI LC @BOB>I, >KA >OB PFQQFKD NRFBQIV >Q VLRO @LJMRQBO TFQE > @RM LC T>OJ @E>JLJFIB QB>. )QgP VLRO LIA COFBKA 3L >KA 3LgP ?FOQEA>V >KA VLRgSB AB@FABA VLRgA IFHB QL J>HB > DOBBQFKD @>OA FK 0OL@BPPFKD. (LT >?LRQ PLJB @LKCBQQF CLO > ?FOQEA>V? 0ROMIB @LKCBQQF, MFKH @LKCBQQF, PQ>O-PE>MBA @LKCBQQF, PNR>OB @LKCBQQF, C>PQ @LKCBQQF, CIRQQBOV @LKCBQQF, BQ@. !II LC QEBPB MFB@BP LC @LKCBQQF TFQE AFCCBOBKQ >MMB>O>K@BP >KA AFCCBOBKQ ?BE>SFLOP BUMILAB LKQL QEB P@OBBK >Q LK@B. 7E>Q TBgSB DLQ EBOB FP @IB>OIV > M>OQF@IB PVPQBJe> @LIIB@QFLK LC FKAFSFAR>I MFB@BP LC @LKCBQQF (F.B. M>OQF@IBP). 7B JFDEQ ?B >?IB QL @IBSBOIV ABPFDK LRO .;LNC=F? @I>PP QL E>SB S>OF>?IBP QE>Q PQLOB FQP @LILO, PE>MB, ?BE>SFLO, BQ@. !KA MBOE>MP TB FKFQF>IFWB QEB S>IRBP LC QEBPB S>OF>?IBP O>KALJIV. "RQ TE>Q FC VLRO M>OQF@IBP >OB AO>PQF@>IIV AFCCBOBKQ? 4EFP @LRIA ?B@LJB SBOV JBPPV, E>SFKD >II PLOQP LC @LAB CLO AFCCBOBKQ T>VP LC ?BFKD > M>OQF@IB FK QEB P>JB @I>PP. 7BII, VLR JFDEQ @LKPFABO ALFKD QEB CLIILTFKD:
=F;MM &;JJS!IH@?NNC U W =F;MM $OH!IH@?NNC U W =F;MM 4;=ES!IH@?NNC U W

4EFP FP > KF@B PLIRQFLK: TB E>SB QEOBB AFCCBOBKQ @I>PPBP QL ABP@OF?B QEB AFCCBOBKQ HFKAP LC MFB@BP LC @LKCBQQF QE>Q @LRIA ?B M>OQ LC LRO M>OQF@IB PVPQBJ. 4EB .;LNC=F?0SMN?G @LKPQOR@QLO @LRIA QEBK E>SB PLJB @LAB QL MF@H O>KALJIV COLJ QEB QEOBB @I>PPBP TEBK CFIIFKD QEB ALL;S*CMN . .LQB QE>Q QEFP MOL?>?FIFPQF@ JBQELA FP QEB P>JB LKB TB BJMILVBA FK LRO O>KALJ T>IH BU>JMIBP FK QEB )KQOLAR@QFLK (PBB M>DB 2).

160

4EB .>QROB LC #LAB (S1.0)


=F;MM .;LNC=F?0SMN?G U .;LNC=F?0SMN?G(CHN HOG) U J;LNC=F?M = H?Q ALL;S*CMN(); @IL (CHN C = 0; C < HOG; C++) U @FI;N L = L;H>IG(1); Randomly picking a "kind" of particle C@ (L < 0.33) U J;LNC=F?M.;>>(H?Q &;JJS!IH@?NNC()); W ?FM? C@ (L < 0.67) U J;LNC=F?M.;>>(H?Q $OH!IH@?NNC()); W ?FM? U J;LNC=F?M.;>>(H?Q 4;=ES!IH@?NNC()); W W W

/+, TB KLT KBBA QL M>RPB CLO > JLJBKQ. 7BgSB ALKB KLQEFKD TOLKD. !II TB T>KQBA QL AL T>P TFPE LRO COFBKA > E>MMV ?FOQEA>V >KA BKGLV TOFQFKD PLJB @LAB. "RQ TEFIB QEB OB>PLKFKD ?BEFKA QEB >?LSB >MMOL>@E FP NRFQB PLRKA, TBgSB LMBKBA RM QTL J>GLO MOL?IBJP.

0OL?IBJ #1: !OBKgQ TB DLFKD QL ?B @LMVFKD/M>PQFKD > ILQ LC @LAB ?BQTBBK QEB AFCCBOBKQ h@LKCBQQFi @I>PPBP?

9BP. %SBK QELRDE LRO HFKAP LC M>OQF@IBP >OB AFCCBOBKQ BKLRDE QL JBOFQ LRO ?OB>HFKD QEBJ LRQ FKQL PBM>O>QB @I>PPBP, QEBOB FP PQFII > QLK LC @LAB QE>Q QEBV TFII IFHBIV PE>OB. 4EBVgII >II E>SB .3?=NIL P QL HBBM QO>@H LC IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK; >K OJ>;N?() CRK@QFLK QE>Q FJMIBJBKQP LRO JLQFLK >IDLOFQEJ; BQ@. 4EFP FP TEBOB 4930=4?,9.0 @LJBP FK. )KEBOFQ>K@B >IILTP RP QL TOFQB > @I>PP QE>Q =B<9F=HG S>OF>?IBP >KA CRK@QFLKP COLJ >KLQEBO @I>PP, >II QEB TEFIB FJMIBJBKQFKD FQP LTK @RPQLJ CB>QROBP.

0OL?IBJ #2: (LT TFII QEB !OO>V,FPQ HKLT TEF@E L?GB@QP >OB TEF@E QVMB?

4EFP FP > MOBQQV PBOFLRP MOL?IBJ. 2BJBJ?BO, TB TBOB RPFKD DBKBOF@P QL QBII QEB ALL;S*CMN TE>Q QVMB LC L?GB@QP TBgOB DLFKD QL MRQ FKPFAB FQ. !OB TB PRAABKIV DLFKD QL KBBA QEOBB AFCCBOBKQ ALL;S*CMN P?
ALL;S*CMN<&;JJS!IH@?NNC> ;1 = H?Q ALL;S*CMN<&;JJS!IH@?NNC>(); ALL;S*CMN<$OH!IH@?NNC> ;2 = H?Q ALL;S*CMN<$OH!IH@?NNC>(); ALL;S*CMN<4;=ES!IH@?NNC> ;3 = H?Q ALL;S*CMN<4;=ES!IH@?NNC>();

4EFP PBBJP >TCRIIV FK@LKSBKFBKQ, DFSBK QE>Q TB OB>IIV GRPQ T>KQ LKB IFPQ QL HBBM QO>@H LC >II QEB PQRCC FK QEB M>OQF@IB PVPQBJ. 4E>Q @>K ?B J>AB MLPPF?IB TFQE MLIVJLOMEFPJ. 0LIVJLOMEFPJ TFII >IILT RP QL @LKPFABO L?GB@QP LC AFCCBOBKQ QVMBP >P QEB P>JB QVMB >KA PQLOB QEBJ FK > PFKDIB ALL;S*CMN . 161

#E>MQBO 4. 0>OQF@IB 3VPQBJP

.LT QE>Q TB RKABOPQ>KA QEB MOL?IBJ, IBQgP ILLH >Q QEBPB QTL @LK@BMQP FK > ?FQ JLOB ABQ>FI >KA QEBK @OB>QB > M>OQF@IB PVPQBJ BU>JMIB QE>Q FJMIBJBKQP ?LQE FKEBOFQ>K@B >KA MLIVJLOMEFPJ.

4.7 Inheritance Basics


,BQgP Q>HB > AFCCBOBKQ BU>JMIB, QEB TLOIA LC >KFJ>IP: ALDP, @>QP, JLKHBVP, M>KA>P, TLJ?>QP, >KA PB> KBQQIBP. 7BgII PQ>OQ ?V MOLDO>JJFKD > "IA @I>PP. ! "IA L?GB@Q TFII E>SB >K >DB S>OF>?IB (>K FKQBDBO), >P TBII >P ?;N() , MF??J() , >KA <;LE() CRK@QFLKP.
=F;MM "IA U CHN ;A?; "IA() U ;A? = 0; W PIC> ?;N() U JLCHNFH("6OG!"); W PIC> MF??J() U JLCHNFH("7TTTTT"); W Dogs and cats have the same variables (age) and functions (eat, sleep).

PIC> <;LE() U JLCHNFH("4--$!"); W W

A unique function for barking.

.LT, IBQgP JLSB LK QL @>QP.

162

4EB .>QROB LC #LAB (S1.0)


=F;MM !;N U CHN ;A?; !;N() U ;A? = 0; W PIC> ?;N() U JLCHNFH("6OG!"); W PIC> MF??J() U JLCHNFH("7TTTTT"); W PIC> G?IQ() U JLCHNFH("+#-4!"); W W

!P TB OBTOFQB QEB P>JB @LAB CLO CFPE, ELOPBP, HL>I>P, >KA IBJROP, QEFP MOL@BPP TFII ?B@LJB O>QEBO QBAFLRP. )KPQB>A, IBQgP ABSBILM > DBKBOF@ AHCG;F @I>PP QE>Q @>K ABP@OF?B >KV QVMB LC >KFJ>I. !II >KFJ>IP B>Q >KA PIBBM, >CQBO >II. 7B @LRIA QEBK P>V: j ! ALD FP >K >KFJ>I >KA E>P >II QEB MOLMBOQFBP LC >KFJ>IP >KA @>K AL >II QEB QEFKDP >KFJ>IP AL. !IPL, > ALD @>K ?>OH. j ! @>Q FP >K >KFJ>I >KA E>P >II QEB MOLMBOQFBP LC >KFJ>IP >KA @>K AL >II QEB QEFKDP >KFJ>IP AL. !IPL, > @>Q @>K JBLT. )KEBOFQ>K@B J>HBP QEFP >II MLPPF?IB. 7FQE FKEBOFQ>K@B, @I>PPBP @>K FKEBOFQ MOLMBOQFBP (S>OF>?IBP) >KA CRK@QFLK>IFQV (JBQELAP) COLJ LQEBO @I>PPBP. ! "IA @I>PP FP > @EFIA ( >@-.7,>> ) LC >K AHCG;F @I>PP. #EFIAOBK TFII >RQLJ>QF@>IIV FKEBOFQ >II S>OF>?IBP >KA CRK@QFLKP COLJ QEB M>OBKQ (>@;0=.7,>> ), ?RQ @>K >IPL FK@IRAB CRK@QFLKP >KA S>OF>?IBP KLQ CLRKA FK QEB M>OBKQ. ,FHB > MEVILDBKBQF@ "QOBB LC IFCB," FKEBOFQ>K@B CLIILTP > QOBB PQOR@QROB. $LDP FKEBOFQ COLJ @>KFKBP, TEF@E FKEBOFQ COLJ J>JJ>IP, TEF@E FKEBOFQ COLJ >KFJ>IP, BQ@.

F64B?2 4.2

163

#E>MQBO 4. 0>OQF@IB 3VPQBJP

(BOB FP ELT QEB PVKQ>U TLOHP TFQE FKEBOFQ>K@B.


=F;MM AHCG;F U The Animal class is the parent (or super) class. Dog and Cat inherit the variable age.

CHN ;A?; AHCG;F() U ;A? = 0; W PIC> ?;N() U JLCHNFH("6OG!"); W PIC> MF??J() U JLCHNFH("7TTTTT"); W W 9B7II E= ;NJ;D:I AD?C7B Q

Dog and Cat inherit the functions eat() and sleep().

"IA() U IKF;H(); W PIC> <;LE() U JLCHNFH("4--$!"); W W =F;MM !;N ?RN?H>M AHCG;F U !;N() U MOJ?L(); W PIC> G?IQ() U JLCHNFH("+#-4!"); W W

The Dog class is the child (or sub) class, indicated by the code "extends Animal". super() executes code found in the parent class. We define bark() in the child class, since it isn't part of the parent class.

4EFP ?OFKDP RM QTL KBT QBOJP: j 0C?09/> d 4EFP HBVTLOA FP RPBA QL FKAF@>QB > M>OBKQ CLO QEB @I>PP ?BFKD ABCFKBA. .LQB QE>Q @I>PPBP @>K LKIV BUQBKA CB9 @I>PP. (LTBSBO, @I>PPBP @>K BUQBKA @I>PPBP QE>Q BUQBKA LQEBO @I>PPBP, F.B. "IA ?RN?H>M AHCG;F , 1?LLC?L ?RN?H>M "IA . %SBOVQEFKD FP FKEBOFQBA >II QEB T>V ALTK QEB IFKB. j >@;0=() d 4EFP @>IIP QEB @LKPQOR@QLO FK QEB M>OBKQ @I>PP. )K LQEBO TLOAP, TE>QBSBO VLR AL FK QEB M>OBKQ @LKPQOR@QLO, AL PL FK QEB @EFIA @LKPQOR@QLO >P TBII. /QEBO @LAB @>K ?B TOFQQBK FKQL QEB @LKPQOR@QLO FK >AAFQFLK QL MOJ?L() . MOJ?L() @>K >IPL

164

4EB .>QROB LC #LAB (S1.0)

OB@BFSB >ODRJBKQP FC QEBOB FP > M>OBKQ @LKPQOR@QLO ABCFKBA TFQE J>Q@EFKD >ODRJBKQP. ! PR?@I>PP @>K ?B BUM>KABA QL FK@IRAB >AAFQFLK>I CRK@QFLKP >KA MOLMBOQFBP ?BVLKA TE>Q FP @LKQ>FKBA FK QEB PRMBO@I>PP. &LO BU>JMIB, IBQgP >PPRJB QE>Q > "IA L?GB@Q E>P > E>FO@LILO S>OF>?IB FK >AAFQFLK QL >DB, TEF@E FP PBQ O>KALJIV FK QEB @LKPQOR@QLO. 4EB @I>PP TLRIA KLT ILLH IFHB QEFP:
=F;MM "IA ?RN?H>M AHCG;F U =IFIL B;CL=IFIL; "IA() U MOJ?L(); B;CL=IFIL = =IFIL(L;H>IG(255)); W PIC> <;LE() U JLCHNFH("4--$!"); W W A child class can introduce new variables not included in the parent.

.LQB ELT QEB M>OBKQ @LKPQOR@QLO FP @>IIBA SF> MOJ?L() , TEF@E PBQP QEB >DB QL 0, ?RQ QEB E>FO@LILO FP PBQ FKPFAB QEB "IA @LKPQOR@QLO FQPBIC. )C > "IA L?GB@Q B>QP AFCCBOBKQIV QE>K > DBKBOF@ AHCG;F L?GB@Q, M>OBKQ CRK@QFLKP @>K ?B CJ9FF=889B ?V OBTOFQFKD QEB CRK@QFLK FKPFAB QEB PR?@I>PP.
=F;MM "IA ?RN?H>M AHCG;F U =IFIL B;CL=IFIL; "IA() U MOJ?L(); B;CL=IFIL = =IFIL(L;H>IG(255)); W PIC> ?;N() U A child can override a parent function if necessary. A Dog's specific eating characteristics

JLCHNFH("4II@! 4II@! 0FOLJ.") W PIC> <;LE() U JLCHNFH("4--$!"); W W

"RQ TE>Q FC > ALD B>QP QEB P>JB T>V >P > DBKBOF@ >KFJ>I, GRPQ TFQE PLJB BUQO> CRK@QFLK>IFQV? ! PR?@I>PP @>K ?LQE ORK QEB @LAB COLJ > M>OBKQ @I>PP >KA FK@LOMLO>QB @RPQLJ @LAB.

165

#E>MQBO 4. 0>OQF@IB 3VPQBJP


=F;MM "IA ?RN?H>M AHCG;F U =IFIL B;CL=IFIL; "IA() U MOJ?L(); B;CL=IFIL = =IFIL(L;H>IG(255)); W PIC> ?;N() U IKF;H.;7J(); Call eat() from Animal. A child can execute a function from the parent while adding its own code. Add some additional code for a Dog's specific eating characteristics.

JLCHNFH("4II@!!!"); W PIC> <;LE() U JLCHNFH("4--$!"); W W

4.8 Particles with Inheritance


.LT QE>Q TBgSB E>A >K FKQOLAR@QFLK QL QEB QEBLOV LC FKEBOFQ>K@B >KA FQP PVKQ>U, TB @>K ABSBILM > TLOHFKD BU>JMIB FK 0OL@BPPFKD ?>PBA LK LRO .;LNC=F? @I>PP. ,BQgP OBSFBT > PFJMIB .;LNC=F? FJMIBJBKQ>QFLK, CROQEBO PFJMIFCFBA COLJ %U>JMIB 4.1 (PBB M>DB 147):

166

4EB .>QROB LC #LAB (S1.0)


=F;MM .;LNC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; .;LNC=F?(.3?=NIL F) U ;==?F?L;NCIH = H?Q .3?=NIL(0,0.05); P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0)); FI=;NCIH = F.A?N(); W PIC> LOH() U OJ>;N?(); >CMJF;S(); W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); W PIC> >CMJF;S() U @CFF(0); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8); W W

.BUQ, TB @OB>QB > PR?@I>PP COLJ .;LNC=F? (IBQgP @>II FQ !IH@?NNC ). )Q TFII FKEBOFQ >II QEB FKPQ>K@B S>OF>?IBP >KA JBQELAP COLJ .;LNC=F? . 7B TOFQB > KBT @LKPQOR@QLO TFQE QEB K>JB !IH@?NNC >KA BUB@RQB QEB @LAB COLJ QEB M>OBKQ @I>PP ?V @>IIFKD MOJ?L() .
=F;MM !IH@?NNC ?RN?H>M .;LNC=F? U We could add variables for only Confetti here.

!IH@?NNC(.3?=NIL F) U MOJ?L(F); W

There is no code here because we inherit update() from parent. PIC> >CMJF;S() U L?=N+I>?(!#,1#/); @CFF(175); MNLIE?(0); L?=N(FI=;NCIH.R,FI=;NCIH.S,8,8); W W Override the display method.

167

#E>MQBO 4. 0>OQF@IB 3VPQBJP

,BQgP J>HB QEFP > ?FQ JLOB PLMEFPQF@>QBA. ,BQgP P>V TB T>KQ QL E>SB QEB !IH@?NNC M>OQF@IB OLQ>QB >P FQ CIFBP QEOLRDE QEB >FO. 7B @LRIA, LC @LROPB, JLABI >KDRI>O SBIL@FQV >KA >@@BIBO>QFLK >P TB AFA FK #E>MQBO 3. )KPQB>A, TBgII QOV > NRF@H >KA AFOQV PLIRQFLK. 7B HKLT > M>OQF@IB E>P >K L IL@>QFLK PLJBTEBOB ?BQTBBK 0 >KA QEB TFAQE LC QEB TFKALT. 7E>Q FC TB P>FA: TEBK QEB M>OQF@IBgP L IL@>QFLK FP 0, FQP OLQ>QFLK PELRIA ?B 0; TEBK FQP L IL@>QFLK FP BNR>I QL QEB TFAQE, FQP OLQ>QFLK PELRIA ?B BNR>I QL 14-:.' ? $LBP QEFP OFKD > ?BII? 7EBKBSBO TB E>SB > S>IRB TFQE LKB O>KDB QE>Q TB T>KQ QL J>M QL >KLQEBO O>KDB, TB @>K RPB 0OL@BPPFKDgP G;J() CRK@QFLK, TEF@E TB IB>OKBA >?LRQ FK QEB )KQOLAR@QFLK (PBB M>DB 17)!
@FI;N ;HAF? = G;J(FI=;NCIH.R,0,QC>NB,0,14-:.');

!KA GRPQ QL DFSB FQ > ?FQ JLOB PMFK, TB @>K >@QR>IIV J>M QEB >KDIBgP O>KDB COLJ 0 QL 14-:.' *2. ,BQgP ILLH >Q ELT QEFP @LAB CFQP FKQL QEB >CMJF;S() CRK@QFLK.
PIC> >CMJF;S() U @FI;N NB?N; = G;J(FI=;NCIH.R,0,QC>NB,0,14-:.'*2); L?=N+I>?(!#,1#/); @CFF(0,FC@?MJ;H); MNLIE?(0,FC@?MJ;H); JOMB+;NLCR(); NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S); LIN;N?(NB?N;); L?=N(0,0,8,8); JIJ+;NLCR(); W If we rotate() a shape in Processing, we need to familiarize ourselves with transformations. For more, visit: http://processing.org/learning/transform2d/

Exercise 4.7
)KPQB>A LC RPFKD G;J() QL @>I@RI>QB QEBQ>, ELT TLRIA VLR JLABI >KDRI>O SBIL@FQV >KA >@@BIBO>QFLK? .LT QE>Q TB E>SB > !IH@?NNC @I>PP QE>Q BUQBKAP LRO ?>PB .;LNC=F? @I>PP, TB KBBA QL CFDROB LRQ ELT LRO .;LNC=F?0SMN?G @I>PP @>K J>K>DB M>OQF@IBP LC AFCCBOBKQ QVMBP TFQEFK QEB P>JB PVPQBJ. 4L >@@LJMIFPE QEFP DL>I, IBQgP OBQROK QL QEB >KFJ>I HFKDALJ FKEBOFQ>K@B BU>JMIB >KA PBB ELT QEB @LK@BMQ BUQBKAP FKQL QEB TLOIA LC MLIVJLOMEFPJ.

4.9 Polymorphism Basics


7FQE QEB @LK@BMQ LC FKEBOFQ>K@B RKABO LRO ?BIQP, TB @>K FJ>DFKB ELT TB TLRIA MOLDO>J > AFSBOPB >KFJ>I HFKDALJ RPFKD ALL;S*CMN Pe>K >OO>V LC ALDP, >K >OO>V LC @>QP, LC QROQIBP, LC HFTFP, BQ@. COLIF@HFKD >?LRQ.

168

4EB .>QROB LC #LAB (S1.0)

ALL;S*CMN<"IA> >IAM = H?Q ALL;S*CMN<"IA>(); Separate ArrayLists for each animal ALL;S*CMN<!;N> =;NM = H?Q ALL;S*CMN<!;N>(); ALL;S*CMN<1OLNF?> NOLNF?M = H?Q ALL;S*CMN<1OLNF?>(); ALL;S*CMN<)CQC> ECQCM = H?Q ALL;S*CMN<)CQC>(); @IL (CHN C = 0; C < 10; C++) U >IAM.;>>(H?Q "IA()); W @IL (CHN C = 0; C < 15; C++) U =;NM.;>>(H?Q !;N()); W @IL (CHN C = 0; C < 6; C++) U NOLNF?M.;>>(H?Q 1OLNF?()); W @IL (CHN C = 0; C < 98; C++) U ECQCM.;>>(H?Q )CQC()); W

!P QEB A>V ?BDFKP, QEB >KFJ>IP >OB >II MOBQQV ERKDOV >KA >OB ILLHFKD QL B>Q. 3L FQgP LCC QL ILLMFKD QFJB (BKE>K@BA ILLMFKD QFJB!)k
@IL ("IA >: >IAM) U >.?;N(); W @IL (!;N =: =;NM) U =.?;N(); W @IL (1OLNF? N: NOLNF?M) U N.?;N(); W @IL ()CQC E: ECQCM) U E.?;N(); W Separate loops for each animal

4EFP TLOHP TBII, ?RQ >P LRO TLOIA BUM>KAP QL FK@IRAB J>KV JLOB >KFJ>I PMB@FBP, TBgOB DLFKD QL DBQ PQR@H TOFQFKD > ILQ LC FKAFSFAR>I ILLMP. )P QEFP OB>IIV KB@BPP>OV? !CQBO >II, QEB @OB>QROBP >OB >II >KFJ>IP, >KA QEBV >II IFHB QL B>Q. 7EV KLQ GRPQ E>SB LKB ALL;S*CMN LC AHCG;F L?GB@QP >KA CFII FQ TFQE >II AFCCBOBKQ ?=B8G LC >KFJ>IP?

169

#E>MQBO 4. 0>OQF@IB 3VPQBJP

ALL;S*CMN<AHCG;F> ECHA>IG = H?Q ALL;S*CMN<AHCG;F>(); Just one ArrayList for all the animals! @IL (CHN C = 0; C < 1000; C++) U C@ (C < 100) ECHA>IG.;>>(H?Q "IA()); ?FM? C@ (C < 400) ECHA>IG.;>>(H?Q !;N()); ?FM? C@ (C < 900) ECHA>IG.;>>(H?Q 1OLNF?()); ?FM? ECHA>IG.;>>(H?Q )CQC()); W @IL (AHCG;F ;: ECHA>IG) U ;.?;N(); W

4EB >?FIFQV QL QOB>Q > "IA L?GB@Q >P BFQEBO > JBJ?BO LC QEB "IA @I>PP LO QEB AHCG;F @I>PP (FQP M>OBKQ) FP >K BU>JMIB LC MLIVJLOMEFPJ. %:7D8:=;34>8 (COLJ QEB 'OBBH DC@MACFD<CG, JB>KFKD J>KV CLOJP) OBCBOP QL QEB QOB>QJBKQ LC > PFKDIB FKPQ>K@B LC >K L?GB@Q FK JRIQFMIB CLOJP. ! ALD FP @BOQ>FKIV > ALD, ?RQ PFK@B "IA ?RN?H>M AHCG;F , FQ @>K >IPL ?B @LKPFABOBA >K >KFJ>I. )K @LAB, TB @>K OBCBO QL FQ ?LQE T>VP.
"IA LIP?L = H?Q "IA(); AHCG;F MJIN = H?Q "IA();

!IQELRDE QEB PB@LKA IFKB LC @LAB JFDEQ FKFQF>IIV PBBJ QL SFLI>QB PVKQ>U ORIBP, ?LQE T>VP LC AB@I>OFKD > "IA L?GB@Q >OB IBD>I. %SBK QELRDE TB AB@I>OB MJIN >P >K AHCG;F L?GB@Q, TBgOB OB>IIV J>HFKD > "IA L?GB@Q >KA PQLOFKD FQ FK QEB MJIN S>OF>?IB. !KA TB @>K P>CBIV @>II >II LC QEB AHCG;F @I>PP JBQELAP LK PMLQ ?B@>RPB QEB ORIBP LC FKEBOFQ>K@B AF@Q>QB QE>Q > ALD @>K AL >KVQEFKD >K >KFJ>I @>K. 7E>Q FC QEB "IA @I>PP, ELTBSBO, LSBOOFABP QEB ?;N() CRK@QFLK FK QEB AHCG;F @I>PP? %SBK FC PMLQ FP AB@I>OBA >P >K AHCG;F , *>S> TFII ABQBOJFKB QE>Q FQP QORB FABKQFQV FP QE>Q LC > "IA >KA ORK QEB >MMOLMOF>QB SBOPFLK LC QEB ?;N() CRK@QFLK. 4EFP FP M>OQF@RI>OIV RPBCRI TEBK TB E>SB >K >OO>V LO ALL;S*CMN .

4.10 Particle Systems with Polymorphism


,BQgP MOBQBKA CLO > JLJBKQ QE>Q MLIVJLOMEFPJ ALBPKgQ BUFPQ >KA OBTOFQB > .;LNC=F?0SMN?G @I>PP QL FK@IRAB J>KV .;LNC=F? L?GB@QP >KA J>KV !IH@?NNC L?GB@QP.
=F;MM .;LNC=F?0SMN?G U AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I; AHH7O'?IJ<CED<;JJ?> 9ED<;JJ?; Were stuck doing everything twice with two lists!

170

4EB .>QROB LC #LAB (S1.0)


.3?=NIL ILCACH; .;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U ILCACH = FI=;NCIH.A?N(); F7HJ?9B;I = D;M AHH7O'?IJ<+7HJ?9B;>(); 9ED<;JJ? = D;M AHH7O'?IJ<CED<;JJ?>(); W PIC> ;>>.;LNC=F?() U F7HJ?9B;I.7::(D;M +7HJ?9B;(EH?=?D)); F7HJ?9B;I.7::(D;M CED<;JJ?(EH?=?D)); W PIC> LOH() U %J;H7JEH<+7HJ?9B;> ?J = F7HJ?9B;I.?J;H7JEH(); QBCF? (CN.B;M,?RN()) U .;LNC=F? J = CN.H?RN(); J.LOH(); C@ (J.CM"?;>()) U CN.L?GIP?(); W W ?J = 9ED<;JJ?.?J;H7JEH(); QBCF? (CN.B;M,?RN()) U !IH@?NNC = = CN.H?RN(); =.LOH(); C@ (=.CM"?;>()) U CN.L?GIP?(); W W W W Were stuck doing everything twice with two lists! Were stuck doing everything twice with two lists! Were stuck doing everything twice with two lists!

.LQF@B ELT TB E>SB QTL PBM>O>QB IFPQP, LKB CLO M>OQF@IBP >KA LKB CLO @LKCBQQF. %SBOV >@QFLK TB T>KQ QL MBOCLOJ TB E>SB QL AL QTF@B! 0LIVJLOMEFPJ >IILTP RP QL PFJMIFCV QEB >?LSB ?V GRPQ J>HFKD LKB ALL;S*CMN LC M>OQF@IBP QE>Q @LKQ>FKP ?LQE PQ>KA>OA .;LNC=F? L?GB@QP >P TBII >P !IH@?NNC L?GB@QP. 7B ALKgQ E>SB QL TLOOV >?LRQ TEF@E >OB TEF@E; QEFP TFII >II ?B Q>HBK @>OB LC CLO RP! (!IPL, KLQB QE>Q QEB @LAB CLO QEB J>FK MOLDO>J >KA QEB @I>PPBP E>P KLQ @E>KDBA, PL TB >OBKgQ FK@IRAFKD FQ EBOB. 3BB QEB TB?PFQB CLO QEB CRII BU>JMIB.)

171

#E>MQBO 4. 0>OQF@IB 3VPQBJP

Example 4.5: Particle system inheritance and polymorphism


=F;MM .;LNC=F?0SMN?G U AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I; .3?=NIL ILCACH; .;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U ILCACH = FI=;NCIH.A?N(); J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>(); W PIC> ;>>.;LNC=F?() U @FI;N L = L;H>IG(1); C@ (L < 0.5) U F7HJ?9B;I.7::(D;M +7HJ?9B;(EH?=?D)); W ?FM? U F7HJ?9B;I.7::(D;M CED<;JJ?(EH?=?D)); W W PIC> LOH() U 'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL(); QBCF? (CN.B;M,?RN()) U +7HJ?9B; F = ?J.D;NJ(); J.LOH(); C@ (J.CM"?;>()) U CN.L?GIP?(); W W W W Polymorphism allows us to treat everything as a Particle, whether it is a Particle or a Confetti. We have a 50% chance of adding each kind of Particle. One list, for anything that is a Particle or extends Particle

172

4EB .>QROB LC #LAB (S1.0)

Exercise 4.8
#OB>QB > M>OQF@IB PVPQBJ TFQE AFCCBOBKQ hHFKAPi LC M>OQF@IBP FK QEB P>JB PVPQBJ. 4OV S>OVFKD JLOB QE>K GRPQ QEB ILLH LC QEB M>OQF@IBP. (LT AL VLR AB>I TFQE AFCCBOBKQ ?BE>SFLOP RPFKD FKEBOFQ>K@B?

4.11 Particle Systems with Forces


3L C>O FK QEFP @E>MQBO, TBgSB ?BBK CL@RPFKD LK PQOR@QROFKD LRO @LAB FK >K L?GB@Q-LOFBKQBA T>V QL J>K>DB > @LIIB@QFLK LC M>OQF@IBP. ->V?B VLR KLQF@BA, LO J>V?B VLR AFAKgQ, ?RQ AROFKD QEFP MOL@BPP TB RKTFQQFKDIV QLLH > @LRMIB PQBMP ?>@HT>OA COLJ TEBOB TB TBOB FK MOBSFLRP @E>MQBOP. ,BQgP BU>JFKB QEB @LKPQOR@QLO LC LRO PFJMIB .;LNC=F? @I>PP.
.;LNC=F?(.3?=NIL F) U 799;B;H7J?ED = D;M +0;9JEH(0,0.05); Were setting acceleration to a constant value!

P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0)); FI=;NCIH = F.A?N(); FC@?MJ;H = 255.0; W

!KA KLT IBQgP ILLH >Q QEB OJ>;N?() CRK@QFLK.


PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); // 4B?L? CM NB? FCH? I@ =I>? NI =F?;L ;==?F?L;NCIH? FC@?MJ;H -= 2.0; W

/RO .;LNC=F? @I>PP FP PQOR@QROBA QL E>SB > @LKPQ>KQ >@@BIBO>QFLK, LKB QE>Q KBSBO @E>KDBP. ! JR@E ?BQQBO CO>JBTLOH TLRIA ?B QL CLIILT .BTQLKgP PB@LKA I>T (& = -* !) >KA FK@LOMLO>QB QEB CLO@B >@@RJRI>QFLK >IDLOFQEJ TB TLOHBA PL E>OA LK FK #E>MQBO 2 (PBB M>DB 68). 3QBM 1 TLRIA ?B QL >AA FK QEB ;JJFS$IL=?() CRK@QFLK. (2BJBJ?BO, TB KBBA QL J>HB > @LMV LC QEB .3?=NIL ?BCLOB TB AFSFAB FQ ?V J>PP.)
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U .3?=NIL @ = @IL=?.A?N(); @.>CP(G;MM); ;==?F?L;NCIH.;>>(@); W

173

#E>MQBO 4. 0>OQF@IB 3VPQBJP

/K@B TB E>SB QEFP, TB @>K >AA FK LKB JLOB IFKB LC @LAB QL @IB>O QEB >@@BIBO>QFLK >Q QEB BKA LC OJ>;N?() .
PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); ;==?F?L;NCIH.GOFN(0); FC@?MJ;H -= 2.0; W There it is!

!KA LRO .;LNC=F? @I>PP FP @LJMIBQB!


=F;MM .;LNC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N FC@?MJ;H; @FI;N G;MM = 1; .;LNC=F?(.3?=NIL F) U ;==?F?L;NCIH = H?Q .3?=NIL(0,0); We now start with acceleration of 0. We could vary mass for more interesting results.

P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0)); FI=;NCIH = F.A?N(); FC@?MJ;H = 255.0; W PIC> LOH() U OJ>;N?(); >CMJF;S(); W PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U .3?=NIL @ = @IL=?.A?N(); @.>CP(G;MM); ;==?F?L;NCIH.;>>(@); W Newtons second law & force accumulation

PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS); ;==?F?L;NCIH.GOFN(0); FC@?MJ;H -= 2.0; W

Standard update

174

4EB .>QROB LC #LAB (S1.0)

PIC> >CMJF;S() U MNLIE?(255,FC@?MJ;H); @CFF(255,FC@?MJ;H); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8); W

Our Particle is a circle.

<IIF?;H CM"?;>() U C@ (FC@?MJ;H < 0.0) U L?NOLH NLO?; W ?FM? U L?NOLH @;FM?; W W W

Should the Particle be deleted?

.LT QE>Q QEB .;LNC=F? @I>PP FP @LJMIBQBA, TB E>SB > SBOV FJMLOQ>KQ NRBPQFLK QL >PH. 7EBOB AL TB @>II QEB ;JJFS$IL=?() CRK@QFLK? 7EBOB FK QEB @LAB FP FQ >MMOLMOF>QB QL >MMIV > CLO@B QL > M>OQF@IB? 4EB QORQE LC QEB J>QQBO FP QE>Q QEBOBgP KL OFDEQ LO TOLKD >KPTBO; FQ OB>IIV ABMBKAP LK QEB BU>@Q CRK@QFLK>IFQV >KA DL>IP LC > M>OQF@RI>O 0OL@BPPFKD PHBQ@E. 3QFII, TB @>K @OB>QB > DBKBOF@ PFQR>QFLK QE>Q TLRIA IFHBIV >MMIV QL JLPQ @>PBP >KA @O>CQ > JLABI CLO >MMIVFKD CLO@BP QL FKAFSFAR>I M>OQF@IBP FK > PVPQBJ. ,BQgP @LKPFABO QEB CLIILTFKD DL>I: !MMIV > CLO@B DIL?>IIV BSBOV QFJB QEOLRDE >L;Q() QL >II M>OQF@IBP. 7BgII MF@H >K B>PV LKB CLO KLT: > CLO@B MLFKQFKD ALTK, IFHB DO>SFQV.
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);

7B P>FA FQ PELRIA >IT>VP ?B >MMIFBA, F.B. FK >L;Q() , PL IBQgP Q>HB > ILLH >Q LRO >L;Q() CRK@QFLK >P FQ PQ>KAP.
PIC> >L;Q() U <;=EALIOH>(100); JM.;>>.;LNC=F?(); JM.LOH(); W

7BII, FQ PBBJP QE>Q TB E>SB > PJ>II MOL?IBJ. ;JJFS$IL=?() FP > JBQELA TOFQQBK FKPFAB QEB .;LNC=F? @I>PP, ?RQ TB ALKgQ E>SB >KV OBCBOBK@B QL QEB FKAFSFAR>I M>OQF@IBP QEBJPBISBP, LKIV QEB .;LNC=F?0SMN?G L?GB@Q: QEB S>OF>?IB JM . 3FK@B TB T>KQ >II M>OQF@IBP QL OB@BFSB QEB CLO@B, ELTBSBO, TB @>K AB@FAB QL >MMIV QEB CLO@B QL QEB M>OQF@IB PVPQBJ >KA IBQ FQ J>K>DB >MMIVFKD QEB CLO@B QL >II QEB FKAFSFAR>I M>OQF@IBP:
PIC> >L;Q() U <;=EALIOH>(100); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);

175

#E>MQBO 4. 0>OQF@IB 3VPQBJP

JM.;JJFS$IL=?(AL;PCNS); JM.;>>.;LNC=F?(); JM.LOH(); W

Applying a force to the system as a whole

/C @LROPB, FC TB @>II > KBT CRK@QFLK LK QEB .;LNC=F?0SMN?G L?GB@Q FK >L;Q() , TBII, TB E>SB QL TOFQB QE>Q CRK@QFLK FK QEB .;LNC=F?0SMN?G @I>PP. ,BQgP ABP@OF?B QEB GL? QE>Q CRK@QFLK KBBAP QL MBOCLOJ: OB@BFSB > CLO@B >P > .3?=NIL >KA >MMIV QE>Q CLO@B QL >II QEB M>OQF@IBP. .LT FK @LAB:
PIC> ;JJFS$IL=?(.3?=NIL @) U @IL (.;LNC=F? J: J;LNC=F?M) U J.;JJFS$IL=?(@); W W

)Q >IJLPQ PBBJP PFIIV QL TOFQB QEFP CRK@QFLK. 7E>Q TBgOB P>VFKD FP h>MMIV > CLO@B QL > M>OQF@IB PVPQBJ PL QE>Q QEB PVPQBJ @>K >MMIV QE>Q CLO@B QL >II LC QEB FKAFSFAR>I M>OQF@IBP.i .BSBOQEBIBPP, FQgP OB>IIV NRFQB OB>PLK>?IB. !CQBO >II, QEB .;LNC=F?0SMN?G L?GB@Q FP FK @E>ODB LC J>K>DFKD QEB M>OQF@IBP, PL FC TB T>KQ QL Q>IH QL QEB M>OQF@IBP, TBgSB DLQ QL Q>IH QL QEBJ QEOLRDE QEBFO J>K>DBO. (!IPL, EBOBgP > @E>K@B CLO QEB BKE>K@BA ILLM PFK@B TB >OBKgQ ABIBQFKD M>OQF@IBP!) (BOB FP QEB CRII BU>JMIB (>PPRJFKD QEB BUFPQBK@B LC QEB .;LNC=F? @I>PP TOFQQBK >?LSB; KL KBBA QL FK@IRAB FQ >D>FK PFK@B KLQEFKD E>P @E>KDBA):

176

4EB .>QROB LC #LAB (S1.0)

Example 4.6: Particle system with forces


.;LNC=F?0SMN?G JM; PIC> M?NOJ() U MCT?(640,360); JM = H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(QC>NB/2,50)); W PIC> >L;Q() U <;=EALIOH>(100); +0;9JEH =H7L?JO = D;M +0;9JEH(0,0.1); FI.7FFBO"EH9;(=H7L?JO); JM.;>>.;LNC=F?(); JM.LOH(); W Apply a force to all particles.

=F;MM .;LNC=F?0SMN?G U ALL;S*CMN<.;LNC=F?> J;LNC=F?M; .3?=NIL ILCACH; .;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U ILCACH = FI=;NCIH.A?N(); J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>(); W PIC> ;>>.;LNC=F?() U J;LNC=F?M.;>>(H?Q .;LNC=F?(ILCACH)); W PIC> ;JJFS$IL=?(.3?=NIL @) U @IL (.;LNC=F? J: J;LNC=F?M) U J.;JJFS$IL=?(@); W W PIC> LOH() U 'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL(); QBCF? (CN.B;M,?RN()) U .;LNC=F? J = (.;LNC=F?) CN.H?RN(); J.LOH(); C@ (J.CM"?;>()) U CN.L?GIP?(); W W W W Cant use the enhanced loop because we want to check for particles to delete. Using an enhanced loop to apply the force to all particles

177

#E>MQBO 4. 0>OQF@IB 3VPQBJP

4.12 Particle Systems with Repellers


7E>Q FC TB T>KQBA QL Q>HB QEFP BU>JMIB LKB PQBM CROQEBO >KA >AA > /?J?FF?L L?GB@QeQEB FKSBOPB LC QEB ANNL;=NIL L?GB@Q TB @LSBOBA FK #E>MQBO 2 (PBB M>DB 88) QE>Q MRPEBP >KV M>OQF@IBP >T>V QE>Q DBQ @ILPB? 4EFP OBNRFOBP > ?FQ JLOB PLMEFPQF@>QFLK ?B@>RPB, RKIFHB QEB DO>SFQV CLO@B, B>@E CLO@B >K >QQO>@QLO LO OBMBIIBO BUBOQP LK > M>OQF@IB JRPQ ?B @>I@RI>QBA CLO B>@E M>OQF@IB.

F64B?2 4.3: G?.C6AF 3<?02HC20A<?@ .?2 .99 612;A60.9

F64B?2 4.4: AAA?.0A<? 3<?02HC20A<?@ .?2 .99 16332?2;A

,BQgP PQ>OQ PLISFKD QEFP MOL?IBJ ?V BU>JFKFKD ELT TB TLRIA FK@LOMLO>QB > KBT /?J?FF?L L?GB@Q FKQL LRO PFJMIB M>OQF@IB PVPQBJ MIRP CLO@BP BU>JMIB. 7BgOB DLFKD QL KBBA QTL J>GLO >AAFQFLKP QL LRO @LAB: 1. ! /?J?FF?L L?GB@Q (AB@I>OBA, FKFQF>IFWBA, >KA AFPMI>VBA). 2. ! CRK@QFLK QE>Q M>PPBP QEB /?J?FF?L L?GB@Q FKQL QEB .;LNC=F?0SMN?G PL QE>Q FQ @>K >MMIV > CLO@B QL B>@E M>OQF@IB L?GB@Q.
.;LNC=F?0SMN?G JM; ,;F;BB;H H;F;BB;H; PIC> M?NOJ() U MCT?(640,360); JM = H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(QC>NB/2,50)); New thing: we declare a Repeller object.

178

4EB .>QROB LC #LAB (S1.0)

H;F;BB;H = D;M ,;F;BB;H(M?:J>/2-20,>;?=>J/2); W PIC> >L;Q() U <;=EALIOH>(100); JM.;>>.;LNC=F?(); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1); JM.;JJFS$IL=?(AL;PCNS); FI.7FFBO,;F;BB;H(H;F;BB;H); JM.LOH(); H;F;BB;H.:?IFB7O(); W

New thing: we initialize a Repeller object.

New thing: we need a function to apply a force from a repeller.

New thing: we display the Repeller object.

->HFKD > /?J?FF?L L?GB@Q FP NRFQB B>PV; FQgP > ARMIF@>QB LC QEB ANNL;=NIL @I>PP COLJ #E>MQBO 2, %U>JMIB 2.6 .
=F;MM /?J?FF?L U .3?=NIL FI=;NCIH; @FI;N L = 10; /?J?FF?L(@FI;N R, @FI;N S) U FI=;NCIH = H?Q .3?=NIL(R,S); W PIC> >CMJF;S() U MNLIE?(255); @CFF(255); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,L*2,L*2); W W A Repeller doesnt move, so you just need location.

4EB JLOB AFCCF@RIQ NRBPQFLK FP, ELT AL TB TOFQB QEB ;JJFS/?J?FF?L() CRK@QFLK? )KPQB>A LC M>PPFKD > .3?=NIL FKQL > CRK@QFLK IFHB TB AL TFQE ;JJFS$IL=?() , TBgOB DLFKD QL FKPQB>A M>PP > /?J?FF?L L?GB@Q FKQL ;JJFS/?J?FF?L() >KA >PH QE>Q CRK@QFLK QL AL QEB TLOH LC @>I@RI>QFKD QEB CLO@B ?BQTBBK QEB OBMBIIBO >KA >II M>OQF@IBP. ,BQgP ILLH >Q ?LQE LC QEBPB CRK@QFLKP PFAB ?V PFAB.

179

#E>MQBO 4. 0>OQF@IB 3VPQBJP

applyForce()
PIC> ;JJFS$IL=?(.3?=NIL @) U @IL (.;LNC=F? J: J;LNC=F?M) U J.;JJFS$IL=?(@); W W

applyRepeller
PIC> ;JJFS/?J?FF?L(/?J?FF?L L) U @IL (.;LNC=F? J: J;LNC=F?M) U .3?=NIL @IL=? = L.L?J?F(J); J.;JJFS$IL=?(@IL=?); W W

4EB CRK@QFLKP >OB >IJLPQ FABKQF@>I. 4EBOB >OB LKIV QTL AFCCBOBK@BP. /KB TB JBKQFLKBA ?BCLOBe> /?J?FF?L L?GB@Q FP QEB >ODRJBKQ, KLQ > .3?=NIL . 4EB PB@LKA AFCCBOBK@B FP QEB FJMLOQ>KQ LKB. 7B JRPQ @>I@RI>QB > @RPQLJ .3?=NIL CLO@B CLO B>@E >KA BSBOV M>OQF@IB >KA >MMIV QE>Q CLO@B. (LT FP QE>Q CLO@B @>I@RI>QBA? )K > CRK@QFLK @>IIBA L?J?F() , TEF@E FP QEB FKSBOPB LC QEB ;NNL;=N() CRK@QFLK TB TOLQB CLO QEB ANNL;=NIL @I>PP.
.3?=NIL L?J?F(.;LNC=F? J) U All the same steps we had to calculate an attractive force, only pointing in the opposite direction. 1) Get force direction.

.3?=NIL >CL = .3?=NIL.MO<(FI=;NCIH,J.FI=;NCIH); @FI;N > = >CL.G;A(); > = =IHMNL;CH(>,5,100); >CL.HILG;FCT?(); @FI;N @IL=? = -1 * % / (> * >); >CL.GOFN(@IL=?); L?NOLH >CL; W

2) Get distance (constrain distance).

3) Calculate magnitude. 4) Make a vector out of direction and magnitude.

.LQF@B ELT QEOLRDELRQ QEFP BKQFOB MOL@BPP LC >AAFKD > OBMBIIBO QL QEB BKSFOLKJBKQ, TBgSB KBSBO LK@B @LKPFABOBA BAFQFKD QEB .;LNC=F? @I>PP FQPBIC. ! M>OQF@IB ALBPKgQ >@QR>IIV E>SB QL HKLT >KVQEFKD >?LRQ QEB ABQ>FIP LC FQP BKSFOLKJBKQ; FQ PFJMIV KBBAP QL J>K>DB FQP IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK, >P TBII >P E>SB QEB >?FIFQV QL OB@BFSB >K BUQBOK>I CLO@B >KA >@Q LK FQ. 3L TB @>K KLT ILLH >Q QEFP BU>JMIB FK FQP BKQFOBQV, >D>FK IB>SFKD LRQ QEB .;LNC=F? @I>PP, TEF@E E>PKgQ @E>KDBA.

180

4EB .>QROB LC #LAB (S1.0)

Example 4.7: ParticleSystem with repeller


.;LNC=F?0SMN?G JM; /?J?FF?L L?J?FF?L; PIC> M?NOJ() U MCT?(640,360); JM = H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(QC>NB/2,50)); L?J?FF?L = H?Q /?J?FF?L(QC>NB/2-20,B?CABN/2); W PIC> >L;Q() U <;=EALIOH>(100); JM.;>>.;LNC=F?(); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1); JM.;JJFS$IL=?(AL;PCNS); JM.;JJFS/?J?FF?L(L?J?FF?L); JM.LOH(); L?J?FF?L.>CMJF;S(); W Applying the repeller Were applying a universal gravity. One ParticleSystem One repeller

=F;MM .;LNC=F?0SMN?G U ALL;S*CMN<.;LNC=F?> J;LNC=F?M; .3?=NIL ILCACH; .;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U ILCACH = FI=;NCIH.A?N(); J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>(); W PIC> ;>>.;LNC=F?() U J;LNC=F?M.;>>(H?Q .;LNC=F?(ILCACH)); W

The ParticleSystem manages all the Particles.

181

#E>MQBO 4. 0>OQF@IB 3VPQBJP

PIC> ;JJFS$IL=?(.3?=NIL @) U @IL (.;LNC=F? J: J;LNC=F?M) U J.;JJFS$IL=?(@); W W PIC> ;JJFS/?J?FF?L(/?J?FF?L L) U @IL (.;LNC=F? J: J;LNC=F?M) U .3?=NIL @IL=? = L.L?J?F(J); J.;JJFS$IL=?(@IL=?); W W PIC> LOH() U 'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL(); QBCF? (CN.B;M,?RN()) U .;LNC=F? J = (.;LNC=F?) CN.H?RN(); J.LOH(); C@ (J.CM"?;>()) U CN.L?GIP?(); W W W W =F;MM /?J?FF?L U @FI;N MNL?HANB = 100; .3?=NIL FI=;NCIH; @FI;N L = 10; /?J?FF?L(@FI;N R, @FI;N S) U FI=;NCIH = H?Q .3?=NIL(R,S); W PIC> >CMJF;S() U MNLIE?(255); @CFF(255); ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,L*2,L*2); W .3?=NIL L?J?F(.;LNC=F? J) U .3?=NIL >CL = .3?=NIL.MO<(FI=;NCIH,J.FI=;NCIH); @FI;N > = >CL.G;A(); >CL.HILG;FCT?(); > = =IHMNL;CH(>,5,100); @FI;N @IL=? = -1 * MNL?HANB / (> * >); >CL.GOFN(@IL=?); L?NOLH >CL;

Applying a force as a PVector

Calculating a force for each Particle based on a Repeller

How strong is the repeller?

This is the same repel algorithm we used in Chapter 2: forces based on gravitational attraction.

182

4EB .>QROB LC #LAB (S1.0)


W W

Exercise 4.9
%UM>KA QEB >?LSB BU>JMIB QL FK@IRAB J>KV OBMBIIBOP (RPFKD >K >OO>V LO ALL;S*CMN ).

Exercise 4.10
#OB>QB > M>OQF@IB PVPQBJ FK TEF@E B>@E M>OQF@IB OBPMLKAP QL BSBOV LQEBO M>OQF@IB. (.LQB QE>Q TBgII ?B DLFKD QEOLRDE QEFP FK ABQ>FI FK #E>MQBO 6.)

4.13 Image Textures and Additive Blending


%SBK QELRDE QEFP ?LLH FP OB>IIV >?LRQ ?BE>SFLOP >KA >IDLOFQEJP O>QEBO QE>K @LJMRQBO DO>MEF@P >KA ABPFDK, ) ALKgQ QEFKH TB TLRIA ?B >?IB QL IFSB TFQE LROPBISBP FC TB TBKQ QEOLRDE > AFP@RPPFLK LC M>OQF@IB PVPQBJP >KA KBSBO LK@B ILLHBA >Q >K BU>JMIB QE>Q FKSLISBP QBUQROFKD B>@E M>OQF@IB TFQE >K FJ>DB. 4EB T>V VLR @ELLPB QL AO>T > M>OQF@IB FP > ?FD M>OQ LC QEB MRWWIB FK QBOJP LC ABPFDKFKD @BOQ>FK QVMBP LC SFPR>I BCCB@QP. ,BQgP QOV QL @OB>QB > PJLHB PFJRI>QFLK FK 0OL@BPPFKD. 4>HB > ILLH >Q QEB CLIILTFKD QTL FJ>DBP:

,56A2 06?092@

FBGGF 6:.42@ D6A5 A?.;@=.?2;0F

"LQE LC QEBPB FJ>DBP TBOB DBKBO>QBA COLJ FABKQF@>I >IDLOFQEJP. 4EB LKIV AFCCBOBK@B FP QE>Q > TEFQB @FO@IB FP AO>TK FK FJ>DB ! CLO B>@E M>OQF@IB >KA > hCRWWVi ?IL? FP AO>TK CLO B>@E FK ".

183

#E>MQBO 4. 0>OQF@IB 3VPQBJP

F64B?2 4.5

4EB DLLA KBTP EBOB FP QE>Q VLR DBQ > ILQ LC ?>KD CLO SBOV IFQQIB ?R@H. "BCLOB VLR TOFQB >KV @LAB, ELTBSBO, VLRgSB DLQ QL J>HB VLRO FJ>DB QBUQROB! ) OB@LJJBKA RPFKD 0.' CLOJ>Q, >P 0OL@BPPFKD TFII OBQ>FK QEB >IME> @E>KKBI (F.B. QO>KPM>OBK@V) TEBK AO>TFKD QEB FJ>DB, TEF@E FP KBBABA CLO ?IBKAFKD QEB QBUQROB >P M>OQF@IBP I>VBO LK QLM LC B>@E LQEBO. /K@B VLRgSB J>AB VLRO 0.' >KA ABMLPFQBA FQ FK VLRO PHBQ@EgP hA>Q>i CLIABO, VLR >OB LK VLRO T>V TFQE GRPQ > CBT IFKBP LC @LAB. &FOPQ, TBgII KBBA QL AB@I>OB > .'G;A? L?GB@Q. Example 4.8: Image texture particle system
.'G;A? CGA;

,L>A QEB FJ>DB FK M?NOJ() .


PIC> M?NOJ() U CGA = FI;>'G;A?("N?RNOL?.JHA"); W Loading the PNG

!KA TEBK FQ @LJBP QFJB QL AO>T QEB M>OQF@IB, TBgII RPB QEB FJ>DB OBCBOBK@B FKPQB>A LC AO>TFKD >K BIIFMPB LO OB@Q>KDIB.
PIC> L?H>?L() U CG;A?+I>?(!#,1#/); NCHN(255,FC@?MJ;H); CG;A?(CGA,FI=.R,FI=.S); W Note how tint() is the image equivalent of shapes fill().

)K@FABKQ>IIV, QEFP PJLHB BU>JMIB FP > KF@B BU@RPB QL OBSFPFQ QEB '>RPPF>K KRJ?BO AFPQOF?RQFLKP COLJ QEB )KQOLAR@QFLK (PBB M>DB 10). 4L J>HB QEB PJLHB >MMB>O > ?FQ JLOB OB>IFPQF@, TB ALKgQ T>KQ QL I>RK@E >II QEB M>OQF@IBP FK > MROBIV O>KALJ AFOB@QFLK. )KPQB>A, ?V @OB>QFKD FKFQF>I SBIL@FQV SB@QLOP JLPQIV >OLRKA > JB>K S>IRB (TFQE > ILTBO MOL?>?FIFQV LC LRQIFBOP), TBgII DBQ >K BCCB@Q QE>Q >MMB>OP IBPP CLRKQ>FK-IFHB >KA JLOB IFHB PJLHB (LO CFOB).

184

4EB .>QROB LC #LAB (S1.0)

!PPRJFKD > /;H>IG L?GB@Q @>IIBA hDBKBO>QLOi, TB @LRIA @OB>QB FKFQF>I SBIL@FQFBP >P CLIILTP:
@FI;N PR = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H()*0.3; @FI;N PS = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H()*0.3 - 1.0; P?F = H?Q .3?=NIL(PR,PS);

&FK>IIV, FK QEFP BU>JMIB, > TFKA CLO@B FP >MMIFBA QL QEB PJLHB J>MMBA COLJ QEB JLRPBgP ELOFWLKQ>I IL@>QFLK.
PIC> >L;Q() U <;=EALIOH>(0); @FI;N >R = G;J(GIOM?5,0,QC>NB,-0.2,0.2); .3?=NIL QCH> = H?Q .3?=NIL(>R,0); JM.;JJFS$IL=?(QCH>); JM.LOH(); @IL (CHN C = 0; C < 2; C++) U JM.;>>.;LNC=F?(); W W Two particles are added each cycle through draw(). Wind force points towards mouseX.

Exercise 4.11
4OV @OB>QFKD VLRO LTK QBUQROBP CLO AFCCBOBKQ QVMBP LC BCCB@QP. #>K VLR J>HB FQ ILLH IFHB CFOB, FKPQB>A LC PJLHB?

Exercise 4.12
5PB >K >OO>V LC FJ>DBP >KA >PPFDK B>@E .;LNC=F? L?GB@Q > AFCCBOBKQ FJ>DB. %SBK QELRDE PFKDIB FJ>DBP >OB AO>TK ?V JRIQFMIB M>OQF@IBP, J>HB PROB VLR ALKgQ @>II FI;>'G;A?() >KV JLOB QE>K VLR KBBA QL, F.B. LK@B CLO B>@E FJ>DB CFIB. &FK>IIV, FQgP TLOQE KLQFKD QE>Q QEBOB >OB J>KV AFCCBOBKQ >IDLOFQEJP CLO ?IBKAFKD @LILOP FK @LJMRQBO DO>MEF@P. 4EBPB >OB LCQBK OBCBOOBA QL >P h?IBKA JLABP.i "V ABC>RIQ, TEBK TB AO>T PLJBQEFKD LK QLM LC PLJBQEFKD BIPB FK 0OL@BPPFKD, TB LKIV PBB QEB QLM I>VBOeQEFP FP @LJJLKIV OBCBOOBA QL >P > hKLOJ>Ii ?IBKA JLAB. 7EBK QEB MFUBIP E>SB >IME> QO>KPM>OBK@V (>P QEBV AL FK QEB PJLHB BU>JMIB), 0OL@BPPFKD RPBP >K >IME> @LJMLPFQFKD >IDLOFQEJ QE>Q @LJ?FKBP > MBO@BKQ>DB LC QEB ?>@HDOLRKA MFUBIP TFQE QEB KBT CLOBDOLRKA MFUBIP ?>PBA LK QEB >IME> S>IRBP. (LTBSBO, FQgP MLPPF?IB QL AO>T RPFKD LQEBO ?IBKA JLABP, >KA > JR@E ILSBA ?IBKA JLAB CLO M>OQF@IB PVPQBJP FP h>AAFQFSB.i !AAFQFSB ?IBKAFKD FK 0OL@BPPFKD T>P MFLKBBOBA ?V 2L?BOQ (LADFK (EQQM://OL?BOQELADFK.@LJ/) FK EFP C>JLRP M>OQF@IB PVPQBJ >KA CLO@BP BUMILO>QFLK,

185

#E>MQBO 4. 0>OQF@IB 3VPQBJP

->DKBQLPMEBOB, TEF@E I>QBO ?B@>JB QEB F4RKBP SFPR>IFWBO. &LO JLOB PBB: ->DKBQLPMEBOB (EQQM://OL?BOQELADFK.@LJ/J>DKBQLPMEBOB-M>OQ-2/). !AAFQFSB ?IBKAFKD FP FK C>@Q LKB LC QEB PFJMIBPQ ?IBKA >IDLOFQEJP >KA FKSLISBP >AAFKD QEB MFUBI S>IRBP LC LKB I>VBO QL >KLQEBO (@>MMFKD >II S>IRBP >Q 255 LC @LROPB). 4EFP OBPRIQP FK > PM>@B->DB DILT BCCB@Q ARB QL QEB @LILOP DBQQFKD ?OFDEQBO >KA ?OFDEQBO TFQE JLOB I>VBOP.

4L >@EFBSB >AAFQFSB ?IBKAFKD FK 0OL@BPPFKD, VLRgII KBBA QL RPB QEB .2" LO .3" OBKABOBO. Example 4.9: Additive blending
PIC> M?NOJ() U MCT?(640,360,.2"); W Using the P2D renderer

4EBK, ?BCLOB VLR DL QL AO>T >KVQEFKD, VLR PBQ QEB ?IBKA JLAB RPFKD <F?H>+I>?() :
PIC> >L;Q() U <F?H>+I>?(A""); Additive blending

<;=EALIOH>(0);

Note that the glowing effect of additive blending will not work with a white (or very bright) background. All your other particle stuff would go here.

Exercise 4.13
5PB NCHN() FK @LJ?FK>QFLK TFQE >AAFQFSB ?IBKAFKD QL @OB>QB > O>FK?LT BCCB@Q.

186

4EB .>QROB LC #LAB (S1.0)

Exercise 4.14
4OV ?IBKAFKD TFQE LQEBO JLABP, PR@E >P 02 1/A!1 , *'%&1#01 , "A/)#01 , "'$$#/#,!# , #5!*20'-, ,LO +2*1'.*6 .

187

#E>MQBO 4. 0>OQF@IB 3VPQBJP

The Ecosystem Project


Step 4 Exercise: Take your creature from Step 3 and build a system of creatures. How can they interact with each other? Can you use inheritance and polymorphism to create a variety of creatures, derived from the same code base? Develop a methodology for how they compete for resources (for example, food). Can you track a creatures health much like we tracked a particles lifespan, removing creatures when appropriate? What rules can you incorporate to control how creatures are born? (Also, you might consider using a particle system itself in the design of a creature. What happens if your emitter is tied to the creatures location?)

188

4EB .>QROB LC #LAB (S1.0)

Chapter 5. Physics Libraries


IA 96/?.?F 6:=962@ .; .0A <3 3.6A5/,5605 42;2?.A6<;@ @A699 6; 1.?8;2@@ 561/ )64; 6; A526? ;645A 6; D6A;2@@ <3 A52 1.D;.J Victor Hugo

"BCLOB TB JLSB LK QL >KVQEFKD BIPB, IBQgP OBSFPFQ PLJB LC QEB QEFKDP TBgSB ALKB FK QEB CFOPQ CLRO @E>MQBOP. 7B E>SB: 1. ,B>OKBA >?LRQ @LK@BMQP COLJ QEB TLOIA LC MEVPF@P e 7E>Q FP > SB@QLO? 7E>Q FP > CLO@B? 7E>Q FP > T>SB? BQ@. 2. 5KABOPQLLA QEB J>QE >KA >IDLOFQEJP ?BEFKA PR@E @LK@BMQP. 3. )JMIBJBKQBA QEB >IDLOFQEJP FK 0OL@BPPFKD TFQE >K L?GB@Q-LOFBKQBA >MMOL>@E. 4EBPB >@QFSFQFBP E>SB VFBIABA > PBQ LC JLQFLK PFJRI>QFLK BU>JMIBP, >IILTFKD RP QL @OB>QFSBIV ABCFKB QEB MEVPF@P LC QEB TLOIAP TB ?RFIA (TEBQEBO OB>IFPQF@ LO C>KQ>PQF@>I). /C @LROPB, TB >OBKgQ QEB CFOPQ QL QOV QEFP. 4EB TLOIA LC @LJMRQBO DO>MEF@P >KA MOLDO>JJFKD FP CRII LC PLRO@B @LAB ABAF@>QBA QL PFJRI>QFLK. *RPQ QOV 'LLDIFKD hLMBK-PLRO@B MEVPF@P BKDFKBi >KA VLR @LRIA PMBKA QEB OBPQ LC VLRO A>V MLROFKD LSBO OF@E >KA @LJMIBU @LAB. !KA PL TB JRPQ >PH QEB NRBPQFLK: )C > @LAB IF?O>OV TFII Q>HB @>OB LC MEVPF@P PFJRI>QFLK, TEV PELRIA TB ?LQEBO IB>OKFKD ELT QL TOFQB >KV LC QEB >IDLOFQEJP LROPBISBP? (BOB FP TEBOB QEB MEFILPLMEV ?BEFKA QEFP ?LLH @LJBP FKQL MI>V. 7EFIB J>KV LC QEB IF?O>OFBP LRQ QEBOB DFSB RP MEVPF@P (>KA PRMBO >TBPLJB >AS>K@BA MEVPF@P >Q QE>Q) CLO COBB, QEBOB >OB

189

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

PFDKFCF@>KQ OB>PLKP CLO IB>OKFKD QEB CRKA>JBKQ>IP COLJ P@O>Q@E ?BCLOB AFSFKD FKQL IF?O>OFBP. &FOPQ, TFQELRQ >K RKABOPQ>KAFKD LC SB@QLOP, CLO@BP, >KA QOFDLKLJBQOV, TBgA ?B @LJMIBQBIV ILPQ GRPQ OB>AFKD QEB AL@RJBKQ>QFLK LC > IF?O>OV. 3B@LKA, BSBK QELRDE > IF?O>OV J>V Q>HB @>OB LC QEB J>QE CLO RP, FQ TLKgQ KB@BPP>OFIV PFJMIFCV LRO @LAB. !P TBgII PBB FK > JLJBKQ, QEBOB @>K ?B > DOB>Q AB>I LC LSBOEB>A FK PFJMIV RKABOPQ>KAFKD ELT > IF?O>OV TLOHP >KA TE>Q FQ BUMB@QP COLJ VLR @LAB-TFPB. &FK>IIV, >P TLKABOCRI >P > MEVPF@P BKDFKB JFDEQ ?B, FC VLR ILLH ABBM ALTK FKQL VLRO EB>OQP, FQgP IFHBIV QE>Q VLR PBBH QL @OB>QB TLOIAP >KA SFPR>IFW>QFLKP QE>Q PQOBQ@E QEB IFJFQP LC FJ>DFK>QFLK. ! IF?O>OV FP DOB>Q, ?RQ FQ MOLSFABP > IFJFQBA PBQ LC CB>QROBP. )QgP FJMLOQ>KQ QL HKLT ?LQE TEBK QL IFSB TFQEFK IFJFQ>QFLKP FK QEB MROPRFQ LC > 0OL@BPPFKD MOLGB@Q >KA TEBK QELPB IFJFQP MOLSB QL ?B @LKCFKFKD. 4EFP @E>MQBO FP ABAF@>QBA QL BU>JFKFKD QTL LMBK-PLRO@B MEVPF@P IF?O>OFBPe"LU2$ >KA QLUF@IF?Pg 6BOIBQ0EVPF@P BKDFKB. 7FQE B>@E IF?O>OV, TBgII BS>IR>QB FQP MOLP >KA @LKP >KA ILLH >Q OB>PLKP TEV VLR JFDEQ @ELLPB LKB LC QEBPB IF?O>OFBP CLO > DFSBK MOLGB@Q.

5.1 What Is Box2D and When Is It Useful?


"LU2$ ?BD>K >P > PBQ LC MEVPF@P QRQLOF>IP TOFQQBK FK #++ ?V %OFK #>QQL CLO QEB '>JB $BSBILMBOgP #LKCBOBK@B FK 2006. /SBO QEB I>PQ CFSB VB>OP FQ E>P BSLISBA FKQL >K OF@E >KA BI>?LO>QB LMBK-PLRO@B MEVPF@P BKDFKB. )QgP ?BBK RPBA CLO @LRKQIBPP MOLGB@QP, JLPQ KLQ>?IV EFDEIV PR@@BPPCRI D>JBP PR@E >P QEB >T>OA-TFKKFKD MRWWIB D>JB #O>VLK 0EVPF@P >KA QEB ORK>T>V JL?FIB >KA Q>?IBQ EFQ !KDOV "FOAP. /KB LC QEB HBV QEFKDP QL OB>IFWB >?LRQ "LU2$ FP QE>Q FQ FP > QORB MEVPF@P BKDFKB. "LU2$ HKLTP KLQEFKD >?LRQ @LJMRQBO DO>MEF@P >KA QEB TLOIA LC MFUBIP; FQ FP PFJMIV > IF?O>OV QE>Q Q>HBP FK KRJ?BOP >KA PMFQP LRQ JLOB KRJ?BOP. !KA TE>Q >OB QELPB KRJ?BOP? -BQBOP, HFILDO>JP, PB@LKAP, BQ@. !II LC "LU2$gP JB>PROBJBKQP >KA @>I@RI>QFLKP >OB CLO OB>I-TLOIA JB>PROBJBKQPeLKIV FQP hTLOIAi FP > QTL-AFJBKPFLK>I MI>KB TFQE QLM, ?LQQLJ, IBCQ, >KA OFDEQ BADBP. 9LR QBII FQ QEFKDP IFHB: h4EB DO>SFQV LC LRO TLOIA FP 9.81 KBTQLKP MBO HFILDO>J, >KA > @FO@IB TFQE > O>AFRP LC CLRO JBQBOP >KA > J>PP LC CFCQV HFILDO>JP FP IL@>QBA QBK JBQBOP >?LSB QEB TLOIAgP ?LQQLJ.i "LU2$ TFII QEBK QBII VLR QEFKDP IFHB: h/KB PB@LKA I>QBO, QEB OB@Q>KDIB FP >Q CFSB JBQBOP COLJ QEB ?LQQLJ; QTL PB@LKAP I>QBO, FQ FP QBK JBQBOP ?BILT,i BQ@. 7EFIB QEFP MOLSFABP CLO >K >J>WFKD >KA OB>IFPQF@ MEVPF@P BKDFKB, FQ >IPL KB@BPPFQ>QBP ILQP LC @LJMIF@>QBA @LAB FK LOABO QL QO>KPI>QB ?>@H >KA CLOQE ?BQTBBK QEB MEVPF@P hTLOIAi (> HBV QBOJ FK "LU2$) >KA QEB TLOIA TB T>KQ QL AO>T LK eQEB hMFUBIi TLOIA LC 0OL@BPPFKD. 3L TEBK FP FQ TLOQE FQ QL E>SB QEFP >AAFQFLK>I LSBOEB>A? )C ) GRPQ T>KQ QL PFJRI>QB > @FO@IB C>IIFKD ALTK > 0OL@BPPFKD TFKALT TFQE DO>SFQV, AL ) OB>IIV KBBA QL TOFQB >II QEB BUQO> "LU2$ @LAB GRPQ QL DBQ QE>Q BCCB@Q? #BOQ>FKIV, QEB >KPTBO FP KL. 7B P>T ELT QL AL QEFP O>QEBO B>PFIV FK QEB CFOPQ @E>MQBO LC QEFP ?LLH. ,BQgP @LKPFABO >KLQEBO P@BK>OFL. 7E>Q FC ) T>KQ QL E>SB > ERKAOBA LC QELPB @FO@IBP C>IIFKD? !KA TE>Q FC QELPB @FO@IBP >OBKgQ @FO@IBP >Q >II, ?RQ FOOBDRI>OIV PE>MBA MLIVDLKP? !KA TE>Q FC ) T>KQ QEBPB MLIVDLKP QL ?LRK@B LCC B>@E LQEBO FK > OB>IFPQF@ J>KKBO TEBK QEBV @LIIFAB?

190

4EB .>QROB LC #LAB (S1.0)

9LR J>V E>SB KLQF@BA QE>Q QEB CFOPQ CLRO @E>MQBOP LC QEFP ?LLH, TEFIB @LSBOFKD JLQFLK >KA CLO@BP FK ABQ>FI, E>P PHFMMBA LSBO > O>QEBO FJMLOQ>KQ >PMB@Q LC MEVPF@P PFJRI>QFLKe 7C@@=G=CBG. ,BQgP MOBQBKA CLO > JLJBKQ QE>Q VLR >OBKgQ OB>AFKD > @E>MQBO >?LRQ IF?O>OFBP >KA QE>Q TB AB@FABA OFDEQ KLT QL @LSBO ELT QL E>KAIB @LIIFPFLKP FK > M>OQF@IB PVPQBJ. 7BgA E>SB QL BS>IR>QB >KA IB>OK QTL AFPQFK@Q >IDLOFQEJP QE>Q >AAOBPP QEBPB NRBPQFLKP: 1. (LT AL ) ABQBOJFKB FC QTL PE>MBP >OB @LIIFAFKD (F.B. FKQBOPB@QFKD)? 2. (LT AL ) ABQBOJFKB QEB PE>MBPg SBIL@FQV >CQBO QEB @LIIFPFLK? )C TBgOB QEFKHFKD >?LRQ PE>MBP IFHB OB@Q>KDIBP LO @FO@IBP, NRBPQFLK #1 FPKgQ QLL QLRDE. 9LRgSB IFHBIV BK@LRKQBOBA QEFP ?BCLOB. &LO BU>JMIB, TB HKLT QTL @FO@IBP >OB FKQBOPB@QFKD FC QEB AFPQ>K@B ?BQTBBK QEBJ FP IBPP QE>K QEB PRJ LC QEBFO O>AFF.

F64B?2 5.1

/+. .LT QE>Q TB HKLT ELT QL ABQBOJFKB FC QTL @FO@IBP >OB @LIIFAFKD, ELT AL TB @>I@RI>QB QEBFO SBIL@FQFBP >CQBO QEB @LIIFPFLK? 4EFP FP TEBOB TBgOB DLFKD QL PQLM LRO AFP@RPPFLK. 7EV, VLR >PH? )QgP KLQ QE>Q RKABOPQ>KAFKD QEB J>QE ?BEFKA @LIIFPFLKP FPKgQ FJMLOQ>KQ LO S>IR>?IB. ()K C>@Q, )gJ FK@IRAFKD >AAFQFLK>I BU>JMIBP LK QEB TB?PFQB OBI>QBA QL @LIIFPFLKP TFQELRQ > MEVPF@P IF?O>OV.) 4EB OB>PLK CLO PQLMMFKD FP QE>Q IFCB FP PELOQ (IBQ QEFP >IPL ?B > OB>PLK CLO VLR QL @LKPFABO DLFKD LRQPFAB >KA COLIF@HFKD FKPQB>A LC MOLDO>JJFKD >IQLDBQEBO). 7B @>KgQ BUMB@Q QL J>PQBO BSBOV ABQ>FI LC MEVPF@P PFJRI>QFLK. !KA TEFIB TB @LRIA @LKQFKRB QEFP AFP@RPPFLK CLO @FO@IBP, FQgP LKIV DLFKD QL IB>A RP QL T>KQFKD QL TLOH TFQE OB@Q>KDIBP. !KA PQO>KDBIV PE>MBA MLIVDLKP. !KA @ROSBA PROC>@BP. !KA PTFKDFKD MBKARIRJP @LIIFAFKD TFQE PMOFKDV PMOFKDP. !KA >KA >KA >KA >KA. 7LOHFKD TFQE @LIIFPFLKP FK LRO 0OL@BPPFKD PHBQ@E TEFIB PQFII E>SFKD QFJB QL PMBKA TFQE LRO COFBKAP >KA C>JFIVeQE>QgP QEB OB>PLK CLO QEFP @E>MQBO. %OFK #>QQL PMBKQ VB>OP ABSBILMFKD PLIRQFLKP QL QEBPB HFKAP LC MOL?IBJP PL VLR ALKgQ KBBA QL BKDFKBBO QEBJ VLROPBISBP, >Q IB>PQ CLO KLT.

191

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

)K @LK@IRPFLK, FC VLR CFKA VLROPBIC ABP@OF?FKD >K FAB> CLO > 0OL@BPPFKD PHBQ@E >KA QEB TLOA h@LIIFPFLKPi @LJBP RM, QEBK FQgP IFHBIV QFJB QL IB>OK "LU2$. (7BgII >IPL BK@LRKQBO LQEBO TLOAP QE>Q JFDEQ IB>A VLR ALTK QEFP M>QE QL "LU2$, PR@E >P hGLFKQ,i hEFKDB,i hMRIIBV,i hJLQLO,i BQ@.)

5.2 Getting Box2D in Processing


3L, FC "LU2$ FP > MEVPF@P BKDFKB QE>Q HKLTP KLQEFKD >?LRQ MFUBI-?>PBA @LJMRQBO DO>MEF@P >KA FP TOFQQBK FK #++, ELT >OB TB PRMMLPBA QL RPB FQ FK 0OL@BPPFKD? 4EB DLLA KBTP FP QE>Q "LU2$ FP PR@E >K >J>WFKD >KA RPBCRI IF?O>OV QE>Q BSBOVLKB T>KQP QL RPB FQe&I>PE, *>S>P@OFMQ, 0VQELK, 2R?V MOLDO>JJBOP. /E, >KA *>S> MOLDO>JJBOP. 4EBOB FP PLJBQEFKD @>IIBA *"LU2$, > *>S> MLOQ LC "LU2$. !KA ?B@>RPB 0OL@BPPFKD FP ?RFIQ LK QLM LC *>S>, *"LU2$ @>K ?B RPBA AFOB@QIV FK 0OL@BPPFKD! 3L EBOBgP TEBOB TB >OB PL C>O. j "LU2$ PFQB (EQQM://TTT.?LU2A.LOD/) CLO OBCBOBK@B. j *"LU2$ PFQB (EQQM://TTT.G?LU2A.LOD/) CLO 0OL@BPPFKD @LJM>QF?FIFQV. 4EFP FP >II VLR KBBA QL DBQ PQ>OQBA TOFQFKD "LU2$ @LAB FK 0OL@BPPFKD. (LTBSBO, >P TB >OB DLFKD QL PBB FK > JLJBKQ, QEBOB >OB PBSBO>I MFB@BP LC CRK@QFLK>IFQV TBgII OBMB>QBAIV KBBA FK LRO 0OL@BPPFKD @LAB, >KA PL FQgP TLOQE E>SFKD LKB >AAFQFLK>I I>VBO ?BQTBBK LRO PHBQ@EBP >KA *"LU2$. )gJ @>IIFKD QEFP 0"LU2$e> 0OL@BPPFKD "LU2$ hEBIMBOi IF?O>OV FK@IRABA >P M>OQ LC QEFP ?LLHgP @LAB BU>JMIB ALTKIL>AP. j 0"LU2$ 'FQ(R? OBMLPFQLOV (EQQM://DFQER?.@LJ/PEFCCJ>K/0"LU2$) )QgP FJMLOQ>KQ QL OB>IFWB QE>Q 0"LU2$ FP KLQ > 0OL@BPPFKD TO>MMBO CLO >II LC "LU2$. !CQBO >II, "LU2$ FP > QELRDEQCRIIV LOD>KFWBA >KA TBII-PQOR@QROBA !0) >KA QEBOBgP KL OB>PLK QL Q>HB FQ >M>OQ >KA OB-FJMIBJBKQ FQ. (LTBSBO, FQgP RPBCRI QL E>SB > PJ>II PBQ LC CRK@QFLKP QE>Q EBIM VLR DBQ VLRO "LU2$ TLOIA PBQ RM, >P TBII >P EBIM VLR CFDROB LRQ TEBOB QL AO>T VLRO "LU2$ PE>MBP. !KA QEFP FP TE>Q 0"LU2$ TFII MOLSFAB. ) PELRIA >IPL JBKQFLK ?BCLOB TB JLSB CLOT>OA QE>Q QEBOB >OB LQEBO 0OL@BPPFKD IF?O>OFBP QE>Q TO>M "LU2$ CLO VLR. /KB ) TLRIA OB@LJJBKA Q>HFKD > ILLH >Q FP &FPF@> (EQQM://TTT.OF@>OAJ>OUBO.@LJ/CFPF@>/) ?V 2F@>OA ->OUBO.

5.3 Box2D Basics


$L KLQ ABPM>FO! 7B OB>IIV >OB DLFKD QL DBQ QL QEB @LAB SBOV PLLK, >KA FK PLJB T>VP TBgII ?ILT LRO MOBSFLRP TLOH LRQ LC QEB T>QBO. "RQ ?BCLOB TBgOB OB>AV QL AL QE>Q, FQgP FJMLOQ>KQ QL

192

4EB .>QROB LC #LAB (S1.0)

T>IH QEOLRDE QEB LSBO>II MOL@BPP LC RPFKD "LU2$ FK 0OL@BPPFKD. ,BQgP ?BDFK ?V TOFQFKD > MPBRAL@LAB DBKBO>IFW>QFLK LC >II LC LRO BU>JMIBP FK #E>MQBOP 1 QEOLRDE 4. 'E()%: 1. #OB>QB >II QEB L?GB@QP FK LRO TLOIA. D&A+: 1. #>I@RI>QB >II QEB CLO@BP FK LRO TLOIA. 2. !MMIV >II QEB CLO@BP QL LRO L?GB@QP (& = - * !). 3. 5MA>QB QEB IL@>QFLKP LC >II QEB L?GB@QP ?>PBA LK QEBFO >@@BIBO>QFLK. 4. $O>T >II LC LRO L?GB@QP. 'OB>Q. ,BQgP OBTOFQB QEFP MPBRAL@LAB >P FQ TFII >MMB>O FK LRO "LU2$ BU>JMIBP. 'E()%: 1. #OB>QB >II QEB L?GB@QP FK LRO TLOIA. D&A+: 1. $O>T >II LC LRO L?GB@QP. 4EFP, LC @LROPB, FP QEB C>KQ>PV LC "LU2$. 7BgSB BIFJFK>QBA >II LC QELPB M>FKCRI PQBMP LC CFDROFKD LRQ ELT QEB L?GB@QP >OB JLSFKD >@@LOAFKD QL SBIL@FQV >KA >@@BIBO>QFLK. "LU2$ FP DLFKD QL Q>HB @>OB LC QEFP CLO RP! 4EB DLLA KBTP FP QE>Q QEFP ALBP >@@RO>QBIV OBCIB@Q QEB LSBO>II MOL@BPP. ,BQgP FJ>DFKB "LU2$ >P > J>DF@ ?LU. )K M?NOJ() , TBgOB DLFKD QL P>V QL "LU2$: h(BIIL QEBOB. (BOB >OB >II LC QEB QEFKDP ) T>KQ FK JV TLOIA.i )K >L;Q() , TBgOB DLFKD QL MLIFQBIV >PH "LU2$: h/E, EBIIL >D>FK. )C FQgP KLQ QLL JR@E QOLR?IB, )gA IFHB QL AO>T >II LC QELPB QEFKDP FK JV TLOIA. #LRIA VLR QBII JB TEBOB QEBV >OB?i 4EB ?>A KBTP: FQgP KLQ >P PFJMIB >P QEB >?LSB BUMI>K>QFLK TLRIA IB>A VLR QL ?BIFBSB. &LO LKB, J>HFKD QEB PQRCC QE>Q DLBP FK QEB "LU2$ TLOIA FKSLISBP T>AFKD QEOLRDE QEB AL@RJBKQ>QFLK CLO ELT AFCCBOBKQ HFKAP LC PE>MBP >OB ?RFIQ >KA @LKCFDROBA. 3B@LKA, TB E>SB QL OBJBJ?BO QE>Q TB @>KgQ QBII "LU2$ >KVQEFKD >?LRQ MFUBIP, >P FQ TFII PFJMIV DBQ @LKCRPBA >KA C>II >M>OQ. "BCLOB TB QBII "LU2$ TE>Q TB T>KQ FK LRO TLOIA, TB E>SB QL @LKSBOQ LRO MFUBI RKFQP QL "LU2$ hTLOIAi RKFQP. !KA QEB P>JB FP QORB TEBK FQ @LJBP QFJB QL AO>T LRO PQRCC. "LU2$ FP DLFKD QL QBII RP QEB IL@>QFLK LC QEB QEFKDP FK FQP TLOIA, TEF@E TB QEBK E>SB QL QO>KPI>QB CLO QEB MFUBI TLOIA.

SETUP
1. #OB>QB BSBOVQEFKD QE>Q IFSBP FK LRO MFUBI TLOIA.

193

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

2. 4O>KPI>QB QEB MFUBI TLOIA FKQL QEB "LU2$ TLOIA.

DRAW
1. !PH "LU2$ TEBOB BSBOVQEFKD FP. 2. 4O>KPI>QB "LU2$gP >KPTBO FKQL QEB MFUBI TLOIA. 3. $O>T BSBOVQEFKD. .LT QE>Q TB RKABOPQ>KA QE>Q >KVQEFKD TB @OB>QB FK LRO 0OL@BPPFKD PHBQ@E E>P QL ?B MI>@BA FKQL QEB "LU2$ TLOIA, IBQgP ILLH >Q >K LSBOSFBT LC QEB BIBJBKQP QE>Q J>HB RM QE>Q TLOIA.

Core elements of a Box2D world:


1. +:=7/: ->K>DBP QEB MEVPF@P PFJRI>QFLK. )Q HKLTP BSBOVQEFKD >?LRQ QEB LSBO>II @LLOAFK>QB PM>@B >KA >IPL PQLOBP IFPQP LC BSBOV BIBJBKQ FK QEB TLOIA (PBB 2-4 ?BILT). 2. B:/D: 3BOSBP >P QEB MOFJ>OV BIBJBKQ FK QEB "LU2$ TLOIA. )Q E>P > IL@>QFLK. )Q E>P > SBIL@FQV. 3LRKA C>JFIF>O? 4EB I>S FP BPPBKQF>IIV QEB @I>PP TBgSB ?BBK TOFQFKD LK LRO LTK FK LRO SB@QLOP >KA CLO@BP BU>JMIBP. 3. '3,;0 : +BBMP QO>@H LC >II QEB KB@BPP>OV @LIIFPFLK DBLJBQOV >QQ>@EBA QL > ?LAV. 4. F4C?@=0 : !QQ>@EBP > PE>MB QL > ?LAV >KA PBQP MOLMBOQFBP PR@E >P ABKPFQV, COF@QFLK, >KA OBPQFQRQFLK. 5. J:49? : !@QP >P > @LKKB@QFLK ?BQTBBK QTL ?LAFBP (LO ?BQTBBK LKB ?LAV >KA QEB TLOIA FQPBIC). )K QEB KBUQ CLRO PB@QFLKP, TB >OB DLFKD QL T>IH QEOLRDE B>@E LC QEB >?LSB BIBJBKQP FK ABQ>FI, ?RFIAFKD PBSBO>I BU>JMIBP >ILKD QEB T>V. "RQ CFOPQ QEBOB FP LKB LQEBO FJMLOQ>KQ BIBJBKQ TB PELRIA ?OFBCIV AFP@RPP. 6. *0.2: $BP@OF?BP > SB@QLO FK QEB "LU2$ TLOIA. !KA PL EBOB TB >OB, >OOFSFKD TFQE QOBMFA>QFLK >Q >K RKCLOQRK>QB QORQE FK QEB TLOIA LC RPFKD MEVPF@P IF?O>OFBP. !KV MEVPF@P PFJRI>QFLK FP DLFKD QL FKSLISB QEB @LK@BMQ LC > SB@QLO. 4EFP FP QEB DLLA M>OQ. !CQBO >II, TB GRPQ PMBKQ PBSBO>I @E>MQBOP C>JFIF>OFWFKD LROPBISBP TFQE TE>Q FQ JB>KP QL ABP@OF?B JLQFLK >KA CLO@BP TFQE SB@QLOP. 7B ALKgQ E>SB QL IB>OK >KVQEFKD KBT @LK@BMQR>IIV. .LT CLO QEB M>OQ QE>Q J>HBP QEB PFKDIB QB>O C>II COLJ JV BVB: TB ALKgQ DBQ QL RPB .3?=NIL . )QgP KF@B QE>Q 0OL@BPPFKD E>P .3?=NIL CLO RP, ?RQ >KVQFJB VLR RPB > MEVPF@P IF?O>OV VLR TFII MOL?>?IV AFP@LSBO QE>Q QEB IF?O>OV FK@IRABP FQP LTK SB@QLO FJMIBJBKQ>QFLK. 4EFP J>HBP PBKPB,

194

4EB .>QROB LC #LAB (S1.0)

>CQBO >II; TEV PELRIA "LU2$ ?B BUMB@QBA QL HKLT >?LRQ .3?=NIL ? !KA FK J>KV @>PBP, QEB MEVPF@P BKDFKB TFII T>KQ QL FJMIBJBKQ > SB@QLO @I>PP FK > PMB@FCF@ T>V PL QE>Q FQ FP BPMB@F>IIV @LJM>QF?IB TFQE QEB OBPQ LC QEB IF?O>OVgP @LAB. 3L TEFIB TB ALKgQ E>SB QL IB>OK >KVQEFKD KBT @LK@BMQR>IIV, TB AL E>SB QL DBQ RPBA QL PLJB KBT K>JFKD @LKSBKQFLKP >KA PVKQ>U. ,BQgP NRF@HIV ABJLKPQO>QB > CBT LC QEB ?>PF@P FK 3?=2 >P @LJM>OBA QL QELPB FK .3?=NIL . ,BQgP P>V TB T>KQ QL >AA QTL SB@QLOP QLDBQEBO.

PVector
.3?=NIL ; = H?Q .3?=NIL(1,-1); .3?=NIL < = H?Q .3?=NIL(3,4); ;.;>>(<); .3?=NIL ; = H?Q .3?=NIL(1,-1); .3?=NIL < = H?Q .3?=NIL(3,4); .3?=NIL = = .3?=NIL.;>>(;,<);

Vec2
3?=2 ; = H?Q 3?=2(1,-1); 3?=2 < = H?Q 3?=2(3,4); ;.;>>*I=;F(<); 3?=2 ; = H?Q 3?=2(1,-1); 3?=2 < = H?Q 3?=2(3,4); 3?=2 = = ;.;>>(<);

(LT >?LRQ FC TB T>KQ QL JRIQFMIV >KA P@>IB QEBJ?

PVector
.3?=NIL ; = H?Q .3?=NIL(1,-1); @FI;N H = 5; ;.GOFN(H); .3?=NIL ; = H?Q .3?=NIL(1,-1); @FI;N H = 5; .3?=NIL = = .3?=NIL.GOFN(;,H);

Vec2
3?=2 ; = H?Q 3?=2(1,-1); @FI;N H = 5; ;.GOF*I=;F(H); 3?=2 ; = H?Q 3?=2(1,-1); @FI;N H = 5; 3?=2 = = ;.GOF(H);

->DKFQRAB >KA KLOJ>IFWB?

PVector
.3?=NIL ; = H?Q .3?=NIL(1,-1); @FI;N G = ;.G;A(); ;.HILG;FCT?();

Vec2
3?=2 ; = H?Q 3?=2(1,-1); @FI;N G = ;.F?HANB(); ;.HILG;FCT?();

!P VLR @>K PBB, QEB @LK@BMQP >OB QEB P>JB, ?RQ QEB CRK@QFLK K>JBP >KA QEB >ODRJBKQP >OB PIFDEQIV AFCCBOBKQ. &LO BU>JMIB, FKPQB>A LC PQ>QF@ >KA KLK-PQ>QF@ ;>>() >KA GOFN() , FC > 3?=2 FP >IQBOBA, QEB TLOA hIL@>Ii FP FK@IRABA FK QEB CRK@QFLK K>JBe;>>*I=;F() , GOFN*I=;F() .

195

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

7BgII @LSBO QEB ?>PF@P LC TE>Q VLR KBBA QL HKLT EBOB, ?RQ FC VLR >OB ILLHFKD CLO JLOB, CRII AL@RJBKQ>QFLK LC 3?=2 @>K ?B CLRKA ?V ALTKIL>AFKD QEB *"LU2$ PLRO@B @LAB (EQQM://@LAB.DLLDIB.@LJ/M/G?LU2A/).

5.4 Living in a Box2D World


4EB "LU2$ 4ILF> L?GB@Q FP FK @E>ODB LC BSBOVQEFKD. )Q J>K>DBP QEB @LLOAFK>QB PM>@B LC QEB TLOIA, >II LC QEB PQRCC QE>Q IFSBP FK QEB TLOIA, >KA AB@FABP TEBK QFJB JLSBP CLOT>OA FK QEB TLOIA. )K LOABO QL E>SB "LU2$ >P M>OQ LC LRO 0OL@BPPFKD PHBQ@EBP, QEB 4ILF> FP QEB SBOV CFOPQ QEFKD QE>Q KBBAP QL ?B PBQ RM. (BOB FP TEBOB 0"LU2$ @LJBP FK E>KAV >KA Q>HBP @>OB LC J>HFKD QEB TLOIA CLO RP.
. IR2" <IR2>; PIC> M?NOJ() U <IR2> = H?Q . IR2"(NBCM); <IR2>.=L?;N?4ILF>(); W Initializes a Box2D world with default settings

7EBK VLR @>II =L?;N?4ILF>() , 0"LU2$ TFII PBQ RM > ABC>RIQ DO>SFQV CLO VLR (MLFKQFKD ALTK); ELTBSBO, VLR @>K >IT>VP >IQBO QEB DO>SFQV LC VLRO TLOIA ?V P>VFKD:
<IR2>.M?N%L;PCNS(0, -10);

)QgP TLOQE KLQFKD QE>Q DO>SFQV ALBPKgQ E>SB QL ?B CFUBA, KLO ALBP FQ >IT>VP E>SB QL MLFKQ ALTKT>OAP; VLR @>K >AGRPQ QEB DO>SFQV SB@QLO TEFIB VLRO MOLDO>J FP ORKKFKD. 'O>SFQV @>K ?B QROKBA LCC ?V PBQQFKD FQ QL > (0,0) SB@QLO. 3L, TE>Q >OB QELPB KRJ?BOP 0 >KA -10? 4EFP PELRIA OBJFKA RP LC LKB LC QEB JLPQ FJMLOQ>KQ ABQ>FIP LC RPFKD "LU2$: QEB "LU2$ @LLOAFK>QB PVPQBJ FP KLQ VLRO MFUBI @LLOAFK>QB PVPQBJ! ,BQgP ILLH >Q ELT "LU2$ >KA > 0OL@BPPFKD TFKALT QEFKH AFCCBOBKQIV LC QEBFO TLOIAP.

196

4EB .>QROB LC #LAB (S1.0)

F64B?2 5.2

.LQF@B ELT FK "LU2$ (0,0) FP FK QEB @BKQBO >KA ID FP QEB MLPFQFSB AFOB@QFLK >ILKD QEB V->UFP! "LU2$gP @LLOAFK>QB PVPQBJ FP GRPQ IFHB QE>Q ILSBIV LIA-C>PEFLKBA #>OQBPF>K LKB TFQE (0,0) FK QEB @BKQBO >KA ID MLFKQFKD FK > MLPFQFSB AFOB@QFLK. 0OL@BPPFKD, LK QEB LQEBO E>KA, RPBP > QO>AFQFLK>I @LJMRQBO DO>MEF@P @LLOAFK>QB PVPQBJ TEBOB (0,0) FP FK QEB QLM IBCQ @LOKBO >KA 8CKB FP QEB MLPFQFSB AFOB@QFLK >ILKD QEB V->UFP. 4EFP FP TEV FC TB T>KQ L?GB@QP QL C>II ALTK TFQE DO>SFQV, TB KBBA QL DFSB "LU2$ > DO>SFQV CLO@B TFQE > KBD>QFSB V-S>IRB.
3?=2 AL;PCNS = H?Q 3?=2(0, -10);

,R@HFIV CLO RP, FC TB MOBCBO QL QEFKH FK QBOJP LC MFUBI @LLOAFK>QBP (TEF@E >P 0OL@BPPFKD MOLDO>JJBOP, TB >OB IFHBIV QL AL), 0"LU2$ LCCBOP > PBOFBP LC EBIMBO CRK@QFLKP QE>Q @LKSBOQ ?BQTBBK MFUBI PM>@B >KA "LU2$ PM>@B. "BCLOB TB JLSB LKQL QEB KBUQ PB@QFLK >KA ?BDFK @OB>QFKD "LU2$ ?LAFBP, IBQgP Q>HB > ILLH >Q ELT QEBPB EBIMBO CRK@QFLKP TLOH. ,BQgP P>V TB T>KQ QL QBII "LU2$ TEBOB QEB JLRPB FP FK FQP TLOIA. 7B HKLT QEB JLRPB FP IL@>QBA >Q (GIOM?5,GIOM?6) FK 0OL@BPPFKD. 4L @LKSBOQ FQ, TB P>V TB T>KQ QL @LKSBOQ > h@LLOAFK>QBi COLJ hMFUBIPi QL hTLOIAie=IIL>.CR?FM1I4ILF>() . /O:
3?=2 GIOM?4ILF> = <IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6); Convert mouseX,mouseY to coordinate in Box2D world.

7E>Q FC TB E>A > "LU2$ TLOIA @LLOAFK>QB >KA T>KQBA QL QO>KPI>QB FQ QL LRO MFUBI PM>@B?
3?=2 QILF>.IM = H?Q 3?=2(-10,25); To demonstrate, lets just make up a world position. Convert to pixel space. This is necessary because ultimately we are going to want to draw the elements in our window.

3?=2 JCR?F.IM = <IR2>.=IIL>4ILF>1I.CR?FM(QILF>.IM); ?FFCJM?(JCR?F.IM.R, JCR?F.IM.S,16,16);

197

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

0"LU2$ E>P > PBQ LC CRK@QFLKP QL Q>HB @>OB LC QO>KPI>QFKD ?>@H >KA CLOQE ?BQTBBK QEB "LU2$ TLOIA >KA MFUBIP. )QgP MOL?>?IV B>PFBO QL IB>OK >?LRQ >II LC QEBPB CRK@QFLKP AROFKD QEB @LROPB LC >@QR>IIV FJMIBJBKQFKD LRO BU>JMIBP, ?RQ IBQgP NRF@HIV ILLH LSBO QEB IFPQ LC QEB MLPPF?FIFQFBP.

Task
#LKSBOQ IL@>QFLK COLJ 7LOIA QL 0FUBIP #LKSBOQ IL@>QFLK COLJ 7LOIA QL 0FUBIP #LKSBOQ IL@>QFLK COLJ 0FUBIP QL 7LOIA #LKSBOQ IL@>QFLK COLJ 0FUBIP QL 7LOIA 3@>IB > AFJBKPFLK (PR@E >P EBFDEQ, TFAQE, LO O>AFRP) COLJ 0FUBIP QL 7LOIA 3@>IB > AFJBKPFLK COLJ 7LOIA QL 0FUBIP

Function
3?=2 =IIL>4ILF>1I.CR?FM(3?=2 QILF>) 3?=2 =IIL>4ILF>1I.CR?FM(@FI;N QILF>5, @FI;N QILF>6) 3?=2 =IIL>.CR?FM1I4ILF>(3?=2 M=L??H) 3?=2 =IIL>.CR?FM1I4ILF>(@FI;N JCR?F5, @FI;N JCR?F6) @FI;N M=;F;L.CR?FM1I4ILF>(@FI;N P;F) @FI;N M=;F;L4ILF>1I.CR?FM(@FI;N P;F)

4EBOB >OB >IPL >AAFQFLK>I CRK@QFLKP QE>Q >IILT VLR QL M>PP LO OB@BFSB > .3?=NIL TEBK QO>KPI>QFKD ?>@H >KA CLOQE, ?RQ PFK@B TB >OB LKIV TLOHFKD TFQE "LU2$ FK QEB BU>JMIBP FK QEFP @E>MQBO, FQgP B>PFBPQ QL PQF@H TFQE QEB 3?=2 @I>PP CLO >II SB@QLOP. /K@B QEB TLOIA FP FKFQF>IFWBA, TB >OB OB>AV QL >@QR>IIV MRQ PQRCC FK QEB TLOIAe"LU2$ ?LAFBP.

5.5 Building a Box2D Body


! "LU2$ ?LAV FP QEB MOFJ>OV BIBJBKQ FK QEB "LU2$ TLOIA. )QgP QEB BNRFS>IBKQ QL QEB +IP?L @I>PP TB ?RFIQ LK LRO LTK FK MOBSFLRP @E>MQBOPeQEB QEFKD QE>Q JLSBP >OLRKA QEB PM>@B >KA BUMBOFBK@BP CLO@BP. )Q @>K >IPL ?B PQ>QF@ (JB>KFKD CFUBA >KA KLQ JLSFKD). )QgP FJMLOQ>KQ QL KLQB, ELTBSBO, QE>Q > ?LAV E>P KL DBLJBQOV; FQ FPKgQ >KVQEFKD MEVPF@>I. 2>QEBO, ?LAFBP E>SB "LU2$ PE>MBP >QQ>@EBA QL QEBJ. (4EFP T>V, > ?LAV @>K ?B > PFKDIB OB@Q>KDIB LO > OB@Q>KDIB >QQ>@EBA QL > @FO@IB, BQ@.) 7BgII ILLH >Q PE>MBP FK > JLJBKQ; CFOPQ, IBQgP ?RFIA > ?LAV.

Step 1: Define a body.


4EB CFOPQ QEFKD TB E>SB QL AL FP @OB>QB > h?LAV ABCFKFQFLK.i 4EFP TFII IBQ RP ABCFKB QEB MOLMBOQFBP LC QEB ?LAV TB FKQBKA QL J>HB. 4EFP J>V PBBJ > ?FQ >THT>OA >Q CFOPQ, ?RQ FQgP ELT "LU2$ FP PQOR@QROBA. !KVQFJB VLR T>KQ QL J>HB > hQEFKD,i VLR E>SB QL J>HB > hQEFKD ABCFKFQFLKi CFOPQ. 4EFP TFII ELIA QORB CLO ?LAFBP, PE>MBP, >KA GLFKQP.

198

4EB .>QROB LC #LAB (S1.0)

I>S"?@ <> = H?Q

I>S"?@();

Make a body definition before making a Body.

Step 2: Configure the body definition.


4EB ?LAV ABCFKFQFLK FP TEBOB TB @>K PBQ PMB@FCF@ MOLMBOQFBP LO >QQOF?RQBP LC QEB ?LAV TB FKQBKA QL J>HB. /KB >QQOF?RQB LC > ?LAV, CLO BU>JMIB, FP FQP PQ>OQFKD IL@>QFLK. ,BQgP P>V TB T>KQ QL MLPFQFLK QEB ?LAV FK QEB @BKQBO LC QEB 0OL@BPPFKD TFKALT.
3?=2 =?HN?L = H?Q 3?=2(QC>NB/2,B?CABN/2); A Vec2 in the center of the Processing window

$>KDBO, A>KDBO! )gJ KLQ DLFKD QL >AAOBPP QEFP TFQE BSBOV PFKDIB BU>JMIB, ?RQ FQgP FJMLOQ>KQ QL >Q IB>PQ MLFKQ LRQ QEB MBOFILRP M>QE TB >OB Q>HFKD TFQE QEB >?LSB IFKB LC @LAB. 2BJBJ?BO, FC TB >OB DLFKD QL QBII "LU2$ TEBOB TB T>KQ QEB ?LAV QL PQ>OQ, TB JRPQ DFSB "LU2$ > TLOIA @LLOAFK>QB! 9BP, TB T>KQ QL QEFKH LC FQP IL@>QFLK FK QBOJP LC MFUBIP, ?RQ "LU2$ ALBPKgQ @>OB. !KA PL ?BCLOB TB M>PP QE>Q MLPFQFLK QL QEB ?LAV ABCFKFQFLK, TB JRPQ J>HB PROB QL RPB LKB LC LRO EBIMBO @LKSBOPFLK CRK@QFLKP.
3?=2 =?HN?L = <IR2>.=IIL>.CR?FM1I4ILF>(QC>NB/2,B?CABN/2)); A Vec2 in the center of the Processing window converted to Box2D World coordinates! Setting the position attribute of the Box2D body definition

<>.JIMCNCIH.M?N(=?HN?L);

4EB ?LAV ABCFKFQFLK JRPQ >IPL PMB@FCV QEB hQVMBi LC ?LAV TB T>KQ QL J>HB. 4EBOB >OB QEOBB MLPPF?FIFQFBP: j DD9,84.. 4EFP FP TE>Q TB TFII RPB JLPQ LCQBKe> hCRIIV PFJRI>QBAi ?LAV. ! AVK>JF@ ?LAV JLSBP >OLRKA QEB TLOIA, @LIIFABP TFQE LQEBO ?LAFBP, >KA OBPMLKAP QL QEB CLO@BP FK FQP BKSFOLKJBKQ. j '?,?4.. ! PQ>QF@ ?LAV FP LKB QE>Q @>KKLQ JLSB (>P FC FQ E>A >K FKCFKFQB J>PP). 7BgII RPB PQ>QF@ ?LAFBP CLO CFUBA MI>QCLOJP >KA ?LRKA>OFBP. j 4908,?4.. ! HFKBJ>QF@ ?LAV @>K ?B JLSBA J>KR>IIV ?V PBQQFKD FQP SBIL@FQV AFOB@QIV. )C VLR E>SB > RPBO-@LKQOLIIBA L?GB@Q FK VLRO TLOIA, VLR @>K RPB > HFKBJ>QF@ ?LAV. .LQB QE>Q HFKBJ>QF@ ?LAFBP @LIIFAB LKIV TFQE AVK>JF@ ?LAFBP >KA KLQ TFQE LQEBO PQ>QF@ LO HFKBJ>QF@ LKBP.

4EBOB >OB PBSBO>I LQEBO MOLMBOQFBP VLR @>K PBQ FK QEB ?LAV ABCFKFQFLK. &LO BU>JMIB, FC VLR T>KQ VLRO ?LAV QL E>SB > CFUBA OLQ>QFLK (F.B. KBSBO OLQ>QB), VLR @>K P>V:
<>.@CR?>/IN;NCIH = NLO?;

199

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

9LR @>K >IPL PBQ > S>IRB CLO IFKB>O LO >KDRI>O A>JMFKD, PL QE>Q QEB L?GB@Q @LKQFKRLRPIV PILTP >P FC QEBOB FP COF@QFLK.
<>.FCH?;L";GJCHA = 0.8; <>.;HAOF;L";GJCHA = 0.9;

)K >AAFQFLK, C>PQ-JLSFKD L?GB@QP FK "LU2$ PELRIA ?B PBQ >P ?RIIBQP. 4EFP QBIIP QEB "LU2$ BKDFKB QE>Q QEB L?GB@Q J>V JLSB SBOV NRF@HIV >KA QL @EB@H FQP @LIIFPFLKP JLOB @>OBCRIIV PL QE>Q FQ ALBPKgQ >@@FABKQ>IIV GRJM LSBO >KLQEBO ?LAV.
<>.<OFF?N = NLO?;

Step 3: Create the body.


/K@B TBgOB ALKB TFQE QEB ABCFKFQFLK ( I>S"?@ ), TB @>K @OB>QB QEB I>S L?GB@Q FQPBIC. 0"LU2$ MOLSFABP > EBIMBO CRK@QFLK CLO QEFPe=L?;N? I>S ().
I>S <I>S = <IR2>.=L?;N? I>S(<>); The Body object is created by passing in the Body Definition. (This allows for making multiple bodies from one definition.)

Step 4: Set any other conditions for the bodys starting state.
&FK>IIV, QELRDE KLQ OBNRFOBA, FC VLR T>KQ QL PBQ >KV LQEBO FKFQF>I @LKAFQFLKP CLO QEB ?LAV, PR@E >P IFKB>O LO >KDRI>O SBIL@FQV, VLR @>K AL PL TFQE QEB KBTIV @OB>QBA I>S L?GB@Q.
<I>S.M?N*CH?;L3?FI=CNS(H?Q 3?=2(0,3)); <I>S.M?NAHAOF;L3?FI=CNS(1.2); Setting an arbitrary initial velocity Setting an arbitrary initial angular velocity

5.6 Threes Company: Bodies and Shapes and Fixtures


! ?LAV LK FQP LTK ALBPKgQ MEVPF@>IIV BUFPQ FK QEB TLOIA. )QgP IFHB > PLRI TFQE KL ERJ>K CLOJ QL FKE>?FQ. &LO > ?LAV QL E>SB J>PP, TB JRPQ CFOPQ ABCFKB > PE>MB >KA >QQ>@E QE>Q PE>MB QL QEB ?LAV TFQE PLJBQEFKD HKLTK >P > CFUQROB. 4EB GL? LC QEB "LU2$ 0B;J? @I>PP FP QL HBBM QO>@H LC >II QEB KB@BPP>OV @LIIFPFLK DBLJBQOV >QQ>@EBA QL > ?LAV. ! PE>MB >IPL E>P PBSBO>I FJMLOQ>KQ MOLMBOQFBP QE>Q >CCB@Q QEB ?LAVgP JLQFLK. 4EBOB FP ABKPFQV, TEF@E RIQFJ>QBIV ABQBOJFKBP QE>Q ?LAVgP J>PP. 3E>MBP >IPL E>SB :F=7H=CB >KA F9GH=HIH=CB (h?LRK@FKBPPi) TEF@E TFII ?B ABCFKBA QEOLRDE > CFUQROB. /KB LC QEB

200

4EB .>QROB LC #LAB (S1.0)

KF@B QEFKDP >?LRQ "LU2$gP JBQELALILDV, TEF@E PBM>O>QBP QEB @LK@BMQP LC ?LAFBP >KA PE>MBP FKQL QTL PBM>O>QB L?GB@QP, FP QE>Q VLR @>K >QQ>@E JRIQFMIB PE>MBP QL > PFKDIB ?LAV FK LOABO QL @OB>QB JLOB @LJMIBU CLOJP. 7BgII PBB QEFP FK > CRQROB BU>JMIB. 4L @OB>QB > PE>MB, TB KBBA QL CFOPQ AB@FAB TE>Q HFKA LC PE>MB TB T>KQ QL J>HB. &LO JLPQ KLK-@FO@RI>O PE>MBP, > .IFSAIH0B;J? L?GB@Q TFII TLOH GRPQ CFKB. &LO BU>JMIB, IBQgP ILLH >Q ELT TB ABCFKB > OB@Q>KDIB.

Step 1: Define a shape.


.IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); Define the shape: a polygon.

.BUQ RM, TB E>SB QL ABCFKB QEB TFAQE >KA EBFDEQ LC QEB OB@Q>KDIB. ,BQgP P>V TB T>KQ LRO OB@Q>KDIB QL ?B 150Y100 MFUBIP. 2BJBJ?BO, MFUBI RKFQP >OB KL DLLA CLO "LU2$ PE>MBP! 3L TB E>SB QL RPB LRO EBIMBO CRK@QFLKP QL @LKSBOQ QEBJ CFOPQ.
@FI;N <IR2"Q = <IR2>.M=;F;L.CR?FM1I4ILF>(150); @FI;N <IR2"B = <IR2>.M=;F;L.CR?FM1I4ILF>(100); Scale dimensions from pixels to Box2D world.

JM.M?NAM IR(<IR2"Q, <IR2"B);

Use setAsBox() function to define shape as a rectangle.

Step 2: Create a fixture.


4EB PE>MB >KA ?LAV >OB J>AB >P QTL PBM>O>QB BKQFQFBP. )K LOABO QL >QQ>@E > PE>MB QL > ?LAV, TB JRPQ J>HB > CFUQROB. ! CFUQROB FP @OB>QBA, GRPQ >P TFQE QEB ?LAV, SF> > CFUQROB ABCFKFQFLK (F.B. $CRNOL?"?@ @I>PP) >KA >PPFDKBA > PE>MB.
$CRNOL?"?@ @> = H?Q $CRNOL?"?@(); @>.MB;J? = JM; The fixture is assigned the PolygonShape we just made.

/K@B TB E>SB QEB CFUQROB ABCFKFQFLK, TB @>K PBQ M>O>JBQBOP QE>Q >CCB@Q QEB MEVPF@P CLO QEB PE>MB ?BFKD >QQ>@EBA.
@>.@LC=NCIH = 0.3; The coefficient of friction for the shape, typically between 0 and 1 The Shapes restitution (i.e. elasticity), typically between 0 and 1 The Shapes density, measured in kilograms per meter squared

@>.L?MNCNONCIH = 0.5;

@>.>?HMCNS = 1.0;

201

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Step 3: Attach the shape to the body with the fixture.


/K@B QEB CFUQROB FP ABCFKBA, >II TB E>SB IBCQ QL AL FP >QQ>@E QEB PE>MB QL QEB ?LAV TFQE QEB CFUQROB ?V @>IIFKD QEB =L?;N?$CRNOL?() CRK@QFLK.
<I>S.=L?;N?$CRNOL?(@>); Creates the Fixture and attaches the Shape to the Body object

) PELRIA KLQB QE>Q 3QBM 2 @>K ?B PHFMMBA FC VLR AL KLQ KBBA QL PBQ QEB MEVPF@P MOLMBOQFBP. ("LU2$ TFII RPB ABC>RIQ S>IRBP.) 9LR @>K @OB>QB > CFUQROB >KA >QQ>@E QEB PE>MB >II FK LKB PQBM ?V P>VFKD:
<I>S.=L?;N?$CRNOL?(JM,1); Creates the Fixture and attaches the Shape with a density of 1

7EFIB JLPQ LC LRO BU>JMIBP TFII Q>HB @>OB LC >QQ>@EFKD PE>MBP LKIV LK@B TEBK QEB ?LAV FP CFOPQ ?RFIQ, QEFP FP KLQ > IFJFQ>QFLK LC "LU2$. "LU2$ >IILTP CLO PE>MBP QL ?B @OB>QBA >KA ABPQOLVBA LK QEB CIV. "BCLOB TB MRQ >KV LC QEFP @LAB TBgSB ?BBK TOFQFKD FKQL > 0OL@BPPFKD PHBQ@E, IBQgP OBSFBT >II QEB PQBMP TB QLLH QL @LKPQOR@Q > "LAV. 1. $BCFKB > ?LAV RPFKD > I>S"?@ L?GB@Q (PBQ >KV MOLMBOQFBP, PR@E >P IL@>QFLK). 2. #OB>QB QEB I>S L?GB@Q COLJ QEB ?LAV ABCFKFQFLK. 3. $BCFKB > 0B;J? L?GB@Q RPFKD .IFSAIH0B;J? , !CL=F?0B;J? , LO >KV LQEBO PE>MB @I>PP. 4. $BCFKB > CFUQROB RPFKD $CRNOL?"?@ >KA >PPFDK QEB CFUQROB > PE>MB (PBQ >KV MOLMBOQFBP, PR@E >P COF@QFLK, ABKPFQV, >KA OBPQFQRQFLK). 5. !QQ>@E QEB PE>MB QL QEB ?LAV.
I>S"?@ <> = H?Q I>S"?@(); Step 1. Define the body.

<>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(QC>NB/2,B?CABN/2)); I>S <I>S = <IR2>.=L?;N? I>S(<>); Step 2. Create the body.

.IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); @FI;N Q = <IR2>.M=;F;L.CR?FM1I4ILF>(150); @FI;N B = <IR2>.M=;F;L.CR?FM1I4ILF>(100); JM.M?NAM IR(Q, B);

Step 3. Define the shape.

202

4EB .>QROB LC #LAB (S1.0)

$CRNOL?"?@ @> = H?Q $CRNOL?"?@(); @>.MB;J? = JM; @>.>?HMCNS = 1; @>.@LC=NCIH = 0.3; @>.L?MNCNONCIH = 0.5; <I>S.=L?;N?$CRNOL?(@>);

Step 4. Define the fixture.

Step 5. Attach the shape to the body with the Fixture.

Exercise 5.1
+KLTFKD TE>Q VLR HKLT >?LRQ "LU2$ PL C>O, CFII FK QEB ?I>KH FK QEB @LAB ?BILT QE>Q ABJLKPQO>QBP ELT QL J>HB > @FO@RI>O PE>MB FK "LU2$.
!CL=F?0B;J? =M = H?Q !CL=F?0B;J?(); @FI;N L;>COM = 10; =M.G:L;>COM = ::::::::::::::::::::; $CRNOL?"?@ @> = H?Q $CRNOL?"?@(); @>.MB;J? = =M; @>.>?HMCNS = 1; @>.@LC=NCIH = 0.1; @>.L?MNCNONCIH = 0.3; <I>S.=L?;N?$CRNOL?(@>);

5.7 Box2D and Processing: Reunited and It Feels So Good


/K@B > ?LAV FP J>AB, FQ IFSBP FK QEB "LU2$ MEVPF@P TLOIA. "LU2$ TFII >IT>VP HKLT FQgP QEBOB, @EB@H FQ CLO @LIIFPFLKP, JLSB FQ >MMOLMOF>QBIV >@@LOAFKD QL QEB CLO@BP, BQ@. )QgII AL >II QE>Q CLO VLR TFQELRQ VLR E>SFKD QL IFCQ > CFKDBO! 7E>Q FQ TLKgQ AL, ELTBSBO, FP AFPMI>V QEB ?LAV CLO VLR. 4EFP FP > DLLA QEFKD. 4EFP FP VLRO QFJB QL PEFKB. 7EBK TLOHFKD TFQE "LU2$, TE>Q TBgOB BPPBKQF>IIV P>VFKD FP, h) T>KQ QL ?B QEB ABPFDKBO LC JV TLOIA, >KA ) T>KQ VLR, "LU2$, QL @LJMRQB >II QEB MEVPF@P.i .LT, "LU2$ TFII HBBM > IFPQ LC >II QEB ?LAFBP QE>Q BUFPQ FK QEB TLOIA. 4EFP @>K ?B >@@BPPBA ?V @>IIFKD QEB 7LOIA L?GB@QgP A?N I>S*CMN() CRK@QFLK. .BSBOQEBIBPP, TE>Q )gJ DLFKD QL ABJLKPQO>QB EBOB FP > QB@EKFNRB CLO HBBMFKD VLRO LTK ?LAV IFPQP. 9BP, QEFP J>V ?B > ?FQ OBARKA>KQ >KA TB MBOE>MP P>@OFCF@B > ?FQ LC BCCF@FBK@V. "RQ TB JLOB QE>K J>HB RM CLO QE>Q TFQE B>PB LC RPB. 4EFP JBQELALILDV TFII >IILT RP QL MOLDO>J IFHB TBgOB RPBA QL FK 0OL@BPPFKD, >KA TB @>K B>PFIV HBBM QO>@H LC TEF@E ?LAFBP >OB TEF@E >KA OBKABO QEBJ >MMOLMOF>QBIV. ,BQgP @LKPFABO QEB PQOR@QROB LC QEB CLIILTFKD 0OL@BPPFKD PHBQ@E:

203

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

F64B?2 5.3

4EFP ILLHP IFHB >KV LIg 0OL@BPPFKD PHBQ@E. 7B E>SB > J>FK Q>? @>IIBA h"LUBPi >KA > h"LRKA>OVi >KA > h"LUi Q>?. ,BQgP QEFKH >?LRQ QEB "LU Q>? CLO > JLJBKQ. 4EB "LU Q>? FP TEBOB TB TFII TOFQB > PFJMIB @I>PP QL ABP@OF?B > IR L?GB@Q, > OB@Q>KDRI>O ?LAV FK LRO TLOIA.
=F;MM IR U Our Box object has an x,y location and a width and a height.

@FI;N R,S; @FI;N Q,B; IR(@FI;N R:, @FI;N S:) U R = R:; S = S:; Q = 16; B = 16; W PIC> >CMJF;S() U @CFF(175); MNLIE?(0); L?=N+I>?(!#,1#/); L?=N(R,S,Q,B); W W

The location is initalized in the constructor via arguments

We draw the Box object using Processings rect() function.

,BQgP TOFQB > J>FK Q>? QE>Q @OB>QBP > KBT IR TEBKBSBO QEB JLRPB FP MOBPPBA >KA PQLOBP >II QEB IR L?GB@QP FK >K ALL;S*CMN . (4EFP FP SBOV PFJFI>O QL LRO >MMOL>@E FK QEB M>OQF@IB PVPQBJ BU>JMIBP COLJ #E>MQBO 4.)

204

4EB .>QROB LC #LAB (S1.0)

Example 5.1: A comfortable and cozy Processing sketch that needs a little Box2D
ALL;S*CMN< IR> <IR?M; PIC> M?NOJ() U MCT?(400,300); <IR?M = H?Q ALL;S*CMN< IR>(); W PIC> >L;Q() U <;=EALIOH>(255); C@ (GIOM?.L?MM?>) U IR J = H?Q IR(GIOM?5,GIOM?6); <IR?M.;>>(J); W When the mouse is pressed, add a new Box object. A list to store all Box objects

@IL ( IR <: <IR?M) U <.>CMJF;S(); W W

Display all the Box objects.

.LT, EBOBgP LRO >PPFDKJBKQ. 4>HB QEB >?LSB BU>JMIB SBO?>QFJ, ?RQ FKPQB>A LC AO>TFKD CFUBA ?LUBP LK QEB P@OBBK, AO>T ?LUBP QE>Q BUMBOFBK@B MEVPF@P (SF> "LU2$) >P PLLK >P QEBV >MMB>O. 7BgII KBBA QTL J>GLO PQBMP QL >@@LJMIFPE LRO DL>I.

Step 1: Add Box2D to our main program (i.e. setup() and draw()).
4EFP M>OQ FP KLQ QLL QLRDE. 7B P>T QEFP >IOB>AV FK LRO AFP@RPPFLK LC ?RFIAFKD > "LU2$ TLOIA. 4EFP FP Q>HBK @>OB LC CLO RP ?V QEB 0"LU2$ EBIMBO @I>PP. 7B @>K @OB>QB > 0"LU2$ L?GB@Q >KA FKFQF>IFWB FQ FK M?NOJ() .
. IR2" <IR2>; PIC> M?NOJ() U <IR2> = H?Q . IR2"(NBCM); <IR2>.=L?;N?4ILF>(); W Initialize and create the Box2D world.

4EBK FK >L;Q() , TB KBBA QL J>HB PROB TB @>II LKB SBOV FJMLOQ>KQ CRK@QFLK: MN?J() . 7FQELRQ QEFP CRK@QFLK, KLQEFKD TLRIA BSBO E>MMBK! MN?J() >AS>K@BP QEB "LU2$ TLOIA > PQBM CROQEBO FK QFJB. )KQBOK>IIV, "LU2$ PTBBMP QEOLRDE >KA ILLHP >Q >II LC QEB "LAFBP >KA CFDROBP LRQ TE>Q QL AL TFQE QEBJ. *RPQ @>IIFKD MN?J() LK FQP LTK JLSBP QEB "LU2$ TLOIA CLOT>OA TFQE ABC>RIQ PBQQFKDP; ELTBSBO, FQ FP @RPQLJFW>?IB (>KA QEFP FP AL@RJBKQBA FK QEB 0"LU2$ PLRO@B).

205

#E>MQBO 5. 0EVPF@P ,F?O>OFBP


PIC> >L;Q() U <IR2>.MN?J(); W We must always step through time!

Step 2: Link every Processing Box object with a Box2D Body object.
!P LC QEFP JLJBKQ, QEB IR @I>PP FK@IRABP S>OF>?IBP CLO IL@>QFLK >KA TFAQE >KA EBFDEQ. 7E>Q TB KLT T>KQ QL P>V FP: h) EBOB?V OBIFKNRFPE QEB @LJJ>KA LC QEFP L?GB@QgP MLPFQFLK QL "LU2$. ) KL ILKDBO KBBA QL HBBM QO>@H LC >KVQEFKD OBI>QBA QL IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK. )KPQB>A, ) LKIV KBBA QL HBBM QO>@H LC > "LU2$ ?LAV >KA E>SB C>FQE QE>Q "LU2$ TFII AL QEB OBPQ.i
=F;MM IR U Instead of any of the usual variables, we will store a reference to a Box2D body.

I>S <I>S; @FI;N Q; @FI;N B;

7B ALKgQ KBBA (L,M) >KVJLOB PFK@B, >P TBgII PBB, QEB ?LAV FQPBIC TFII HBBM QO>@H LC FQP IL@>QFLK. 4EB ?LAV QB@EKF@>IIV @LRIA >IPL HBBM QO>@H LC QEB TFAQE >KA EBFDEQ CLO RP, ?RQ PFK@B "LU2$ FPKgQ DLFKD QL AL >KVQEFKD QL >IQBO QELPB S>IRBP LSBO QEB IFCB LC QEB IR L?GB@Q, TB JFDEQ >P TBII GRPQ ELIA LKQL QEBJ LROPBISBP RKQFI FQgP QFJB QL AO>T QEB IR . 4EBK, FK LRO @LKPQOR@QLO, FK >AAFQFLK QL FKFQF>IFWFKD QEB TFAQE >KA EBFDEQ, TB @>K DL >EB>A >KA FK@IRAB >II LC QEB ?LAV >KA PE>MB @LAB TB IB>OKBA FK QEB MOBSFLRP QTL PB@QFLKP!
IR() U Q = 16; B = 16; I>S"?@ <> = H?Q I>S"?@(); Build body.

<>.NSJ? = I>S1SJ?."6,A+'!; <>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6)); <I>S = <IR2>.=L?;N? I>S(<>); .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); @FI;N <IR2>4 = <IR2>.M=;F;L.CR?FM1I4ILF>(Q/2); @FI;N <IR2>& = <IR2>.M=;F;L.CR?FM1I4ILF>(B/2); Build shape. Box2D considers the width and height of a rectangle to be the distance from the center to the edge (so half of what we normally think of as width or height).

206

4EB .>QROB LC #LAB (S1.0)


JM.M?NAM IR(<IR2>4, <IR2>&); $CRNOL?"?@ @> = H?Q $CRNOL?"?@(); @>.MB;J? = JM; @>.>?HMCNS = 1; @>.@LC=NCIH = 0.3; @>.L?MNCNONCIH = 0.5; <I>S.=L?;N?$CRNOL?(@>); W Attach the Shape to the Body with the Fixture. Set physics parameters.

/+, TBgOB >IJLPQ QEBOB. "BCLOB TB FKQOLAR@BA "LU2$, FQ T>P B>PV QL AO>T QEB IR . 4EB L?GB@QgP IL@>QFLK T>P PQLOBA FK S>OF>?IBP R >KA S .
PIC> >CMJF;S() U @CFF(175); MNLIE?(0); L?=N+I>?(!#,1#/); L?=N(R,S,Q,B); W Drawing the object using rect()

"RQ KLT "LU2$ J>K>DBP QEB L?GB@QgP JLQFLK, PL TB @>K KL ILKDBO RPB LRO LTK S>OF>?IBP QL AFPMI>V QEB PE>MB. .LQ QL CB>O! /RO IR L?GB@Q E>P > OBCBOBK@B QL QEB "LU2$ ?LAV >PPL@F>QBA TFQE FQ. 3L >II TB KBBA QL AL FP MLIFQBIV >PH QEB ?LAV, h0>OALK JB, TEBOB >OB VLR IL@>QBA?i 3FK@B QEFP FP > Q>PH TBgII KBBA QL AL NRFQB LCQBK, 0"LU2$ FK@IRABP > EBIMBO CRK@QFLK: A?N I>S.CR?F!IIL>() .
3?=2 JIM = <IR2>.A?N I>S.CR?F!IIL>(<I>S);

*RPQ HKLTFKD QEB IL@>QFLK LC > ?LAV FPKgQ BKLRDE; TB >IPL KBBA QL HKLT FQP >KDIB LC OLQ>QFLK.
@FI;N ; = <I>S.A?NAHAF?();

/K@B TB E>SB QEB IL@>QFLK >KA >KDIB, FQgP B>PV QL AFPMI>V QEB L?GB@Q RPFKD NL;HMF;N?() >KA LIN;N?() . .LQB, ELTBSBO, QE>Q QEB "LU2$ @LLOAFK>QB PVPQBJ @LKPFABOP OLQ>QFLK FK QEB LMMLPFQB AFOB@QFLK COLJ 0OL@BPPFKD, PL TB KBBA QL JRIQFMIV QEB >KDIB ?V -1.

207

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

F64B?2 5.4
PIC> >CMJF;S() U 3?=2 JIM = <IR2>.A?N I>S.CR?F!IIL>(<I>S); @FI;N ; = <I>S.A?NAHAF?(); JOMB+;NLCR(); NL;HMF;N?(JIM.R,JIM.S); LIN;N?(-;); @CFF(175); MNLIE?(0); L?=N+I>?(!#,1#/); L?=N(0,0,Q,B); JIJ+;NLCR(); W Using the Vec2 position and float angle to translate and rotate the rectangle We need the Bodys location and angle.

)K @>PB TB T>KQ QL E>SB L?GB@QP QE>Q @>K ?B OBJLSBA COLJ QEB "LU2$ TLOIA, FQgP >IPL RPBCRI QL FK@IRAB > CRK@QFLK QL ABPQOLV > ?LAV, PR@E >P:
PIC> ECFF I>S() U <IR2>.>?MNLIS I>S(<I>S); W This function removes a body from the Box2D world.

208

4EB .>QROB LC #LAB (S1.0)

Exercise 5.2

)K QEFP @E>MQBOgP @LAB ALTKIL>AP, CFKA QEB PHBQ@E K>JBA h?LU2A=BUBO@FPB.i 5PFKD QEB JBQELALILDV LRQIFKBA FK QEFP @E>MQBO, >AA QEB KB@BPP>OV @LAB QL QEB J>FK >KA "LU Q>?P QL FJMIBJBKQ "LU2$ MEVPF@P. 4EB OBPRIQ PELRIA >MMB>O >P FK QEB P@OBBKPELQ >?LSB. "B JLOB @OB>QFSB FK ELT VLR OBKABO QEB ?LUBP.

5.8 Fixed Box2D Objects


)K QEB BU>JMIB TB GRPQ @OB>QBA, QEB IR L?GB@QP >MMB>O >Q QEB JLRPB IL@>QFLK >KA C>II ALTKT>OAP ARB QL "LU2$gP ABC>RIQ DO>SFQV CLO@B. 7E>Q FC TB T>KQBA QL FKPQ>II PLJB FJJLS>?IB ?LRKA>OFBP FK QEB "LU2$ TLOIA QE>Q TLRIA ?IL@H QEB M>QE LC QEB IR L?GB@QP (>P FK QEB FIIRPQO>QFLK ?BILT)? "LU2$ J>HBP QEFP B>PV CLO RP ?V MOLSFAFKD > JB>KP QL IL@H > ?LAV (>KA >KV >PPL@F>QBA PE>MBP) FK MI>@B. *RPQ PBQ QEB I>S"?@ L?GB@QgP QVMB QL 01A1'! .
I>S"?@ <> = H?Q <>.NSJ? = I>S"?@(); When BodyDef type = STATIC, the Body is locked in place.

I>S1SJ?.01A1'!;

7B @>K >AA QEFP CB>QROB QL LRO "LUBP BU>JMIB ?V TOFQFKD > IOH>;LS @I>PP >KA E>SFKD B>@E ?LRKA>OV @OB>QB > CFUBA "LU2$ ?LAV.

209

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Example 5.2: Falling boxes hitting boundaries


=F;MM IOH>;LS U A boundary is a simple rectangle with x, y, width, and height.

@FI;N R,S; @FI;N Q,B; I>S <; IOH>;LS(@FI;N R:,@FI;N S:, @FI;N Q:, @FI;N B:) U R = R:; S = S:; Q = Q:; B = B:; I>S"?@ <> = H?Q I>S"?@();

Build the Box2D Body and Shape.

<>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(R,S)); <>.NSJ? = I>S1SJ?.01A1'!; Make it fixed by setting type to STATIC!

< = <IR2>.=L?;N? I>S(<>); @FI;N <IR2>4 = <IR2>.M=;F;L.CR?FM1I4ILF>(Q/2); @FI;N <IR2>& = <IR2>.M=;F;L.CR?FM1I4ILF>(B/2); .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); JM.M?NAM IR(<IR2>4, <IR2>&); The PolygonShape is just a box.

<.=L?;N?$CRNOL?(JM,1); W PIC> >CMJF;S() U @CFF(0); MNLIE?(0); L?=N+I>?(!#,1#/); L?=N(R,S,Q,B); W W

Using the createFixture() shortcut

Since we know it can never move, we can just draw it the old-fashioned way, using our original variables. No need to query Box2D.

210

4EB .>QROB LC #LAB (S1.0)

5.9 A Curvy Boundary


)C VLR T>KQ > CFUBA ?LRKA>OV QE>Q FP > @ROSBA PROC>@B (>P LMMLPBA QL > MLIVDLK), QEFP @>K ?B >@EFBSBA TFQE QEB PE>MB !B;CH0B;J? . 4EB !B;CH0B;J? @I>PP FP >KLQEBO PE>MB IFHB .IFSAIH0B;J? LO !CL=F?0B;J? , PL QL FK@IRAB LKB FK LRO PVPQBJ, TB CLIILT QEB P>JB PQBMP.

Step 1: Define a body.


I>S"?@ <> = H?Q I>S"?@(); The body does not need a position; the EdgeShape will take care of that for us. It also does not need a type, as it is STATIC by default.

I>S <I>S = <IR2>.QILF>.=L?;N? I>S(<>);

Step 2: Define the Shape.


!B;CH0B;J? =B;CH = H?Q !B;CH0B;J?();

Step 3: Configure the Shape.


4EB !B;CH0B;J? L?GB@Q FP > PBOFBP LC @LKKB@QBA SBOQF@BP. 4L @OB>QB QEB @E>FK, TB JRPQ CFOPQ PMB@FCV >K >OO>V LC SBOQF@BP (B>@E >P > 3?=2 L?GB@Q). &LO BU>JMIB, FC TB T>KQBA > PQO>FDEQ IFKB COLJ QEB IBCQ-E>KA PFAB LC LRO TFKALT QL QEB OFDEQ-E>KA PFAB, TB TLRIA GRPQ KBBA >K >OO>V LC QTL SBOQF@BP: (0,150) >KA (TFAQE,150). (.LQB QE>Q FC VLR T>KQ QL @OB>QB > ILLM TEBOB QEB CFOPQ SBOQBU @LKKB@QP QL QEB I>PQ SBOQBU FK > ILLM, VLR @>K RPB QEB !B;CH*IIJ @I>PP FKPQB>A.)
3?=289 P?LNC=?M = H?Q 3?=2829; P?LNC=?M809 = <IR2>.=IIL>.CR?FM1I4ILF>(0,150); P?LNC=?M819 = <IR2>.=IIL>.CR?FM1I4ILF>(QC>NB,150); Adding a vertex on the right side of window Adding a vertex on the left side of window

4L @OB>QB QEB @E>FK TFQE QEB SBOQF@BP, QEB >OO>V FP QEBK M>PPBA FKQL > CRK@QFLK @>IIBA =L?;N?!B;CH() .
=B;CH.=L?;N?!B;CH(P?LNC=?M, P?LNC=?M.F?HANB); If you dont want to use the entire array, you can specify a value less than length.

211

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Step 4: Attach the Shape to the body with a Fixture.


! 0B;J? FP KLQ M>OQ LC "LU2$ RKIBPP FQ FP >QQ>@EBA QL > ?LAV. %SBK FC FQ FP > CFUBA ?LRKA>OV >KA KBSBO JLSBP, FQ JRPQ PQFII ?B >QQ>@EBA. *RPQ >P TFQE LQEBO PE>MBP, > !B;CH0B;J? L?GB@Q @>K ?B DFSBK MOLMBOQFBP IFHB OBPQFQRQFLK >KA COF@QFLK TFQE > $CRNOL? .
$CRNOL?"?@ @> = H?Q $CRNOL?"?@(); @>.MB;J? = =B;CH; @>.>?HMCNS = 1; @>.@LC=NCIH = 0.3; @>.L?MNCNONCIH = 0.5; <I>S.=L?;N?$CRNOL?(@>); A fixture assigned to the ChainShape

.LT, FC TB T>KQ QL FK@IRAB > !B;CH0B;J? L?GB@Q FK LRO PHBQ@E, TB @>K CLIILT QEB P>JB PQO>QBDV >P TB AFA TFQE > CFUBA ?LRKA>OV. ,BQgP TOFQB > @I>PP @>IIBA 0OL@;=? :

212

4EB .>QROB LC #LAB (S1.0)

Example 5.3: ChainShape with three hard-coded vertices


=F;MM 0OL@;=? U ALL;S*CMN<3?=2> MOL@;=?; 0OL@;=?() U MOL@;=? = H?Q ALL;S*CMN<3?=2>(); MOL@;=?.;>>(H?Q 3?=2(0, B?CABN/2+50)); MOL@;=?.;>>(H?Q 3?=2(QC>NB/2, B?CABN/2+50)); MOL@;=?.;>>(H?Q 3?=2(QC>NB, B?CABN/2)); !B;CH0B;J? =B;CH = H?Q !B;CH0B;J?(); 3?=289 P?LNC=?M = H?Q 3?=28MOL@;=?.MCT?()9; Make an array of Vec2 for the ChainShape. 3 vertices in pixel coordinates

@IL (CHN C = 0; C < P?LNC=?M.F?HANB; C++) U

Convert each vertex to Box2D World coordinates.

P?LNC=?M8C9 = <IR2>.=IIL>.CR?FM1I4ILF>(MOL@;=?.A?N(C)); W =B;CH.=L?;N?!B;CH(P?LNC=?M, P?LNC=?M.F?HANB); Create the ChainShape with array of Vec2.

I>S"?@ <> = H?Q I>S"?@(); I>S <I>S = <IR2>.QILF>.=L?;N? I>S(<>); <I>S.=L?;N?$CRNOL?(=B;CH, 1); W

Attach the Shape to the Body.

.LQF@B ELT QEB >?LSB @I>PP FK@IRABP >K ALL;S*CMN QL PQLOB > PBOFBP LC 3?=2 L?GB@QP. %SBK QELRDE TB CRIIV FKQBKA QL PQLOB QEB @LLOAFK>QBP LC QEB @E>FK FK QEB @E>FK PE>MB FQPBIC, TB >OB @ELLPFKD QEB B>PB LC OBARKA>K@V >KA HBBMFKD LRO LTK IFPQ LC QELPB MLFKQP >P TBII. ,>QBO, TEBK TB DL QL AO>T QEB 0OL@;=? L?GB@Q, TB ALKgQ E>SB QL >PH "LU2$ CLO QEB IL@>QFLKP LC QEB @E>FK PE>MBgP SBOQF@BP.
PIC> >CMJF;S() U MNLIE?4?CABN(1); MNLIE?(0); HI$CFF(); <?ACH0B;J?(); @IL (3?=2 P: MOL@;=?) U P?LN?R(P.R,P.S); W ?H>0B;J?(); W W Draw the ChainShape as a series of vertices.

213

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

7E>Q TB KBBA FK M?NOJ() >KA >L;Q() CLO QEB 0OL@;=? L?GB@Q FP NRFQB PFJMIB, DFSBK QE>Q "LU2$ Q>HBP @>OB LC >II LC QEB MEVPF@P CLO RP.
. IR2" <IR2>; 0OL@;=? MOL@;=?; PIC> M?NOJ() U MCT?(500,300); <IR2> = H?Q . IR2"(NBCM); <IR2>.=L?;N?4ILF>(); MOL@;=? = H?Q 0OL@;=?(); W PIC> >L;Q() U <IR2>.MN?J(); <;=EALIOH>(255); MOL@;=?.>CMJF;S(); W Draw the Surface. Make a Surface object.

Exercise 5.3
2BSFBT ELT TB IB>OKBA QL AO>T > T>SB M>QQBOK FK #E>MQBO 3. #OB>QB > !B;CH0B;J? L?GB@Q LRQ LC > PFKB T>SB. 4OV RPFKD 0BOIFK KLFPB (PBB M>DB 17) >P TBII.

@6;2 D.C2

'2?96; ;<6@2

214

4EB .>QROB LC #LAB (S1.0)

5.10 Complex Forms


.LT QE>Q TBgSB PBBK ELT B>PV FQ FP QL J>HB PFJMIB DBLJBQOF@ CLOJP FK "LU2$, IBQgP FJ>DFKB QE>Q VLR T>KQ QL E>SB > JLOB @LJMIBU CLOJ, PR@E >P > IFQQIB >IFBK PQF@H CFDROB. 4EBOB >OB QTL PQO>QBDFBP FK "LU2$ CLO J>HFKD CLOJP QE>Q >OB JLOB >AS>K@BA QE>K > ?>PF@ @FO@IB LO PNR>OB. /KB FP QL RPB > .IFSAIH0B;J? FK > AFCCBOBKQ T>V. )K LRO MOBSFLRP BU>JMIBP, TB RPBA .IFSAIH0B;J? QL DBKBO>QB > OB@Q>KDRI>O PE>MB TFQE QEB M?NAM IR() CRK@QFLK.
.IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); JM.M?NAM IR(<IR2>4, <IR2>&);

F64B?2 5.5

4EFP T>P > DLLA T>V QL PQ>OQ ?B@>RPB LC QEB FKEBOBKQ PFJMIF@FQV LC TLOHFKD TFQE OB@Q>KDIBP. (LTBSBO, > .IFSAIH0B;J? L?GB@Q @>K >IPL ?B DBKBO>QBA COLJ >K >OO>V LC SB@QLOP, TEF@E >IILTP VLR QL ?RFIA > @LJMIBQBIV @RPQLJ PE>MB >P > PBOFBP LC @LKKB@QBA SBOQF@BP. 4EFP TLOHP SBOV PFJFI>OIV QL QEB !B;CH0B;J? @I>PP.

Example 5.4: Polygon shapes


3?=289 P?LNC=?M = H?Q 3?=2849; // AH ;LL;S P?LNC=?M809 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q P?LNC=?M819 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q P?LNC=?M829 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q P?LNC=?M839 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); JM.M?N(P?LNC=?M, P?LNC=?M.F?HANB); I@ 4 P?=NILM 3?=2(-15, 25)); 3?=2(15, 0)); 3?=2(20, -15)); 3?=2(-10, -10)); Making a polygon from that array

215

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

7EBK ?RFIAFKD VLRO LTK MLIVDLK FK "LU2$, VLR JRPQ OBJBJ?BO QTL FJMLOQ>KQ ABQ>FIP.

F64B?2 5.6

1. Order of vertices! )C VLR >OB QEFKHFKD FK QBOJP LC MFUBIP (>P >?LSB) QEB SBOQF@BP PELRIA ?B ABCFKBA FK @LRKQBO@IL@HTFPB LOABO. (7EBK QEBV >OB QO>KPI>QBA QL "LU2$ 7LOIA SB@QLOP, QEBV TFII >@QR>IIV ?B FK @IL@HTFPB LOABO PFK@B QEB SBOQF@>I >UFP FP CIFMMBA.) 2. Convex shapes only! ! @LK@>SB PE>MB FP LKB TEBOB QEB PROC>@B @ROSBP FKT>OA. #LKSBU FP QEB LMMLPFQB (PBB FIIRPQO>QFLK ?BILT). .LQB ELT FK > @LK@>SB PE>MB BSBOV FKQBOK>I >KDIB JRPQ ?B 180 ABDOBBP LO IBPP. "LU2$ FP KLQ @>M>?IB LC E>KAIFKD @LIIFPFLKP CLO @LK@>SB PE>MBP. )C VLR KBBA > @LK@>SB PE>MB, VLR TFII E>SB QL ?RFIA LKB LRQ LC JRIQFMIB @LKSBU PE>MBP (JLOB >?LRQ QE>Q FK > JLJBKQ).

216

4EB .>QROB LC #LAB (S1.0)

F64B?2 5.7: A 0<;0.C2 @5.=2 0.; /2 1?.D; D6A5 :B9A6=92 0<;C2E @5.=2@.

.LT, TEBK FQ @LJBP QFJB QL AFPMI>V QEB PE>MB FK 0OL@BPPFKD, TB @>K KL ILKDBO GRPQ RPB L?=N() LO ?FFCJM?() . 3FK@B QEB PE>MB FP ?RFIQ LRQ LC @RPQLJ SBOQF@BP, TBgII T>KQ QL RPB 0OL@BPPFKDgP <?ACH0B;J?() , ?H>0B;J?() , >KA P?LN?R() CRK@QFLKP. !P TB P>T TFQE QEB !B;CH0B;J? , TB @LRIA @ELLPB QL PQLOB QEB MFUBI IL@>QFLKP LC QEB SBOQF@BP FK LRO LTK ALL;S*CMN CLO AO>TFKD. (LTBSBO, FQgP >IPL RPBCRI QL PBB ELT TB @>K >PH "LU2$ QL OBMLOQ ?>@H QL RPB QEB SBOQBU IL@>QFLKP.
PIC> >CMJF;S() U 3?=2 JIM = <IR2>.A?N I>S.CR?F!IIL>(<I>S); @FI;N ; = <I>S.A?NAHAF?(); $CRNOL? @ = <I>S.A?N$CRNOL?*CMN(); First we get the Fixture attached to the body... ...then the Shape attached to the Fixture.

.IFSAIH0B;J? JM = (.IFSAIH0B;J?) @.A?N0B;J?(); L?=N+I>?(!#,1#/); JOMB+;NLCR(); NL;HMF;N?(JIM.R,JIM.S); LIN;N?(-;); @CFF(175); MNLIE?(0); <?ACH0B;J?(); @IL (CHN C = 0; C < JM.A?N3?LN?R!IOHN(); C++) U

We can loop through that array and convert each vertex from Box2D space to pixels.

3?=2 P = <IR2>.P?=NIL4ILF>1I.CR?FM(JM.A?N3?LN?R(C)); P?LN?R(P.R,P.S); W ?H>0B;J?(!*-0#); JIJ+;NLCR(); W

217

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Exercise 5.4
5PFKD QEB .IFSAIH0B;J? @I>PP, @OB>QB VLRO LTK MLIVDLK ABPFDK (OBJBJ?BO, FQ JRPQ ?B @LK@>SB). 3LJB MLPPF?FIFQFBP ?BILT.

! MLIVDLK PE>MB TFII DBQ RP MOBQQV C>O FK "LU2$. .BSBOQEBIBPP, QEB @LKSBU PE>MB OBNRFOBJBKQ TFII PBSBOBIV IFJFQ QEB O>KDB LC MLPPF?FIFQFBP. 4EB DLLA KBTP FP QE>Q TB @>K @LJMIBQBIV BIFJFK>QB QEFP OBPQOF@QFLK ?V @OB>QFKD > PFKDIB "LU2$ ?LAV LRQ LC JRIQFMIB PE>MBP! ,BQgP OBQROK QL LRO IFQQIB >IFBK @OB>QROB >KA PFJMIFCV QEB PE>MB QL ?B > QEFK OB@Q>KDIB TFQE > @FO@IB LK QLM. (LT @>K TB ?RFIA > PFKDIB ?LAV TFQE QTL PE>MBP? ,BQgP CFOPQ OBSFBT ELT TB ?RFIQ > PFKDIB ?LAV TFQE LKB PE>MB. .H9D 1: 9:=B9 H<9 6C8M. .H9D 2: CF95H9 H<9 6C8M. '?0; 3: D01490 ?30 >3,;0. '?0; 4: A??,.3 ?30 >3,;0 ?: ?30 -:/D. .H9D 5: "=B5@=N9 H<9 6C8MSG A5GG. !QQ>@EFKD JLOB QE>K LKB PE>MB QL > ?LAV FP >P PFJMIB >P OBMB>QFKD PQBMP 3 >KA 4 LSBO >KA LSBO >D>FK. '?0; 3,: D01490 >3,;0 1. '?0; 4,: A??,.3 >3,;0 1 ?: ?30 -:/D. '?0; 3-: D01490 >3,;0 2. '?0; 4-: A??,.3 >3,;0 2 ?: ?30 -:/D. BQ@. BQ@. BQ@. ,BQgP PBB TE>Q QEFP TLRIA ILLH IFHB TFQE >@QR>I "LU2$ @LAB.
I>S"?@ <> = H?Q I>S"?@(); <>.NSJ? = I>S1SJ?."6,A+'!; <>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(=?HN?L)); <I>S = <IR2>.=L?;N? I>S(<>); Making the body

218

4EB .>QROB LC #LAB (S1.0)

.IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?(); @FI;N <IR2>4 = <IR2>.M=;F;L.CR?FM1I4ILF>(Q/2); @FI;N <IR2>& = <IR2>.M=;F;L.CR?FM1I4ILF>(B/2); M>.M?NAM IR(<IR2>4, <IR2>&);

Making shape 1 (the rectangle)

!CL=F?0B;J? =M = H?Q !CL=F?0B;J?(); =M.G:L;>COM = <IR2>.M=;F;L.CR?FM1I4ILF>(L);

Making shape 2 (the circle)

<I>S.=L?;N?$CRNOL?(JM,1.0); <I>S.=L?;N?$CRNOL?(=M, 1.0);

Attach both shapes with a fixture.

4EB >?LSB ILLHP MOBQQV DLLA, ?RQ P>AIV, FC TB ORK FQ, TBgII DBQ QEB CLIILTFKD OBPRIQ:

F64B?2 5.8

7EBK VLR >QQ>@E > PE>MB QL > ?LAV, ?V ABC>RIQ, QEB @BKQBO LC QEB PE>MB TFII ?B IL@>QBA >Q QEB @BKQBO LC QEB ?LAV. "RQ FK LRO @>PB, FC TB Q>HB QEB @BKQBO LC QEB OB@Q>KDIB QL ?B QEB @BKQBO LC QEB ?LAV, TB T>KQ QEB @BKQBO LC QEB @FO@IB QL ?B LCCPBQ >ILKD QEB V->UFP COLJ QEB ?LAVgP @BKQBO.

219

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

F64B?2 5.9

4EFP FP >@EFBSBA ?V RPFKD QEB IL@>I MLPFQFLK LC > PE>MB, >@@BPPBA SF> > 3?=2 S>OF>?IB @>IIBA G:J .
3?=2 I@@M?N = H?Q 3?=2(0,-B/2); I@@M?N = <IR2>.P?=NIL.CR?FM1I4ILF>(I@@M?N); =CL=F?.G:J.M?N(I@@M?N.R,I@@M?N.S); Our offset in pixels Converting the vector to Box2D world Setting the local position of the circle

4EBK, TEBK TB DL QL AO>T QEB ?LAV, TB RPB ?LQE L?=N() >KA ?FFCJM?() TFQE QEB @FO@IB LCCPBQ QEB P>JB T>V.

220

4EB .>QROB LC #LAB (S1.0)

Example 5.5: Multiple shapes on one body


PIC> >CMJF;S() U 3?=2 JIM = <IR2>.A?N I>S.CR?F!IIL>(<I>S); @FI;N ; = <I>S.A?NAHAF?(); L?=N+I>?(!#,1#/); JOMB+;NLCR(); NL;HMF;N?(JIM.R,JIM.S); LIN;N?(-;); @CFF(175); MNLIE?(0); L?=N(0,0,Q,B); ?FFCJM?(0,-B/2,L*2,L*2); JIJ+;NLCR(); W First the rectangle at (0,0) Then the ellipse offset at (0,-h/2)

&FKFPEFKD LCC QEFP PB@QFLK, ) T>KQ QL PQOBPP QEB CLIILTFKD: QEB PQRCC VLR AO>T FK VLRO 0OL@BPPFKD TFKALT ALBPKgQ J>DF@>IIV BUMBOFBK@B MEVPF@P PFJMIV ?B@>RPB TB @OB>QBA PLJB "LU2$ ?LAFBP >KA PE>MBP. 4EBPB BU>JMIBP TLOH ?B@>RPB TB SBOV @>OBCRIIV J>Q@EBA ELT TB AO>T LRO BIBJBKQP TFQE ELT TB ABCFKBA QEB ?LAFBP >KA PE>MBP TB MRQ FKQL QEB "LU2$ TLOIA. )C VLR >@@FABKQ>IIV AO>T VLRO PE>MB AFCCBOBKQIV, VLR TLKgQ DBQ >K BOOLO, KLQ COLJ 0OL@BPPFKD LO COLJ "LU2$. (LTBSBO, VLRO PHBQ@E TFII ILLH LAA >KA QEB MEVPF@P TLKgQ TLOH @LOOB@QIV. &LO BU>JMIB, TE>Q FC TB E>A TOFQQBK:
3?=2 I@@M?N = H?Q 3?=2(0,-B/2);

TEBK TB @OB>QBA QEB PE>MB, ?RQ:


?FFCJM?(0,B/2,L*2,L*2);

TEBK FQ @>JB QFJB QL AFPMI>V QEB PE>MB?

221

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

4EB OBPRIQP TLRIA ILLH IFHB QEB FJ>DB >?LSB, TEBOB @IB>OIV, QEB @LIIFPFLKP >OB KLQ CRK@QFLKFKD >P BUMB@QBA. 4EFP FP KLQ ?B@>RPB QEB MEVPF@P FP ?OLHBK; FQgP ?B@>RPB TB AFA KLQ @LJJRKF@>QB MOLMBOIV TFQE "LU2$, BFQEBO TEBK TB MRQ PQRCC FK QEB J>DF@ TLOIA LO NRBOFBA QEB TLOIA CLO IL@>QFLKP.

Exercise 5.5
->HB VLRO LTK IFQQIB >IFBK ?BFKD RPFKD JRIQFMIB PE>MBP >QQ>@EBA QL > PFKDIB ?LAV. 4OV RPFKD JLOB QE>K LKB MLIVDLK QL J>HB > @LK@>SB PE>MB. 2BJBJ?BO, VLR >OBKgQ IFJFQBA QL RPFKD QEB PE>MB AO>TFKD CRK@QFLKP FK 0OL@BPPFKD; VLR @>K RPB FJ>DBP, @LILOP, >AA E>FO TFQE IFKBP, BQ@. 4EFKH LC QEB "LU2$ PE>MBP LKIV >P PHBIBQLKP CLO VLRO @OB>QFSB >KA C>KQ>PQF@>I ABPFDK!

5.11 Feeling AttachedBox2D Joints


"LU2$ GLFKQP >IILT VLR QL @LKKB@Q LKB ?LAV QL >KLQEBO, BK>?IFKD JLOB >AS>K@BA PFJRI>QFLKP LC PTFKDFKD MBKARIRJP, BI>PQF@ ?OFADBP, PNRFPEV @E>O>@QBOP, TEBBIP PMFKKFKD LK >K >UIB, BQ@. 4EBOB >OB J>KV AFCCBOBKQ HFKAP LC "LU2$ GLFKQP. )K QEFP @E>MQBO TBgOB DLFKD QL ILLH >Q QEOBB: AFPQ>K@B GLFKQP, OBSLIRQB GLFKQP, >KA hJLRPBi GLFKQP. ,BQgP ?BDFK TFQE > AFPQ>K@B GLFKQ, > GLFKQ QE>Q @LKKB@QP QTL ?LAFBP TFQE > CFUBA IBKDQE. 4EB GLFKQ FP >QQ>@EBA QL B>@E ?LAV >Q > PMB@FCFBA >K@ELO MLFKQ (> MLFKQ OBI>QFSB QL QEB ?LAVgP @BKQBO). &LO >KV "LU2$ GLFKQ, TB KBBA QL CLIILT QEBPB PQBMP. 4EFP, LC @LROPB, FP PFJFI>O QL QEB JBQELALILDV TB RPBA QL ?RFIA ?LAFBP >KA PE>MBP, TFQE PLJB NRFOHP.

F64B?2 5.10

'?0; 1. ",60 >@=0 D:@ 3,A0 ?B: -:/40> =0,/D ?: 2:. '?0; 2. D01490 ?30 5:49?. '?0; 3. C:9142@=0 ?30 5:49?H> ;=:;0=?40> (+3,? ,=0 ?30 -:/40>? +30=0 ,=0 ?30 ,9.3:=>? +3,? 4> 4?> =0>? 7092?3? I> 4? 07,>?4. := =424/?) '?0; 4. C=0,?0 ?30 5:49?. ,BQgP >PPRJB TB E>SB QTL .;LNC=F? L?GB@QP QE>Q B>@E PQLOB > OBCBOBK@B QL > "LU2$ I>S L?GB@Q. 7BgII @>II QEBJ M>OQF@IBP J1 >KA J2 .

222

4EB .>QROB LC #LAB (S1.0)


.;LNC=F? J1 = H?Q .;LNC=F?(); .;LNC=F? J2 = H?Q .;LNC=F?();

/+, LKQL 3QBM 2. ,BQgP ABCFKB QEB GLFKQ.


"CMN;H=?(ICHN"?@ >D> = H?Q "CMN;H=?(ICHN"?@();

%>PV, OFDEQ? .LT FQgP QFJB QL @LKCFDROB QEB GLFKQ. &FOPQ TB QBII QEB GLFKQ TEF@E QTL ?LAFBP FQ @LKKB@QP:
>D>.<I>SA = J1.<I>S; >D>.<I>S = J2.<I>S;

4EBK TB PBQ RM > OBPQ IBKDQE. 2BJBJ?BO, FC LRO OBPQ IBKDQE FP FK MFUBIP, TB KBBA QL @LKSBOQ FQ!
>D>.F?HANB = <IR2>.M=;F;L.CR?FM1I4ILF>(10);

! AFPQ>K@B GLFKQ >IPL FK@IRABP QTL LMQFLK>I PBQQFKDP QE>Q @>K J>HB QEB GLFKQ PLCQ, IFHB > PMOFKD @LKKB@QFLK: @L?KO?H=S&T >KA >;GJCHA/;NCI .
>D>.@L?KO?H=S&T = :::; Measured in Hz, like the frequency of harmonic oscillation; try values between 1 and 5. Dampens the spring; typically a number between 0 and 1.

>D>.>;GJCHA/;NCI = :::;

&FK>IIV, TB @OB>QB QEB GLFKQ.


"CMN;H=?(ICHN >D = ("CMN;H=?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(>D>);

"LU2$ TLKgQ HBBM QO>@H LC TE>Q HFKA LC GLFKQ TB >OB J>HFKD, PL TB E>SB QL @>PQ FQ >P > "CMN;H=?(ICHN RMLK @OB>QFLK. 7B @>K @OB>QB "LU2$ GLFKQP >KVTEBOB FK LRO 0OL@BPPFKD PHBQ@E. (BOBgP >K BU>JMIB LC ELT TB JFDEQ TOFQB > @I>PP QL ABP@OF?B QTL "LU2$ ?LAFBP @LKKB@QBA TFQE > PFKDIB GLFKQ.

223

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Example 5.6: DistanceJoint


=F;MM .;CL U .;LNC=F? J1; .;LNC=F? J2; @FI;N F?H = 32; .;CL(@FI;N R, @FI;N S) U Problems can result if the bodies are J1 = H?Q .;LNC=F?(R,S); initialized at the same location. J2 = H?Q .;LNC=F?(R+L;H>IG(-1,1),S+L;H>IG(-1,1)); Two objects that each have a Box2D body

Arbitrary rest length

"CMN;H=?(ICHN"?@ >D> = H?Q "CMN;H=?(ICHN"?@(); >D>.<I>SA = J1.<I>S; >D>.<I>S = J2.<I>S; >D>.F?HANB = <IR2>.M=;F;L.CR?FM1I4ILF>(F?H); >D>.@L?KO?H=S&T = 0; // 1LS ; P;FO? F?MM NB;H 5 >D>.>;GJCHA/;NCI = 0; // /;HA?M <?NQ??H 0 ;H> 1

Making the joint!

"CMN;H=?(ICHN >D = ("CMN;H=?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(>D>); W PIC> >CMJF;S() U 3?=2 JIM1 = <IR2>.A?N I>S.CR?F!IIL>(J1.<I>S); 3?=2 JIM2 = <IR2>.A?N I>S.CR?F!IIL>(J2.<I>S); MNLIE?(0); FCH?(JIM1.R,JIM1.S,JIM2.R,JIM2.S); J1.>CMJF;S(); J2.>CMJF;S(); W W Make the joint. Note that we aren't storing a reference to the joint anywhere! We might need to someday, but for now it's OK.

224

4EB .>QROB LC #LAB (S1.0)

Exercise 5.6
#OB>QB > PFJRI>QFLK LC > ?OFADB ?V RPFKD AFPQ>K@B GLFKQP QL @LKKB@Q > PBNRBK@B LC @FO@IBP (LO OB@Q>KDIBP) >P FIIRPQO>QBA QL QEB OFDEQ. !PPFDK > ABKPFQV LC WBOL QL IL@H QEB BKAMLFKQP FK MI>@B. %UMBOFJBKQ TFQE AFCCBOBKQ S>IRBP QL J>HB QEB ?OFADB JLOB LO IBPP hPMOFKDV.i )Q PELRIA >IPL ?B KLQBA QE>Q QEB GLFKQP QEBJPBISBP E>SB KL MEVPF@>I DBLJBQOV, PL FK LOABO CLO VLRO ?OFADB KLQ QL E>SB ELIBP, PM>@FKD ?BQTBBK QEB KLABP TFII ?B FJMLOQ>KQ.

!KLQEBO GLFKQ VLR @>K @OB>QB FK "LU2$ FP > F9JC@IH9 >C=BH. ! OBSLIRQB GLFKQ @LKKB@QP QTL "LU2$ ?LAFBP >Q > @LJJLK >K@ELO MLFKQ, TEF@E @>K >IPL ?B OBCBOOBA QL >P > hEFKDB.i 4EB GLFKQ E>P >K h>KDIBi QE>Q ABP@OF?BP QEB OBI>QFSB OLQ>QFLK LC B>@E ?LAV. 4L RPB > OBSLIRQB GLFKQ, TB CLIILT QEB P>JB PQBMP TB AFA TFQE QEB AFPQ>K@B GLFKQ.

F64B?2 5.11

Step 1: Make sure you have two bodies ready to go.


,BQgP >PPRJB TB E>SB QTL IR L?GB@QP, B>@E LC TEF@E PQLOBP > OBCBOBK@B QL > "LU2$ ?LAV.
IR <IR1 = H?Q IR <IR2 = H?Q IR(); IR();

225

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Step 2: Define the joint.


.LT TB T>KQ > /?PIFON?(ICHN"?@ L?GB@Q.
/?PIFON?(ICHN"?@ LD> = H?Q /?PIFON?(ICHN"?@();

Step 3: Configure the joints properties.


4EB JLPQ FJMLOQ>KQ MOLMBOQFBP LC > OBSLIRQB GLFKQ >OB QEB QTL ?LAFBP FQ @LKKB@QP >P TBII >P QEBFO JRQR>I >K@ELO MLFKQ (F.B. TEBOB QEBV >OB @LKKB@QBA). 4EBV >OB PBQ TFQE QEB CRK@QFLK CHCNC;FCT?() .
LD>.CHCNC;FCT?(<IR1.<I>S, <IR2.<I>S, <IR1.<I>S.A?N4ILF>!?HN?L());

.LQF@B ELT QEB CFOPQ QTL >ODRJBKQP PMB@FCV QEB ?LAFBP >KA QEB PB@LKA MLFKQ PMB@FCFBP QEB >K@ELO, TEF@E FK QEFP @>PB FP IL@>QBA >Q QEB @BKQBO LC QEB CFOPQ ?LAV. !K BU@FQFKD CB>QROB LC > /?PIFON?(ICHN L?GB@Q FP QE>Q VLR @>K JLQLOFWB FQ PL FQ PMFKP >RQLKLJLRPIV. &LO BU>JMIB:
LD>.?H;<F?+INIL = NLO?; LD>.GINIL0J??> = .'*2; LD>.G;R+INIL1ILKO? = 1000.0; Turn on the motor. How fast is the motor? How powerful is the motor?

4EB JLQLO @>K ?B BK>?IBA >KA AFP>?IBA TEFIB QEB MOLDO>J FP ORKKFKD. &FK>IIV, QEB >?FIFQV CLO > OBSLIRQB GLFKQ QL PMFK @>K ?B @LKPQO>FKBA ?BQTBBK QTL >KDIBP. ("V ABC>RIQ, FQ @>K OLQ>QB > CRII 360 ABDOBBP, LO 14-:.' O>AF>KP.)
LD>.?H;<F?*CGCN = NLO?; LD>.FIQ?LAHAF? = -.'/8; LD>.OJJ?LAHAF? = .'/8;

Step 4: Create the joint.


/?PIFON?(ICHN DICHN = (/?PIFON?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(LD>);

,BQgP Q>HB > ILLH >Q >II LC QEBPB PQBMP QLDBQEBO FK > @I>PP @>IIBA 4CH>GCFF , TEF@E @LKKB@QP QTL ?LUBP TFQE > OBSLIRQB GLFKQ. )K QEFP @>PB, <IR1 E>P > ABKPFQV LC WBOL, PL LKIV <IR2 PMFKP >OLRKA > CFUBA MLFKQ.

226

4EB .>QROB LC #LAB (S1.0)

Example 5.7: Spinning Windmill


=F;MM 4CH>GCFF U /?PIFON?(ICHN DICHN; IR <IR1; IR <IR2; 4CH>GCFF(@FI;N R, @FI;N S) U <IR1 = H?Q <IR2 = H?Q IR(R,S,120,10,@;FM?); IR(R,S,10,40,NLO?); In this example, the Box class expects a boolean argument that will be used to determine if the Box is fixed or not. See website for the Box class code. Our Windmill is two boxes and one joint.

/?PIFON?(ICHN"?@ LD> = H?Q /?PIFON?(ICHN"?@(); LD>.CHCNC;FCT?(<IR1.<I>S, <IR2.<I>S, <IR1.<I>S.A?N4ILF>!?HN?L()); The joint connects two bodies and is anchored at the center of the first body. LD>.GINIL0J??> = .'*2; LD>.G;R+INIL1ILKO? = 1000.0; LD>.?H;<F?+INIL = NLO?; DICHN = (/?PIFON?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(LD>); W PIC> NIAAF?+INIL() U <IIF?;H GINILMN;NOM = DICHN.CM+INIL#H;<F?>(); DICHN.?H;<F?+INIL(!GINILMN;NOM); W Create the Joint. Turning the motor on or off A motor!

227

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

PIC> >CMJF;S() U <IR1.>CMJF;S(); <IR2.>CMJF;S(); W W

Exercise 5.7
5PB > OBSLIRQB GLFKQ CLO QEB TEBBIP LC > @>O. 5PB JLQLOP PL QE>Q QEB @>O AOFSBP >RQLKLJLRPIV. 4OV RPFKD > @E>FK PE>MB CLO QEB OL>AgP PROC>@B.

4EB I>PQ GLFKQ TBgII ILLH >Q FP > JLRPB GLFKQ. ! JLRPB GLFKQ FP QVMF@>IIV RPBA CLO JLSFKD > ?LAV TFQE QEB JLRPB. (LTBSBO, FQ @>K >IPL ?B RPBA QL AO>D >K L?GB@Q >OLRKA QEB P@OBBK >@@LOAFKD QL PLJB >O?FQO>OV L >KA M . 4EB GLFKQ CRK@QFLKP ?V MRIIFKD QEB ?LAV QLT>OAP > hQ>ODBQi MLPFQFLK. "BCLOB TB ILLH >Q QEB +IOM?(ICHN L?GB@Q FQPBIC, IBQgP >PH LROPBISBP TEV TB BSBK KBBA FQ FK QEB CFOPQ MI>@B. )C VLR ILLH >Q QEB "LU2$ AL@RJBKQ>QFLK, QEBOB FP > CRK@QFLK @>IIBA M?N1L;HM@ILG() QE>Q PMB@FCF@>IIV hPBQP QEB MLPFQFLK LC QEB ?LAVgP LOFDFK >KA OLQ>QFLK (O>AF>KP).i )C > ?LAV E>P > MLPFQFLK, @>KgQ TB GRPQ >PPFDK QEB ?LAVgP MLPFQFLK QL QEB JLRPB?
3?=2 GIOM? = <IR2>.M=L??H1I4ILF>(R,S); <I>S.M?N1L;HM@ILG(GIOM?,0);

7EFIB QEFP TFII FK C>@Q JLSB QEB ?LAV, FQ TFII >IPL E>SB QEB RKCLOQRK>QB OBPRIQ LC ?OB>HFKD QEB MEVPF@P. ,BQgP FJ>DFKB VLR ?RFIQ > QBIBMLOQ>QFLK J>@EFKB QE>Q >IILTP VLR QL QBIBMLOQ COLJ VLRO ?BAOLLJ QL VLRO HFQ@EBK (DLLA CLO I>QB-KFDEQ PK>@HFKD). .LT, DL >EB>A >KA OBTOFQB .BTQLKgP I>TP LC JLQFLK QL >@@LRKQ CLO QEB MLPPF?FIFQV LC QBIBMLOQ>QFLK. .LQ PL B>PV, OFDEQ? "LU2$ E>P QEB P>JB MOL?IBJ. )C VLR J>KR>IIV >PPFDK QEB IL@>QFLK LC >K ?LAV, FQgP IFHB P>VFKD hQBIBMLOQ QE>Q ?LAVi >KA "LU2$ KL ILKDBO HKLTP ELT QL @LJMRQB QEB MEVPF@P MOLMBOIV.

228

4EB .>QROB LC #LAB (S1.0)

(LTBSBO, "LU2$ ALBP >IILT VLR QL QFB > OLMB QL VLROPBIC >KA DBQ > COFBKA LC VLROP QL PQ>KA FK QEB HFQ@EBK >KA AO>D VLR QEBOB. 4EFP FP TE>Q QEB +IOM?(ICHN ALBP. )QgP IFHB > PQOFKD VLR >QQ>@E QL > ?LAV >KA MRII QLT>OAP > Q>ODBQ. ,BQgP ILLH >Q J>HFKD QEFP GLFKQ, >PPRJFKD TB E>SB > IR L?GB@Q @>IIBA <IR . 4EFP @LAB TFII ILLH FABKQF@>I QL LRO AFPQ>K@B GLFKQ TFQE LKB PJ>II AFCCBOBK@B.
+IOM?(ICHN"?@ G> = H?Q +IOM?(ICHN"?@(); Just like before, define the Joint.

C:.8E:OA = 8EN2:.=;J#HEKD:BE:O(); G>.<I>S = <IR.<I>S;

Whoa, this is new! Attach the Box body.

G>.G;R$IL=? = 5000.0; G>.@L?KO?H=S&T = 5.0; G>.>;GJCHA/;NCI = 0.9; +IOM?(ICHN GIOM?(ICHN = (+IOM?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(G>);.

Set properties.

Create the joint.

3L, TE>QgP QEFP IFKB LC @LAB >II >?LRQ?


C:.8E:OA = 8EN2:.=;J#HEKD:BE:O();

7BII, >P TBgSB PQ>QBA, > GLFKQ FP > @LKKB@QFLK ?BQTBBK HKC ?LAFBP. 7FQE > JLRPB GLFKQ, TBgOB P>VFKD QE>Q QEB PB@LKA ?LAV FP, TBII, QEB DOLRKA. (JJ. 7E>Q QEB EB@H FP QEB ;FCIB8 FK "LU2$? /KB T>V QL FJ>DFKB FQ FP QL QEFKH LC QEB P@OBBK >P QEB DOLRKA. 7E>Q TBgOB ALFKD FP J>HFKD > GLFKQ QE>Q @LKKB@QP > OB@Q>KDIB AO>TK LK QEB TFKALT TFQE QEB 0OL@BPPFKD TFKALT FQPBIC. !KA QEB MLFKQ FK QEB TFKALT QL TEF@E QEB @LKKB@QFLK FP QFBA FP > JLSFKD Q>ODBQ. /K@B TB E>SB > JLRPB GLFKQ, TBgII T>KQ QL RMA>QB QEB Q>ODBQ IL@>QFLK @LKQFKR>IIV TEFIB QEB PHBQ@E FP ORKKFKD.
3?=2 GIOM?4ILF> = <IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6); GIOM?(ICHN.M?N1;LA?N(GIOM?4ILF>);

4L J>HB QEFP TLOH FK >K >@QR>I 0OL@BPPFKD PHBQ@E, TBgII T>KQ QL E>SB QEB CLIILTFKD: 1. B:C .7,>> e!K L?GB@Q QE>Q OBCBOBK@BP > "LU2$ ?LAV. 2. ';=492 .7,>> e!K L?GB@Q QE>Q J>K>DBP QEB JLRPB GLFKQ QE>Q AO>DP QEB IR L?GB@Q >OLRKA.

229

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

3. ",49 ?,-e7EBKBSBO GIOM?.L?MM?>() FP @>IIBA, QEB JLRPB GLFKQ FP @OB>QBA; TEBKBSBO GIOM?/?F?;M?>() FP @>IIBA, QEB JLRPB GLFKQ FP ABPQOLVBA. 4EFP >IILTP RP QL FKQBO>@Q TFQE > ?LAV LKIV TEBK QEB JLRPB FP MOBPPBA. ,BQgP Q>HB > ILLH >Q QEB J>FK Q>?. 9LR @>K CFKA QEB OBPQ LC QEB @LAB CLO QEB IR >KA 0JLCHA @I>PPBP SF> QEB ?LLH TB?PFQB.

Example 5.8: MouseJoint demonstration


. IR2" <IR2>; IR <IR; 0JLCHA MJLCHA; PIC> M?NOJ() U MCT?(400,300); <IR2> = H?Q . IR2"(NBCM); <IR2>.=L?;N?4ILF>(); <IR = H?Q IR(QC>NB/2,B?CABN/2); The MouseJoint is really null until we click the mouse. One Box Object to manage MouseJoint

MJLCHA = H?Q 0JLCHA(); W PIC> GIOM?.L?MM?>() U C@ (<IR.=IHN;CHM(GIOM?5, GIOM?6)) U MJLCHA.<CH>(GIOM?5,GIOM?6,<IR); W W PIC> GIOM?/?F?;M?>() U

Was the mouse clicked inside the Box? If so, attach the MouseJoint.

230

4EB .>QROB LC #LAB (S1.0)

MJLCHA.>?MNLIS(); W PIC> >L;Q() U <;=EALIOH>(255); <IR2>.MN?J(); MJLCHA.OJ>;N?(GIOM?5,GIOM?6); <IR.>CMJF;S(); MJLCHA.>CMJF;S(); W

When the mouse is released, were done with the MouseJoint.

We must always update the MouseJoints target.

Exercise 5.8
5PB > JLRPB GLFKQ QL JLSB > "LU2$ ?LAV >OLRKA QEB P@OBBK >@@LOAFKD QL >K >IDLOFQEJ LO FKMRQ LQEBO QE>K QEB JLRPB. &LO BU>JMIB, >PPFDK FQ > IL@>QFLK >@@LOAFKD QL 0BOIFK KLFPB LO HBV MOBPPBP. /O ?RFIA VLRO LTK @LKQOLIIBO RPFKD >K !OARFKL (EQQM://TTT.>OARFKL.@@/). )QgP TLOQE KLQFKD QE>Q TEFIB QEB QB@EKFNRB CLO AO>DDFKD >K L?GB@Q >OLRKA RPFKD > +IOM?(ICHN FP RPBCRI, "LU2$ >IPL >IILTP > ?LAV QL E>SB > )',#+A1'! QVMB.
I>S"?@ <> = H?Q <>.NSJ? = I>S"?@(); Setting the body type to Kinematic

I>S1SJ?.)',#+A1'!;

+FKBJ>QF@ ?LAFBP @>K ?B @LKQOLIIBA ?V QEB RPBO ?V PBQQFKD QEBFO SBIL@FQV AFOB@QIV. &LO BU>JMIB, IBQgP P>V VLR T>KQ >K L?GB@Q QL CLIILT > Q>ODBQ (IFHB VLRO JLRPB). 9LR @LRIA @OB>QB > SB@QLO QE>Q MLFKQP COLJ > ?LAVgP IL@>QFLK QL > Q>ODBQ.

F64B?2 5.12

3?=2 JIM = <I>S.A?N4ILF>!?HN?L(); 3?=2 N;LA?N = <IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6); 3?=2 P = N;LA?N.MO<(JIM); A vector pointing from the body position to the Mouse

231

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

/K@B VLR E>SB QE>Q SB@QLO, VLR @LRIA >PPFDK FQ QL QEB ?LAVgP SBIL@FQV PL QE>Q FQ JLSBP QL QEB Q>ODBQ.
<I>S.M?N*CH?;L3?FI=CNS(P); Assigning a bodys velocity directly, overriding physics!

9LR @>K >IPL AL QEB P>JB TFQE >KDRI>O SBIL@FQV (LO IB>SB FQ >ILKB >KA >IILT QEB MEVPF@P QL Q>HB LSBO). )Q FP FJMLOQ>KQ QL KLQB QE>Q HFKBJ>QF@ ?LAFBP AL KLQ @LIIFAB TFQE LQEBO HFKBJ>QF@ LO PQ>QF@ ?LAFBP. )K QEBPB @>PBP, QEB JLRPB GLFKQ PQO>QBDV FP MOBCBO>?IB.

Exercise 5.9
2BAL %UBO@FPB 5.8, ?RQ RPB > HFKBJ>QF@ ?LAV FKPQB>A.

5.12 Bringing It All Back Home to Forces


)K #E>MQBO 2, TB PMBKQ > ILQ LC QFJB QEFKHFKD >?LRQ ?RFIAFKD BKSFOLKJBKQP TFQE JRIQFMIB CLO@BP. !K L?GB@Q JFDEQ OBPMLKA QL DO>SFQ>QFLK>I >QQO>@QFLK, TFKA, >FO OBPFPQ>K@B, BQ@. #IB>OIV QEBOB >OB CLO@BP >Q TLOH FK "LU2$ >P TB T>Q@E OB@Q>KDIBP >KA @FO@IBP PMFK >KA CIV >OLRKA QEB P@OBBK. "RQ PL C>O, TBgSB LKIV E>A QEB >?FIFQV QL J>KFMRI>QB > PFKDIB DIL?>I CLO@BeDO>SFQV.
<IR2> = H?Q . IR2"(NBCM); <IR2>.=L?;N?4ILF>(); <IR2>.M?N%L;PCNS(0, -20); Setting the global gravity force

)C TB T>KQ QL RPB >KV LC LRO #E>MQBO 2 QB@EKFNRBP TFQE "LU2$, TB KBBA ILLH KL CROQEBO QE>K LRO QORPQV ;JJFS$IL=?() CRK@QFLK. )K LRO +IP?L @I>PP TB TOLQB > CRK@QFLK @>IIBA ;JJFS$IL=?() , TEF@E OB@BFSBA > SB@QLO, AFSFABA FQ ?V J>PP, >KA >@@RJRI>QBA FQ FKQL QEB JLSBOgP >@@BIBO>QFLK. 7FQE "LU2$, QEB P>JB CRK@QFLK BUFPQP, ?RQ TB ALKgQ KBBA QL TOFQB FQ LROPBISBP. )KPQB>A, TB @>K @>II QEB "LU2$ ?LAVgP ;JJFS$IL=?() CRK@QFLK!
=F;MM IR U I>S <I>S; PIC> ;JJFS$IL=?(3?=2 @IL=?) U 3?=2 JIM = <I>S.A?N4ILF>!?HN?L(); <I>S.;JJFS$IL=?(@IL=?, JIM); W W Calling the Body's applyForce() function

232

4EB .>QROB LC #LAB (S1.0)

(BOB TB >OB OB@BFSFKD > CLO@B SB@QLO >KA M>PPFKD FQ >ILKD QL QEB "LU2$ I>S L?GB@Q. 4EB HBV AFCCBOBK@B FP QE>Q "LU2$ FP > JLOB PLMEFPQF@>QBA BKDFKB QE>K LRO BU>JMIBP COLJ #E>MQBO 2. /RO B>OIFBO CLO@BP BU>JMIBP >PPRJBA QE>Q QEB CLO@B T>P >IT>VP >MMIFBA >Q QEB JLSBOgP @BKQBO. (BOB TB DBQ QL PMB@FCV BU>@QIV TEBOB LK QEB ?LAV QEB CLO@B FP >MMIFBA. )K QEB >?LSB @LAB, TBgOB GRPQ >MMIVFKD FQ QL QEB @BKQBO ?V >PHFKD QEB ?LAV CLO FQP @BKQBO, ?RQ QEFP @LRIA ?B >AGRPQBA. ,BQgP P>V TB T>KQBA QL RPB > DO>SFQ>QFLK>I >QQO>@QFLK CLO@B. 2BJBJ?BO QEB @LAB TB TOLQB ?>@H FK #E>MQBO 2 FK LRO ANNL;=NIL @I>PP?
.3?=NIL ;NNL;=N(+IP?L G) U .3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH); @FI;N >CMN;H=? = @IL=?.G;A(); >CMN;H=? = =IHMNL;CH(>CMN;H=?,5.0,25.0); @IL=?.HILG;FCT?(); @FI;N MNL?HANB = (A * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?); @IL=?.GOFN(MNL?HANB); L?NOLH @IL=?; W

7B @>K OBTOFQB QEB BU>@Q P>JB CRK@QFLK RPFKD 3?=2 FKPQB>A >KA RPB FQ FK > "LU2$ BU>JMIB. .LQB ELT CLO LRO CLO@B @>I@RI>QFLK TB @>K PQ>V @LJMIBQBIV TFQEFK QEB "LU2$ @LLOAFK>QB PVPQBJ >KA KBSBO QEFKH >?LRQ MFUBIP.
3?=2 ;NNL;=N(+IP?L G) U 3?=2 JIM = <I>S.A?N4ILF>!?HN?L(); We have to ask Box2D for the locations first!

3?=2 GIP?L.IM = G.<I>S.A?N4ILF>!?HN?L(); 3?=2 @IL=? = JIM.MO<(GIP?L.IM); @FI;N >CMN;H=? = @IL=?.F?HANB(); >CMN;H=? = =IHMNL;CH(>CMN;H=?,1,5); @IL=?.HILG;FCT?(); @FI;N MNL?HANB = (% * 1 * G.<I>S.G:G;MM) / (>CMN;H=? * >CMN;H=?); @IL=?.GOF*I=;F(MNL?HANB); L?NOLH @IL=?; W Remember, its mulLocal() for Vec2.

233

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Exercise 5.10
4>HB >KV BU>JMIB VLR J>AB MOBSFLRPIV RPFKD > CLO@B @>I@RI>QFLK >KA ?OFKD QE>Q CLO@B @>I@RI>QFLK FKQL "LU2$.

5.13 Collision Events


.LT TBgSB PBBK > PROSBV LC TE>Q @>K ?B ALKB TFQE "LU2$. 3FK@B QEFP ?LLH FP KLQ @>IIBA h4EB .>QROB LC "LU2$,i FQgP KLQ JV FKQBKQFLK QL @LSBO BSBOV PFKDIB MLPPF?IB CB>QROB LC QEB "LU2$ BKDFKB. "RQ ELMBCRIIV ?V ILLHFKD >Q QEB ?>PF@P LC ?RFIAFKD ?LAFBP, PE>MBP, >KA GLFKQP, TEBK FQ @LJBP QFJB QL RPB >K >PMB@Q LC "LU2$ QE>Q TB E>SBKgQ @LSBOBA, QEB PHFIIP TBgSB D>FKBA EBOB TFII J>HB QE>Q MOL@BPP @LKPFABO>?IV IBPP M>FKCRI. 4EBOB FP LKB JLOB CB>QROB LC "LU2$, ELTBSBO, QE>Q ) AL QEFKH FP TLOQE @LSBOFKD. ,BQgP >PH > NRBPQFLK VLRgSB IFHBIV ?BBK TLKABOFKD >?LRQ: 2<5H =: % K5BH GCA9H<=B; HC <5DD9B K<9B HKC BCL2 6C8=9G 7C@@=89? % A95B, 8CBSH ;9H A9 KFCB;R%SA H<F=@@98 H<5H BCL2 =G <5B8@=B; 5@@ C: H<9 7C@@=G=CBG :CF A9. BIH =: =H H5?9G 75F9 C: 9J9FMH<=B; :CF A9, <CK 5A % GIDDCG98 HC ?BCK K<9B H<=B;G 5F9 <5DD9B=B;? 9LRO CFOPQ QELRDEQP TEBK @LKPFABOFKD >K BSBKQ AROFKD TEF@E QTL L?GB@QP @LIIFAB JFDEQ ?B >P CLIILTP: 7BII, FC ) HKLT >II QEB ?LAFBP FK QEB PVPQBJ, >KA ) HKLT TEBOB QEBV >OB >II IL@>QBA, QEBK ) @>K GRPQ PQ>OQ @LJM>OFKD QEB IL@>QFLKP, PBB TEF@E LKBP >OB FKQBOPB@QFKD, >KA ABQBOJFKB QE>Q QEBVgSB @LIIFABA. 4E>QgP > KF@B QELRDEQ, ?RQ EBIIL??!? 4EB TELIB MLFKQ LC RPFKD "LU2$ FP QE>Q "LU2$ TFII Q>HB @>OB LC QE>Q CLO RP. )C TB >OB DLFKD QL AL QEB DBLJBQOV QL QBPQ CLO FKQBOPB@QFLK LROPBISBP, QEBK >II TBgOB ALFKD FP OB-FJMIBJBKQFKD "LU2$. /C @LROPB, "LU2$ E>P QELRDEQ LC QEFP MOL?IBJ ?BCLOB. )QgP > MOBQQV @LJJLK LKB. !CQBO >II, FC VLR FKQBKA QL J>HB > ?>GFIIFLK ALII>OP PBIIFKD PLJB D>JB @>IIBA !KDOV "FOAP, VLR ?BQQBO TBII J>HB PLJBQEFKD E>MMBK TEBK >K FII-QBJMBOBA MFDBLK PJ>PEBP FKQL > @>OA?L>OA ?LU. "LU2$ >IBOQP VLR QL JLJBKQP LC @LIIFPFLK TFQE PLJBQEFKD @>IIBA >K hFKQBOC>@B.i )QgP TLOQE IB>OKFKD >?LRQ FKQBOC>@BP, >K >AS>K@BA CB>QROB LC L?GB@Q-LOFBKQBA MOLDO>JJFKD. 9LR @>K 234

4EB .>QROB LC #LAB (S1.0)

Q>HB > ILLH >Q QEB *>S> )KQBOC>@B 4RQLOF>I (EQQM://ALTKIL>A.LO>@IB.@LJ/G>S>PB/QRQLOF>I/G>S>/ @LK@BMQP/FKQBOC>@B.EQJI) >P TBII >P QEB *"LU2$ !IHN;=N*CMN?H?L @I>PP. () E>SB >IPL FK@IRABA >K BU>JMIB LK QEB TB?PFQB QE>Q ABJLKPQO>QBP RPFKD QEB FKQBOC>@B AFOB@QIV.) )C VLR >OB RPFKD 0"LU2$, >P TB >OB EBOB, VLR ALKgQ KBBA QL FJMIBJBKQ VLRO LTK FKQBOC>@B. $BQB@QFKD @LIIFPFLK BSBKQP FP ALKB QEOLRDE > @>II?>@H CRK@QFLK. -R@E IFHB GIOM?.L?MM?>() FP QOFDDBOBA TEBK QEB JLRPB FP MOBPPBA, <?ACH!IHN;=N() FP QOFDDBOBA TEBK QTL PE>MBP @LIIFAB.
PIC> GIOM?.L?MM?>() U JLCHNFH("1B? GIOM? Q;M JL?MM?>!"); W PIC> <?ACH!IHN;=N(!IHN;=N =J) U JLCHNFH("0IG?NBCHA =IFFC>?> CH NB? W IR2" 4ILF>!"); What our "beginContact" event looks like. The mousePressed event with which we are comfortable.

"BCLOB QEB >?LSB TFII TLOH, VLR JRPQ CFOPQ IBQ 0"LU2$ HKLT VLR FKQBKA QL IFPQBK CLO @LIIFPFLKP. (4EFP >IILTP QEB IF?O>OV QL OBAR@B LSBOEB>A ?V ABC>RIQ; FQ TLKgQ ?LQEBO IFPQBKFKD FC FQ ALBPKgQ E>SB QL.)
PIC> M?NOJ() U <IR2> = H?Q . IR2"(NBCM); <IR2>.=L?;N?4ILF>(); <IR2>.FCMN?H$IL!IFFCMCIHM(); W Add this line if you want to listen for collisions.

4EBOB >OB CLRO @LIIFPFLK BSBKQ @>II?>@HP. 1. <?ACH!IHN;=N() e4OFDDBOBA TEBKBSBO QTL PE>MBP CFOPQ @LJB FKQL @LKQ>@Q TFQE B>@E LQEBO. 2. ?H>!IHN;=N() e4OFDDBOBA LSBO >KA LSBO >D>FK >P ILKD >P PE>MBP @LKQFKRB QL ?B FK @LKQ>@Q. 3. JL?0IFP?() e4OFDDBOBA ?BCLOB "LU2$ PLISBP QEB LRQ@LJB LC QEB @LIIFPFLK, F.B. ?BCLOB <?ACH!IHN;=N() . )Q @>K ?B RPBA QL AFP>?IB > @LIIFPFLK FC KB@BPP>OV. 4. JIMN0IFP?() e4OFDDBOBA >CQBO QEB LRQ@LJB LC QEB @LIIFPFLK FP PLISBA. )Q >IILTP VLR QL D>QEBO FKCLOJ>QFLK >?LRQ QE>Q hPLIRQFLKi (HKLTK >P >K hFJMRIPBi). 4EB ABQ>FIP ?BEFKA JL?0IFP?() >KA JIMN0IFP?() >OB ?BVLKA QEB P@LMB LC QEFP ?LLH; ELTBSBO, TB >OB DLFKD QL Q>HB > @ILPB ILLH >Q <?ACH!IHN;=N() , TEF@E TFII @LSBO QEB J>GLOFQV LC @LKSBKQFLK>I @>PBP FK TEF@E VLR T>KQ QL QOFDDBO >K >@QFLK TEBK > @LIIFPFLK L@@ROP. ?H>!IHN;=N() TLOHP FABKQF@>IIV QL <?ACH!IHN;=N() , QEB LKIV AFCCBOBK@B ?BFKD QE>Q FQ L@@ROP QEB JLJBKQ ?LAFBP PBM>O>QB.

235

#E>MQBO 5. 0EVPF@P ,F?O>OFBP <?ACH!IHN;=N() FP TOFQQBK >P CLIILTP:


PIC> <?ACH!IHN;=N(!IHN;=N =J) U W

.LQF@B QE>Q QEB CRK@QFLK >?LSB FK@IRABP >K >ODRJBKQ LC QVMB !IHN;=N . ! !IHN;=N L?GB@Q FK@IRABP >II QEB A>Q> >PPL@F>QBA TFQE > @LIIFPFLKeQEB DBLJBQOV >KA QEB CLO@BP. ,BQgP P>V TB E>SB > 0OL@BPPFKD PHBQ@E TFQE .;LNC=F? L?GB@QP QE>Q PQLOB > OBCBOBK@B QL > "LU2$ ?LAV. (BOB FP QEB MOL@BPP TB >OB DLFKD QL CLIILT.

Step 1: Contact, could you tell me what two things collided?


.LT, TE>Q E>P @LIIFABA EBOB? )P FQ QEB ?LAFBP? 4EB PE>MBP? 4EB CFUQROBP? "LU2$ ABQB@QP @LIIFPFLKP ?BQTBBK PE>MBP; >CQBO >II, QEBPB >OB QEB BKQFQFBP QE>Q E>SB DBLJBQOV. (LTBSBO, ?B@>RPB PE>MBP >OB >QQ>@EBA QL ?LAFBP TFQE CFUQROBP, TE>Q TB OB>IIV T>KQ QL >PH "LU2$ FP: h#LRIA VLR QBII JB TEF@E QTL CFUQROBP @LIIFABA?i
$CRNOL? @1 = =J.A?N$CRNOL?A(); $CRNOL? @2 = =J.A?N$CRNOL? (); The contact stores the fixtures as A and B.

Step 2: Fixtures, could you tell me which body you are attached to?
I>S <1 = @1.A?N I>S(); I>S <2 = @2.A?N I>S(); getBody() gives us the body to which the Fixture is attached.

Step 3: Bodies, could you tell me which Particles you are associated with?
/+, QEFP FP QEB E>OABO M>OQ. !CQBO >II, "LU2$ ALBPKgQ HKLT >KVQEFKD >?LRQ LRO @LAB. 3ROB, FQ FP ALFKD >II PLOQP LC PQRCC QL HBBM QO>@H LC QEB OBI>QFLKPEFMP ?BQTBBK PE>MBP >KA ?LAFBP >KA GLFKQP, ?RQ FQgP RM QL RP QL J>K>DB LRO LTK L?GB@QP >KA QEBFO >PPL@F>QFLKP TFQE "LU2$ BIBJBKQP. ,R@HFIV CLO RP, "LU2$ MOLSFABP > CRK@QFLK QE>Q >IILTP RP QL >QQ>@E LRO 0OL@BPPFKD L?GB@Q (> .;LNC=F? ) QL > "LU2$ ?LAV SF> QEB M?N2M?L";N;() >KA A?N2M?L";N;() JBQELAP. ,BQgP Q>HB > ILLH >Q QEB @LKPQOR@QLO FK LRO .;LNC=F? @I>PP TEBOB QEB ?LAV FP J>AB. 7B >OB BUM>KAFKD LRO ?LAV-J>HFKD MOL@BAROB ?V LKB IFKB LC @LAB, KLQBA ?BILT.

236

4EB .>QROB LC #LAB (S1.0)


=F;MM .;LNC=F? U I>S <I>S; .;LNC=F?(@FI;N R, @FI;N S, @FI;N L) U I>S"?@ <> = H?Q I>S"?@(); <>.JIMCNCIH = <IR2>.=IIL>.CR?FM1I4ILF>(R, S); <>.NSJ? = I>S1SJ?."6,A+'!; <I>S = <IR2>.=L?;N? I>S(<>); !CL=F?0B;J? =M = H?Q !CL=F?0B;J?(); =M.G:L;>COM = <IR2>.M=;F;L.CR?FM1I4ILF>(L); <I>S.=L?;N?$CRNOL?(@>,1); 8E:O.I;J/I;H 7J7(J>?I); W "this" refers to this Particle object. We are telling the Box2D Body to store a reference to this Particle that we can access later.

,>QBO, FK LRO ;>>!IHN;=N() CRK@QFLK, LK@B TB HKLT QEB ?LAV, TB @>K >@@BPP QEB .;LNC=F? L?GB@Q TFQE A?N2M?L";N;() .

Example 5.9: CollisionListening


PIC> <?ACH!IHN;=N(!IHN;=N =J) U $CRNOL? @1 = =J.A?N$CRNOL?A(); $CRNOL? @2 = =J.A?N$CRNOL? (); I>S <1 = @1.A?N I>S(); I>S <2 = @2.A?N I>S(); .;LNC=F? J1 = (.;LNC=F?) <1.A?N2M?L";N;(); .;LNC=F? J2 = (.;LNC=F?) <2.A?N2M?L";N;(); When we pull the user data object out of the Body object, we have to remind our program that it is a Particle object. Box2D doesnt know this. Once we have the particles, we can do anything to them. Here we just call a function that changes their color.

J1.=B;HA?(); J2.=B;HA?(); W

237

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

.LT, FK J>KV @>PBP, TB @>KKLQ >PPRJB QE>Q QEB L?GB@QP QE>Q @LIIFABA >OB >II .;LNC=F? L?GB@QP. 7B JFDEQ E>SB > PHBQ@E TFQE IOH>;LS L?GB@QP, .;LNC=F? L?GB@QP, IR L?GB@QP, BQ@. 3L LCQBK TB TFII E>SB QL NRBOV QEB hRPBO A>Q>i >KA CFKA LRQ TE>Q HFKA LC L?GB@Q FQ FP ?BCLOB MOL@BBAFKD.
-<D?=N I1 = <1.A?N2M?L";N;(); C@ (I1.A?N!F;MM() == .;LNC=F?.=F;MM) U .;LNC=F? J = (.;LNC=F?) I1; J.=B;HA?(); W Getting a generic object Asking that object if its a Particle

)Q PELRIA >IPL ?B KLQBA QE>Q ARB QL ELT "LU2$ QOFDDBOP QEBPB @>II?>@HP, VLR @>KKLQ @OB>QB LO ABPQOLV "LU2$ BKQFQFBP FKPFAB LC <?ACH!IHN;=N() , ?H>!IHN;=N() , JL?0IFP?() , LO JIMN0IFP?() . )C VLR T>KQ QL AL QEFP, VLRgII KBBA QL PBQ > S>OF>?IB FKPFAB >K L?GB@Q (PLJBQEFKD IFHB: G;LE$IL"?F?NCIH = NLO? ), TEF@E VLR @EB@H AROFKD >L;Q() >KA QEBK ABIBQB L?GB@QP.

Exercise 5.11
#LKPFABO ELT MLIVJLOMEFPJ @LRIA EBIM FK QEB >?LSB @>PB. "RFIA >K BU>JMIB FK TEF@E PBSBO>I @I>PPBP BUQBKA LKB @I>PP >KA QEBOBCLOB BIFJFK>QB QEB KBBA CLO PR@E QBPQFKD.

Exercise 5.12
#OB>QB > PFJRI>QFLK FK TEF@E .;LNC=F? L?GB@QP AFP>MMB>O TEBK QEBV @LIIFAB TFQE LKB >KLQEBO. 5PB QEB JBQELALILDV ) GRPQ ABP@OF?BA.

5.14 A Brief InterludeIntegration Methods


(>P QEB CLIILTFKD BSBO E>MMBKBA QL VLR? 9LRgOB >Q > C>K@V @L@HQ>FI M>OQV OBD>IFKD VLRO COFBKAP TFQE Q>II Q>IBP LC PLCQT>OB MEVPF@P PFJRI>QFLKP. 3LJBLKB MFMBP RM: h%K@E>KQFKD! "RQ TE>Q FKQBDO>QFLK JBQELA >OB VLR RPFKD?i h7E>Q?!i VLR QEFKH QL VLROPBIC. h)KQBDO>QFLK?i ->V?B VLRgSB EB>OA QEB QBOJ ?BCLOB. !ILKD TFQE hAFCCBOBKQF>QFLK,i FQgP LKB LC QEB QTL J>FK LMBO>QFLKP FK @>I@RIRP. 2FDEQ, @>I@RIRP. 4EB DLLA KBTP FP, TBgSB DLQQBK QEOLRDE >?LRQ 90% LC QEB J>QBOF>I FK QEFP ?LLH OBI>QBA QL MEVPF@P PFJRI>QFLK >KA TB E>SBKgQ OB>IIV KBBABA QL AFSB FKQL @>I@RIRP. "RQ >P TBgOB @LJFKD @ILPB QL CFKFPEFKD QEFP QLMF@, FQgP TLOQE Q>HFKD > JLJBKQ QL BU>JFKB QEB @>I@RIRP ?BEFKA TE>Q TB E>SB ?BBK ALFKD >KA ELT FQ OBI>QBP QL QEB JBQELALILDV FK @BOQ>FK MEVPF@P IF?O>OFBP (IFHB "LU2$ >KA QEB RM@LJFKD QLUF@IF?P).

238

4EB .>QROB LC #LAB (S1.0)

,BQgP ?BDFK ?V >KPTBOFKD QEB NRBPQFLK: h7E>Q ALBP FKQBDO>QFLK E>SB QL AL TFQE IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK?i 7BII, CFOPQ IBQgP ABCFKB /4110=09?4,?4:9, QEB MOL@BPP LC CFKAFKD > hABOFS>QFSB.i 4EB ABOFS>QFSB LC > CRK@QFLK FP > JB>PROB LC ELT > CRK@QFLK @E>KDBP LSBO QFJB. #LKPFABO IL@>QFLK >KA FQP ABOFS>QFSB. ,L@>QFLK FP > MLFKQ FK PM>@B, TEFIB SBIL@FQV FP @E>KDB FK IL@>QFLK LSBO QFJB. 4EBOBCLOB, SBIL@FQV @>K ?B ABP@OF?BA >P QEB hABOFS>QFSBi LC IL@>QFLK. 7E>Q FP >@@BIBO>QFLK? 4EB @E>KDB FK SBIL@FQV LSBO QFJBeF.B. QEB hABOFS>QFSBi LC SBIL@FQV. .LT QE>Q TB RKABOPQ>KA QEB ABOFS>QFSB (AFCCBOBKQF>QFLK), TB @>K ABCFKB QEB FKQBDO>I (FKQBDO>QFLK) >P QEB FKSBOPB LC QEB ABOFS>QFSB. )K LQEBO TLOAP, QEB FKQBDO>I LC >K L?GB@QgP SBIL@FQV LSBO QFJB QBIIP RP QEB L?GB@QgP KBT IL@>QFLK TEBK QE>Q QFJB MBOFLA BKAP. ,L@>QFLK FP QEB FKQBDO>I LC SBIL@FQV, >KA SBIL@FQV FP QEB FKQBDO>I LC >@@BIBO>QFLK. 3FK@B LRO MEVPF@P PFJRI>QFLK FP CLRKABA RMLK QEB MOL@BPP LC @>I@RI>QFKD >@@BIBO>QFLK ?>PBA LK CLO@BP, TB KBBA FKQBDO>QFLK QL CFDROB LRQ TEBOB QEB L?GB@Q FP >CQBO > @BOQ>FK MBOFLA LC QFJB (IFHB LKB CO>JB LC >KFJ>QFLK!) 3L TBgSB ?BBK ALFKD FKQBDO>QFLK >II >ILKD! )Q ILLHP IFHB QEFP:
P?FI=CNS.;>>(;==?F?L;NCIH); FI=;NCIH.;>>(P?FI=CNS);

4EB >?LSB JBQELALILDV FP HKLTK >P %RIBO FKQBDO>QFLK (K>JBA CLO QEB J>QEBJ>QF@F>K ,BLKE>OA %RIBO, MOLKLRK@BA h/FIBOi) LO QEB %RIBO JBQELA. )QgP BPPBKQF>IIV QEB PFJMIBPQ CLOJ LC FKQBDO>QFLK >KA SBOV B>PV QL FJMIBJBKQ FK LRO @LAB (PBB QEB QTL IFKBP >?LSB!) (LTBSBO, FQ FP KLQ KB@BPP>OFIV QEB JLPQ BCCF@FBKQ CLOJ, KLO FP FQ @ILPB QL ?BFKD QEB JLPQ >@@RO>QB. 7EV FP %RIBO FK>@@RO>QB? ,BQgP QEFKH >?LRQ FQ QEFP T>V. 7EBK VLR AOFSB > @>O ALTK QEB OL>A MOBPPFKD QEB D>P MBA>I TFQE VLRO CLLQ >KA >@@BIBO>QFKD, ALBP QEB @>O PFQ FK LKB IL@>QFLK >Q QFJB BNR>IP LKB PB@LKA, QEBK AFP>MMB>O >KA PRAABKIV OB>MMB>O FK > KBT IL@>QFLK >Q QFJB BNR>IP QTL PB@LKAP, >KA AL QEB P>JB QEFKD CLO QEOBB PB@LKAP, >KA CLRO, >KA CFSB? .L, LC @LROPB KLQ. 4EB @>O JLSBP @LKQFKRLRPIV ALTK QEB OL>A. "RQ TE>QgP E>MMBKFKD FK LRO 0OL@BPPFKD PHBQ@E? ! @FO@IB FP >Q LKB IL@>QFLK >Q CO>JB 0, >KLQEBO >Q CO>JB 1, >KLQEBO >Q CO>JB 2. 3ROB, >Q QEFOQV CO>JBP MBO PB@LKA, TBgOB PBBFKD QEB FIIRPFLK LC JLQFLK. "RQ TB LKIV @>I@RI>QB > KBT IL@>QFLK BSBOV , RKFQP LC QFJB, TEBOB>P QEB OB>I TLOIA FP MBOCB@QIV @LKQFKRLRP. 4EFP OBPRIQP FK PLJB FK>@@RO>@FBP, >P PELTK FK QEB AF>DO>J ?BILT:

239

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

F64B?2 5.13

4EB hOB>I TLOIAi FP QEB @ROSB; %RIBO PFJRI>QFLK FP QEB PBOFBP LC IFKB PBDJBKQP. /KB LMQFLK QL FJMOLSB LK %RIBO FP QL RPB PJ>IIBO QFJBPQBMPeFKPQB>A LC LK@B MBO CO>JB, TB @LRIA OB@>I@RI>QB >K L?GB@QgP IL@>QFLK QTBKQV QFJBP MBO CO>JB. "RQ QEFP FPKgQ MO>@QF@>I; LRO PHBQ@E TLRIA QEBK ORK QLL PILTIV. ) PQFII ?BIFBSB QE>Q %RIBO FP QEB ?BPQ JBQELA CLO IB>OKFKD QEB ?>PF@P, >KA FQgP >IPL MBOCB@QIV >ABNR>QB CLO JLPQ LC QEB MOLGB@QP TB JFDEQ J>HB FK 0OL@BPPFKD. !KVQEFKD TB ILPB FK BCCF@FBK@V LO FK>@@RO>@V TB J>HB RM FK B>PB LC RPB >KA RKABOPQ>KA>?FIFQV. &LO ?BQQBO >@@RO>@V, "LU2$ RPBP PLJBQEFKD @>IIBA PVJMIB@QF@ %RIBO LO PBJF-BUMIF@FQ %RIBO (EQQM://BK.TFHFMBAF>.LOD/TFHF/3VJMIB@QF@=%RIBO=JBQELA), > PIFDEQ JLAFCF@>QFLK LC %RIBO. 4EBOB FP >IPL >K FKQBDO>QFLK JBQELA @>IIBA 2RKDB-+RQQ> (K>JBA CLO 'BOJ>K J>QEBJ>QF@F>KP #. 2RKDB >KA -. 7. +RQQ>), TEF@E FP RPBA FK PLJB MEVPF@P BKDFKBP. ! SBOV MLMRI>O FKQBDO>QFLK JBQELA QE>Q LRO KBUQ MEVPF@P IF?O>OV RPBP FP HKLTK >P h6BOIBQ FKQBDO>QFLK.i ! PFJMIB T>V QL ABP@OF?B 6BOIBQ FKQBDO>QFLK FP QL QEFKH LC LRO QVMF@>I JLQFLK >IDLOFQEJ TFQELRQ SBIL@FQV. !CQBO >II, TB ALKgQ OB>IIV KBBA QL PQLOB QEB SBIL@FQV. )C TB >IT>VP HKLT TEBOB >K L?GB@Q T>P >Q LKB MLFKQ FK QFJB >KA TEBOB FQ FP KLT, TB @>K BUQO>MLI>QB FQP SBIL@FQV. 6BOIBQ FKQBDO>QFLK ALBP MOB@FPBIV QEFP, QELRDE FKPQB>A LC E>SFKD > S>OF>?IB CLO SBIL@FQV, FQ @>I@RI>QBP SBIL@FQV TEFIB QEB MOLDO>J FP ORKKFKD. 6BOIBQ FKQBDO>QFLK FP M>OQF@RI>OIV TBII PRFQBA CLO M>OQF@IB PVPQBJP, BPMB@F>IIV M>OQF@IB PVPQBJP TFQE PMOFKD @LKKB@QFLKP ?BQTBBK QEB M>OQF@IBP. 7B ALKgQ KBBA QL TLOOV >?LRQ QEB ABQ>FIP ?B@>RPB QLUF@IF?P, >P TBgII PBB ?BILT, Q>HBP @>OB LC QEBJ CLO RP. (LTBSBO, FC VLR >OB FKQBOBPQBA, EBOB FP QEB PBJFK>I M>MBO LK 6BOIBQ MEVPF@P, COLJ TEF@E GRPQ >?LRQ BSBOV 6BOIBQ @LJMRQBO DO>MEF@P PFJRI>QFLK FP ABOFSBA: "!AS>K@BA #E>O>@QBO 0EVPF@P" (EQQM://TTT.D>J>PRQO>.@LJ/OBPLRO@B=DRFAB/ 20030121/G>@L?PLK=MCS.EQJ). !KA LC @LROPB, VLR @>K CFKA LRQ JLOB >?LRQ 6BOIBQ FKQBDO>QFLK COLJ 7FHFMBAF> (EQQM://BK.TFHFMBAF>.LOD/TFHF/6BOIBQ=FKQBDO>QFLK).

240

4EB .>QROB LC #LAB (S1.0)

5.15 Verlet Physics with toxiclibs


&OLJ QLUF@IF?P.LOD: THCL=7@=6G =G 5B =B89D9B89BH, CD9B GCIF79 @=6F5FM 7C@@97H=CB :CF 7CADIH5H=CB5@ 89G=;B H5G?G K=H< &5J5 & ,FC79GG=B; 89J9@CD98 6M '5FGH9B THCL=U .7<A=8H (H<IG :5F). /<9 7@5GG9G 5F9 DIFDCG9:I@@M ?9DH :5=F@M ;9B9F=7 =B CF89F HC A5L=A=N9 F9-IG9 =B 8=::9F9BH 7CBH9LHG F5B;=B; :FCA ;9B9F5H=J9 89G=;B, 5B=A5H=CB, =BH9F57H=CB/=BH9F:579 89G=;B, 85H5 J=GI5@=N5H=CB HC 5F7<=H97HIF9 5B8 8=;=H5@ :56F=75H=CB, IG9 5G H957<=B; HCC@ 5B8 ACF9.U )K LQEBO TLOAP, TB PELRIA QE>KH LRO IR@HV PQ>OP CLO QLUF@IF?P. 7B >OB LKIV DLFKD QL CL@RP LK > CBT BU>JMIBP OBI>QBA QL 6BOIBQ MEVPF@P, ?RQ QLUF@IF?P FK@IRABP > PRFQB LC LQEBO TLKABOCRI M>@H>DBP QE>Q EBIM TFQE >RAFL, @LILO, DBLJBQOV, >KA JLOB. )K M>OQF@RI>O, FC VLR >OB ILLHFKD QL TLOH TFQE CLOJ >KA C>?OF@>QFLK FK 0OL@BPPFKD, Q>HB > ILLH >Q QEB DBLJBQOV M>@H>DB. $BJLP @>K ?B CLRKA >Q /MBK 0OL@BPPFKD (EQQM://TTT.LMBKMOL@BPPFKD.LOD/MLOQ>I/?RPBO)$=4530). 7B PELRIA KLQB QE>Q QLUF@IF?P T>P ABPFDKBA PMB@FCF@>IIV CLO RPB TFQE 0OL@BPPFKD. 4EFP FP DOB>Q KBTP. 4EB QOLR?IB TB E>A TFQE J>HFKD "LU2$ TLOH FK 0OL@BPPFKD (JRIQFMIB @LLOAFK>QB PVPQBJP, "LU2$ SP. *"LU2$ SP. 0"LU2$) FP KLQ >K FPPRB EBOB. QLUF@IF?P FP > IF?O>OV QE>Q VLR GRPQ ALTKIL>A, PQF@H FK VLRO IF?O>OFBP CLIABO, >KA RPB. !KA QEB @LLOAFK>QB PVPQBJ QE>Q TBgII RPB CLO QEB MEVPF@P BKDFKB FP QEB @LLOAFK>QB PVPQBJ LC 0OL@BPPFKD, PL KL QO>KPI>QFKD ?>@H >KA CLOQE. )K >AAFQFLK, QLUF@IF?P FP KLQ IFJFQBA QL > 2$ TLOIA; >II LC QEB MEVPF@P PFJRI>QFLKP >KA CRK@QFLKP TLOH FK ?LQE QTL >KA QEOBB AFJBKPFLKP. 3L ELT AL VLR AB@FAB TEF@E IF?O>OV VLR PELRIA RPB? "LU2$ LO QLUF@IF?P? )C VLR C>II FKQL LKB LC QEB CLIILTFKD QTL @>QBDLOFBP, VLRO AB@FPFLK FP > ?FQ B>PFBO: 1. My project involves collisions. I have circles, squares, and other strangely shaped objects that knock each other around and bounce off each other. )K QEFP @>PB, VLR >OB DLFKD QL KBBA "LU2$. QLUF@IF?P ALBP KLQ E>KAIB @LIIFPFLKP. 2. My project involves lots of particles flying around the screen. Sometimes they attract each other. Sometimes they repel each other. And sometimes they are connected with springs. )K QEFP @>PB, QLUF@IF?P FP IFHBIV VLRO ?BPQ @ELF@B. )Q FP PFJMIBO QL RPB QE>K "LU2$ >KA M>OQF@RI>OIV TBII PRFQBA QL @LKKB@QBA PVPQBJP LC M>OQF@IBP. QLUF@IF?P FP >IPL SBOV EFDE MBOCLOJ>K@B, ARB QL QEB PMBBA LC QEB 6BOIBQ FKQBDO>QFLK >IDLOFQEJ (KLQ QL JBKQFLK QEB C>@Q QE>Q QEB MOLDO>J DBQP QL FDKLOB >II LC QEB @LIIFPFLK DBLJBQOV). (BOB FP > IFQQIB @E>OQ QE>Q @LSBOP PLJB LC QEB CB>QROBP CLO B>@E MEVPF@P IF?O>OV.

241

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Feature #LIIFPFLK DBLJBQOV 3$ MEVPF@P 0>OQF@IB >QQO>@QFLK / OBMRIPFLK CLO@BP 3MOFKD @LKKB@QFLKP /QEBO @LKKB@QFLKP: OBSLIRQB, MRIIBV, DB>O, MOFPJ>QF@ -LQLOP &OF@QFLK

Box2D 9BP .L .L 9BP 9BP 9BP 9BP

toxiclibs VerletPhysics .L 9BP 9BP 9BP .L .L .L

Getting toxiclibs
%SBOVQEFKD VLR KBBA QL ALTKIL>A >KA FKPQ>II QLUF@IF?P @>K ?B CLRKA >Q: QLUF@IF?P (EQQM://QLUF@IF?P.LOD/) 7EBK VLR ALTKIL>A QEB IF?O>OV, VLRgII KLQF@B QE>Q FQ @LJBP TFQE BFDEQ JLARIBP (F.B. PR?CLIABOP), B>@E > IF?O>OV FK FQP LTK OFDEQ. &LO QEB BU>JMIBP FK QEFP @E>MQBO, VLR TFII LKIV KBBA hSBOIBQMEVPF@Pi >KA hQLUF@IF?P@LOBi; ELTBSBO, ) OB@LJJBKA VLR Q>HB > ILLH >Q >KA @LKPFABO RPFKD >II LC QEB JLARIBP! /K@B VLR E>SB QEB IF?O>OV FKPQ>IIBA QL VLRO 0OL@BPPFKD IF?O>OV CLIABO (EQQM://TFHF.MOL@BPPFKD.LOD/T/(LT=QL=)KPQ>II=>=#LKQOF?RQBA=,F?O>OV), VLR >OB OB>AV QL PQ>OQ ILLHFKD >Q QEB CLIILTFKD BU>JMIBP.

Core Elements of VerletPhysics


7B PMBKQ > ILQ LC QFJB TLOHFKD QEOLRDE QEB @LOB BIBJBKQP LC > "LU2$ TLOIA: TLOIA, ?LAV, PE>MB, GLFKQ. 4EFP DFSBP RP > EB>A PQ>OQ LK RKABOPQ>KAFKD QLUF@IF?P, PFK@B FQ CLIILTP > PFJFI>O PQOR@QROB.

242

4EB .>QROB LC #LAB (S1.0)

Box2D 7LOIA "LAV 3E>MB

toxiclibs VerletPhysics 6BOIBQ0EVPF@P 6BOIBQ0>OQF@IB .LQEFKD! QLUF@IF?P ALBP KLQ E>KAIB PE>MB DBLJBQOV .LQEFKD! QLUF@IF?P ALBP KLQ E>KAIB PE>MB DBLJBQOV 6BOIBQ3MOFKD

&FUQROB *LFKQ

Vectors with toxiclibs


(BOB TB DL >D>FK. 2BJBJ?BO >II QE>Q QFJB TB PMBKQ IB>OKFKD QEB FKP >KA LRQP LC QEB .3?=NIL @I>PP? 4EBK OBJBJ?BO ELT TEBK TB DLQ QL "LU2$, TB E>A QL QO>KPI>QB >II QELPB @LK@BMQP QL > "LU2$ SB@QLO @I>PP: 3?=2 ? 7BII, FQgP QFJB QL AL FQ >D>FK. QLUF@IF?P >IPL FK@IRABP FQP LTK SB@QLO @I>PPBP, LKB CLO QTL AFJBKPFLKP >KA LKB CLO QEOBB: 3?=2" >KA 3?=3" . !D>FK, QLUF@IF?P SB@QLOP >OB QEB P>JB @LK@BMQR>IIV, ?RQ TB KBBA QL IB>OK > ?FQ LC KBT PVKQ>U. 9LR @>K CFKA >II LC QEB AL@RJBKQ>QFLK CLO QEBPB SB@QLO @I>PPBP EBOB: 6B@2$ (EQQM://QLUF@IF?P.LOD/AL@P/@LOB/QLUF/DBLJ/6B@2$.EQJI) 6B@3$ (EQQM://QLUF@IF?P.LOD/AL@P/@LOB/QLUF/DBLJ/6B@3$.EQJI) !KA IBQgP GRPQ OBSFBT PLJB LC QEB ?>PF@ SB@QLO J>QE LMBO>QFLKP TFQE .3?=NIL QO>KPI>QBA QL 3?=2" (TBgOB PQF@HFKD TFQE 2$ CLO PFJMIF@FQVgP P>HB).

PVector
.3?=NIL ; = H?Q .3?=NIL(1,-1); .3?=NIL < = H?Q .3?=NIL(3,4); ;.;>>(<); .3?=NIL ; = H?Q .3?=NIL(1,-1); .3?=NIL < = H?Q .3?=NIL(3,4); .3?=NIL = = .3?=NIL.;>>(;,<); .3?=NIL ; = H?Q .3?=NIL(1,-1); @FI;N G = ;.G;A(); ;.HILG;FCT?();

Vec2D
3?=2" ; = H?Q 3?=2"(1,-1); 3?=2" < = H?Q 3?=2"(3,4); ;.;>>0?F@(<); 3?=2" ; = H?Q 3?=2"(1,-1); 3?=2" < = H?Q 3?=2"(3,4); 3?=2" = = ;.;>>(<); 3?=2" ; = H?Q 3?=2"(1,-1); @FI;N G = ;.G;AHCNO>?(); ;.HILG;FCT?();

243

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Building the toxiclibs physics world


4EB CFOPQ QEFKD TB KBBA QL AL QL @OB>QB > QLUF@IF?P MEVPF@P TLOIA FK LRO BU>JMIBP FP FJMLOQ QEB IF?O>OV FQPBIC.
CGJILN NIRC.JBSMC=M2>.*; CGJILN NIRC.JBSMC=M2>.<?B;PCILM.*; CGJILN NIRC.A?IG.*; Importing the libraries

4EBK TBgII KBBA > OBCBOBK@B QL LRO MEVPF@P TLOIA, > 3?LF?N.BSMC=M LO 3?LF?N.BSMC=M2" L?GB@Q (ABMBKAFKD LK TEBQEBO TB >OB TLOHFKD FK QTL LO QEOBB AFJBKPFLKP). 4EB BU>JMIBP FK QEFP @E>MQBO TFII LMBO>QB FK 2$ LKIV CLO PFJMIF@FQV, ?RQ QEBV @LRIA B>PFIV ?B BUQBKABA FKQL 3$ (>KA 3$ SBOPFLKP >OB >S>FI>?IB TFQE QEB @E>MQBO ALTKIL>A).
3?LF?N.BSMC=M2" JBSMC=M; PIC> M?NOJ() U JBSMC=M=H?Q 3?LF?N.BSMC=M2"(); Creating a toxiclibs Verlet physics world

/K@B VLR E>SB VLRO 3?LF?N.BSMC=M L?GB@Q, VLR @>K PBQ PLJB DIL?>I MOLMBOQFBP CLO VLRO TLOIA. &LO BU>JMIB, FC VLR T>KQ FQ QL E>SB E>OA ?LRKA>OFBP M>PQ TEF@E L?GB@QP @>KKLQ QO>SBI, VLR @>K PBQ FQP IFJFQP:
JBSMC=M.M?N4ILF> IOH>M(H?Q /?=N(0,0,QC>NB,B?CABN));

)K >AAFQFLK, VLR @>K >AA DO>SFQV QL QEB MEVPF@P TLOIA TFQE > %L;PCNS ?B;PCIL L?GB@Q. ! DO>SFQV ?BE>SFLO OBNRFOBP > SB@QLOeELT PQOLKD >KA FK TE>Q AFOB@QFLK FP QEB DO>SFQV?
JBSMC=M.;>> ?B;PCIL(H?Q %L;PCNS ?B;PCIL(H?Q 3?=2"(0,0.5))); W

&FK>IIV, FK LOABO QL @>I@RI>QB QEB MEVPF@P LC QEB TLOIA >KA JLSB QEB L?GB@QP FK QEB TLOIA, TB E>SB QL @>II OJ>;N?() . 4VMF@>IIV QEFP TLRIA E>MMBK LK@B MBO CO>JB FK >L;Q() .
PIC> >L;Q() U JBSMC=M.OJ>;N?(); W This is the same as Box2Ds step() function

5.16 Particles and Springs in toxiclibs


)K QEB "LU2$ BU>JMIBP, TB P>T ELT TB @>K @OB>QB LRO LTK @I>PP (@>IIBA, P>V, .;LNC=F? ) >KA FK@IRAB > OBCBOBK@B QL > "LU2$ ?LAV.

244

4EB .>QROB LC #LAB (S1.0)


=F;MM .;LNC=F? U I>S <I>S;

4EFP QB@EKFNRB FP PLJBTE>Q OBARKA>KQ PFK@B "LU2$ FQPBIC HBBMP QO>@H LC >II LC QEB ?LAFBP FK FQP TLOIA. (LTBSBO, FQ >IILTP RP QL J>K>DB TEF@E ?LAV FP TEF@E (>KA QEBOBCLOB ELT B>@E ?LAV FP AO>TK) TFQELRQ E>SFKD QL OBIV LK FQBO>QFKD QEOLRDE "LU2$gP FKQBOK>I IFPQP. ,BQgP ILLH >Q ELT TB JFDEQ Q>HB QEB P>JB >MMOL>@E TFQE QEB @I>PP 3?LF?N.;LNC=F?2" FK QLUF@IF?P. 7B T>KQ QL J>HB LRO LTK .;LNC=F? @I>PP PL QE>Q TB @>K AO>T LRO M>OQF@IBP > @BOQ>FK T>V >KA FK@IRAB >KV @RPQLJ MOLMBOQFBP. 7BgA MOL?>?IV TOFQB LRO @LAB >P CLIILTP:
=F;MM .;LNC=F? U 3?LF?N.;LNC=F?2" J; .;LNC=F?(3?=2" JIM) U J = H?Q 3?LF?N.;LNC=F?2"(JIM); W PIC> >CMJF;S() U @CFF(0,150); MNLIE?(0); ?FFCJM?(J.R,J.S,16,16); W W When it comes time to draw the Particle, we ask the VerletParticle for its x and y coordinates. A VerletParticle needs an initial location (an x and y). Our Particle has a reference to a VerletParticle.

,LLHFKD >Q QEB >?LSB, TB PELRIA CFOPQ ?B QEOFIIBA QL KLQF@B QE>Q AO>TFKD QEB M>OQF@IB FP >P PFJMIB >P DO>??FKD QEB L >KA M >KA RPFKD QEBJ. .L >THT>OA @LKSBOPFLKP ?BQTBBK @LLOAFK>QB PVPQBJP EBOB PFK@B QLUF@IF?P FP ABPFDKBA QL QEFKH FK MFUBIP. 3B@LKA, VLR JFDEQ KLQF@B QE>Q QEFP .;LNC=F? @I>PPgP PLIB MROMLPB FP QL PQLOB > OBCBOBK@B QL > 3?LF?N.;LNC=F?2" L?GB@Q. 4EFP EFKQP >Q PLJBQEFKD. 2BJBJ?BO LRO AFP@RPPFLK LC FKEBOFQ>K@B ?>@H FK #E>MQBO 4: 0>OQF@IB 3VPQBJP? 7E>Q FP > .;LNC=F? L?GB@Q LQEBO QE>K >K h>RDJBKQBAi 3?LF?N.;LNC=F? ? 7EV ?LQEBO J>HFKD > 6BOIBQ M>OQF@IB FKPFAB > M>OQF@IB TEBK TB @LRIA PFJMIV ?RN?H> 3?LF?N.;LNC=F? ?
=F;MM .;LNC=F? ?RN?H>M 3?LF?N.;LNC=F?2" U .;LNC=F?(3?=2" FI=) U MOJ?L(FI=); W PIC> >CMJF;S() U @CFF(175); MNLIE?(0); Calling super() so that the object is initialized properly

We want this to be just like a VerletParticle, only with a display() method.

245

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

?FFCJM?(R,S,16,16); W W

Weve inherited x and y from VerletParticle!

2BJBJ?BO LRO JRIQF-PQBM MOL@BPP TFQE QEB "LU2$ BU>JMIBP? 7B E>A QL >PH QEB ?LAV CLO FQP IL@>QFLK, QEBK @LKSBOQ QE>Q IL@>QFLK QL MFUBIP, QEBK RPB QE>Q IL@>QFLK FK > AO>TFKD CRK@QFLK. .LT, ?B@>RPB TB E>SB FKEBOFQBA BSBOVQEFKD COLJ QEB 3?LF?N.;LNC=F? @I>PP, LRO LKIV PQBM FP QL AO>T QEB PE>MB >Q R >KA S ! )K@FABKQ>IIV, FQgP FKQBOBPQFKD QL KLQB QE>Q QEB 3?LF?N.;LNC=F?2" @I>PP FP > PR?@I>PP LC 3?=2" . 3L FK >AAFQFLK QL FKEBOFQFKD BSBOVQEFKD COLJ 3?LF?N.;LNC=F?2" , LRO .;LNC=F? @I>PP >@QR>IIV E>P >II LC QEB 3?=2" CRK@QFLKP >S>FI>?IB >P TBII. 7B @>K KLT @OB>QB M>OQF@IBP >KVTEBOB TFQEFK LRO PHBQ@E.
.;LNC=F? J1 = H?Q .;LNC=F?(H?Q 3?=2"(100,20)); .;LNC=F? J2 = H?Q .;LNC=F?(H?Q 3?=2"(100,180));

*RPQ J>HFKD > M>OQF@IB FPKgQ BKLRDE, ELTBSBO. 7B E>SB QL J>HB PROB TB QBII LRO MEVPF@P TLOIA >?LRQ QEBJ TFQE QEB ;>>.;LNC=F?() CRK@QFLK.
JBSMC=M.;>>.;LNC=F?(J1); JBSMC=M.;>>.;LNC=F?(J2);

)C VLR ILLH >Q QEB QLUF@IF?P AL@RJBKQ>QFLK, VLRgII PBB QE>Q QEB ;>>.;LNC=F?() BUMB@QP > 3?LF?N.;LNC=F?2" L?GB@Q.
;>>.;LNC=F?(3?LF?N.;LNC=F?2" J;LNC=F?)

!KA ELT @>K TB QEBK M>PP FKQL QEB CRK@QFLK LRO LTK .;LNC=F? L?GB@Q? 2BJBJ?BO QE>Q LQEBO QBKBQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKDeMLIVJLOMEFPJ? (BOB, ?B@>RPB LRO .;LNC=F? @I>PP ?RN?H>M 3?LF?N.;LNC=F?2" , TB @>K @ELLPB QL QOB>Q LRO M>OQF@IB FK QTL AFCCBOBKQ T>VPe>P > .;LNC=F? LO >P > 3?LF?N.;LNC=F?2" . 4EFP FP >K FK@OBAF?IV MLTBOCRI CB>QROB LC L?GB@Q-LOFBKQBA MOLDO>JJFKD. )C TB ?RFIA LRO @RPQLJ @I>PPBP ?>PBA LK @I>PPBP COLJ QLUF@IF?P, TB @>K RPB LRO L?GB@QP FK @LKGRK@QFLK TFQE >II LC QEB CRK@QFLKP QLUF@IF?P E>P QL LCCBO. )K >AAFQFLK QL QEB 3?LF?N.;LNC=F? @I>PP, QLUF@IF?P E>P > PBQ LC @I>PPBP QE>Q >IILT VLR QL @LKKB@Q M>OQF@IBP TFQE PMOFKD CLO@BP. 4EBOB >OB QEOBB QVMBP LC PMOFKDP FK QLUF@IF?P: j 3?LF?N0JLCHA : 4EFP @I>PP @OB>QBP > PMOFKDV @LKKB@QFLK ?BQTBBK QTL M>OQF@IBP FK PM>@B. ! PMOFKDgP MOLMBOQFBP @>K ?B @LKCFDROBA FK PR@E > T>V >P QL @OB>QB > PQFCC PQF@H-IFHB @LKKB@QFLK LO > EFDEIV BI>PQF@ PQOBQ@EV @LKKB@QFLK. ! M>OQF@IB @>K >IPL ?B IL@HBA PL QE>Q LKIV LKB BKA LC QEB PMOFKD @>K JLSB.

246

4EB .>QROB LC #LAB (S1.0)

j 3?LF?N!IHMNL;CH?>0JLCHA : ! 3?LF?N!IHMNL;CH?>0JLCHA L?GB@Q FP > PMOFKD TELPB J>UFJRJ AFPQ>K@B @>K ?B IFJFQBA. 4EFP @>K EBIM QEB TELIB PMOFKD PVPQBJ >@EFBSB ?BQQBO PQ>?FIFQV. j 3?LF?N+CH"CMN;H=?0JLCHA : ! 3?LF?N+CH"CMN;H=?0JLCHA L?GB@Q FP > PMOFKD QE>Q LKIV BKCLO@BP FQP OBPQ IBKDQE FC QEB @ROOBKQ AFPQ>K@B FP IBPP QE>K FQP OBPQ IBKDQE. 4EFP FP E>KAV FC VLR T>KQ QL BKPROB L?GB@QP >OB >Q IB>PQ > @BOQ>FK AFPQ>K@B COLJ B>@E LQEBO, ?RQ ALKgQ @>OB FC QEB AFPQ>K@B FP ?FDDBO QE>K QEB BKCLO@BA JFKFJRJ. 4EB FKEBOFQ>K@B >KA MLIVJLOMEFPJ QB@EKFNRB TB BJMILVBA FK QEB MOBSFLRP PB@QFLK >IPL MOLSBP QL ?B RPBCRI TEBK @OB>QFKD PMOFKDP. ! PMOFKD BUMB@QP QTL M>OQF@IBP TEBK FQ FP @OB>QBA. !KA >D>FK, ?B@>RPB LRO .;LNC=F? @I>PP ?RN?H>M 3?LF?N.;LNC=F? , > 3?LF?N0JLCHA L?GB@Q TFII >@@BMQ LRO .;LNC=F? L?GB@QP M>PPBA FKQL QEB @LKPQOR@QLO. ,BQgP Q>HB > ILLH >Q PLJB BU>JMIB @LAB QE>Q >PPRJBP QEB BUFPQBK@B LC LRO QTL MOBSFLRP M>OQF@IBP J1 >KA J2 >KA @OB>QBP > @LKKB@QFLK ?BQTBBK QEBJ TFQE > DFSBK OBPQ IBKDQE >KA PQOBKDQE.
@FI;N F?H = 80; @FI;N MNL?HANB = 0.01; What is the rest length of the spring? How strong is the spring?

3?LF?N0JLCHA2" MJLCHA=H?Q 3?LF?N0JLCHA2"(J1,J2,F?H,MNL?HANB);

*RPQ >P TFQE M>OQF@IBP, FK LOABO CLO QEB @LKKB@QFLK QL >@QR>IIV ?B M>OQ LC QEB MEVPF@P TLOIA, TB KBBA QL BUMIF@FQIV >AA FQ.
JBSMC=M.;>>0JLCHA(MJLCHA);

5.17 Putting It All Together: A Simple Interactive Spring


/KB QEFKD TB P>T TFQE "LU2$ FP QE>Q QEB MEVPF@P PFJRI>QFLK ?OLHB ALTK TEBK TB LSBOOLAB FQ >KA J>KR>IIV PBQ QEB IL@>QFLK LC > ?LAV. 7FQE QLUF@IF?P, TB ALKgQ E>SB QEFP MOL?IBJ. )C TB T>KQ QL JLSB QEB IL@>QFLK LC > M>OQF@IB, TB @>K PFJMIV PBQ FQP L >KA M IL@>QFLK J>KR>IIV. (LTBSBO, ?BCLOB TB AL PL, FQgP DBKBO>IIV > DLLA FAB> QL @>II QEB FI=E() CRK@QFLK.
FI=E() FP QVMF@>IIV RPBA QL IL@H > M>OQF@IB FK MI>@B >KA FP FABKQF@>I QL PBQQFKD > "LU2$ ?LAVgP

ABKPFQV QL 0. (LTBSBO, EBOB TB >OB DLFKD QL PELT ELT QL IL@H > M>OQF@IB QBJMLO>OFIV, JLSB FQ, >KA QEBK RKIL@H FQ PL QE>Q FQ @LKQFKRBP QL JLSB >@@LOAFKD QL QEB MEVPF@P PFJRI>QFLK. , BQgP P>V VLR T>KQ QL JLSB > DFSBK M>OQF@IB TEBKBSBO VLR @IF@H QEB JLRPB.
C@ (GIOM?.L?MM?>) U

247

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

J2.FI=E(); J2.R = GIOM?5; J2.S = GIOM?6; J2.OHFI=E(); W

First lock the particle, then set the x and y, then unlock() it.

!KA KLT TBgOB OB>AV QL MRQ >II LC QEBPB BIBJBKQP QLDBQEBO FK > PFJMIB BU>JMIB QE>Q @LKKB@QP QTL M>OQF@IBP TFQE > PMOFKD. /KB M>OQF@IB FP IL@HBA FK MI>@B, >KA QEB LQEBO @>K ?B JLSBA ?V AO>DDFKD QEB JLRPB. .LQB QE>Q QEFP BU>JMIB FP SFOQR>IIV FABKQF@>I QL %U>JMIB 3.11 (PBB M>DB 139).

Example 5.10: Simple Spring with toxiclibs


CGJILN NIRC.JBSMC=M2>.*; CGJILN NIRC.JBSMC=M2>.<?B;PCILM.*; CGJILN NIRC.A?IG.*; 3?LF?N.BSMC=M2" JBSMC=M; .;LNC=F? J1; .;LNC=F? J2; PIC> M?NOJ() U MCT?(640,360); JBSMC=M=H?Q 3?LF?N.BSMC=M2"(); Creating a physics world

JBSMC=M.;>> ?B;PCIL(H?Q %L;PCNS ?B;PCIL2"(H?Q 3?=2"(0,0.5))); JBSMC=M.M?N4ILF> IOH>M(H?Q /?=N(0,0,QC>NB,B?CABN)); J1 = H?Q .;LNC=F?(H?Q 3?=2"(100,20)); J2 = H?Q .;LNC=F?(H?Q 3?=2"(100,180)); J1.FI=E(); Locking Particle 1 in place Creating two Particles

3?LF?N0JLCHA2" MJLCHA=H?Q 3?LF?N0JLCHA2"(J1,J2,80,0.01); Creating one Spring

248

4EB .>QROB LC #LAB (S1.0)

JBSMC=M.;>>.;LNC=F?(J1); JBSMC=M.;>>.;LNC=F?(J2); JBSMC=M.;>>0JLCHA(MJLCHA); W PIC> >L;Q() U JBSMC=M.OJ>;N?(); <;=EALIOH>(255); FCH?(J1.R,J1.S,J2.R,J2.S); J1.>CMJF;S(); J2.>CMJF;S(); C@ (GIOM?.L?MM?>) U J2.FI=E(); J2.R = GIOM?5; J2.S = GIOM?6; J2.OHFI=E(); W W =F;MM .;LNC=F? ?RN?H>M 3?LF?N.;LNC=F?2" U .;LNC=F?(3?=2" FI=) U MOJ?L(FI=); W PIC> >CMJF;S() U @CFF(175); MNLIE?(0); ?FFCJM?(R,S,16,16); W W

Must add everything to the world

Must update the physics

Drawing everything

Moving a Particle according to the mouse

How cute is our simple Particle class?!

5.18 Connected Systems, Part I: String


4EB >?LSB BU>JMIB, QTL M>OQF@IBP @LKKB@QBA TFQE > PFKDIB PMOFKD, FP QEB @LOB ?RFIAFKD ?IL@H CLO TE>Q QLUF@IF?Pg MEVPF@P FP M>OQF@RI>OIV TBII PRFQBA CLO: PLCQ ?LAV PFJRI>QFLKP. &LO BU>JMIB, > PQOFKD @>K ?B PFJRI>QBA ?V @LKKB@QFKD > IFKB LC M>OQF@IBP TFQE PMOFKDP. ! ?I>KHBQ @>K ?B PFJRI>QBA ?V @LKKB@QFKD > DOFA LC M>OQF@IBP TFQE PMOFKDP. !KA > @RQB, @RAAIV, PNRFPEV @>OQLLK @E>O>@QBO @>K ?B PFJRI>QBA ?V > @RPQLJ I>VLRQ LC M>OQF@IBP @LKKB@QBA TFQE PMOFKDP.

249

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

F64B?2 5.14

,BQgP ?BDFK ?V PFJRI>QFKD > hPLCQ MBKARIRJie> ?L? E>KDFKD COLJ > PQOFKD, FKPQB>A LC > OFDFA >OJ IFHB TB E>A FK #E>MQBO 3 (PBB M>DB 131). ,BQgP RPB QEB "PQOFKD" FK &FDROB 5.14 >?LSB >P LRO JLABI. &FOPQ, TBgII KBBA > IFPQ LC M>OQF@IBP (IBQgP RPB QEB P>JB .;LNC=F? @I>PP TB ?RFIQ FK QEB MOBSFLRP BU>JMIB).
ALL;S*CMN<.;LNC=F?> J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();

.LT, IBQgP P>V TB T>KQ QL E>SB 20 M>OQF@IBP, >II PM>@BA 10 MFUBIP >M>OQ.

F64B?2 5.15
@FI;N F?H = 10; @FI;N HOG.;LNC=F?M = 20;

7B @>K ILLM COLJ C BNR>IP 0 >II QEB T>V RM QL 20, TFQE B>@E M>OQF@IBgP M IL@>QFLK PBQ QL C * 10 PL QE>Q QEB CFOPQ M>OQF@IB FP >Q (0,10) , QEB PB@LKA >Q (0,20) , QEB QEFOA >Q (0,30) , BQ@.
@IL(CHN C=0; C < HOG.ICHNM; C++) U .;LNC=F? J;LNC=F?=H?Q .;LNC=F?(C*F?H,10); JBSMC=M.;>>.;LNC=F?(J;LNC=F?); Spacing them out along the x-axis Add the particle to our list.

250

4EB .>QROB LC #LAB (S1.0)

J;LNC=F?M.;>>(J;LNC=F?); W

Add the particle to the physics world.

%SBK QELRDE FQgP > ?FQ OBARKA>KQ, TBgOB DLFKD QL >AA QEB M>OQF@IB QL ?LQE QEB QLUF@IF?P MEVPF@P TLOIA >KA QL LRO LTK IFPQ. )K @>PB TB BSBKQR>IIV E>SB JRIQFMIB PQOFKDP, QEFP TFII >IILT RP QL HKLT TEF@E M>OQF@IBP >OB @LKKB@QBA QL TEF@E PQOFKDP. .LT CLO QEB CRK M>OQ: )QgP QFJB QL @LKKB@Q >II QEB M>OQF@IBP. 0>OQF@IB 1 TFII ?B @LKKB@QBA QL M>OQF@IB 0, M>OQF@IB 2 QL M>OQF@IB 1, 3 QL 2, 4 QL 3, BQ@.

F64B?2 5.16

)K LQEBO TLOAP, M>OQF@IB C KBBAP QL ?B @LKKB@QBA QL M>OQF@IB C-1 (BU@BMQ CLO TEBK C BNR>IP WBOL).
C@ (C != 0) U .;LNC=F? JL?PCIOM = J;LNC=F?M.A?N(C-1); First we need a reference to the previous particle.

3?LF?N0JLCHA2" MJLCHA = H?Q 3?LF?N0JLCHA2"(J;LNC=F?,JL?PCIOM,F?H,MNL?HANB); Then we make a spring connection between the particle and the previous particle with a rest length and strength (both floats). JBSMC=M.;>>0JLCHA(MJLCHA); W We must not forget to add the spring to the physics world.

.LT, TE>Q FC TB T>KQ QEB PQOFKD QL E>KD COLJ > CFUBA MLFKQ? 7B @>K IL@H LKB LC QEB M>OQF@IBPeQEB CFOPQ, QEB I>PQ, QEB JFAAIB LKB, BQ@. (BOBgP ELT TB TLRIA >@@BPP QEB CFOPQ M>OQF@IB (FK QEB ALL;S*CMN ) >KA IL@H FQ.
.;LNC=F? B?;>=J;LNC=F?M.A?N(0); B?;>.FI=E();

!KA FC TB T>KQ QL AO>T >II QEB M>OQF@IBP >P ?BFKD @LKKB@QBA TFQE > IFKB, >ILKD TFQE > @FO@IB CLO QEB I>PQ M>OQF@IB, TB @>K RPB <?ACH0B;J?() , ?H>0B;J?() , >KA P?LN?R() , >@@BPPFKD QEB M>OQF@IB IL@>QFLKP COLJ LRO ALL;S*CMN .

251

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Example 5.11: Soft swinging pendulum


MNLIE?(0); HI$CFF(); <?ACH0B;J?(); @IL (.;LNC=F? J : J;LNC=F?M) U P?LN?R(J.R,J.S); W ?H>0B;J?(); .;LNC=F? N;CF = J;LNC=F?M.A?N(HOG.ICHNM-1); N;CF.>CMJF;S(); This draws the last particle as a circle. Each particle is one point in the line.

4EB CRII @LAB >S>FI>?IB TFQE QEB @E>MQBO ALTKIL>A >IPL ABJLKPQO>QBP ELT QL AO>D QEB Q>FI M>OQF@IB TFQE QEB JLRPB.

Exercise 5.13
#OB>QB > E>KDFKD @ILQE PFJRI>QFLK RPFKD QEB QB@EKFNRB >?LSB, ?RQ @LKKB@Q >II QEB M>OQF@IBP TFQE > DOFA >P ABJLKPQO>QBA FK QEB P@OBBKPELQ ?BILT.

252

4EB .>QROB LC #LAB (S1.0)

5.19 Connected Systems, Part II: Force-Directed Graph


(>SB VLR BSBO BK@LRKQBOBA QEB CLIILTFKD P@BK>OFL? h) E>SB > TELIB ?RK@E LC PQRCC ) T>KQ QL AO>T LK QEB P@OBBK >KA ) T>KQ >II QE>Q PQRCC QL ?B PM>@BA LRQ BSBKIV FK > KF@B, KB>Q, LOD>KFWBA J>KKBO. /QEBOTFPB ) E>SB QOLR?IB PIBBMFKD >Q KFDEQ.i 4EFP FP KLQ >K RK@LJJLK MOL?IBJ FK @LJMRQ>QFLK>I ABPFDK. /KB PLIRQFLK FP QVMF@>IIV OBCBOOBA QL >P > hCLO@B-AFOB@QBA DO>ME.i ! CLO@B-AFOB@QBA DO>ME FP > SFPR>IFW>QFLK LC BIBJBKQPeIBQgP @>II QEBJ hKLABPieFK TEF@E QEB MLPFQFLKP LC QELPB KLABP >OB KLQ J>KR>IIV >PPFDKBA. 2>QEBO, QEB KLABP >OO>KDB QEBJPBISBP >@@LOAFKD QL > PBQ LC CLO@BP. 7EFIB >KV CLO@BP @>K ?B RPBA, > QVMF@>I BU>JMIB FKSLISBP PMOFKD CLO@BP. !KA PL QLUF@IF?P FP MBOCB@Q CLO QEFP P@BK>OFL.

(LT AL TB FJMIBJBKQ QEB >?LSB? &FOPQ, TBgII KBBA > ,I>? @I>PP. 4EFP FP QEB B>PV M>OQ; FQ @>K ?RN?H> 3?LF?N.;LNC=F?2" . 2B>IIV, QEFP FP GRPQ TE>Q TB AFA ?BCLOB, LKIV TBgOB @>IIFKD FQ ,I>? KLT FKPQB>A LC .;LNC=F? .
=F;MM ,I>? ?RN?H>M 3?LF?N.;LNC=F?2" U ,I>?(3?=2" JIM) U MOJ?L(JIM); W PIC> >CMJF;S() U @CFF(0,150); MNLIE?(0); ?FFCJM?(R,S,16,16); W W

.BUQ TB @>K TOFQB > @I>PP @>IIBA !FOMN?L , TEF@E TFII ABP@OF?B > IFPQ LC KLABP. 253

#E>MQBO 5. 0EVPF@P ,F?O>OFBP


=F;MM !FOMN?L U ALL;S*CMN<,I>?> HI>?M; @FI;N >C;G?N?L; !FOMN?L(CHN H, @FI;N >, 3?=2" =?HN?L) U HI>?M = H?Q ALL;S*CMN<,I>?>(); >C;G?N?L = >; @IL (CHN C = 0; C < H; C++) U HI>?M.;>>(H?Q ,I>?(=?HN?L.;>>(3?=2".L;H>IG3?=NIL()))); Heres a funny little detail. Were going to have a problem if all the Node objects start in exactly the same location. So we add a random vector to the center location so that each Node is slightly offset. Well use this variable for the rest length between all the nodes.

W W

,BQgP >PPRJB TB >AABA > >CMJF;S() CRK@QFLK QL AO>T >II QEB KLABP FK QEB @IRPQBO >KA @OB>QBA > !FOMN?L L?GB@Q FK M?NOJ() >KA AFPMI>VBA FQ FK >L;Q() . )C TB O>K QEB PHBQ@E >P FP, KLQEFKD TLRIA E>MMBK. 7EV? "B@>RPB TB CLODLQ QEB TELIB CLO@B-AFOB@QBA DO>ME M>OQ! 7B KBBA QL @LKKB@Q BSBOV PFKDIB KLAB QL BSBOV LQEBO KLAB TFQE > CLO@B. "RQ TE>Q BU>@QIV AL TB JB>K ?V QE>Q? ,BQgP >PPRJB TB E>SB CLRO ,I>? L?GB@QP: 0, 1, 2 >KA 3. (BOB >OB LRO @LKKB@QFLKP:
0 9EDD;9J;: JE 1 0 9EDD;9J;: JE 2 0 9EDD;9J;: JE 3 1 9EDD;9J;: JE 2 1 9EDD;9J;: JE 3 2 9EDD;9J;: JE 3

.LQF@B QTL FJMLOQ>KQ ABQ>FIP >?LRQ LRO @LKKB@QFLK IFPQ. j #: 9:/0 4> .:990.?0/ ?: 4?>071. 7B ALKgQ E>SB 0 @LKKB@QBA QL 0 LO 1 @LKKB@QBA QL 1. j +0 /:9H? 900/ ?: =0;0,? .:990.?4:9> 49 =0A0=>0. )K LQEBO TLOAP, FC TBgSB >IOB>AV P>FA 0 FP @LKKB@QBA QL 1, TB ALKgQ KBBA QL P>V 1 FP @LKKB@QBA QL 0 ?B@>RPB, TBII, FQ >IOB>AV FP! 3L ELT AL TB TOFQB @LAB QL J>HB QEBPB @LKKB@QFLKP CLO * KRJ?BO LC KLABP? ,LLH >Q QEB IBCQ @LIRJK. )Q OB>AP: 000 11 22. 3L TB HKLT TB KBBA QL >@@BPP B>@E KLAB FK QEB IFPQ COLJ 0 QL *-1.
@IL (CHN C = 0; C < HI>?M.MCT?()-1; C++) U 3?LF?N.;LNC=F?2" HC = HI>?M.A?N(C);

254

4EB .>QROB LC #LAB (S1.0)

.LT, TB HKLT TB KBBA QL @LKKB@Q KLAB 0 QL KLABP 1,2,3. &LO KLAB 1: 2,3. &LO KLAB 2: 3. 3L CLO BSBOV KLAB F, TB JRPQ ILLM COLJ F+1 RKQFI QEB BKA LC QEB IFPQ.
<EH (?DJ @ = ?+1; @ < DE:;I.I?P;(); @++) Q 3?LF?N.;LNC=F?2" HD = HI>?M.A?N(D); Look how we start j at i + 1.

7FQE BSBOV QTL ,I>? P TB CFKA, >II TB E>SB QL AL QEBK FP J>HB > PMOFKD.
JBSMC=M.;>>0JLCHA(H?Q 3?LF?N0JLCHA2"(HC,HD,>C;G?N?L,0.01)); W W The Spring connects Nodes ni and nj.

!PPRJFKD QELPB @LKKB@QFLKP >OB J>AB FK QEB !FOMN?L @LKPQOR@QLO, TB @>K KLT @OB>QB > @IRPQBO FK LRO J>FK Q>? >KA PBB QEB OBPRIQP!

Example 5.12: Cluster


CGJILN NIRC.A?IG.*; CGJILN NIRC.JBSMC=M2>.*; 3?LF?N.BSMC=M2" JBSMC=M; !FOMN?L =FOMN?L; PIC> M?NOJ() U MCT?(300,300); JBSMC=M=H?Q 3?LF?N.BSMC=M2"(); =FOMN?L = H?Q !FOMN?L(8,100,H?Q 3?=2"(QC>NB/2,B?CABN/2)); W PIC> >L;Q() U JBSMC=M.OJ>;N?(); <;=EALIOH>(255); Make a cluster.

255

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

=FOMN?L.>CMJF;S(); W

Draw the cluster.

Exercise 5.14
5PB QEB !FOMN?L PQOR@QROB >P > PHBIBQLK CLO > @RQB, @RAAIV, PNRFPEV @OB>QROB (Z I> h.LHF> &OFBKAPi). !AA DO>SFQV >KA >IPL >IILT QEB @OB>QROB QL ?B AO>DDBA TFQE QEB JLRPB.

Exercise 5.15
%UM>KA QEB CLO@B-AFOB@QBA DO>ME QL E>SB JLOB QE>K LKB !FOMN?L L?GB@Q. 5PB > 3?LF?N+CH"CMN;H=?0JLCHA2" L?GB@Q QL @LKKB@Q @IRPQBO QL @IRPQBO.

5.20 Attraction and Repulsion Behaviors


7EBK TB ILLHBA >Q >AAFKD >K >QQO>@QFLK CLO@B QL "LU2$, TB CLRKA QE>Q QEB "LU2$ I>S @I>PP FK@IRABA >K ;JJFS$IL=?() CRK@QFLK. !II TB KBBABA QL AL T>P @>I@RI>QB QEB >QQO>@QFLK CLO@B (&LO@B = ' * J>PP1 * J>PP2 / AFPQ>K@B PNR>OBA) >P > SB@QLO >KA >MMIV FQ QL QEB ?LAV. QLUF@IF?P 3?LF?N.;LNC=F? @I>PP >IPL FK@IRABP > CRK@QFLK @>IIBA ;>>$IL=?() QE>Q TB @>K RPB QL >MMIV >KV @>I@RI>QBA CLO@B QL > M>OQF@IB. (LTBSBO, QLUF@IF?P >IPL Q>HBP QEFP FAB> LKB PQBM CROQEBO ?V >IILTFKD RP QL >QQ>@E PLJB @LJJLK CLO@BP (IBQgP @>II QEBJ h?BE>SFLOPi) QL M>OQF@IBP, @>I@RI>QFKD QEBJ >KA >MMIVFKD QEBJ CLO RP! &LO BU>JMIB, FC TB >QQ>@E >K ANNL;=NCIH ?B;PCIL L?GB@Q QL > M>OQF@IB, QEBK >II LQEBO M>OQF@IBP FK QEB MEVPF@P TLOIA TFII ?B >QQO>@QBA QL QE>Q M>OQF@IB.

256

4EB .>QROB LC #LAB (S1.0)

,BQgP P>V TB E>SB > .;LNC=F? @I>PP (QE>Q ?RN?H>M 3?LF?N.;LNC=F? ).


.;LNC=F? J = H?Q .;LNC=F?(H?Q 3?=2"(200,200));

/K@B TBgSB J>AB > .;LNC=F? L?GB@Q, TB @>K @OB>QB >K ANNL;=NCIH ?B;PCIL L?GB@Q >PPL@F>QBA TFQE QE>Q M>OQF@IB.
@FI;N >CMN;H=? = 20; @FI;N MNL?HANB = 0.1; ANNL;=NCIH ?B;PCIL <?B;PCIL = H?Q ANNL;=NCIH ?B;PCIL(J, >CMN;H=?, MNL?HANB);

.LQF@B ELT QEB ?BE>SFLO FP @OB>QBA TFQE QTL M>O>JBQBOPe >CMN;H=? >KA MNL?HANB . 4EB AFPQ>K@B PMB@FCFBP QEB O>KDB TFQEFK TEF@E QEB ?BE>SFLO TFII ?B >MMIFBA. &LO BU>JMIB, FK QEB >?LSB P@BK>OFL, LKIV LQEBO M>OQF@IBP TFQEFK QTBKQV MFUBIP TFII CBBI QEB >QQO>@QFLK CLO@B. 4EB PQOBKDQE, LC @LROPB, PMB@FCFBP ELT PQOLKD QEB CLO@B FP. &FK>IIV, FK LOABO CLO QEB CLO@B QL ?B >@QFS>QBA, QEB ?BE>SFLO KBBAP QL ?B >AABA QL QEB MEVPF@P TLOIA.
JBSMC=M.;>> ?B;PCIL(<?B;PCIL);

4EFP JB>KP BSBOVQEFKD QE>Q IFSBP FK QEB MEVPF@P PFJRI>QFLK TFII >IT>VP ?B >QQO>@QBA QL QE>Q M>OQF@IB, >P ILKD >P FQ FP TFQEFK QEB AFPQ>K@B QEOBPELIA. %SBK QELRDE QLUF@IF?P ALBP KLQ E>KAIB @LIIFPFLKP, VLR @>K @OB>QB > @LIIFPFLK-IFHB BCCB@Q ?V >AAFKD > OBMRIPFSB ?BE>SFLO QL B>@E >KA BSBOV M>OQF@IB (PL QE>Q BSBOV M>OQF@IB OBMBIP BSBOV LQEBO M>OQF@IB). ,BQgP ILLH >Q ELT TB JFDEQ JLAFCV LRO .;LNC=F? @I>PP QL AL QEFP.
=F;MM .;LNC=F? ?RN?H>M 3?LF?N.;LNC=F?2" U @FI;N L; .;LNC=F? (3?=2" FI=) U MOJ?L(FI=); L = 4; JBSMC=M.;>> ?B;PCIL(H?Q ANNL;=NCIH ?B;PCIL(NBCM, L*4, -1)); W PIC> >CMJF;S () U @CFF (255); MNLIE? (255); ?FFCJM? (R, S, L*2, L*2); W W Every time a Particle is made, an AttractionBehavior is generated and added to the physics world. Note that when the strength is negative, its a repulsive force! Weve added a radius to every Particle.

7B @LRIA KLT OB@OB>QB LRO >QQO>@QFLK BU>JMIB ?V E>SFKD > PFKDIB ANNL;=NIL L?GB@Q QE>Q BUBOQP >K >QQO>@QFLK ?BE>SFLO LSBO QEB BKQFOB TFKALT.

257

#E>MQBO 5. 0EVPF@P ,F?O>OFBP

Example 5.13: Attraction/Repulsion


=F;MM ANNL;=NIL ?RN?H>M 3?LF?N.;LNC=F?2" U @FI;N L; ANNL;=NIL (3?=2" FI=) U MOJ?L (FI=); L = 24; JBSMC=M.;>> ?B;PCIL(H?Q ANNL;=NCIH ?B;PCIL(NBCM, QC>NB, 0.1)); W PIC> >CMJF;S () U @CFF(0); ?FFCJM? (R, S, L*2, L*2); W W The AttractionBehavior distance equals the width so that it covers the entire window.

Exercise 5.16
#OB>QB >K L?GB@Q QE>Q ?LQE >QQO>@QP >KA OBMBIP. 7E>Q FC FQ >QQO>@QP >KV M>OQF@IB QE>Q FP C>O >T>V ?RQ OBMBIP QELPB M>OQF@IBP >Q > PELOQ AFPQ>K@B?

Exercise 5.17
5PB ANNL;=NCIH ?B;PCIL FK @LKGRK@QFLK TFQE PMOFKD CLO@BP.

258

4EB .>QROB LC #LAB (S1.0)

The Ecosystem Project


Step 5 Exercise: Take your system of creatures from Step 4 and use a physics engine to drive their motion and behaviors. Some possibilities:

Use Box2D to allow collisions between creatures. Consider triggering events when creatures collide. Use Box2D to augment the design of your creatures. Build a skeleton with distance joints or make appendages with revolute joints. Use toxiclibs to augment the design of your creature. Use a chain of toxiclibs particles for tentacles or a mesh of springs as a skeleton. Use toxiclibs to add attraction and repulsion behaviors to your creatures. Use spring (or joint) connections between objects to control their interactions. Create and delete these springs on the fly. Consider making these connections visible or invisible to the viewer.

259

#E>MQBO 6. !RQLKLJLRP !DBKQP

Chapter 6. Autonomous Agents


I*56@ 6@ .; 2E2?06@2 6; 360A6<;.9 @062;02, <? @062;02 360A6<;, 63 F<B 9682 A5.A /2AA2?.J Valentino Braitenberg

"BIFBSB FQ LO KLQ, QEBOB FP > MROMLPB. 7BII, >Q IB>PQ QEBOBgP > MROMLPB QL QEB CFOPQ CFSB @E>MQBOP LC QEFP ?LLH. 7B @LRIA PQLM OFDEQ EBOB; >CQBO >II, TBgSB ILLHBA >Q PBSBO>I AFCCBOBKQ T>VP LC JLABIFKD JLQFLK >KA PFJRI>QFKD MEVPF@P. !KDOV "FOAP, EBOB TB @LJB! 3QFII, IBQgP QEFKH CLO > JLJBKQ. 7EV >OB TB EBOB? 4EB B5HIF9 LC @LAB, OFDEQ? 7E>Q E>SB TB ?BBK ABPFDKFKD PL C>O? )K>KFJ>QB L?GB@QP. ,FCBIBPP PE>MBP PFQQFKD LK LRO P@OBBKP QE>Q CILM >OLRKA TEBK >CCB@QBA ?V CLO@BP FK QEBFO BKSFOLKJBKQ. 7E>Q FC TB @LRIA ?OB>QEB IFCB FKQL QELPB PE>MBP? 7E>Q FC QELPB PE>MBP @LRIA IFSB ?V QEBFO LTK ORIBP? #>K PE>MBP E>SB ELMBP >KA AOB>JP >KA CB>OP? 4EFP FP TE>Q TB >OB EBOB FK QEFP @E>MQBO QL ALeABSBILM 5IHCBCACIG 5;9BHG.

6.1 Forces from Within


4EB QBOJ ,@?:9:8:@> ,209? DBKBO>IIV OBCBOP QL >K BKQFQV QE>Q J>HBP FQP LTK @ELF@BP >?LRQ ELT QL >@Q FK FQP BKSFOLKJBKQ TFQELRQ >KV FKCIRBK@B COLJ > IB>ABO LO DIL?>I MI>K. &LO RP, h>@QFKDi TFII JB>K JLSFKD. 4EFP >AAFQFLK FP > PFDKFCF@>KQ @LK@BMQR>I IB>M. )KPQB>A LC > ?LU PFQQFKD LK > ?LRKA>OV T>FQFKD QL ?B MRPEBA ?V >KLQEBO C>IIFKD ?LU, TB >OB KLT DLFKD QL

260

4EB .>QROB LC #LAB (S1.0)

ABPFDK > ?LU QE>Q E>P QEB >?FIFQV >KA hABPFOBi QL IB>M LRQ LC QEB T>V LC QE>Q LQEBO C>IIFKD ?LU, FC FQ PL @ELLPBP. 7EFIB QEB @LK@BMQ LC CLO@BP QE>Q @LJB COLJ TFQEFK FP > J>GLO PEFCQ FK LRO ABPFDK QEFKHFKD, LRO @LAB ?>PB TFII ?>OBIV @E>KDB, >P QEBPB ABPFOBP >KA >@QFLKP >OB PFJMIV QE>Qe:CF79G. (BOB >OB QEOBB HBV @LJMLKBKQP LC >RQLKLJLRP >DBKQP QE>Q TBgII T>KQ QL HBBM FK JFKA >P TB ?RFIA LRO BU>JMIBP. j An autonomous agent has a 7484?0/ ability to perceive environment. )Q J>HBP PBKPB QE>Q > IFSFKD, ?OB>QEFKD ?BFKD PELRIA E>SB >K >T>OBKBPP LC FQP BKSFOLKJBKQ. 7E>Q ALBP QEFP JB>K CLO RP, ELTBSBO? !P TB ILLH >Q BU>JMIBP FK QEFP @E>MQBO, TB TFII MLFKQ LRQ MOLDO>JJFKD QB@EKFNRBP CLO >IILTFKD L?GB@QP QL PQLOB OBCBOBK@BP QL LQEBO L?GB@QP >KA QEBOBCLOB hMBO@BFSBi QEBFO BKSFOLKJBKQ. )QgP >IPL @OR@F>I QE>Q TB @LKPFABO QEB TLOA @=A=H98 EBOB. !OB TB ABPFDKFKD >K >II-HKLTFKD OB@Q>KDIB QE>Q CIFBP >OLRKA > 0OL@BPPFKD TFKALT, >T>OB LC BSBOVQEFKD BIPB FK QE>Q TFKALT? /O >OB TB @OB>QFKD > PE>MB QE>Q @>K LKIV BU>JFKB >KV LQEBO L?GB@Q TFQEFK CFCQBBK MFUBIP LC FQPBIC? /C @LROPB, QEBOB FP KL OFDEQ >KPTBO QL QEFP NRBPQFLK; FQ >II ABMBKAP. 7BgII BUMILOB PLJB MLPPF?FIFQFBP >P TB JLSB CLOT>OA. &LO > PFJRI>QFLK QL CBBI JLOB hK>QRO>I,i ELTBSBO, IFJFQ>QFLKP >OB > DLLA QEFKD. !K FKPB@Q, CLO BU>JMIB, J>V LKIV ?B >T>OB LC QEB PFDEQP >KA PJBIIP QE>Q FJJBAF>QBIV PROOLRKA FQ. &LO > OB>I-TLOIA @OB>QROB, TB @LRIA PQRAV QEB BU>@Q P@FBK@B LC QEBPB IFJFQ>QFLKP. ,R@HFIV CLO RP, TB @>K GRPQ J>HB PQRCC RM >KA QOV FQ LRQ. j An autonomous agent processes the information from its environment and calculates an action. 4EFP TFII ?B QEB B>PV M>OQ CLO RP, >P QEB >@QFLK FP > CLO@B. 4EB BKSFOLKJBKQ JFDEQ QBII QEB >DBKQ QE>Q QEBOBgP > ?FD P@>OV-ILLHFKD PE>OH PTFJJFKD OFDEQ >Q FQ, >KA QEB >@QFLK TFII ?B > MLTBOCRI CLO@B FK QEB LMMLPFQB AFOB@QFLK. j An autonomous agent has no leader. 4EFP QEFOA MOFK@FMIB FP PLJBQEFKD TB @>OB > IFQQIB IBPP >?LRQ. !CQBO >II, FC VLR >OB ABPFDKFKD > PVPQBJ TEBOB FQ J>HBP PBKPB QL E>SB > IB>ABO ?>OHFKD @LJJ>KAP >Q S>OFLRP BKQFQFBP, QEBK QE>QgP TE>Q VLRgII T>KQ QL FJMIBJBKQ. .BSBOQEBIBPP, J>KV LC QEBPB BU>JMIBP TFII E>SB KL IB>ABO CLO >K FJMLOQ>KQ OB>PLK. !P TB DBQ QL QEB BKA LC QEFP @E>MQBO >KA BU>JFKB DOLRM ?BE>SFLOP, TB TFII ILLH >Q ABPFDKFKD @LIIB@QFLKP LC >RQLKLJLRP >DBKQP QE>Q BUEF?FQ QEB MOLMBOQFBP LC @LJMIBU PVPQBJPe FKQBIIFDBKQ >KA PQOR@QROBA DOLRM AVK>JF@P QE>Q BJBODB KLQ COLJ > IB>ABO, ?RQ COLJ QEB IL@>I FKQBO>@QFLKP LC QEB BIBJBKQP QEBJPBISBP. )K QEB I>QB 1980P, @LJMRQBO P@FBKQFPQ #O>FD 2BVKLIAP (EQQM://TTT.OBA3A.@LJ/@TO/) ABSBILMBA >IDLOFQEJF@ PQBBOFKD ?BE>SFLOP CLO >KFJ>QBA @E>O>@QBOP. 4EBPB ?BE>SFLOP >IILTBA FKAFSFAR>I BIBJBKQP QL K>SFD>QB QEBFO AFDFQ>I BKSFOLKJBKQP FK > hIFCBIFHBi J>KKBO TFQE PQO>QBDFBP CLO CIBBFKD, T>KABOFKD, >OOFSFKD, MROPRFKD, BS>AFKD, BQ@. 5PBA FK QEB @>PB LC > PFKDIB >RQLKLJLRP >DBKQ, QEBPB ?BE>SFLOP >OB C>FOIV PFJMIB QL RKABOPQ>KA >KA FJMIBJBKQ. )K >AAFQFLK, ?V ?RFIAFKD > PVPQBJ LC JRIQFMIB @E>O>@QBOP QE>Q PQBBO QEBJPBISBP >@@LOAFKD QL PFJMIB, IL@>IIV ?>PBA ORIBP, PROMOFPFKD IBSBIP LC @LJMIBUFQV BJBODB. 4EB JLPQ C>JLRP BU>JMIB FP 2BVKLIAPgP h?LFAPi JLABI CLO hCIL@HFKD/PT>OJFKDi ?BE>SFLO.

261

#E>MQBO 6. !RQLKLJLRP !DBKQP

6.2 Vehicles and Steering


.LT QE>Q TB RKABOPQ>KA QEB @LOB @LK@BMQP ?BEFKA >RQLKLJLRP >DBKQP, TB @>K ?BDFK TOFQFKD QEB @LAB. 4EBOB >OB J>KV MI>@BP TEBOB TB @LRIA PQ>OQ. !OQFCF@F>I PFJRI>QFLKP LC >KQ >KA QBOJFQB @LILKFBP >OB C>KQ>PQF@ ABJLKPQO>QFLKP LC PVPQBJP LC >RQLKLJLRP >DBKQP. (&LO JLOB LK QEFP QLMF@, ) BK@LRO>DB VLR QL OB>A /IFH@9G, /9FA=H9G, 5B8 /F5::=7 &5AG ?V -FQ@EBI 2BPKF@H.) (LTBSBO, TB T>KQ QL ?BDFK ?V BU>JFKFKD >DBKQ ?BE>SFLOP QE>Q ?RFIA LK QEB TLOH TBgSB ALKB FK QEB CFOPQ CFSB @E>MQBOP LC QEFP ?LLH: JLABIFKD JLQFLK TFQE SB@QLOP >KA AOFSFKD JLQFLK TFQE CLO@BP. !KA PL FQgP QFJB QL OBK>JB LRO +IP?L @I>PP QE>Q ?B@>JB LRO .;LNC=F? @I>PP LK@B >D>FK. 4EFP QFJB TB >OB DLFKD QL @>II FQ 3?BC=F? .
=F;MM 3?BC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; // 4B;N ?FM? >I Q? H??> NI ;>>?

)K EFP 1999 M>MBO h3QBBOFKD "BE>SFLOP CLO !RQLKLJLRP #E>O>@QBOP,i 2BVKLIAP RPBP QEB TLOA hSBEF@IBi QL ABP@OF?B EFP >RQLKLJLRP >DBKQP, PL TB TFII CLIILT PRFQ.

Why Vehicle?
)K 1986, )Q>IF>K KBROLP@FBKQFPQ >KA @V?BOKBQF@FPQ 6>IBKQFKL "O>FQBK?BOD ABP@OF?BA > PBOFBP LC EVMLQEBQF@>I SBEF@IBP TFQE PFJMIB FKQBOK>I PQOR@QROBP FK EFP ?LLH 19<=7@9G: !LD9F=A9BHG =B .MBH<9H=7 ,GM7<C@C;M. "O>FQBK?BOD >ODRBP QE>Q EFP BUQO>LOAFK>OFIV PFJMIB JB@E>KF@>I SBEF@IBP J>KFCBPQ ?BE>SFLOP PR@E >P CB>O, >DDOBPPFLK, ILSB, CLOBPFDEQ, >KA LMQFJFPJ. 2BVKLIAP QLLH EFP FKPMFO>QFLK COLJ "O>FQBK?BOD, >KA TBgII Q>HB LROP COLJ 2BVKLIAP.

2BVKLIAP ABP@OF?BP QEB JLQFLK LC =895@=N98 SBEF@IBP (FAB>IFWBA ?B@>RPB TB >OB KLQ @LK@BOKBA TFQE QEB >@QR>I BKDFKBBOFKD LC PR@E SBEF@IBP, ?RQ PFJMIV >PPRJB QE>Q QEBV BUFPQ >KA TFII OBPMLKA QL LRO ORIBP) >P > PBOFBP LC QEOBB I>VBOPeAction Selection, Steering, >KA Locomotion. 1. A.?4:9 '070.?4:9. ! SBEF@IB E>P > DL>I (LO DL>IP) >KA @>K PBIB@Q >K >@QFLK (LO > @LJ?FK>QFLK LC >@QFLKP) ?>PBA LK QE>Q DL>I. 4EFP FP BPPBKQF>IIV TEBOB TB IBCQ LCC TFQE >RQLKLJLRP >DBKQP. 4EB SBEF@IB Q>HBP > ILLH >Q FQP BKSFOLKJBKQ >KA @>I@RI>QBP >K >@QFLK ?>PBA LK > ABPFOB: h) PBB > WLJ?FB J>O@EFKD QLT>OAP JB. 3FK@B ) ALKgQ T>KQ JV ?O>FKP QL ?B B>QBK, )gJ DLFKD QL CIBB COLJ QEB WLJ?FB.i 4EB DL>I FP QL HBBM LKBgP ?O>FKP >KA QEB >@QFLK FP QL CIBB. 2BVKLIAPgP M>MBO ABP@OF?BP J>KV DL>IP >KA >PPL@F>QBA >@QFLKP PR@E >P: PBBH > Q>ODBQ, >SLFA >K L?PQ>@IB, >KA

262

4EB .>QROB LC #LAB (S1.0)

CLIILT > M>QE. )K > JLJBKQ, TBgII PQ>OQ ?RFIAFKD QEBPB BU>JMIBP LRQ TFQE 0OL@BPPFKD @LAB. 2. '?00=492. /K@B >K >@QFLK E>P ?BBK PBIB@QBA, QEB SBEF@IB E>P QL @>I@RI>QB FQP KBUQ JLSB. &LO RP, QEB KBUQ JLSB TFII ?B > CLO@B; JLOB PMB@FCF@>IIV, > PQBBOFKD CLO@B. ,R@HFIV, 2BVKLIAP E>P ABSBILMBA > PFJMIB PQBBOFKD CLO@B CLOJRI> QE>Q TBgII RPB QEOLRDELRQ QEB BU>JMIBP FK QEFP @E>MQBO: >?00=492 1:=.0 = /0>4=0/ A07:.4?D - .@==09? A07:.4?D . 7BgII DBQ FKQL QEB ABQ>FIP LC QEFP CLOJRI> >KA TEV FQ TLOHP PL BCCB@QFSBIV FK QEB KBUQ PB@QFLK. 3. !:.:8:?4:9. &LO QEB JLPQ M>OQ, TBgOB DLFKD QL FDKLOB QEFP QEFOA I>VBO. )K QEB @>PB LC CIBBFKD WLJ?FBP, QEB IL@LJLQFLK @LRIA ?B ABP@OF?BA >P hIBCQ CLLQ, OFDEQ CLLQ, IBCQ CLLQ, OFDEQ CLLQ, >P C>PQ >P VLR @>K.i )K LRO 0OL@BPPFKD TLOIA, ELTBSBO, > OB@Q>KDIB LO @FO@IB LO QOF>KDIBgP >@QR>I JLSBJBKQ >@OLPP > TFKALT FP FOOBIBS>KQ DFSBK QE>Q FQgP >II >K FIIRPFLK FK QEB CFOPQ MI>@B. .BSBOQEBIBPP, QEFP FPKgQ QL P>V QE>Q VLR PELRIA FDKLOB IL@LJLQFLK BKQFOBIV. 9LR TFII CFKA DOB>Q S>IRB FK QEFKHFKD >?LRQ QEB IL@LJLQFSB ABPFDK LC VLRO SBEF@IB >KA ELT VLR @ELLPB QL >KFJ>QB FQ. 4EB BU>JMIBP FK QEFP @E>MQBO TFII OBJ>FK SFPR>IIV ?>OB, >KA > DLLA BUBO@FPB TLRIA ?B QL BI>?LO>QB LK QEB >KFJ>QFLK PQVIB e@LRIA VLR >AA PMFKKFKD TEBBIP LO LP@FII>QFKD M>AAIBP LO PERCCIFKD IBDP? 5IQFJ>QBIV, QEB JLPQ FJMLOQ>KQ I>VBO CLO VLR QL @LKPFABO FP #1eA7H=CB .9@97H=CB . 7E>Q >OB QEB BIBJBKQP LC VLRO PVPQBJ >KA TE>Q >OB QEBFO DL>IP? )K QEFP @E>MQBO, TB >OB DLFKD QL ILLH >Q > PBOFBP LC PQBBOFKD ?BE>SFLOP (F.B. >@QFLKP): PBBH, CIBB, CLIILT > M>QE, CLIILT > CILT CFBIA, CIL@H TFQE VLRO KBFDE?LOP, BQ@. )QgP FJMLOQ>KQ QL OB>IFWB, ELTBSBO, QE>Q QEB MLFKQ LC RKABOPQ>KAFKD ELT QL TOFQB QEB @LAB CLO QEBPB ?BE>SFLOP FP KLQ ?B@>RPB VLR PELRIA RPB QEBJ FK >II LC VLRO MOLGB@QP. 2>QEBO, QEBPB >OB > PBQ LC ?RFIAFKD ?IL@HP, > CLRKA>QFLK COLJ TEF@E VLR @>K ABPFDK >KA ABSBILM SBEF@IBP TFQE @OB>QFSB DL>IP >KA KBT >KA BU@FQFKD ?BE>SFLOP. !KA BSBK QELRDE TB TFII QEFKH IFQBO>IIV FK QEFP @E>MQBO (CLIILT QE>Q MFUBI!), VLR PELRIA >IILT VLROPBIC QL QEFKH JLOB >?PQO>@QIV (IFHB "O>FQBK?BOD). 7E>Q TLRIA FQ JB>K CLO VLRO SBEF@IB QL E>SB hILSBi LO hCB>Oi >P FQP DL>I, FQP AOFSFKD CLO@B? &FK>IIV (>KA TBgII >AAOBPP QEFP I>QBO FK QEB @E>MQBO), VLR TLKgQ DBQ SBOV C>O ?V ABSBILMFKD PFJRI>QFLKP TFQE LKIV LKB >@QFLK. 9BP, LRO CFOPQ BU>JMIB TFII ?B hPBBH > Q>ODBQ.i "RQ CLO VLR QL ?B @OB>QFSBeQL J>HB QEBPB PQBBOFKD ?BE>SFLOP MCIF CKB eFQ TFII >II @LJB ALTK QL JFUFKD >KA J>Q@EFKD JRIQFMIB >@QFLKP TFQEFK QEB P>JB SBEF@IB. 3L SFBT QEBPB BU>JMIBP KLQ >P PFKDRI>O ?BE>SFLOP QL ?B BJRI>QBA, ?RQ >P MFB@BP LC > I>ODBO MRWWIB QE>Q VLR TFII BSBKQR>IIV >PPBJ?IB.

6.3 The Steering Force


7B @>K BKQBOQ>FK LROPBISBP ?V AFP@RPPFKD QEB QEBLOBQF@>I MOFK@FMIBP ?BEFKA >RQLKLJLRP >DBKQP >KA PQBBOFKD >P JR@E >P TB IFHB, ?RQ TB @>KgQ DBQ >KVTEBOB TFQELRQ CFOPQ RKABOPQ>KAFKD QEB @LK@BMQ LC > PQBBOFKD CLO@B. #LKPFABO QEB CLIILTFKD P@BK>OFL. ! SBEF@IB JLSFKD TFQE SBIL@FQV ABPFOBP QL PBBH > Q>ODBQ.

263

#E>MQBO 6. !RQLKLJLRP !DBKQP

)QP DL>I >KA PR?PBNRBKQ >@QFLK FP QL PBBH QEB Q>ODBQ FK &FDROB 6.1. )C VLR QEFKH ?>@H QL #E>MQBO 2, VLR JFDEQ ?BDFK ?V J>HFKD QEB Q>ODBQ >K >QQO>@QLO >KA >MMIV > DO>SFQ>QFLK>I CLO@B QE>Q MRIIP QEB SBEF@IB QL QEB Q>ODBQ. 4EFP TLRIA ?B > MBOCB@QIV OB>PLK>?IB PLIRQFLK, ?RQ @LK@BMQR>IIV FQgP KLQ TE>Q TBgOB ILLHFKD CLO EBOB. 7B ALKgQ T>KQ QL PFJMIV @>I@RI>QB > CLO@B QE>Q MRPEBP QEB SBEF@IB QLT>OAP FQP Q>ODBQ; O>QEBO, TB >OB >PHFKD QEB SBEF@IB QL J>HB >K FKQBIIFDBKQ AB@FPFLK QL PQBBO QLT>OAP QEB Q>ODBQ ?>PBA F64B?2 6.1 LK FQP MBO@BMQFLK LC FQP PQ>QB >KA BKSFOLKJBKQ (F.B. ELT C>PQ >KA FK TE>Q AFOB@QFLK FP FQ @ROOBKQIV JLSFKD). 4EB SBEF@IB PELRIA ILLH >Q ELT FQ ABPFOBP QL JLSB (> SB@QLO MLFKQFKD QL QEB Q>ODBQ), @LJM>OB QE>Q DL>I TFQE ELT NRF@HIV FQ FP @ROOBKQIV JLSFKD (FQP SBIL@FQV), >KA >MMIV > CLO@B >@@LOAFKDIV. >?00=492 1:=.0 = /0>4=0/ A07:.4?D - .@==09? A07:.4?D /O >P TB JFDEQ TOFQB FK 0OL@BPPFKD:
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);

)K QEB >?LSB CLOJRI>, SBIL@FQV FP KL MOL?IBJ. !CQBO >II, TBgSB DLQ > S>OF>?IB CLO QE>Q. (LTBSBO, TB ALKgQ E>SB QEB 89G=F98 J9@C7=HM; QEFP FP PLJBQEFKD TB E>SB QL @>I@RI>QB. ,BQgP Q>HB > ILLH >Q &FDROB 6.2. )C TBgSB ABCFKBA QEB SBEF@IBgP DL>I >P hPBBHFKD QEB Q>ODBQ,i QEBK FQP ABPFOBA SBIL@FQV FP > SB@QLO QE>Q MLFKQP COLJ FQP @ROOBKQ IL@>QFLK QL QEB Q>ODBQ IL@>QFLK.

F64B?2 6.2

!PPRJFKD > .3?=NIL Q>ODBQ, TB QEBK E>SB:


.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);

264

4EB .>QROB LC #LAB (S1.0)

"RQ QEFP FPKgQ M>OQF@RI>OIV OB>IFPQF@. 7E>Q FC TB E>SB > SBOV EFDE-OBPLIRQFLK TFKALT >KA QEB Q>ODBQ FP QELRP>KAP LC MFUBIP >T>V? 3ROB, QEB SBEF@IB JFDEQ ABPFOB QL QBIBMLOQ FQPBIC FKPQ>KQIV QL QEB Q>ODBQ IL@>QFLK TFQE > J>PPFSB SBIL@FQV, ?RQ QEFP TLKgQ J>HB CLO >K BCCB@QFSB >KFJ>QFLK. 7E>Q TB OB>IIV T>KQ QL P>V FP: /<9 J9<=7@9 89G=F9G HC ACJ9 HCK5F8G H<9 H5F;9H 5H A5L=AIA GD998. )K LQEBO TLOAP, QEB SB@QLO PELRIA MLFKQ COLJ IL@>QFLK QL Q>ODBQ >KA TFQE > J>DKFQRAB BNR>I QL J>UFJRJ PMBBA (F.B. QEB C>PQBPQ QEB SBEF@IB @>K DL). 3L CFOPQ, TB KBBA QL J>HB PROB TB >AA > S>OF>?IB QL LRO 3?BC=F? @I>PP QE>Q PQLOBP J>UFJRJ PMBBA.
=F;MM 3?BC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N G;RMJ??>; Maximum speed

4EBK, FK LRO ABPFOBA SBIL@FQV @>I@RI>QFLK, TB P@>IB >@@LOAFKD QL J>UFJRJ PMBBA.


.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>);

F64B?2 6.3

0RQQFKD QEFP >II QLDBQEBO, TB @>K TOFQB > CRK@QFLK @>IIBA M??E() QE>Q OB@BFSBP > .3?=NIL Q>ODBQ >KA @>I@RI>QBP > PQBBOFKD CLO@B QLT>OAP QE>Q Q>ODBQ.
PIC> M??E(.3?=NIL N;LA?N) U .3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>); Calculating the desired velocity to target at max speed Reynoldss formula for steering force

.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);

265

#E>MQBO 6. !RQLKLJLRP !DBKQP

;JJFS$IL=?(MN??L); W

Using our physics model and applying the force to the objects acceleration

.LQB ELT FK QEB >?LSB CRK@QFLK TB CFKFPE ?V M>PPFKD QEB PQBBOFKD CLO@B FKQL ;JJFS$IL=?() . 4EFP >PPRJBP QE>Q TB >OB ?>PFKD QEFP BU>JMIB LK QEB CLRKA>QFLK TB ?RFIQ FK #E>MQBO 2 (PBB M>DB 66). (LTBSBO, VLR @LRIA GRPQ >P B>PFIV RPB QEB PQBBOFKD CLO@B TFQE "LU2$gP ;JJFS$IL=?() CRK@QFLK LO QLUF@IF?Pg ;>>$IL=?() CRK@QFLK. 3L TEV ALBP QEFP >II TLOH PL TBII? ,BQgP PBB TE>Q QEB PQBBOFKD CLO@B ILLHP IFHB OBI>QFSB QL QEB SBEF@IB >KA Q>ODBQ IL@>QFLKP.

F64B?2 6.4

!D>FK, KLQF@B ELT QEFP FP KLQ >Q >II QEB P>JB CLO@B >P DO>SFQ>QFLK>I >QQO>@QFLK. 2BJBJ?BO LKB LC LRO MOFK@FMIBP LC >RQLKLJLRP >DBKQP: !K >RQLKLJLRP >DBKQ E>P > @=A=H98 >?FIFQV QL MBO@BFSB FQP BKSFOLKJBKQ. (BOB FP QE>Q >?FIFQV, PR?QIV BJ?BAABA FKQL 2BVKLIAPgP PQBBOFKD CLOJRI>. )C QEB SBEF@IB TBOBKgQ JLSFKD >Q >II (WBOL SBIL@FQV), ABPFOBA JFKRP SBIL@FQV TLRIA ?B BNR>I QL ABPFOBA. "RQ QEFP FP KLQ QEB @>PB. 4EB SBEF@IB FP >T>OB LC FQP LTK SBIL@FQV >KA FQP PQBBOFKD CLO@B @LJMBKP>QBP >@@LOAFKDIV. 4EFP @OB>QBP > JLOB >@QFSB PFJRI>QFLK, >P QEB T>V FK TEF@E QEB SBEF@IB JLSBP QLT>OAP QEB Q>ODBQP ABMBKAP LK QEB T>V FQ FP JLSFKD FK QEB CFOPQ MI>@B. )K >II LC QEFP BU@FQBJBKQ, ELTBSBO, TBgSB JFPPBA LKB I>PQ PQBM. 7E>Q PLOQ LC SBEF@IB FP QEFP? )P FQ > PRMBO PIBBH O>@B @>O TFQE >J>WFKD E>KAIFKD? /O > DF>KQ ->@H QOR@H QE>Q KBBAP > ILQ LC >AS>K@B KLQF@B QL QROK? ! DO>@BCRI M>KA>, LO > IRJ?BOFKD BIBME>KQ? /RO BU>JMIB @LAB, >P FQ PQ>KAP, E>P KL CB>QROB QL >@@LRKQ CLO QEFP S>OF>?FIFQV FK PQBBOFKD >?FIFQV. 3QBBOFKD >?FIFQV @>K ?B @LKQOLIIBA ?V IFJFQFKD QEB J>DKFQRAB LC QEB PQBBOFKD CLO@B. ,BQgP @>II QE>Q IFJFQ QEB hJ>UFJRJ CLO@Bi (LO G;R@IL=? CLO PELOQ). !KA PL CFK>IIV, TB E>SB:
=F;MM 3?BC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH;

266

4EB .>QROB LC #LAB (S1.0)

@FI;N G;RMJ??>; @FI;N G;R@IL=?;

Maximum speed Now we also have maximum force.

CLIILTBA ?V:
PIC> M??E(.3?=NIL N;LA?N) U .3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>); .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W Limit the magnitude of the steering force.

,FJFQFKD QEB PQBBOFKD CLO@B ?OFKDP RM >K FJMLOQ>KQ MLFKQ. 7B JRPQ >IT>VP OBJBJ?BO QE>Q FQgP KLQ >@QR>IIV LRO DL>I QL DBQ QEB SBEF@IB QL QEB Q>ODBQ >P C>PQ >P MLPPF?IB. )C QE>Q TBOB QEB @>PB, TB TLRIA GRPQ P>V hIL@>QFLK BNR>IP Q>ODBQi >KA QEBOB QEB SBEF@IB TLRIA ?B. /RO DL>I, >P 2BVKLIAP MRQP FQ, FP QL JLSB QEB SBEF@IB FK > hIFCBIFHB >KA FJMOLSFP>QFLK>I J>KKBO.i 7BgOB QOVFKD QL J>HB FQ >MMB>O >P FC QEB SBEF@IB FP PQBBOFKD FQP T>V QL QEB Q>ODBQ, >KA PL FQgP RM QL RP QL MI>V TFQE QEB CLO@BP >KA S>OF>?IBP LC QEB PVPQBJ QL PFJRI>QB > DFSBK ?BE>SFLO. &LO BU>JMIB, > I>ODB J>UFJRJ PQBBOFKD CLO@B TLRIA OBPRIQ FK > SBOV AFCCBOBKQ M>QE QE>K > PJ>II LKB. /KB FP KLQ FKEBOBKQIV ?BQQBO LO TLOPB QE>K QEB LQEBO; FQ ABMBKAP LK VLRO ABPFOBA BCCB@Q. (!KA LC @LROPB, QEBPB S>IRBP KBBA KLQ ?B CFUBA >KA @LRIA @E>KDB ?>PBA LK LQEBO @LKAFQFLKP. 0BOE>MP > SBEF@IB E>P EB>IQE: QEB EFDEBO QEB EB>IQE, QEB ?BQQBO FQ @>K PQBBO.)

F64B?2 6.5

(BOB FP QEB CRII 3?BC=F? @I>PP, FK@LOMLO>QFKD QEB OBPQ LC QEB BIBJBKQP COLJ QEB #E>MQBO 2 +IP?L L?GB@Q.

267

#E>MQBO 6. !RQLKLJLRP !DBKQP

Example 6.1: Seeking a target


=F;MM 3?BC=F? U .3?=NIL FI=;NCIH; .3?=NIL P?FI=CNS; .3?=NIL ;==?F?L;NCIH; @FI;N L; @FI;N G;R@IL=?; @FI;N G;RMJ??>; 3?BC=F?(@FI;N R, ;==?F?L;NCIH = P?FI=CNS = H?Q FI=;NCIH = H?Q L = 3.0; G;RMJ??> = 4; G;R@IL=? = 0.1; W PIC> OJ>;N?() U P?FI=CNS.;>>(;==?F?L;NCIH); P?FI=CNS.FCGCN(G;RMJ??>); FI=;NCIH.;>>(P?FI=CNS); ;==?F?L;NCIH.GOFN(0); W PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U ;==?F?L;NCIH.;>>(@IL=?); W Newtons second law; we could divide by mass if we wanted. Our standard Euler integration motion model @FI;N S) U H?Q .3?=NIL(0,0); .3?=NIL(0,0); .3?=NIL(R,S); Arbitrary values for maxspeed and force; try varying these! Additional variable for size

268

4EB .>QROB LC #LAB (S1.0)

PIC> M??E(.3?=NIL N;LA?N) U .3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>); .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W PIC> >CMJF;S() U @FI;N NB?N; = P?FI=CNS.B?;>CHA() + .'/2; @CFF(175); MNLIE?(0); JOMB+;NLCR(); NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S); LIN;N?(NB?N;); <?ACH0B;J?(); P?LN?R(0, -L*2); P?LN?R(-L, L*2); P?LN?R(L, L*2); ?H>0B;J?(!*-0#); JIJ+;NLCR(); W

Our seek steering force algorithm

Vehicle is a triangle pointing in the direction of velocity; since it is drawn pointing up, we rotate it an additional 90 degrees.

Exercise 6.1
)JMIBJBKQ > hCIBBFKDi PQBBOFKD ?BE>SFLO (ABPFOBA SB@QLO FP FKSBOPB LC hPBBHi).

Exercise 6.2
)JMIBJBKQ PBBHFKD > JLSFKD Q>ODBQ, LCQBK OBCBOOBA QL >P hMROPRFQ.i )K QEFP @>PB, VLRO ABPFOBA SB@QLO TLKgQ MLFKQ QLT>OAP QEB L?GB@QgP @ROOBKQ IL@>QFLK, ?RQ O>QEBO FQP hCRQROBi IL@>QFLK >P BUQO>MLI>QBA COLJ FQP @ROOBKQ SBIL@FQV. 7BgII PBB QEFP >?FIFQV CLO > SBEF@IB QL hMOBAF@Q QEB CRQROBi FK I>QBO BU>JMIBP.

Exercise 6.3
#OB>QB > PHBQ@E TEBOB > SBEF@IBgP J>UFJRJ CLO@B >KA J>UFJRJ PMBBA AL KLQ OBJ>FK @LKPQ>KQ, ?RQ O>QEBO S>OV >@@LOAFKD QL BKSFOLKJBKQ>I C>@QLOP.

269

#E>MQBO 6. !RQLKLJLRP !DBKQP

6.4 Arriving Behavior


!CQBO TLOHFKD CLO > ?FQ TFQE QEB PBBHFKD ?BE>SFLO, VLR MOL?>?IV >OB >PHFKD VLROPBIC, h7E>Q FC ) T>KQ JV SBEF@IB QL PILT ALTK >P FQ >MMOL>@EBP QEB Q>ODBQ?i "BCLOB TB @>K BSBK ?BDFK QL >KPTBO QEFP NRBPQFLK, TB PELRIA ILLH >Q QEB OB>PLKP ?BEFKA TEV QEB PBBH ?BE>SFLO @>RPBP QEB SBEF@IB QL CIV M>PQ QEB Q>ODBQ PL QE>Q FQ E>P QL QROK >OLRKA >KA DL ?>@H. ,BQgP @LKPFABO QEB ?O>FK LC > PBBHFKD SBEF@IB. 7E>Q FP FQ QEFKHFKD? &O>JB &O>JB &O>JB &O>JB &O>JB BQ@. 1: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 2: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 3: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 4: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 5: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!

4EB SBEF@IB FP PL DLPE A>OK BU@FQBA >?LRQ DBQQFKD QL QEB Q>ODBQ QE>Q FQ ALBPKgQ ?LQEBO QL J>HB >KV FKQBIIFDBKQ AB@FPFLKP >?LRQ FQP PMBBA OBI>QFSB QL QEB Q>ODBQgP MOLUFJFQV. 7EBQEBO FQgP C>O >T>V LO SBOV @ILPB, FQ >IT>VP T>KQP QL DL >P C>PQ >P MLPPF?IB.

F64B?2 6.6

)K PLJB @>PBP, QEFP FP QEB ABPFOBA ?BE>SFLO (FC > JFPPFIB FP CIVFKD >Q > Q>ODBQ, FQ PELRIA >IT>VP QO>SBI >Q J>UFJRJ PMBBA.) (LTBSBO, FK J>KV LQEBO @>PBP (> @>O MRIIFKD FKQL > M>OHFKD PMLQ, > ?BB I>KAFKD LK > CILTBO), QEB SBEF@IBgP QELRDEQ MOL@BPP KBBAP QL @LKPFABO FQP PMBBA OBI>QFSB QL QEB AFPQ>K@B COLJ FQP Q>ODBQ. &LO BU>JMIB: &O>JB &O>JB &O>JB &O>JB &O>JB &O>JB 1: )gJ SBOV C>O >T>V. ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 2: )gJ SBOV C>O >T>V. ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 3: )gJ PLJBTE>Q C>O >T>V. ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ! 4: )gJ DBQQFKD @ILPB. ) T>KQ QL DL JLOB PILTIV QLT>OAP QEB Q>ODBQ! 5: )gJ >IJLPQ QEBOB. ) T>KQ QL DL SBOV PILTIV QLT>OAP QEB Q>ODBQ! 6: )gJ QEBOB. ) T>KQ QL PQLM!

F64B?2 6.7

(LT @>K TB FJMIBJBKQ QEFP h>OOFSFKDi ?BE>SFLO FK @LAB? ,BQgP OBQROK QL LRO M??E() CRK@QFLK >KA CFKA QEB IFKB LC @LAB TEBOB TB PBQ QEB J>DKFQRAB LC QEB ABPFOBA SBIL@FQV.

270

4EB .>QROB LC #LAB (S1.0)


.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>);

)K %U>JMIB 6.1, QEB J>DKFQRAB LC QEB ABPFOBA SB@QLO FP >IT>VP hJ>UFJRJi PMBBA.

F64B?2 6.8

7E>Q FC TB FKPQB>A P>FA QEB ABPFOBA SBIL@FQV FP BNR>I QL E>IC QEB AFPQ>K@B?

F64B?2 6.9
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.>CP(2);

7EFIB QEFP KF@BIV ABJLKPQO>QBP LRO DL>I LC > ABPFOBA PMBBA QFBA QL LRO AFPQ>K@B COLJ QEB Q>ODBQ, FQgP KLQ M>OQF@RI>OIV OB>PLK>?IB. !CQBO >II, 10 MFUBIP >T>V FP O>QEBO @ILPB >KA > ABPFOBA PMBBA LC 5 FP O>QEBO I>ODB. 3LJBQEFKD IFHB > ABPFOBA SBIL@FQV TFQE > J>DKFQRAB LC 5% LC QEB AFPQ>K@B TLRIA TLOH JR@E ?BQQBO.

271

#E>MQBO 6. !RQLKLJLRP !DBKQP


.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.GOFN(0.05);

2BVKLIAP ABP@OF?BP > JLOB PLMEFPQF@>QBA >MMOL>@E. ,BQgP FJ>DFKB > @FO@IB >OLRKA QEB Q>ODBQ TFQE > DFSBK O>AFRP. )C QEB SBEF@IB FP TFQEFK QE>Q @FO@IB, FQ PILTP ALTKe>Q QEB BADB LC QEB @FO@IB, FQP ABPFOBA PMBBA FP J>UFJRJ PMBBA, >KA >Q QEB Q>ODBQ FQPBIC, FQP ABPFOBA PMBBA FP 0.

F64B?2 6.10

)K LQEBO TLOAP, FC QEB AFPQ>K@B COLJ QEB Q>ODBQ FP IBPP QE>K O, QEB ABPFOBA PMBBA FP ?BQTBBK 0 >KA J>UFJRJ PMBBA J>MMBA >@@LOAFKD QL QE>Q AFPQ>K@B.

Example 6.2: Arrive steering behavior


PIC> ;LLCP?(.3?=NIL N;LA?N) U .3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); @FI;N > = >?MCL?>.G;A(); >?MCL?>.HILG;FCT?(); The distance is the magnitude of the vector pointing from location to target.

272

4EB .>QROB LC #LAB (S1.0)

C@ (> < 100) U @FI;N G = G;J(>,0,100,0,G;RMJ??>); >?MCL?>.GOFN(G); W ?FM? U >?MCL?>.GOFN(G;RMJ??>); W .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W

If we are closer than 100 pixels... ...set the magnitude according to how close we are.

Otherwise, proceed at maximum speed.

The usual steering = desired - velocity

4EB >OOFSB ?BE>SFLO FP > DOB>Q ABJLKPQO>QFLK LC QEB J>DF@ LC hABPFOBA JFKRP SBIL@FQV.i ,BQgP BU>JFKB QEFP JLABI >D>FK OBI>QFSB QL ELT TB @>I@RI>QBA CLO@BP FK B>OIFBO @E>MQBOP. )K QEB hDO>SFQ>QFLK>I >QQO>@QFLKi BU>JMIBP, QEB CLO@B >IT>VP MLFKQBA AFOB@QIV COLJ QEB L?GB@Q QL QEB Q>ODBQ (QEB BU>@Q AFOB@QFLK LC QEB ABPFOBA SBIL@FQV), TEBQEBO QEB CLO@B T>P PQOLKD LO TB>H. 4EB PQBBOFKD CRK@QFLK, ELTBSBO, P>VP: h) E>SB QEB >?FIFQV QL MBO@BFSB QEB BKSFOLKJBKQ.i 4EB CLO@B FPKgQ ?>PBA LK GRPQ QEB ABPFOBA SBIL@FQV, ?RQ LK QEB ABPFOBA SBIL@FQV OBI>QFSB QL QEB @ROOBKQ SBIL@FQV. /KIV QEFKDP QE>Q >OB >IFSB @>K HKLT QEBFO @ROOBKQ SBIL@FQV. ! ?LU C>IIFKD LCC > Q>?IB ALBPKgQ HKLT FQgP C>IIFKD. ! @EBBQ>E @E>PFKD FQP MOBV, ELTBSBO, HKLTP FQ FP @E>PFKD. 4EB PQBBOFKD CLO@B, QEBOBCLOB, FP BPPBKQF>IIV > J>KFCBPQ>QFLK LC QEB @ROOBKQ SBIL@FQVgP 0==:=: ")gJ PRMMLPBA QL ?B DLFKD QEFP C>PQ FK QEFP AFOB@QFLK, ?RQ )gJ >@QR>IIV DLFKD QEFP C>PQ FK >KLQEBO AFOB@QFLK. -V BOOLO FP QEB AFCCBOBK@B ?BQTBBK TEBOB ) T>KQ QL DL >KA TEBOB ) >J @ROOBKQIV DLFKD." 4>HFKD QE>Q BOOLO >KA >MMIVFKD FQ >P > PQBBOFKD CLO@B OBPRIQP FK JLOB AVK>JF@, IFCBIFHB PFJRI>QFLKP. 7FQE DO>SFQ>QFLK>I >QQO>@QFLK, VLR TLRIA KBSBO E>SB > CLO@B MLFKQFKD >T>V COLJ QEB Q>ODBQ, KL J>QQBO ELT @ILPB. "RQ TFQE >OOFSFKD SF> PQBBOFKD, FC VLR >OB JLSFKD QLL C>PQ QLT>OAP QEB Q>ODBQ, QEB BOOLO TLRIA >@QR>IIV QBII VLR QL PILT ALTK!

F64B?2 6.11

273

#E>MQBO 6. !RQLKLJLRP !DBKQP

6.5 Your Own Desires: Desired Velocity


4EB CFOPQ QTL BU>JMIBP TBgSB @LSBOBAePBBH >KA >OOFSBe?LFI ALTK QL @>I@RI>QFKD > PFKDIB SB@QLO CLO B>@E ?BE>SFLO: QEB 89G=F98 SBIL@FQV. !KA FK C>@Q, BSBOV PFKDIB LKB LC 2BVKLIAPgP PQBBOFKD ?BE>SFLOP CLIILTP QEFP P>JB M>QQBOK. )K QEFP @E>MQBO, TBgOB DLFKD QL T>IH QEOLRDE PBSBO>I JLOB LC 2BVKLIAPgP ?BE>SFLOPeCILT CFBIA, M>QE-CLIILTFKD, CIL@HFKD. &FOPQ, ELTBSBO, ) T>KQ QL BJME>PFWB >D>FK QE>Q QEBPB >OB 9L5AD@9GeABJLKPQO>QFLKP LC @LJJLK PQBBOFKD ?BE>SFLOP QE>Q >OB RPBCRI FK MOL@BARO>I >KFJ>QFLK. 4EBV >OB KLQ QEB ?B->II >KA BKA->II LC TE>Q MCI @>K AL. !P ILKD >P VLR @>K @LJB RM TFQE > SB@QLO QE>Q ABP@OF?BP > SBEF@IBgP 89G=F98 SBIL@FQV, QEBK VLR E>SB @OB>QBA VLRO LTK PQBBOFKD ?BE>SFLO. ,BQgP PBB ELT 2BVKLIAP ABCFKBP QEB ABPFOBA SBIL@FQV CLO EFP T>KABOFKD ?BE>SFLO.
I,.;12?6;4 6@ . AF=2 <3 ?.;1<: @A22?6;4 D5605 5.@ @<:2 9<;4 A2?: <?12?: A52 @A22?6;4 16?20A6<; <; <;2 3?.:2 6@ ?29.A21 A< A52 @A22?6;4 16?20A6<; <; A52 ;2EA 3?.:2. *56@ =?<1B02@ :<?2 6;A2?2@A6;4 :<A6<; A5.;, 3<? 2E.:=92, @6:=9F 42;2?.A6;4 . ?.;1<: @A22?6;4 16?20A6<; 2.05 3?.:2.J HC?.64 (2F;<91@ (5AA=://DDD.?2131.0<:/0D?/@A22?/,.;12?.5A:9)

&LO 2BVKLIAP, QEB DL>I LC T>KABOFKD FP KLQ PFJMIV O>KALJ JLQFLK, ?RQ O>QEBO > PBKPB LC JLSFKD FK LKB AFOB@QFLK CLO > IFQQIB TEFIB, T>KABOFKD LCC QL QEB KBUQ CLO > IFQQIB ?FQ, >KA PL LK >KA PL CLOQE. 3L ELT ALBP 2BVKLIAP @>I@RI>QB > ABPFOBA SB@QLO QL >@EFBSB PR@E >K BCCB@Q? &FDROB 6.12 FIIRPQO>QBP ELT QEB SBEF@IB MOBAF@QP FQP CRQROB IL@>QFLK >P > CFUBA AFPQ>K@B FK COLKQ LC FQ (FK QEB AFOB@QFLK LC FQP SBIL@FQV), AO>TP > @FO@IB TFQE O>AFRP L >Q QE>Q IL@>QFLK, >KA MF@HP > O>KALJ MLFKQ >ILKD F64B?2 6.12 QEB @FO@RJCBOBK@B LC QEB @FO@IB. 4E>Q O>KALJ MLFKQ JLSBP O>KALJIV >OLRKA QEB @FO@IB FK B>@E CO>JB LC >KFJ>QFLK. !KA QE>Q O>KALJ MLFKQ FP QEB SBEF@IBgP Q>ODBQ, FQP ABPFOBA SB@QLO MLFKQFKD FK QE>Q AFOB@QFLK. 3LRKAP > ?FQ >?PROA, OFDEQ? /O, >Q QEB SBOV IB>PQ, O>QEBO >O?FQO>OV. )K C>@Q, QEFP FP > SBOV @IBSBO >KA QELRDEQCRI PLIRQFLKeFQ RPBP O>KALJKBPP QL AOFSB > SBEF@IBgP PQBBOFKD, ?RQ @LKPQO>FKP QE>Q O>KALJKBPP >ILKD QEB M>QE LC > @FO@IB QL HBBM QEB SBEF@IBgP JLSBJBKQ COLJ >MMB>OFKD GFQQBOV, >KA, TBII, O>KALJ. "RQ QEB PBBJFKDIV O>KALJ >KA >O?FQO>OV K>QROB LC QEFP PLIRQFLK PELRIA AOFSB ELJB QEB MLFKQ )gJ QOVFKD QL J>HBeQEBPB >OB J>AB-RM ?BE>SFLOP FKPMFOBA ?V OB>I-IFCB JLQFLK. 9LR @>K GRPQ >P B>PFIV @LK@L@Q PLJB BI>?LO>QB P@BK>OFL QL @LJMRQB > ABPFOBA SBIL@FQV VLROPBIC. !KA VLR PELRIA.

274

4EB .>QROB LC #LAB (S1.0)

Exercise 6.4
7OFQB QEB @LAB CLO 2BVKLIAPgP T>KABOFKD ?BE>SFLO. 5PB MLI>O @LLOAFK>QBP QL @>I@RI>QB QEB SBEF@IBgP Q>ODBQ >ILKD > @FO@RI>O M>QE.

,BQgP P>V TB T>KQ QL @OB>QB > PQBBOFKD ?BE>SFLO @>IIBA hPQ>V TFQEFK T>IIP.i 7BgII ABCFKB QEB ABPFOBA SBIL@FQV >P: I1 , A034.70 .:80> B4?349 , /4>?,9.0 A :1 , B,77, 4? /0>4=0> ?: 8:A0 ,? 8,C48@8 >;00/ 49 ?30 :;;:>4?0 /4=0.?4:9 :1 ?30 B,77.

F64B?2 6.13

)C TB ABCFKB QEB T>IIP LC QEB PM>@B >P QEB BADBP LC > 0OL@BPPFKD TFKALT >KA QEB AFPQ>K@B > >P 25, QEB @LAB FP O>QEBO PFJMIB.

275

#E>MQBO 6. !RQLKLJLRP !DBKQP

Example 6.3: Stay within walls steering behavior


C@ (FI=;NCIH. R > 25) U .3?=NIL >?MCL?> = H?Q .3?=NIL(G;RMJ??>,P?FI=CNS.S); .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>, P?FI=CNS); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W Make a desired vector that retains the y direction of the vehicle but points the x direction directly away from the windows left edge.

Exercise 6.5
#LJB RM TFQE VLRO LTK >O?FQO>OV P@EBJB CLO @>I@RI>QFKD > ABPFOBA SBIL@FQV.

6.6 Flow Fields


.LT ?>@H QL QEB Q>PH >Q E>KA. ,BQgP BU>JFKB > @LRMIB JLOB LC 2BVKLIAPgP PQBBOFKD ?BE>SFLOP. &FOPQ, 17:B 1407/ 1:77:B492. 7E>Q FP > CILT CFBIA? 4EFKH LC VLRO 0OL@BPPFKD TFKALT >P > DOFA. )K B>@E @BII LC QEB DOFA IFSBP >K >OOLT MLFKQFKD FK PLJB AFOB@QFLKeVLR HKLT, > SB@QLO. !P > SBEF@IB JLSBP >OLRKA QEB P@OBBK, FQ >PHP, h(BV, TE>Q >OOLT FP ?BKB>QE JB? 4E>QgP JV ABPFOBA SBIL@FQV!i

276

4EB .>QROB LC #LAB (S1.0)

F64B?2 6.14

2BVKLIAPgP CILT CFBIA CLIILTFKD BU>JMIB E>P QEB SBEF@IB MOBAF@QFKD FQP CRQROB IL@>QFLK >KA CLIILTFKD QEB SB@QLO >Q QE>Q PMLQ, ?RQ CLO PFJMIF@FQVgP P>HB, TBgII E>SB QEB SBEF@IB PFJMIV ILLH QL QEB SB@QLO >Q FQP @ROOBKQ IL@>QFLK. "BCLOB TB @>K TOFQB QEB >AAFQFLK>I @LAB CLO LRO 3?BC=F? @I>PP, TBgII KBBA QL ?RFIA > @I>PP QE>Q ABP@OF?BP QEB CILT CFBIA FQPBIC, QEB DOFA LC SB@QLOP. ! QTL-AFJBKPFLK>I >OO>V FP > @LKSBKFBKQ A>Q> PQOR@QROB FK TEF@E QL PQLOB > DOFA LC FKCLOJ>QFLK. )C VLR >OB KLQ C>JFIF>O TFQE 2$ >OO>VP, ) PRDDBPQ OBSFBTFKD QEFP LKIFKB 0OL@BPPFKD QRQLOF>I: 2$ >OO>V (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/ 2A>OO>V/). 4EB 2$ >OO>V FP @LKSBKFBKQ ?B@>RPB TB OBCBOBK@B B>@E BIBJBKQ TFQE QTL FKAF@BP, TEF@E TB @>K QEFKH LC >P @LIRJKP >KA OLTP.
=F;MM $FIQ$C?F> U .3?=NIL8989 @C?F>; CHN =IFM, LIQM; Declaring a 2D array of PVectors How many columns and how many rows in the grid? Resolution of grid relative to window width and height in pixels

CHN L?MIFONCIH;

.LQF@B ELT TB >OB ABCFKFKD > QEFOA S>OF>?IB @>IIBA L?MIFONCIH >?LSB. 7E>Q FP QEFP S>OF>?IB? ,BQgP P>V TB E>SB > 0OL@BPPFKD TFKALT QE>Q FP 200 MFUBIP TFAB ?V 200 MFUBIP EFDE. 7B @LRIA J>HB > CILT CFBIA QE>Q E>P > .3?=NIL L?GB@Q CLO BSBOV PFKDIB MFUBI, LO 40,000 .3?=NIL P (200 * 200). 4EFP FPKgQ QBOOF?IV RKOB>PLK>?IB, ?RQ FK LRO @>PB, FQgP LSBOHFII. 7B ALKgQ KBBA > .3?=NIL

277

#E>MQBO 6. !RQLKLJLRP !DBKQP

CLO BSBOV PFKDIB MFUBI; TB @>K >@EFBSB QEB P>JB BCCB@Q ?V E>SFKD, P>V, LKB BSBOV QBK MFUBIP (20 * 20 = 400). 7B RPB QEFP OBPLIRQFLK QL ABCFKB QEB KRJ?BO LC @LIRJKP >KA OLTP ?>PBA LK QEB PFWB LC QEB TFKALT AFSFABA ?V OBPLIRQFLK:
$FIQ$C?F>() U L?MIFONCIH = 10; =IFM = QC>NB/L?MIFONCIH; Total columns equals width divided by resolution. Total rows equals height divided by resolution.

LIQM = B?CABN/L?MIFONCIH; @C?F> = H?Q .3?=NIL8=IFM98LIQM9; W

.LT QE>Q TBgSB PBQ RM QEB CILT CFBIAgP A>Q> PQOR@QROBP, FQgP QFJB QL @LJMRQB QEB SB@QLOP FK QEB CILT CFBIA FQPBIC. (LT AL TB AL QE>Q? (LTBSBO TB CBBI IFHB FQ! 0BOE>MP TB T>KQ QL E>SB BSBOV SB@QLO FK QEB CILT CFBIA MLFKQFKD QL QEB OFDEQ.

F64B?2 6.15

@IL (CHN C = 0; C < =IFM; C++) U @IL (CHN D = 0; D < LIQM; D++) U @C?F>8C98D9 = H?Q .3?=NIL(1,0); W W

Using a nested loop to hit every column and every row of the flow field Arbitrary decision to make each vector point to the right

/O MBOE>MP TB T>KQ QEB SB@QLOP QL MLFKQ FK O>KALJ AFOB@QFLKP.

278

4EB .>QROB LC #LAB (S1.0)

F64B?2 6.16
@IL (CHN C = 0; C < =IFM; C++) U @IL (CHN D = 0; D < LIQM; D++) U @C?F>8C98D9 = .3?=NIL.2"(); W W A random PVector

7E>Q FC TB RPB 2$ 0BOIFK KLFPB (J>MMBA QL >K >KDIB)?

F64B?2 6.17
@FI;N RI@@ = @IL (CHN C = @FI;N SI@@ @IL (CHN D 0; 0; C < =IFM; C++) U = 0; = 0; D < LIQM; D++) U

Noise

@FI;N NB?N; = G;J(HICM?(RI@@,SI@@),0,1,0,14-:.'); @C?F>8C98D9 = H?Q .3?=NIL(=IM(NB?N;),MCH(NB?N;)); SI@@ += 0.1; W RI@@ += 0.1; W

279

#E>MQBO 6. !RQLKLJLRP !DBKQP

.LT TBgOB DBQQFKD PLJBTEBOB. &ILT CFBIAP @>K ?B RPBA CLO PFJRI>QFKD S>OFLRP BCCB@QP, PR@E >P >K FOOBDRI>O DRPQ LC TFKA LO QEB JB>KABOFKD M>QE LC > OFSBO. #>I@RI>QFKD QEB AFOB@QFLK LC VLRO SB@QLOP RPFKD 0BOIFK KLFPB FP LKB T>V QL >@EFBSB PR@E >K BCCB@Q. /C @LROPB, QEBOBgP KL h@LOOB@Qi T>V QL @>I@RI>QB QEB SB@QLOP LC > CILT CFBIA; FQgP OB>IIV RM QL VLR QL AB@FAB TE>Q VLRgOB ILLHFKD QL PFJRI>QB.

Exercise 6.6
7OFQB QEB @LAB QL @>I@RI>QB > .3?=NIL >Q BSBOV IL@>QFLK FK QEB CILT CFBIA QE>Q MLFKQP QLT>OAP QEB @BKQBO LC > TFKALT.

.3?=NIL P = H?Q .3?=NIL(::::::::::::,::::::::::::); P.::::::::::::::(); @C?F>8C98D9 = P;

.LT QE>Q TB E>SB > QTL-AFJBKPFLK>I >OO>V PQLOFKD >II LC QEB CILT CFBIA SB@QLOP, TB KBBA > T>V CLO > SBEF@IB QL ILLH RM FQP ABPFOBA SB@QLO FK QEB CILT CFBIA. ,BQgP P>V TB E>SB > SBEF@IB QE>Q IFSBP >Q > .3?=NIL : FQP IL@>QFLK. 7B CFOPQ KBBA QL AFSFAB ?V QEB OBPLIRQFLK LC QEB DOFA. &LO BU>JMIB, FC QEB OBPLIRQFLK FP 10 >KA QEB SBEF@IB FP >Q (100,50), TB KBBA QL ILLH RM @LIRJK 10 >KA OLT 5.
CHN =IFOGH = CHN(FI=;NCIH.R/L?MIFONCIH); CHN LIQ = CHN(FI=;NCIH.S/L?MIFONCIH);

"B@>RPB > SBEF@IB @LRIA QEBLOBQF@>IIV T>KABO LCC QEB 0OL@BPPFKD TFKALT, FQgP >IPL RPBCRI CLO RP QL BJMILV QEB =IHMNL;CH() CRK@QFLK QL J>HB PROB TB ALKgQ ILLH LRQPFAB LC QEB CILT CFBIA >OO>V. (BOB FP > CRK@QFLK TBgII @>II FIIEOJ() QE>Q DLBP FK QEB $FIQ$C?F> @I>PPeFQ OB@BFSBP > .3?=NIL (MOBPRJ>?IV QEB IL@>QFLK LC LRO SBEF@IB) >KA OBQROKP QEB @LOOBPMLKAFKD CILT CFBIA .3?=NIL CLO QE>Q IL@>QFLK.
.3?=NIL FIIEOJ(.3?=NIL FIIEOJ) U Using constrain() CHN =IFOGH = CHN(=IHMNL;CH(FIIEOJ.R/L?MIFONCIH,0,=IFM-1)); CHN LIQ = CHN(=IHMNL;CH(FIIEOJ.S/L?MIFONCIH,0,LIQM-1));

280

4EB .>QROB LC #LAB (S1.0)

L?NOLH @C?F>8=IFOGH98LIQ9.A?N(); W

Note the use of get() to ensure we return a copy of the PVector.

"BCLOB TB JLSB LK QL QEB 3?BC=F? @I>PP, IBQgP Q>HB > ILLH >Q QEB $FIQ$C?F> @I>PP >II QLDBQEBO.
=F;MM $FIQ$C?F> U .3?=NIL8989 @C?F>; CHN =IFM, LIQM; CHN L?MIFONCIH; $FIQ$C?F>(CHN L) U L?MIFONCIH = L; =IFM = QC>NB/L?MIFONCIH; LIQM = B?CABN/L?MIFONCIH; @C?F> = H?Q .3?=NIL8=IFM98LIQM9; CHCN(); W PIC> CHCN() U @FI;N RI@@ = @IL (CHN C = @FI;N SI@@ @IL (CHN D Determine the number of columns and rows. A flow field is a two-dimensional array of PVectors.

0; 0; C < =IFM; C++) U = 0; = 0; D < LIQM; D++) U

In this example, we use Perlin noise to seed the vectors.

@FI;N NB?N; = G;J(HICM?(RI@@,SI@@),0,1,0,14-:.'); @C?F>8C98D9 = H?Q .3?=NIL(=IM(NB?N;),MCH(NB?N;)); SI@@ += 0.1; W RI@@ += 0.1; W W .3?=NIL FIIEOJ(.3?=NIL FIIEOJ) U A function to return a PVector based on a location CHN =IFOGH = CHN(=IHMNL;CH(FIIEOJ.R/L?MIFONCIH,0,=IFM-1)); CHN LIQ = CHN(=IHMNL;CH(FIIEOJ.S/L?MIFONCIH,0,LIQM-1)); L?NOLH @C?F>8=IFOGH98LIQ9.A?N(); Polar to Cartesian coordinate transformation to get x and y components of the vector

W W

3L IBQgP >PPRJB TB E>SB > $FIQ$C?F> L?GB@Q @>IIBA hCILTi. 5PFKD QEB FIIEOJ() CRK@QFLK >?LSB, LRO SBEF@IB @>K QEBK OBQOFBSB > ABPFOBA SB@QLO COLJ QEB CILT CFBIA >KA RPB 2BVKLIAPgP ORIBP (PQBBOFKD = ABPFOBA - SBIL@FQV) QL @>I@RI>QB > PQBBOFKD CLO@B.

281

#E>MQBO 6. !RQLKLJLRP !DBKQP

Example 6.4: Flow field following


=F;MM 3?BC=F? U PIC> @IFFIQ($FIQ$C?F> @FIQ) U .3?=NIL >?MCL?> = @FIQ.FIIEOJ(FI=;NCIH); >?MCL?>.GOFN(G;RMJ??>); .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>, P?FI=CNS); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W What is the vector at that spot in the flow field?

Steering is desired minus velocity

Exercise 6.7
!A>MQ QEB CILT CFBIA BU>JMIB PL QE>Q QEB .3?=NIL P @E>KDB LSBO QFJB. ((FKQ: QOV RPFKD QEB QEFOA AFJBKPFLK LC 0BOIFK KLFPB!)

Exercise 6.8
#>K VLR PBBA > CILT CFBIA COLJ > .'G;A? ? &LO BU>JMIB, QOV E>SFKD QEB .3?=NIL P MLFKQ COLJ A>OH QL IFDEQ @LILOP (LO SF@B SBOP>).

6.7 The Dot Product


)K > JLJBKQ, TBgOB DLFKD QL TLOH QEOLRDE QEB >IDLOFQEJ (>ILKD TFQE >@@LJM>KVFKD J>QEBJ>QF@P) >KA @LAB CLO >KLQEBO LC #O>FD 2BVKLIAPgP PQBBOFKD ?BE>SFLOP: 0>QE &LIILTFKD (EQQM://TTT.OBA3A.@LJ/@TO/PQBBO/0>QE&LIILT.EQJI). "BCLOB TB @>K AL QEFP, ELTBSBO, TB E>SB

282

4EB .>QROB LC #LAB (S1.0)

QL PMBKA PLJB QFJB IB>OKFKD >?LRQ >KLQEBO MFB@B LC SB@QLO J>QE QE>Q TB PHFMMBA FK #E>MQBO 1eQEB ALQ MOLAR@Q. 7B E>SBKgQ KBBABA FQ VBQ, ?RQ FQgP IFHBIV DLFKD QL MOLSB NRFQB RPBCRI CLO VLR (?BVLKA GRPQ QEFP M>QE-CLIILTFKD BU>JMIB), PL TBgII DL LSBO FQ FK ABQ>FI KLT. 2BJBJ?BO >II QEB ?>PF@ SB@QLO J>QE TB @LSBOBA FK #E>MQBO 1? !AA, PR?QO>@Q, JRIQFMIV, >KA AFSFAB?

F64B?2 6.18

.LQF@B ELT FK QEB >?LSB AF>DO>J, SB@QLO JRIQFMIF@>QFLK FKSLISBP JRIQFMIVFKD > SB@QLO ?V > P@>I>O S>IRB. 4EFP J>HBP PBKPB; TEBK TB T>KQ > SB@QLO QL ?B QTF@B >P I>ODB (?RQ C>@FKD QEB P>JB AFOB@QFLK), TB JRIQFMIV FQ ?V 2. 7EBK TB T>KQ FQ QL ?B E>IC QEB PFWB, TB JRIQFMIV FQ ?V 0.5. (LTBSBO, QEBOB >OB QTL LQEBO AI@H=D@=75H=CB-@=?9 LMBO>QFLKP TFQE SB@QLOP QE>Q >OB RPBCRI FK @BOQ>FK P@BK>OFLPeQEB ALQ MOLAR@Q >KA QEB @OLPP MOLAR@Q. &LO KLT TBgOB DLFKD QL CL@RP LK QEB ALQ MOLAR@Q, TEF@E FP ABCFKBA >P CLIILTP. !PPRJB SB@QLOP A >KA B :

= (ax, a y) B = (bx, b y)
A

4(% $/4 02/$5#4: A B = ax bx + a y b y &LO BU>JMIB, FC TB E>SB QEB CLIILTFKD QTL SB@QLOP:

= (3, 5) = (10, 1) B = 3 * 10 + 5 * 1 = 30 + 5 = 35

.LQF@B QE>Q QEB OBPRIQ LC QEB ALQ MOLAR@Q FP > P@>I>O S>IRB (> PFKDIB KRJ?BO) >KA KLQ > SB@QLO. )K 0OL@BPPFKD, QEFP TLRIA QO>KPI>QB QL:

283

#E>MQBO 6. !RQLKLJLRP !DBKQP


.3?=NIL ; = H?Q .3?=NIL(-3,5); .3?=NIL < = H?Q .3?=NIL(10,1); @FI;N H = ;.>IN(<); The PVector class includes a function to calculate the dot product.

!KA FC TB TBOB QL ILLH FK QEB DRQP LC QEB .3?=NIL PLRO@B, TBgA CFKA > MOBQQV PFJMIB FJMIBJBKQ>QFLK LC QEFP CRK@QFLK:
JO<FC= @FI;N >IN(.3?=NIL P) U L?NOLH R*P.R + S*P.S + T*P.T; W

4EFP FP PFJMIB BKLRDE, ?RQ TEV AL TB KBBA QEB ALQ MOLAR@Q, >KA TEBK FP FQ DLFKD QL ?B RPBCRI CLO RP FK @LAB? /KB LC QEB JLOB @LJJLK RPBP LC QEB ALQ MOLAR@Q FP QL CFKA QEB >KDIB ?BQTBBK QTL SB@QLOP. !KLQEBO T>V FK TEF@E QEB ALQ MOLAR@Q @>K ?B BUMOBPPBA FP:

B = A B cos()

)K LQEBO TLOAP, ! ALQ " FP BNR>I QL QEB J>DKFQRAB LC ! QFJBP J>DKFQRAB LC " QFJBP @LPFKB LC QEBQ> (TFQE QEBQ> ABCFKBA >P H<9 5B;@9 69HK99B H<9 HKC J97HCFG A 5B8 B ). 4EB QTL CLOJRI>P CLO ALQ MOLAR@Q @>K ?B ABOFSBA COLJ LKB >KLQEBO TFQE QOFDLKLJBQOV (EQQM://J>QETLOIA.TLICO>J.@LJ/$LQ0OLAR@Q.EQJI), ?RQ CLO LRO MROMLPBP TB @>K ?B E>MMV TFQE LMBO>QFKD LK QEB >PPRJMQFLK QE>Q:
B = A B cos() A B = ax bx + a y b y

?LQE ELIA QORB >KA QEBOBCLOB:


ax bx + a y b y = A B cos()

.LT, IBQgP PQ>OQ TFQE QEB CLIILTFKD MOL?IBJ. 7B E>SB QEB SB@QLOP ! >KA ":

= (10, 2) = (4, 3)

7B KLT E>SB > PFQR>QFLK FK TEF@E TB HKLT BSBOVQEFKD BU@BMQ CLO QEBQ>. 7B HKLT QEB @LJMLKBKQP LC QEB SB@QLO >KA @>K @>I@RI>QB QEB J>DKFQRAB LC B>@E SB@QLO. 7B @>K QEBOBCLOB PLISB CLO @LPFKB LC QEBQ>:
cos() = ( A B)/(AB)

F64B?2 6.19

284

4EB .>QROB LC #LAB (S1.0)

4L PLISB CLO QEBQ>, TB @>K Q>HB QEB FKSBOPB @LPFKB (LCQBK BUMOBPPBA >P 7CG=B9-1 LO 5F77CG=B9).
= cos1 ( ( A B)/(AB))

,BQgP KLT AL QEB J>QE TFQE >@QR>I KRJ?BOP:


A B

= 10.2 =5

4EBOBCLOB:

= cos1 ( ( 10 4 + 2 -3 ) / ( 10.2 5 ) ) = cos1 ( 34 / 51 ) = 48


4EB 0OL@BPPFKD SBOPFLK LC QEFP TLRIA ?B:
.3?=NIL ; = H?Q .3?=NIL(10,2); .3?=NIL < = H?Q .3?=NIL(4,-3); @FI;N NB?N; = ;=IM(;.>IN(<) / (;.G;A() * <.G;A()));

!KA, >D>FK, FC TB TBOB QL AFD FKQL QEB DRQP LC QEB 0OL@BPPFKD PLRO@B @LAB, TB TLRIA PBB > CRK@QFLK QE>Q FJMIBJBKQP QEFP BU>@Q >IDLOFQEJ.
MN;NC= JO<FC= @FI;N ;HAF? ?NQ??H(.3?=NIL P1, .3?=NIL P2) U @FI;N >IN = P1.>IN(P2); @FI;N NB?N; = (@FI;N) +;NB.;=IM(>IN / (P1.G;A() * P2.G;A())); L?NOLH NB?N;; W

Exercise 6.9
#OB>QB > PHBQ@E QE>Q AFPMI>VP QEB >KDIB ?BQTBBK QTL .3?=NIL L?GB@QP.

! @LRMIB QEFKDP QL KLQB EBOB:

285

#E>MQBO 6. !RQLKLJLRP !DBKQP

1. )C QTL SB@QLOP ( A >KA B ) >OB LOQELDLK>I (F.B. MBOMBKAF@RI>O), QEB ALQ MOLAR@Q ( A B ) FP BNR>I QL 0. 2. )C QTL SB@QLOP >OB RKFQ SB@QLOP, QEBK QEB ALQ MOLAR@Q FP PFJMIV BNR>I QL @LPFKB LC QEB >KDIB ?BQTBBK QEBJ, F.B. A B = cos() FC A >KA B >OB LC IBKDQE 1.

6.8 Path Following


.LT QE>Q TBgSB DLQ > ?>PF@ RKABOPQ>KAFKD LC QEB ALQ MOLAR@Q RKABO LRO ?BIQ, TB @>K OBQROK QL > AFP@RPPFLK LC #O>FD 2BVKLIAPgP M>QE-CLIILTFKD >IDLOFQEJ. ,BQgP NRF@HIV @I>OFCV PLJBQEFKD. 7B >OB Q>IHFKD >?LRQ M>QE :C@@CK=B; , KLQ M>QE :=B8=B; . 0>QECFKAFKD OBCBOP QL > OBPB>O@E QLMF@ (@LJJLKIV PQRAFBA FK >OQFCF@F>I FKQBIIFDBK@B) QE>Q FKSLISBP PLISFKD CLO QEB PELOQBPQ AFPQ>K@B ?BQTBBK QTL MLFKQP, LCQBK FK > J>WB. 7FQE ;,?3 1:77:B492, QEB M>QE >IOB>AV BUFPQP >KA TBgOB >PHFKD > SBEF@IB QL CLIILT QE>Q M>QE. "BCLOB TB TLOH LRQ QEB FKAFSFAR>I MFB@BP, IBQgP Q>HB > ILLH >Q QEB LSBO>II >IDLOFQEJ CLO M>QE CLIILTFKD, >P ABCFKBA ?V 2BVKLIAP.

F64B?2 6.20

7BgII CFOPQ ABCFKB TE>Q TB JB>K ?V > M>QE. 4EBOB >OB J>KV T>VP TB @LRIA FJMIBJBKQ > M>QE, ?RQ CLO RP, > PFJMIB T>V TFII ?B QL ABCFKB > M>QE >P > PBOFBP LC @LKKB@QBA MLFKQP:

286

4EB .>QROB LC #LAB (S1.0)

F64B?2 6.21: '.A5

!K BSBK PFJMIBO M>QE TLRIA ?B > IFKB ?BQTBBK QTL MLFKQP.

F64B?2 6.22: )6:=92 =.A5

7BgOB >IPL DLFKD QL @LKPFABO > M>QE QL E>SB > O>AFRP. )C TB QEFKH LC QEB M>QE >P > OL>A, QEB O>AFRP ABQBOJFKBP QEB OL>AgP TFAQE. 7FQE > PJ>IIBO O>AFRP, LRO SBEF@IBP TFII E>SB QL CLIILT QEB M>QE JLOB @ILPBIV; > TFABO O>AFRP TFII >IILT QEBJ QL PQO>V > ?FQ JLOB. 0RQQFKD QEFP FKQL > @I>PP, TB E>SB:

=F;MM .;NB U .3?=NIL MN;LN; .3?=NIL ?H>; A path is only two points, start and end.

@FI;N L;>COM; .;NB() U

A path has a radius, i.e. how wide it is.

287

#E>MQBO 6. !RQLKLJLRP !DBKQP

L;>COM = 20; MN;LN = H?Q .3?=NIL(0,B?CABN/3); ?H> = H?Q .3?=NIL(QC>NB,2*B?CABN/3); W PIC> >CMJF;S() U // "CMJF;S NB? J;NB. MNLIE?4?CABN(L;>COM*2); MNLIE?(0,100); FCH?(MN;LN.R,MN;LN.S,?H>.R,?H>.S); MNLIE?4?CABN(1); MNLIE?(0); FCH?(MN;LN.R,MN;LN.S,?H>.R,?H>.S); W W

Picking some arbitrary values to initialize the path

.LT, IBQgP >PPRJB TB E>SB > SBEF@IB (>P ABMF@QBA ?BILT) LRQPFAB LC QEB M>QEgP O>AFRP, JLSFKD TFQE > SBIL@FQV.

F64B?2 6.23

4EB CFOPQ QEFKD TB T>KQ QL AL FP MOBAF@Q, >PPRJFKD > @LKPQ>KQ SBIL@FQV, TEBOB QE>Q SBEF@IB TFII ?B FK QEB CRQROB.
.3?=NIL JL?>C=N = P?F.A?N(); Start by making a copy of the velocity.

JL?>C=N.HILG;FCT?(); JL?>C=N.GOFN(25);

Normalize it and look 25 pixels ahead by scaling the vector up.

.3?=NIL JL?>C=N*I= = .3?=NIL.;>>(FI=, JL?>C=N);

Add vector to location to find the predicted location.

/K@B TB E>SB QE>Q IL@>QFLK, FQgP KLT LRO GL? QL CFKA LRQ QEB SBEF@IBgP @ROOBKQ AFPQ>K@B COLJ QEB M>QE LC QE>Q MOBAF@QBA IL@>QFLK. )C FQgP SBOV C>O >T>V, TBII, QEBK, TBgSB PQO>VBA COLJ QEB M>QE >KA KBBA QL PQBBO ?>@H QLT>OAP FQ. )C FQgP @ILPB, QEBK TBgOB ALFKD /+ >KA >OB CLIILTFKD QEB M>QE KF@BIV.

288

4EB .>QROB LC #LAB (S1.0)

3L, ELT AL TB CFKA QEB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB? 4EFP @LK@BMQ FP HBV. 4EB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB FP ABCFKBA >P QEB IBKDQE LC QEB KLOJ>I ?BQTBBK QE>Q MLFKQ >KA IFKB. 4EB KLOJ>I FP > SB@QLO QE>Q BUQBKAP COLJ QE>Q MLFKQ >KA FP MBOMBKAF@RI>O QL QEB IFKB.

F64B?2 6.24

,BQgP CFDROB LRQ TE>Q TB AL HKLT. 7B HKLT TB E>SB > SB@QLO (@>II FQ A) QE>Q BUQBKAP COLJ QEB M>QEgP PQ>OQFKD MLFKQ QL QEB SBEF@IBgP MOBAF@QBA IL@>QFLK.
.3?=NIL ; = .3?=NIL.MO<(JL?>C=N*I=,J;NB.MN;LN);

7B >IPL HKLT QE>Q TB @>K ABCFKB > SB@QLO (@>II FQ B ) QE>Q MLFKQP COLJ QEB PQ>OQ LC QEB M>QE QL QEB BKA.
.3?=NIL < = .3?=NIL.MO<(J;NB.?H>,J;NB.MN;LN);

.LT, TFQE ?>PF@ QOFDLKLJBQOV, TB HKLT QE>Q QEB AFPQ>K@B COLJ QEB M>QEgP PQ>OQ QL QEB KLOJ>I MLFKQ FP: RAR * 9EI(J>;J7) .

F64B?2 6.25

)C TB HKBT QEBQ>, TB @LRIA B>PFIV ABCFKB QE>Q KLOJ>I MLFKQ >P CLIILTP:

289

#E>MQBO 6. !RQLKLJLRP !DBKQP

@FI;N > = ;.G;A()*=IM(NB?N;); <.HILG;FCT?(); <.GOFN(>); .3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);

The distance from START to NORMAL

Scale PVector b to that distance. The normal point can be found by adding the scaled version of b to the paths starting point.

!KA FC QEB ALQ MOLAR@Q E>P Q>RDEQ RP >KVQEFKD, FQgP QE>Q DFSBK QTL SB@QLOP, TB @>K DBQ QEBQ>, QEB >KDIB ?BQTBBK.
@FI;N NB?N; = .3?=NIL.;HAF? ?NQ??H(;,<); <.HILG;FCT?(); <.GOFN(;.G;A()*=IM(NB?N;)); .3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<); What is theta? The angle between A and B

7EFIB QEB >?LSB @LAB TFII TLOH, QEBOBgP LKB JLOB PFJMIFCF@>QFLK TB @>K J>HB. )C VLRgII KLQF@B, QEB ABPFOBA J>DKFQRAB CLO SB@QLO B FP:
7.C7=()*9EI(J>;J7)

TEF@E FP QEB @LAB QO>KPI>QFLK LC:


A cos()

!KA FC VLR OB@>II:

B = A B cos()

.LT, TE>Q FC SB@QLO B FP > RKFQ SB@QLO, F.B. IBKDQE 1? 4EBK:

B = A 1 cos()

LO

B = A cos()

!KA TE>Q >OB TB ALFKD FK LRO @LAB? .LOJ>IFWFKD ?!


<.HILG;FCT?();

"B@>RPB LC QEFP C>@Q, TB @>K PFJMIFCV LRO @LAB >P:


@FI;N NB?N; = .3?=NIL.;HAF? ?NQ??H(;,<); <.HILG;FCT?();

290

4EB .>QROB LC #LAB (S1.0)

<.GOFN(;.>IN(<)); .3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);

We can use the dot product to scale bs length.

4EFP MOL@BPP FP @LJJLKIV HKLTK >P hP@>I>O MOLGB@QFLK.i FAF .:>(G) 4> ?30 >.,7,= ;=:50.?4:9 :1 A :9?: B.

F64B?2 6.26

/K@B TB E>SB QEB KLOJ>I MLFKQ >ILKD QEB M>QE, TB E>SB QL AB@FAB TEBQEBO QEB SBEF@IB PELRIA PQBBO QLT>OAP QEB M>QE >KA ELT. 2BVKLIAPgP >IDLOFQEJ PQ>QBP QE>Q QEB SBEF@IB PELRIA LKIV PQBBO QLT>OAP QEB M>QE FC FQ PQO>VP ?BVLKA QEB M>QE (F.B., FC QEB AFPQ>K@B ?BQTBBK QEB KLOJ>I MLFKQ >KA QEB MOBAF@QBA CRQROB IL@>QFLK FP DOB>QBO QE>K QEB M>QE O>AFRP).

F64B?2 6.27
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN); C@ (>CMN;H=? > J;NB.L;>COM) U If the vehicle is outside the path, seek the target. We dont have to work out the desired velocity and steering force; all that is taken care of by seek(), which we already wrote in Example 6.1.

M??E(N;LA?N); W

291

#E>MQBO 6. !RQLKLJLRP !DBKQP

"RQ TE>Q FP QEB Q>ODBQ? 2BVKLIAPgP >IDLOFQEJ FKSLISBP MF@HFKD > MLFKQ >EB>A LC QEB KLOJ>I LK QEB M>QE (PBB PQBM #3 >?LSB). "RQ CLO PFJMIF@FQV, TB @LRIA GRPQ P>V QE>Q QEB Q>ODBQ FP QEB KLOJ>I FQPBIC. 4EFP TFII TLOH C>FOIV TBII:
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN); C@ (>CMN;H=? > J;NB.L;>COM) U M??E(HILG;F.ICHN); W Seek the normal point on the path.

3FK@B TB HKLT QEB SB@QLO QE>Q ABCFKBP QEB M>QE (TBgOB @>IIFKD FQ h"i), TB @>K FJMIBJBKQ 2BVKLIAPgP hMLFKQ >EB>A LK QEB M>QEi TFQELRQ QLL JR@E QOLR?IB.

F64B?2 6.28
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN); C@ (>CMN;H=? > J;NB.L;>COM) U <.HILG;FCT?(); <.GOFN(25); .3?=NIL N;LA?N = .3?=NIL.;>>(HILG;F.ICHN,<); M??E(N;LA?N); W Normalize and scale b (pick 25 pixels arbitrarily). By adding b to normalPoint, we now move 25 pixels ahead on the path.

0RQQFKD FQ >II QLDBQEBO, TB E>SB QEB CLIILTFKD PQBBOFKD CRK@QFLK FK LRO 3?BC=F? @I>PP.

292

4EB .>QROB LC #LAB (S1.0)

Example 6.5: Simple path following


PIC> @IFFIQ(.;NB J) U .3?=NIL JL?>C=N = P?F.A?N(); JL?>C=N.HILG;FCT?(); JL?>C=N.GOFN(25); .3?=NIL JL?>C=N*I= = .3?=NIL.;>>(FI=, JL?>C=N); Step 1: Predict the vehicles future location.

.3?=NIL ; = J.MN;LN; Step 2: Find the normal point along the .3?=NIL < = J.?H>; path. .3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);

.3?=NIL >CL = .3?=NIL.MO<(<, ;); >CL.HILG;FCT?(); >CL.GOFN(10); .3?=NIL N;LA?N = .3?=NIL.;>>(HILG;F.ICHN, >CL);

Step 3: Move a little further along the path and set a target.

@FI;N >CMN;H=? = .3?=NIL.>CMN(HILG;F.ICHN, JL?>C=N*I=); C@ (>CMN;H=? > J.L;>COM) U M??E(N;LA?N); W W

Step 4: If we are off the path, seek that target in order to stay on the path.

.LT, VLR J>V KLQF@B >?LSB QE>Q FKPQB>A LC RPFKD >II QE>Q ALQ MOLAR@Q/P@>I>O MOLGB@QFLK @LAB QL CFKA QEB KLOJ>I MLFKQ, TB FKPQB>A @>II > CRK@QFLK: A?N,ILG;F.ICHN() . )K @>PBP IFHB QEFP, FQgP RPBCRI QL ?OB>H LRQ QEB @LAB QE>Q MBOCLOJP > PMB@FCF@ Q>PH (CFKAFKD > KLOJ>I MLFKQ) FKQL > CRK@QFLK QE>Q FQ @>K ?B RPBA DBKBOF@>IIV FK >KV @>PB TEBOB FQ FP OBNRFOBA. 4EB CRK@QFLK Q>HBP QEOBB .3?=NIL P: QEB CFOPQ ABCFKBP > MLFKQ FK #>OQBPF>K PM>@B >KA QEB PB@LKA >KA QEFOA >ODRJBKQP ABCFKB > IFKB PBDJBKQ.

293

#E>MQBO 6. !RQLKLJLRP !DBKQP

F64B?2 6.29
.3?=NIL A?N,ILG;F.ICHN(.3?=NIL J, .3?=NIL ;, .3?=NIL <) U .3?=NIL ;J = .3?=NIL.MO<(J, ;); .3?=NIL ;< = .3?=NIL.MO<(<, ;); PVector that points from a to p PVector that points from a to b

;<.HILG;FCT?(); ;<.GOFN(;J.>IN(;<)); .3?=NIL HILG;F.ICHN = .3?=NIL.;>>(;, ;<); L?NOLH HILG;F.ICHN; W

Using the dot product for scalar projection

Finding the normal point along the line segment

7E>Q AL TB E>SB PL C>O? 7B E>SB > .;NB @I>PP QE>Q ABCFKBP > M>QE >P > IFKB ?BQTBBK QTL MLFKQP. 7B E>SB > 3?BC=F? @I>PP QE>Q ABCFKBP > SBEF@IB QE>Q @>K CLIILT QEB M>QE (RPFKD > PQBBOFKD ?BE>SFLO QL PBBH > Q>ODBQ >ILKD QEB M>QE). 7E>Q FP JFPPFKD? 4>HB > ABBM ?OB>QE. 7BgOB >IJLPQ QEBOB.

6.9 Path Following with Multiple Segments

F64B?2 6.30

7BgSB ?RFIQ > DOB>Q BU>JMIB PL C>O, VBP, ?RQ FQgP MOBQQV A>OK IFJFQFKD. !CQBO >II, TE>Q FC TB T>KQ LRO M>QE QL ?B PLJBQEFKD QE>Q ILLHP JLOB IFHB:

294

4EB .>QROB LC #LAB (S1.0)

F64B?2 6.31

7EFIB FQgP QORB QE>Q TB @LRIA J>HB QEFP BU>JMIB TLOH CLO > @ROSBA M>QE, TBgOB JR@E IBPP IFHBIV QL BKA RM KBBAFKD > @LLI @LJMOBPP LK LRO CLOBEB>A FC TB PQF@H TFQE IFKB PBDJBKQP. )K QEB BKA, TB @>K >IT>VP BJMILV QEB P>JB QB@EKFNRB TB AFP@LSBOBA TFQE "LU2$eTB @>K AO>T TE>QBSBO C>K@V @ROSBA M>QE TB T>KQ >KA >MMOLUFJ>QB FQ ?BEFKA QEB P@BKBP TFQE PFJMIB DBLJBQOF@ CLOJP. 3L, TE>QgP QEB MOL?IBJ? )C TB J>AB M>QE CLIILTFKD TLOH TFQE LKB IFKB PBDJBKQ, ELT AL TB J>HB FQ TLOH TFQE > PBOFBP LC @LKKB@QBA IFKB PBDJBKQP? ,BQgP Q>HB > ILLH >D>FK >Q LRO SBEF@IB AOFSFKD >ILKD QEB P@OBBK. 3>V TB >OOFSB >Q 3QBM 3. '?0; 3: F49/ , ?,=20? ;:49? :9 ?30 ;,?3. 4L CFKA QEB Q>ODBQ, TB KBBA QL CFKA QEB KLOJ>I QL QEB IFKB PBDJBKQ. "RQ KLT QE>Q TB E>SB > PBOFBP LC IFKB PBDJBKQP, TB E>SB > PBOFBP LC KLOJ>I MLFKQP (PBB >?LSB)! 7EF@E LKB AL TB @ELLPB? 4EB PLIRQFLK TBgII BJMILV FP QL MF@H QEB KLOJ>I MLFKQ QE>Q FP (>) @ILPBPQ >KA (?) LK QEB M>QE FQPBIC.

F64B?2 6.32

)C TB E>SB > MLFKQ >KA >K FKCFKFQBIV ILKD IFKB, TBgII >IT>VP E>SB > KLOJ>I. "RQ, >P FK QEB M>QECLIILTFKD BU>JMIB, FC TB E>SB > MLFKQ >KA > IFKB PBDJBKQ, TB TLKgQ KB@BPP>OFIV CFKA > KLOJ>I QE>Q FP LK QEB IFKB PBDJBKQ FQPBIC. 3L FC QEFP E>MMBKP CLO >KV LC QEB PBDJBKQP, TB @>K AFPNR>IFCV QELPB KLOJ>IP. /K@B TB >OB IBCQ TFQE KLOJ>IP QE>Q >OB LK QEB M>QE FQPBIC (LKIV QTL FK QEB >?LSB AF>DO>J), TB PFJMIV MF@H QEB LKB QE>Q FP @ILPBPQ QL LRO SBEF@IBgP IL@>QFLK.

295

#E>MQBO 6. !RQLKLJLRP !DBKQP

)K LOABO QL TOFQB QEB @LAB CLO QEFP, TBgII E>SB QL BUM>KA LRO .;NB @I>PP QL E>SB >K ALL;S*CMN LC MLFKQP (O>QEBO QE>K GRPQ QTL, > PQ>OQ >KA >K BKA).

=F;MM .;NB U ALL;S*CMN<.3?=NIL> JICHNM; @FI;N L;>COM; .;NB() U L;>COM = 20; JICHNM = H?Q ALL;S*CMN<.3?=NIL>(); W PIC> ;>>.ICHN(@FI;N R, @FI;N S) U . .3?=NIL JICHN = H?Q .3?=NIL(R,S); JICHNM.;>>(JICHN); W This function allows us to add points to the path. A Path is now an ArrayList of points (PVector objects).

PIC> >CMJF;S() U MNLIE?(0); HI$CFF(); <?ACH0B;J?(); @IL (.3?=NIL P : JICHNM) U P?LN?R(P.R,P.S); W ?H>0B;J?(); W W

Display the path as a series of points.

.LT QE>Q TB E>SB QEB .;NB @I>PP ABCFKBA, FQgP QEB SBEF@IBgP QROK QL AB>I TFQE JRIQFMIB IFKB PBDJBKQP. !II TB AFA ?BCLOB T>P CFKA QEB KLOJ>I CLO LKB IFKB PBDJBKQ. 7B @>K KLT CFKA QEB KLOJ>IP CLO >II QEB IFKB PBDJBKQP FK > ILLM.
@IL (CHN C = 0; C < J.JICHNM.MCT?()-1; C++) U .3?=NIL ; = J.JICHNM.A?N(C); .3?=NIL < = J.JICHNM.A?N(C+1);

296

4EB .>QROB LC #LAB (S1.0)

.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <); Finding the normals for each line segment

4EBK TB PELRIA J>HB PROB QEB KLOJ>I MLFKQ FP >@QR>IIV ?BQTBBK MLFKQP ; >KA < . 3FK@B TB HKLT LRO M>QE DLBP COLJ IBCQ QL OFDEQ FK QEFP BU>JMIB, TB @>K QBPQ FC QEB L @LJMLKBKQ LC HILG;F.ICHN FP LRQPFAB QEB L @LJMLKBKQP LC ; >KA < .
C@ (HILG;F.ICHN.R < ;.R VV HILG;F.ICHN.R > <.R) U HILG;F.ICHN = <.A?N(); W Use the end point of the segment as our normal point if we cant find one.

!P > IFQQIB QOF@H, TBgII P>V QE>Q FC FQgP KLQ TFQEFK QEB IFKB PBDJBKQ, IBQgP GRPQ MOBQBKA QEB BKA MLFKQ LC QE>Q IFKB PBDJBKQ FP QEB KLOJ>I. 4EFP TFII BKPROB QE>Q LRO SBEF@IB >IT>VP PQ>VP LK QEB M>QE, BSBK FC FQ PQO>VP LRQ LC QEB ?LRKAP LC LRO IFKB PBDJBKQP. &FK>IIV, TBgII KBBA QL J>HB PROB TB CFKA QEB KLOJ>I MLFKQ QE>Q FP @ILPBPQ QL LRO SBEF@IB. 4L >@@LJMIFPE QEFP, TB PQ>OQ TFQE > SBOV EFDE hTLOIA OB@LOAi AFPQ>K@B >KA FQBO>QB QEOLRDE B>@E KLOJ>I MLFKQ QL PBB FC FQ ?B>QP QEB OB@LOA (F.B. FP IBPP QE>K). %>@E QFJB > KLOJ>I MLFKQ ?B>QP QEB OB@LOA, QEB TLOIA OB@LOA FP RMA>QBA >KA QEB TFKKFKD MLFKQ FP PQLOBA FK > S>OF>?IB K>JBA N;LA?N . !Q QEB BKA LC QEB ILLM, TBgII E>SB QEB @ILPBPQ KLOJ>I MLFKQ FK QE>Q S>OF>?IB.

Example 6.6: Path following


.3?=NIL N;LA?N = HOFF;

297

#E>MQBO 6. !RQLKLJLRP !DBKQP

@FI;N QILF>/?=IL> = 1000000;

Start with a very high record that can easily be beaten.

@IL (CHN C = 0; C < J.JICHNM.MCT?()-1; C++) U .3?=NIL ; = J.JICHNM.A?N(C); .3?=NIL < = J.JICHNM.A?N(C+1); .3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <); C@ (HILG;F.ICHN.R < ;.R VV HILG;F.ICHN.R > <.R) U HILG;F.ICHN = <.A?N(); W @FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN); C@ (>CMN;H=? < QILF>/?=IL>) U QILF>/?=IL> = >CMN;H=?; N;LA?N = HILG;F.ICHN.A?N(); W W If we beat the record, then this should be our target!

Exercise 6.10
5MA>QB QEB M>QE-CLIILTFKD BU>JMIB PL QE>Q QEB M>QE @>K DL FK >KV AFOB@QFLK. ((FKQ: VLRgII KBBA QL RPB QEB GCH() >KA G;R() CRK@QFLK TEBK ABQBOJFKFKD FC QEB KLOJ>I MLFKQ FP FKPFAB QEB IFKB PBDJBKQ.)
C@ (HILG;F.ICHN.R < ::::(::::,::::) VV HILG;F.ICHN.R > ::::(::::,::::)) U HILG;F.ICHN = <.A?N(); W

Exercise 6.11
#OB>QB > M>QE QE>Q @E>KDBP LSBO QFJB. #>K QEB MLFKQP QE>Q ABCFKB QEB M>QE FQPBIC E>SB QEBFO LTK PQBBOFKD ?BE>SFLOP?

6.10 Complex Systems


2BJBJ?BO LRO MROMLPB? 4L ?OB>QEB IFCB FKQL QEB QEFKDP QE>Q JLSB >OLRKA LRO 0OL@BPPFKD TFKALTP? "V IB>OKFKD QL TOFQB QEB @LAB CLO >K >RQLKLJLRP >DBKQ >KA ?RFIAFKD > PBOFBP LC BU>JMIBP LC FKAFSFAR>I ?BE>SFLOP, ELMBCRIIV LRO PLRIP CBBI > IFQQIB JLOB CRII. "RQ QEFP FP KL MI>@B QL PQLM >KA OBPQ LK LRO I>ROBIP. 7BgOB GRPQ DBQQFKD PQ>OQBA. !CQBO >II, QEBOB FP > ABBMBO MROMLPB >Q TLOH EBOB. 9BP, > SBEF@IB FP > PFJRI>QBA ?BFKD QE>Q J>HBP AB@FPFLKP >?LRQ ELT QL PBBH >KA CILT >KA CLIILT. "RQ TE>Q FP > IFCB IBA >ILKB, TFQELRQ QEB ILSB >KA PRMMLOQ LC

298

4EB .>QROB LC #LAB (S1.0)

LQEBOP? /RO MROMLPB EBOB FP KLQ LKIV QL ?RFIA FKAFSFAR>I ?BE>SFLOP CLO LRO SBEF@IBP, ?RQ QL MRQ LRO SBEF@IBP FKQL PVPQBJP LC J>KV SBEF@IBP >KA >IILT QELPB SBEF@IBP QL FKQBO>@Q TFQE B>@E LQEBO. ,BQgP QEFKH >?LRQ > QFKV, @O>TIFKD >KQeLKB PFKDIB >KQ. !K >KQ FP >K >RQLKLJLRP >DBKQ; FQ @>K MBO@BFSB FQP BKSFOLKJBKQ (RPFKD >KQBKK>B QL D>QEBO FKCLOJ>QFLK >?LRQ QEB AFOB@QFLK >KA PQOBKDQE LC @EBJF@>I PFDK>IP) >KA J>HB AB@FPFLKP >?LRQ ELT QL JLSB ?>PBA LK QELPB PFDK>IP. "RQ @>K > PFKDIB >KQ >@QFKD >ILKB ?RFIA > KBPQ, D>QEBO CLLA, ABCBKA FQP NRBBK? !K >KQ FP > PFJMIB RKFQ >KA @>K LKIV MBO@BFSB FQP FJJBAF>QB BKSFOLKJBKQ. ! @LILKV LC >KQP, ELTBSBO, FP > PLMEFPQF@>QBA @LJMIBU PVPQBJ, > hPRMBOLOD>KFPJi FK TEF@E QEB @LJMLKBKQP TLOH QLDBQEBO QL >@@LJMIFPE AFCCF@RIQ >KA @LJMIF@>QBA DL>IP. 7B T>KQ QL Q>HB TE>Q TBgSB IB>OKBA AROFKD QEB MOL@BPP LC ?RFIAFKD >RQLKLJLRP >DBKQP FK 0OL@BPPFKD FKQL PFJRI>QFLKP QE>Q FKSLISB J>KV >DBKQP LMBO>QFKD FK M>O>IIBIe>DBKQP QE>Q E>SB >K >?FIFQV QL MBO@BFSB KLQ LKIV QEBFO MEVPF@>I BKSFOLKJBKQ ?RQ >IPL QEB >@QFLKP LC QEBFO CBIILT >DBKQP, >KA QEBK >@Q >@@LOAFKDIV. 7B T>KQ QL @OB>QB @LJMIBU PVPQBJP FK 0OL@BPPFKD. 7E>Q FP > @LJMIBU PVPQBJ? ! @LJMIBU PVPQBJ FP QVMF@>IIV ABCFKBA >P > PVPQBJ QE>Q FP hJLOB QE>K QEB PRJ LC FQP M>OQP.i 7EFIB QEB FKAFSFAR>I BIBJBKQP LC QEB PVPQBJ J>V ?B FK@OBAF?IV PFJMIB >KA B>PFIV RKABOPQLLA, QEB ?BE>SFLO LC QEB PVPQBJ >P > TELIB @>K ?B EFDEIV @LJMIBU, FKQBIIFDBKQ, >KA AFCCF@RIQ QL MOBAF@Q. (BOB >OB QEOBB HBV MOFK@FMIBP LC @LJMIBU PVPQBJP. j '48;70 @94?> B4?3 >3:=?-=,920 =07,?4:9>34;>. 4EFP FP TE>Q TBgSB ?BBK ?RFIAFKD >II >ILKD: SBEF@IBP QE>Q E>SB > IFJFQBA MBO@BMQFLK LC QEBFO BKSFOLKJBKQ. j '48;70 @94?> :;0=,?0 49 ;,=,7707. 4EFP FP TE>Q TB KBBA QL PFJRI>QB FK @LAB. &LO BSBOV @V@IB QEOLRDE 0OL@BPPFKDgP >L;Q() ILLM, B>@E RKFQ TFII AB@FAB ELT QL JLSB (QL @OB>QB QEB >MMB>O>K@B LC QEBJ >II TLOHFKD FK M>O>IIBI). j 'D>?08 ,> , B3:70 0C34-4?> 080=209? ;309:809,. /RQ LC QEB FKQBO>@QFLKP ?BQTBBK QEBPB PFJMIB RKFQP BJBODBP @LJMIBU ?BE>SFLO, M>QQBOKP, >KA FKQBIIFDBK@B. (BOB TBgOB Q>IHFKD >?LRQ QEB OBPRIQ TB >OB ELMFKD CLO FK LRO PHBQ@EBP. 9BP, TB HKLT QEFP E>MMBKP FK K>QROB (>KQ @LILKFBP, QBOJFQBP, JFDO>QFLK M>QQBOKP, B>OQENR>HBP, PKLTCI>HBP, BQ@.), ?RQ @>K TB >@EFBSB QEB P>JB OBPRIQ FK LRO 0OL@BPPFKD PHBQ@EBP? &LIILTFKD >OB QEOBB >AAFQFLK>I CB>QROBP LC @LJMIBU PVPQBJP QE>Q TFII EBIM CO>JB QEB AFP@RPPFLK, >P TBII >P MOLSFAB DRFABIFKBP CLO CB>QROBP TB TFII T>KQ QL FK@IRAB FK LRO PLCQT>OB PFJRI>QFLKP. )QgP FJMLOQ>KQ QL >@HKLTIBADB QE>Q QEFP FP > CRWWV PBQ LC @E>O>@QBOFPQF@P >KA KLQ >II @LJMIBU PVPQBJP E>SB >II LC QEBJ. j #:9-7490,=4?D. 4EFP >PMB@Q LC @LJMIBU PVPQBJP FP LCQBK @>PR>IIV OBCBOOBA QL >P hQEB ?RQQBOCIV BCCB@Q,i @LFKBA ?V J>QEBJ>QF@F>K >KA JBQBLOLILDFPQ %AT>OA .LOQLK ,LOBKW, > MFLKBBO FK QEB PQRAV LC @E>LP QEBLOV. )K 1961, ,LOBKW T>P ORKKFKD > @LJMRQBO TB>QEBO PFJRI>QFLK CLO QEB PB@LKA QFJB >KA, MBOE>MP QL P>SB > IFQQIB QFJB, QVMBA FK > PQ>OQFKD S>IRB LC 0.506 FKPQB>A LC 0.506127. 4EB BKA OBPRIQ T>P @LJMIBQBIV AFCCBOBKQ COLJ QEB CFOPQ OBPRIQ LC QEB PFJRI>QFLK. )K LQEBO TLOAP, QEB QEBLOV FP QE>Q > PFKDIB ?RQQBOCIV CI>MMFKD FQP TFKDP LK QEB LQEBO PFAB LC QEB TLOIA @LRIA @>RPB > J>PPFSB

299

#E>MQBO 6. !RQLKLJLRP !DBKQP

TB>QEBO PEFCQ >KA ORFK LRO TBBHBKA >Q QEB ?B>@E. 7B @>II FQ hKLK-IFKB>Oi ?B@>RPB QEBOB FPKgQ > IFKB>O OBI>QFLKPEFM ?BQTBBK > @E>KDB FK FKFQF>I @LKAFQFLKP >KA > @E>KDB FK LRQ@LJB. ! PJ>II @E>KDB FK FKFQF>I @LKAFQFLKP @>K E>SB > J>PPFSB BCCB@Q LK QEB LRQ@LJB. .LK-IFKB>O PVPQBJP >OB > PRMBOPBQ LC @E>LQF@ PVPQBJP. )K QEB KBUQ @E>MQBO, TBgII PBB ELT BSBK FK > PVPQBJ LC J>KV WBOLP >KA LKBP, FC TB @E>KDB GRPQ LKB ?FQ, QEB OBPRIQ TFII ?B @LJMIBQBIV AFCCBOBKQ. j C:8;0?4?4:9 ,9/ .::;0=,?4:9. /KB LC QEB QEFKDP QE>Q LCQBK J>HBP > @LJMIBU PVPQBJ QF@H FP QEB MOBPBK@B LC ?LQE @LJMBQFQFLK >KA @LLMBO>QFLK ?BQTBBK QEB BIBJBKQP. )K LRO RM@LJFKD CIL@HFKD PVPQBJ, TB TFII E>SB QEOBB ORIBPe>IFDKJBKQ, @LEBPFLK, >KA PBM>O>QFLK. !IFDKJBKQ >KA @LEBPFLK TFII >PH QEB BIBJBKQP QL h@LLMBO>QBieF.B. TLOH QLDBQEBO QL PQ>V QLDBQEBO >KA JLSB QLDBQEBO. 3BM>O>QFLK, ELTBSBO, TFII >PH QEB BIBJBKQP QL h@LJMBQBi CLO PM>@B. !P TB DBQ QL QEB CIL@HFKD PVPQBJ, QOV Q>HFKD LRQ QEB @LLMBO>QFLK LO QEB @LJMBQFQFLK >KA VLRgII PBB ELT VLR >OB IBCQ TFQELRQ @LJMIBUFQV. #LJMBQFQFLK >KA @LLMBO>QFLK >OB CLRKA FK IFSFKD @LJMIBU PVPQBJP, ?RQ KLQ FK KLK-IFSFKD @LJMIBU PVPQBJP IFHB QEB TB>QEBO. j F00/-,.6. #LJMIBU PVPQBJP LCQBK FK@IRAB > CBBA?>@H ILLM TEBOB QEB QEB LRQMRQ LC QEB PVPQBJ FP CBA ?>@H FKQL QEB PVPQBJ QL FKCIRBK@B FQP ?BE>SFLO FK > MLPFQFSB LO KBD>QFSB AFOB@QFLK. ,BQgP P>V VLR AOFSB QL TLOH B>@E A>V ?B@>RPB QEB MOF@B LC D>P FP ILT. )K C>@Q, BSBOVLKB AOFSBP QL TLOH. 4EB MOF@B LC D>P DLBP RM >P ABJ>KA ?BDFKP QL BU@BBA PRMMIV. 9LR, >KA BSBOVLKB BIPB, AB@FAB QL Q>HB QEB QO>FK QL TLOH ?B@>RPB AOFSFKD FP QLL BUMBKPFSB. !KA QEB MOF@B LC D>P AB@IFKBP >P QEB ABJ>KA AB@IFKBP. 4EB MOF@B LC D>P FP ?LQE QEB FKMRQ LC QEB PVPQBJ (ABQBOJFKFKD TEBQEBO VLR @ELLPB QL AOFSB LO OFAB QEB QO>FK) >KA QEB LRQMRQ (QEB ABJ>KA QE>Q OBPRIQP COLJ VLRO @ELF@B). ) PELRIA KLQB QE>Q B@LKLJF@ JLABIP (IFHB PRMMIV/ABJ>KA, QEB PQL@H J>OHBQ) >OB LKB BU>JMIB LC > ERJ>K @LJMIBU PVPQBJ. /QEBOP FK@IRAB C>AP >KA QOBKAP, BIB@QFLKP, @OLTAP, >KA QO>CCF@ CILT. #LJMIBUFQV TFII PBOSB >P > QEBJB CLO QEB OBJ>FKFKD @LKQBKQ FK QEFP ?LLH. )K QEFP @E>MQBO, TBgII ?BDFK ?V >AAFKD LKB JLOB CB>QROB QL LRO 3?BC=F? @I>PP: >K >?FIFQV QL ILLH >Q KBFDE?LOFKD SBEF@IBP.

6.11 Group Behaviors (or: Lets not run into each other)
! DOLRM FP @BOQ>FKIV KLQ > KBT @LK@BMQ. 7BgSB ALKB QEFP ?BCLOBeFK #E>MQBO 4, TEBOB TB ABSBILMBA > CO>JBTLOH CLO J>K>DFKD @LIIB@QFLKP LC M>OQF@IBP FK > .;LNC=F?0SMN?G @I>PP. 4EBOB, TB PQLOBA > IFPQ LC M>OQF@IBP FK >K ALL;S*CMN . 7BgII AL QEB P>JB QEFKD EBOB: PQLOB > ?RK@E LC 3?BC=F? L?GB@QP FK >K ALL;S*CMN .

300

4EB .>QROB LC #LAB (S1.0)

ALL;S*CMN<3?BC=F?> P?BC=F?M; PIC> M?NOJ() U P?BC=F?M = H?Q ALL;S*CMN<3?BC=F?>;

Declare an ArrayList of Vehicle objects.

Initialize and fill the ArrayList with a bunch of Vehicles. @IL (CHN C = 0; C < 100; C++) U P?BC=F?M.;>>(H?Q 3?BC=F?(L;H>IG(QC>NB),L;H>IG(B?CABN))); W W

.LT TEBK FQ @LJBP QFJB QL AB>I TFQE >II QEB SBEF@IBP FK >L;Q() , TB PFJMIV ILLM QEOLRDE >II LC QEBJ >KA @>II QEB KB@BPP>OV CRK@QFLKP.
PIC> >L;Q()U @IL (3?BC=F? P : P?BC=F?M) U P.OJ>;N?(); P.>CMJF;S(); W W

/+, PL J>V?B TB T>KQ QL >AA > ?BE>SFLO, > CLO@B QL ?B >MMIFBA QL >II QEB SBEF@IBP. 4EFP @LRIA ?B PBBHFKD QEB JLRPB.
P.M??E(GIOM?5,GIOM?6);

"RQ QE>QgP >K FKAFSFAR>I ?BE>SFLO. 7BgSB >IOB>AV PMBKQ QEFOQV-LAA M>DBP TLOOVFKD >?LRQ FKAFSFAR>I ?BE>SFLOP. 7BgOB EBOB ?B@>RPB TB T>KQ QL >MMIV > DOLRM ?BE>SFLO. ,BQgP ?BDFK TFQE PBM>O>QFLK, > ?BE>SFLO QE>Q @LJJ>KAP, h!SLFA @LIIFAFKD TFQE VLRO KBFDE?LOP!i
P.M?J;L;N?();

)P QE>Q OFDEQ? )Q PLRKAP DLLA, ?RQ FQgP KLQ. 7E>QgP JFPPFKD? )K QEB @>PB LC PBBH, TB P>FA, h3BBH GIOM?5 >KA GIOM?6 .i )K QEB @>PB LC PBM>O>QB, TBgOB P>VFKD hPBM>O>QB COLJ 9J9FMCB9 9@G9 .i 7EL FP BSBOVLKB BIPB? )QgP QEB IFPQ LC >II QEB LQEBO SBEF@IBP.
P.M?J;L;N?(P?BC=F?M);

4EFP FP QEB ?FD IB>M ?BVLKA TE>Q TB AFA ?BCLOB TFQE M>OQF@IB PVPQBJP. )KPQB>A LC E>SFKD B>@E BIBJBKQ (M>OQF@IB LO SBEF@IB) LMBO>QB LK FQP LTK, TBgOB KLT P>VFKD, h(BV VLR, QEB SBEF@IB! 7EBK FQ @LJBP QFJB CLO VLR QL LMBO>QB, VLR KBBA QL LMBO>QB TFQE >K >T>OBKBPP LC BSBOVLKB BIPB. 3L )gJ DLFKD QL DL >EB>A >KA M>PP VLR QEB ALL;S*CMN LC BSBOVLKB BIPB.i 4EFP FP ELT TBgSB J>MMBA LRQ M?NOJ() >KA >L;Q() QL AB>I TFQE > DOLRM ?BE>SFLO.

301

#E>MQBO 6. !RQLKLJLRP !DBKQP


ALL;S*CMN<3?BC=F?> P?BC=F?M; PIC> M?NOJ() U MCT?(320,240); P?BC=F?M = H?Q ALL;S*CMN<3?BC=F?>(); @IL (CHN C = 0; C < 100; C++) U P?BC=F?M.;>>(H?Q 3?BC=F?(L;H>IG(QC>NB),L;H>IG(B?CABN))); W W PIC> >L;Q() U <;=EALIOH>(255); @IL (3?BC=F? P : P?BC=F?M) U L.I;F7H7J;(L;>?9B;I); P.OJ>;N?(); P.>CMJF;S(); W W This is really the only new thing were doing in this section. Were asking a Vehicle object to examine all the other vehicles in the process of calculating a separation force.

/C @LROPB, QEFP FP GRPQ QEB ?BDFKKFKD. 4EB OB>I TLOH E>MMBKP FKPFAB QEB M?J;L;N?() CRK@QFLK FQPBIC. ,BQgP CFDROB LRQ ELT TB T>KQ QL ABCFKB PBM>O>QFLK. 2BVKLIAP PQ>QBP: F64B?2 6.33 h3QBBO QL >SLFA @OLTAFKD.i )K LQEBO TLOAP, FC > DFSBK SBEF@IB FP QLL @ILPB QL VLR, PQBBO >T>V COLJ QE>Q SBEF@IB. 3LRKA C>JFIF>O? 2BJBJ?BO QEB PBBH ?BE>SFLO TEBOB > SBEF@IB PQBBOP QLT>OAP > Q>ODBQ? 2BSBOPB QE>Q CLO@B >KA TB E>SB QEB CIBB ?BE>SFLO. "RQ TE>Q FC JLOB QE>K LKB SBEF@IB FP QLL @ILPB? )K QEFP @>PB, TBgII ABCFKB PBM>O>QFLK >P QEB >SBO>DB LC >II QEB SB@QLOP MLFKQFKD >T>V COLJ >KV @ILPB SBEF@IBP. ,BQgP ?BDFK QL TOFQB QEB @LAB. !P TB GRPQ TLOHBA LRQ, TBgOB TOFQFKD > CRK@QFLK @>IIBA M?J;L;N?() QE>Q OB@BFSBP >K ALL;S*CMN LC 6BEF@IB L?GB@QP >P >K >ODRJBKQ.
F64B?2 6.34

PIC> M?J;L;N? (ALL;S*CMN<3?BC=F?> P?BC=F?M) U W

)KPFAB QEFP CRK@QFLK, TBgOB DLFKD QL ILLM QEOLRDE >II LC QEB SBEF@IBP >KA PBB FC >KV >OB QLL @ILPB.

302

4EB .>QROB LC #LAB (S1.0)

@FI;N >?MCL?>M?J;L;NCIH = 20; @IL (3?BC=F? INB?L : P?BC=F?M) U @FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);

This variable specifies how close is too close.

What is the distance between me and another Vehicle? C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U Any code here will be executed if the Vehicle is within 20 pixels.

W W

.LQF@B ELT FK QEB >?LSB @LAB, TB >OB KLQ LKIV @EB@HFKD FC QEB AFPQ>K@B FP IBPP QE>K > ABPFOBA PBM>O>QFLK (F.B. QLL @ILPB!), ?RQ >IPL FC QEB AFPQ>K@B FP DOB>QBO QE>K WBOL. 4EFP FP > IFQQIB QOF@H QE>Q J>HBP PROB TB ALKgQ >PH > SBEF@IB QL PBM>O>QB COLJ FQPBIC. 2BJBJ?BO, >II QEB SBEF@IBP >OB FK QEB ALL;S*CMN , PL FC VLR >OBKgQ @>OBCRI VLRgII ?B @LJM>OFKD B>@E SBEF@IB QL FQPBIC! /K@B TB HKLT QE>Q QTL SBEF@IBP >OB QLL @ILPB, TB KBBA QL J>HB > SB@QLO QE>Q MLFKQP >T>V COLJ QEB LCCBKAFKD SBEF@IB.
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U .3?=NIL >C@@ = .3?=NIL.MO<(FI=;NCIH, INB?L.FI=;NCIH); >C@@.HILG;FCT?(); W A PVector pointing away from the others location

4EFP FP KLQ BKLRDE. 7B E>SB QE>Q SB@QLO KLT, ?RQ TB KBBA QL J>HB PROB TB @>I@RI>QB QEB >SBO>DB LC >II SB@QLOP MLFKQFKD >T>V COLJ @ILPB SBEF@IBP. (LT AL TB @LJMRQB >SBO>DB? 7B >AA RM >II QEB SB@QLOP >KA AFSFAB ?V QEB QLQ>I.
+0;9JEH IKC = D;M +0;9JEH(); ?DJ 9EKDJ = 0; @IL (3?BC=F? INB?L : P?BC=F?M) U We have to keep track of how many Vehicles are too close. Start with an empty PVector.

@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH); C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U +0;9JEH :?<< = +0;9JEH.IK8(BE97J?ED, EJ>;H.BE97J?ED); >C@@.HILG;FCT?(); IKC.7::(:?<<); =IOHN++; W W Add all the vectors together and increment the count.

303

#E>MQBO 6. !RQLKLJLRP !DBKQP

?< (9EKDJ > 0) Q IKC.:?L(9EKDJ); S

We have to make sure we found at least one close vehicle. We dont want to bother doing anything if nothing is too close (not to mention we cant divide by zero!)

/K@B TB E>SB QEB >SBO>DB SB@QLO (PQLOBA FK QEB .3?=NIL L?GB@Q hPRJi), QE>Q .3?=NIL @>K ?B P@>IBA QL J>UFJRJ PMBBA >KA ?B@LJB LRO ABPFOBA SBIL@FQVeTB 89G=F9 QL JLSB FK QE>Q AFOB@QFLK >Q J>UFJRJ PMBBA! !KA LK@B TB E>SB QEB ABPFOBA SBIL@FQV, FQgP QEB P>JB LIA 2BVKLIAP PQLOV: PQBBOFKD BNR>IP ABPFOBA JFKRP SBIL@FQV.
C@ (=IOHN > 0) U MOG.>CP(=IOHN); MOG.M?N+;A(G;RMJ??>); Scale average to maxspeed (this becomes desired). Reynoldss steering formula

.3?=NIL MN??L = .3?=NIL.MO<(MOG,P?F); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W

Apply the force to the Vehicles acceleration.

,BQgP PBB QEB CRK@QFLK FK FQP BKQFOBQV. 4EBOB >OB QTL >AAFQFLK>I FJMOLSBJBKQP, KLQBA FK QEB @LAB @LJJBKQP.

Example 6.7: Group behavior: Separation


PIC> M?J;L;N? (ALL;S*CMN<3?BC=F?> P?BC=F?M) U

304

4EB .>QROB LC #LAB (S1.0)

<BE7J :;I?H;:I;F7H7J?ED = H*2;

Note how the desired separation is based on the Vehicles size. .3?=NIL MOG = H?Q .3?=NIL(); CHN =IOHN = 0; @IL (3?BC=F? INB?L : P?BC=F?M) U @FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH); C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U .3?=NIL >C@@ = .3?=NIL.MO<(FI=;NCIH, INB?L.FI=;NCIH); >C@@.HILG;FCT?(); :?<<.:?L(:); MOG.;>>(>C@@); =IOHN++; W W C@ (=IOHN > 0) U MOG.>CP(=IOHN); MOG.HILG;FCT?(); MOG.GOFN(G;RMJ??>); .3?=NIL MN??L = .3?=NIL.MO<(MOG, P?F); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); W W What is the magnitude of the PVector pointing away from the other vehicle? The closer it is, the more we should flee. The farther, the less. So we divide by the distance to weight it appropriately.

Exercise 6.12
2BTOFQB M?J;L;N?() QL TLOH FK QEB LMMLPFQB C>PEFLK (h@LEBPFLKi). )C > SBEF@IB FP ?BVLKA > @BOQ>FK AFPQ>K@B, PQBBO QLT>OAP QE>Q SBEF@IB. 4EFP TFII HBBM QEB DOLRM QLDBQEBO. (.LQB QE>Q FK > JLJBKQ, TBgOB DLFKD QL ILLH >Q TE>Q E>MMBKP TEBK TB E>SB ?LQE @LEBPFLK >KA PBM>O>QFLK FK QEB P>JB PFJRI>QFLK.)

305

#E>MQBO 6. !RQLKLJLRP !DBKQP

Exercise 6.13
!AA QEB PBM>O>QFLK CLO@B QL M>QE CLIILTFKD QL @OB>QB > PFJRI>QFLK LC 2BVKLIAPgP h#OLTA 0>QE &LIILTFKD.i

6.12 Combinations
4EB MOBSFLRP QTL BUBO@FPBP EFKQ >Q TE>Q FP MBOE>MP QEB JLPQ FJMLOQ>KQ >PMB@Q LC QEFP @E>MQBO. !CQBO >II, TE>Q FP > 0OL@BPPFKD PHBQ@E TFQE LKB PQBBOFKD CLO@B @LJM>OBA QL LKB TFQE J>KV? (LT @LRIA TB BSBK ?BDFK QL PFJRI>QB BJBODBK@B FK LRO PHBQ@EBP TFQE LKIV LKB ORIB? 4EB JLPQ BU@FQFKD >KA FKQOFDRFKD ?BE>SFLOP TFII @LJB COLJ JFUFKD >KA J>Q@EFKD JRIQFMIB PQBBOFKD CLO@BP, >KA TBgII KBBA > JB@E>KFPJ CLO ALFKD PL. 9LR J>V ?B QEFKHFKD, h$RE, QEFP FP KLQEFKD KBT. 7B AL QEFP >II QEB QFJB.i 9LR TLRIA ?B OFDEQ. )K C>@Q, TB AFA QEFP >P B>OIV >P #E>MQBO 2.
.3?=NIL QCH> = H?Q .3?=NIL(0.001,0); .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1); GIP?L.;JJFS$IL=?(QCH>); GIP?L.;JJFS$IL=?(AL;PCNS);

(BOB TB E>SB > JLSBO QE>Q OBPMLKAP QL QTL CLO@BP. 4EFP >II TLOHP KF@BIV ?B@>RPB LC QEB T>V TB ABPFDKBA QEB +IP?L @I>PP QL >@@RJRI>QB QEB CLO@B SB@QLOP FKQL FQP >@@BIBO>QFLK SB@QLO. )K QEFP @E>MQBO, ELTBSBO, LRO CLO@BP PQBJ COLJ FKQBOK>I ABPFOBP LC QEB JLSBOP (KLT @>IIBA SBEF@IBP). !KA QELPB ABPFOBP @>K ?B TBFDEQBA. ,BQgP @LKPFABO > PHBQ@E TEBOB >II SBEF@IBP E>SB QTL ABPFOBP: j '006 ?30 8:@>0 7:.,?4:9. j '0;,=,?0 1=:8 ,9D A034.70> ?3,? ,=0 ?:: .7:>0.

306

4EB .>QROB LC #LAB (S1.0)

7B JFDEQ ?BDFK ?V >AAFKD > CRK@QFLK QL QEB 3?BC=F? @I>PP QE>Q J>K>DBP >II LC QEB ?BE>SFLOP. ,BQgP @>II FQ ;JJFS ?B;PCILM() .
PIC> ;JJFS ?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U M?J;L;N?(P?BC=F?M); M??E(H?Q .3?=NIL(GIOM?5,GIOM?6)); W

(BOB TB PBB ELT > PFKDIB CRK@QFLK Q>HBP @>OB LC @>IIFKD QEB LQEBO CRK@QFLKP QE>Q >MMIV QEB CLO@BPeM?J;L;N?() >KA M??E() . 7B @LRIA PQ>OQ JR@HFKD >OLRKA TFQE QELPB CRK@QFLKP >KA PBB FC TB @>K >AGRPQ QEB PQOBKDQE LC QEB CLO@BP QEBV >OB @>I@RI>QFKD. "RQ FQ TLRIA ?B B>PFBO CLO RP QL >PH QELPB CRK@QFLKP QL OBQROK QEB CLO@BP PL QE>Q TB @>K >AGRPQ QEBFO PQOBKDQE ?BCLOB >MMIVFKD QEBJ QL QEB SBEF@IBgP >@@BIBO>QFLK.
PIC> ;JJFS ?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U .3?=NIL M?J;L;N? = M?J;L;N?(P?BC=F?M); .3?=NIL M??E = M??E(H?Q .3?=NIL(GIOM?5,GIOM?6)); ;JJFS$IL=?(M?J;L;N?); ;JJFS$IL=?(M??E); W We have to apply the force here since seek() and separate() no longer do so.

,BQgP ILLH >Q ELT QEB PBBH CRK@QFLK @E>KDBA.


.3?=NIL M??E(.3?=NIL N;LA?N) U .3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>); .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?F); MN??L.FCGCN(G;R@IL=?); ;JJFS$IL=?(MN??L); L?NOLH MN??L; W Instead of applying the force we return the PVector.

4EFP FP > PR?QIB @E>KDB, ?RQ FK@OBAF?IV FJMLOQ>KQ CLO RP: FQ >IILTP RP QL >IQBO QEB PQOBKDQE LC QEBPB CLO@BP FK LKB MI>@B.

307

#E>MQBO 6. !RQLKLJLRP !DBKQP

Example 6.8: Combining steering behaviors: Seek and separate


PIC> ;JJFS ?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U .3?=NIL M?J;L;N? = M?J;L;N?(P?BC=F?M); .3?=NIL M??E = M??E(H?Q .3?=NIL(GIOM?5,GIOM?6)); I;F7H7J;.CKBJ(1.5); I;;A.CKBJ(0.5); These values can be whatever you want them to be! They can be variables that are customized for each vehicle, or they can change over time.

;JJFS$IL=?(M?J;L;N?); ;JJFS$IL=?(M??E); W

Exercise 6.14
2BAL %U>JMIB 6.8 PL QE>Q QEB ?BE>SFLO TBFDEQP >OB KLQ @LKPQ>KQP. 7E>Q E>MMBKP FC QEBV @E>KDB LSBO QFJB (>@@LOAFKD QL > PFKB T>SB LO 0BOIFK KLFPB)? /O FC PLJB SBEF@IBP >OB JLOB @LK@BOKBA TFQE PBBHFKD >KA LQEBOP JLOB @LK@BOKBA TFQE PBM>O>QFKD? #>K VLR FKQOLAR@B LQEBO PQBBOFKD ?BE>SFLOP >P TBII?

6.13 Flocking
&IL@HFKD FP >K DOLRM >KFJ>I ?BE>SFLO QE>Q FP @E>O>@QBOFPQF@ LC J>KV IFSFKD @OB>QROBP, PR@E >P ?FOAP, CFPE, >KA FKPB@QP. )K 1986, #O>FD 2BVKLIAP @OB>QBA > @LJMRQBO PFJRI>QFLK LC CIL@HFKD ?BE>SFLO >KA AL@RJBKQBA QEB >IDLOFQEJ FK EFP M>MBO, h&IL@HP, (BOAP, >KA 3@ELLIP: ! $FPQOF?RQBA "BE>SFLO>I -LABI.i 2B@OB>QFKD QEFP PFJRI>QFLK FK 0OL@BPPFKD TFII ?OFKD QLDBQEBO >II QEB @LK@BMQP FK QEFP @E>MQBO. 1. 29 K=@@ IG9 H<9 GH99F=B; :CF79 :CFAI@5 (GH99F = 89G=F98 - J9@C7=HM) HC =AD@9A9BH H<9 FI@9G C: :@C7?=B;. 2. /<9G9 GH99F=B; :CF79G K=@@ 69 ;FCID 69<5J=CFG 5B8 F9EI=F9 957< J9<=7@9 HC @CC? 5H 5@@ H<9 CH<9F J9<=7@9G. 3. 29 K=@@ 7CA6=B9 5B8 K9=;<H AI@H=D@9 :CF79G. 4. /<9 F9GI@H K=@@ 69 5 7CAD@9L GMGH9AR=BH9@@=;9BH ;FCID 69<5J=CF K=@@ 9A9F;9 :FCA H<9 G=AD@9 FI@9G C: :@C7?=B; K=H<CIH H<9 DF9G9B79 C: 5 79BHF5@=N98 GMGH9A CF @9589F. 4EB DLLA KBTP FP, TBgSB >IOB>AV ALKB FQBJP 1 QEOLRDE 3 FK QEFP @E>MQBO, PL QEFP PB@QFLK TFII ?B >?LRQ GRPQ MRQQFKD FQ >II QLDBQEBO >KA PBBFKD QEB OBPRIQ. 308

4EB .>QROB LC #LAB (S1.0)

"BCLOB TB ?BDFK, ) PELRIA JBKQFLK QE>Q TBgOB DLFKD QL @E>KDB QEB K>JB LC LRO 3?BC=F? @I>PP (VBQ >D>FK). 2BVKLIAP RPBP QEB QBOJ h?LFAi (> J>AB-RM TLOA QE>Q OBCBOP QL > ?FOA-IFHB L?GB@Q) QL ABP@OF?B QEB BIBJBKQP LC > CIL@HFKD PVPQBJ >KA TB TFII AL QEB P>JB. ,BQgP Q>HB >K LSBOSFBT LC QEB QEOBB ORIBP LC CIL@HFKD. 1. '0;,=,?4:9 (>IPL HKLTK >P h>SLFA>K@Bi): 3QBBO QL >SLFA @LIIFAFKD TFQE VLRO KBFDE?LOP. 2. A7429809? (>IPL HKLTK >P h@LMVi): 3QBBO FK QEB P>JB AFOB@QFLK >P VLRO KBFDE?LOP. 3. C:30>4:9 (>IPL HKLTK >P h@BKQBOi): 3QBBO QLT>OAP QEB @BKQBO LC VLRO KBFDE?LOP (PQ>V TFQE QEB DOLRM).

F64B?2 6.35

*RPQ >P TB AFA TFQE LRO PBM>O>QB >KA PBBH BU>JMIB, TBgII T>KQ LRO IC> L?GB@QP QL E>SB > PFKDIB CRK@QFLK QE>Q J>K>DBP >II QEB >?LSB ?BE>SFLOP. 7BgII @>II QEFP CRK@QFLK @FI=E() .
PIC> @FI=E(ALL;S*CMN< IC>> <IC>M) U .3?=NIL M?J = M?J;L;N?(<IC>M); .3?=NIL ;FC = ;FCAH(<IC>M); .3?=NIL =IB = =IB?MCIH(<IC>M); The three flocking rules

M?J.GOFN(1.5); ;FC.GOFN(1.0); =IB.GOFN(1.0);

Arbitrary weights for these forces (Try different ones!)

;JJFS$IL=?(M?J); ;JJFS$IL=?(;FC); ;JJFS$IL=?(=IB); W

Applying all the forces

.LT, FQgP GRPQ > J>QQBO LC FJMIBJBKQFKD QEB QEOBB ORIBP. 7B AFA PBM>O>QFLK ?BCLOB; FQgP FABKQF@>I QL LRO MOBSFLRP BU>JMIB. ,BQgP Q>HB > ILLH >Q >IFDKJBKQ, LO PQBBOFKD FK QEB P>JB AFOB@QFLK >P

309

#E>MQBO 6. !RQLKLJLRP !DBKQP

VLRO KBFDE?LOP. !P TFQE >II LC LRO PQBBOFKD ?BE>SFLOP, TBgSB DLQ QL ?LFI ALTK QEFP @LK@BMQ FKQL > ABPFOB: QEB ?LFAgP ABPFOBA SBIL@FQV FP QEB >SBO>DB SBIL@FQV LC FQP KBFDE?LOP. 3L LRO >IDLOFQEJ FP QL @>I@RI>QB QEB >SBO>DB SBIL@FQV LC >II QEB LQEBO ?LFAP >KA PBQ QE>Q QL ABPFOBA.
.3?=NIL ;FCAH (ALL;S*CMN< IC>> <IC>M) U .3?=NIL MOG = H?Q .3?=NIL(0,0); @IL ( IC> INB?L : <IC>M) U MOG.;>>(INB?L.P?FI=CNS); W MOG.>CP(<IC>M.MCT?()); Add up all the velocities and divide by the total to calculate the average velocity.

MOG.M?N+;A(G;RMJ??>);

We desire to go in that direction at maximum speed. Reynoldss steering force formula

.3?=NIL MN??L = .3?=NIL.MO<(MOG,P?FI=CNS); MN??L.FCGCN(G;R@IL=?); L?NOLH MN??L; W

4EB >?LSB FP MOBQQV DLLA, ?RQ FQgP JFPPFKD LKB O>QEBO @OR@F>I ABQ>FI. /KB LC QEB HBV MOFK@FMIBP ?BEFKA @LJMIBU PVPQBJP IFHB CIL@HFKD FP QE>Q QEB BIBJBKQP (FK QEFP @>PB, ?LFAP) E>SB PELOQO>KDB OBI>QFLKPEFMP. 4EFKHFKD >?LRQ >KQP >D>FK, FQgP MOBQQV B>PV QL FJ>DFKB >K >KQ ?BFKD >?IB QL PBKPB FQP FJJBAF>QB BKSFOLKJBKQ, ?RQ IBPP PL >K >KQ E>SFKD >K >T>OBKBPP LC TE>Q >KLQEBO >KQ FP ALFKD ERKAOBAP LC CBBQ >T>V. 4EB C>@Q QE>Q QEB >KQP @>K MBOCLOJ PR@E @LJMIBU @LIIB@QFSB ?BE>SFLO COLJ LKIV QEBPB KBFDE?LOFKD OBI>QFLKPEFMP FP TE>Q J>HBP QEBJ PL BU@FQFKD FK QEB CFOPQ MI>@B. )K LRO >IFDKJBKQ CRK@QFLK, TBgOB Q>HFKD QEB >SBO>DB SBIL@FQV LC >II QEB ?LFAP, TEBOB>P TB PELRIA OB>IIV LKIV ?B ILLHFKD >Q QEB ?LFAP TFQEFK > @BOQ>FK AFPQ>K@B. 4E>Q AFPQ>K@B QEOBPELIA FP RM QL VLR, LC @LROPB. 9LR @LRIA ABPFDK ?LFAP QE>Q @>K PBB LKIV QTBKQV MFUBIP >T>V LO ?LFAP QE>Q @>K PBB > ERKAOBA MFUBIP >T>V.

310

4EB .>QROB LC #LAB (S1.0)

F64B?2 6.36

-R@E IFHB TB AFA TFQE PBM>O>QFLK (LKIV @>I@RI>QFKD > CLO@B CLO LQEBOP TFQEFK > @BOQ>FK AFPQ>K@B), TBgII T>KQ QL AL QEB P>JB TFQE >IFDKJBKQ (>KA @LEBPFLK).
.3?=NIL ;FCAH (ALL;S*CMN< IC>> <IC>M) U @FI;N H?CAB<IL>CMN = 50; This is an arbitrary value and could vary from boid to boid.

.3?=NIL MOG = H?Q .3?=NIL(0,0); CHN =IOHN = 0; @IL ( IC> INB?L : <IC>M) U @FI;N > = .3?=NIL.>CMN(FI=;NCIH,INB?L.FI=;NCIH); C@ ((> > 0) && (> < H?CAB<IL>CMN)) U MOG.;>>(INB?L.P?FI=CNS); =IOHN++; W W C@ (=IOHN > 0) U MOG.>CP(=IOHN); MOG.HILG;FCT?(); MOG.GOFN(G;RMJ??>); .3?=NIL MN??L = .3?=NIL.MO<(MOG,P?FI=CNS); MN??L.FCGCN(G;R@IL=?); L?NOLH MN??L; W ?FM? U L?NOLH H?Q .3?=NIL(0,0); W W

For an average, we need to keep track of how many boids are within the distance.

If we dont find any close boids, the steering force is zero.

311

#E>MQBO 6. !RQLKLJLRP !DBKQP

Exercise 6.15
#>K VLR TOFQB QEB >?LSB @LAB PL QE>Q ?LFAP @>K LKIV PBB LQEBO ?LFAP QE>Q >OB >@QR>IIV TFQEFK QEBFO hMBOFMEBO>Ii SFPFLK (>P FC QEBV E>A BVBP)?

&FK>IIV, TB >OB OB>AV CLO @LEBPFLK. (BOB LRO @LAB FP SFOQR>IIV FABKQF@>I QL QE>Q CLO >IFDKJBKQeLKIV FKPQB>A LC @>I@RI>QFKD QEB >SBO>DB SBIL@FQV LC QEB ?LFAgP KBFDE?LOP, TB T>KQ QL @>I@RI>QB QEB >SBO>DB IL@>QFLK LC QEB ?LFAgP KBFDE?LOP (>KA RPB QE>Q >P > Q>ODBQ QL PBBH).
.3?=NIL =IB?MCIH (ALL;S*CMN< IC>> <IC>M) U @FI;N H?CAB<IL>CMN = 50; .3?=NIL MOG = H?Q .3?=NIL(0,0); CHN =IOHN = 0; @IL ( IC> INB?L : <IC>M) U @FI;N > = .3?=NIL.>CMN(FI=;NCIH,INB?L.FI=;NCIH); C@ ((> > 0) && (> < H?CAB<IL>CMN)) U MOG.;>>(INB?L.FI=;NCIH); =IOHN++; W W C@ (=IOHN > 0) U MOG.>CP(=IOHN); H;JKHD I;;A(IKC); W ?FM? U L?NOLH H?Q .3?=NIL(0,0); W W Here we make use of the seek() function we wrote in Example 6.8. The target we seek is the average location of our neighbors. Adding up all the others locations

)QgP >IPL TLOQE Q>HFKD QEB QFJB QL TOFQB > @I>PP @>IIBA $FI=E , TEF@E TFII ?B SFOQR>IIV FABKQF@>I QL QEB .;LNC=F?0SMN?G @I>PP TB TOLQB FK #E>MQBO 4 TFQE LKIV LKB QFKV @E>KDB: 7EBK TB @>II LOH() LK B>@E IC> L?GB@Q (>P TB AFA QL B>@E .;LNC=F? L?GB@Q), TBgII M>PP FK > OBCBOBK@B QL QEB BKQFOB ALL;S*CMN LC ?LFAP.

312

4EB .>QROB LC #LAB (S1.0)


=F;MM $FI=E U ALL;S*CMN< IC>> <IC>M; $FI=E() U <IC>M = H?Q ALL;S*CMN< IC>>(); W PIC> LOH() U @IL ( IC> < : <IC>M) U 8.HKD(8E?:I); W W PIC> ;>> IC>( IC> <) U <IC>M.;>>(<); W W Each Boid object must know about all the other Boids.

!KA LRO J>FK MOLDO>J TFII ILLH IFHB:

Example 6.9: Flocking


$FI=E @FI=E; PIC> M?NOJ() U MCT?(300,200); @FI=E = H?Q $FI=E(); @IL (CHN C = 0; C < 100; C++) U IC> < = H?Q IC>(QC>NB/2,B?CABN/2); @FI=E.;>> IC>(<); W W PIC> >L;Q() U <;=EALIOH>(255); @FI=E.LOH(); W The Flock starts out with 100 Boids. A Flock object manages the entire group.

313

#E>MQBO 6. !RQLKLJLRP !DBKQP

Exercise 6.16
#LJ?FKB CIL@HFKD TFQE PLJB LQEBO PQBBOFKD ?BE>SFLOP.

Exercise 6.17
)K EFP ?LLH 4EB CCADIH5H=CB5@ B95IHM C: *5HIF9 (-)4 0OBPP, 2000), '>OV &I>HB ABP@OF?BP > CLROQE ORIB CLO CIL@HFKD: h6FBT: JLSB I>QBO>IIV >T>V COLJ >KV ?LFA QE>Q ?IL@HP QEB SFBT.i (>SB VLRO ?LFAP CLIILT QEFP ORIB.

Exercise 6.18
#OB>QB > CIL@HFKD PFJRI>QFLK TEBOB >II LC QEB M>O>JBQBOP (G9D5F5H=CB K9=;<H , 7C<9G=CB K9=;<H , 5@=;BA9BH K9=;<H , A5L=AIA :CF79, A5L=AIA GD998 ) @E>KDB LSBO QFJB. 4EBV @LRIA ?B @LKQOLIIBA ?V 0BOIFK KLFPB LO ?V RPBO FKQBO>@QFLK. (&LO BU>JMIB, VLR @LRIA RPB > IF?O>OV PR@E >P @LKQOLIM5 (EQQM://TTT.PLG>JL.AB/IF?O>OFBP/@LKQOLI05/) QL QFB QEB S>IRBP QL PIFABO MLPFQFLKP.)

Exercise 6.19
6FPR>IFWB QEB CIL@H FK >K BKQFOBIV AFCCBOBKQ T>V.

314

4EB .>QROB LC #LAB (S1.0)

6.14 Algorithmic Efficiency (or: Why does my $@(*%! run so slowly?)


) TLRIA IFHB QL EFAB QEB A>OH QORQE ?BEFKA TBgSB GRPQ ALKB, ?B@>RPB ) TLRIA IFHB VLR QL ?B E>MMV >KA IFSB > CRICFIIFKD >KA JB>KFKDCRI IFCB. "RQ ) >IPL TLRIA IFHB QL ?B >?IB QL PIBBM >Q KFDEQ TFQELRQ TLOOVFKD >?LRQ VLR PL JR@E. 3L FQ FP TFQE > EB>SV EB>OQ QE>Q ) JRPQ ?OFKD RM QEFP QLMF@. 'OLRM ?BE>SFLOP >OB TLKABOCRI. "RQ QEBV @>K ?B PILT, >KA QEB JLOB BIBJBKQP FK QEB DOLRM, QEB PILTBO QEBV @>K ?B. 5PR>IIV, TEBK TB Q>IH >?LRQ 0OL@BPPFKD PHBQ@EBP ORKKFKD PILTIV, FQgP ?B@>RPB AO>TFKD QL QEB P@OBBK @>K ?B PILTeQEB JLOB VLR AO>T, QEB PILTBO VLRO PHBQ@E ORKP. 4EFP FP >@QR>IIV > @>PB, ELTBSBO, TEBOB QEB PILTKBPP ABOFSBP COLJ QEB >IDLOFQEJ FQPBIC. ,BQgP AFP@RPP. #LJMRQBO P@FBKQFPQP @I>PPFCV >IDLOFQEJP TFQE PLJBQEFKD @>IIBA h"FD / KLQ>QFLK,i TEF@E ABP@OF?BP QEB BCCF@FBK@V LC >K >IDLOFQEJ: ELT J>KV @LJMRQ>QFLK>I @V@IBP ALBP FQ OBNRFOB QL @LJMIBQB? ,BQgP @LKPFABO > PFJMIB >K>ILD PB>O@E MOL?IBJ. 9LR E>SB > ?>PHBQ @LKQ>FKFKD LKB ERKAOBA @EL@LI>QB QOB>QP, LKIV LKB LC TEF@E FP MROB A>OH @EL@LI>QB. 4E>QgP QEB LKB VLR T>KQ QL B>Q. 4L CFKA FQ, VLR MF@H QEB @EL@LI>QBP LRQ LC QEB ?>PHBQ LKB ?V LKB. 3ROB, VLR JFDEQ ?B IR@HV >KA CFKA FQ LK QEB CFOPQ QOV, ?RQ FK QEB TLOPQ-@>PB P@BK>OFL VLR E>SB QL @EB@H >II LKB ERKAOBA ?BCLOB VLR CFKA QEB A>OH @EL@LI>QB. 4L CFKA LKB QEFKD FK LKB ERKAOBA, VLR E>SB QL @EB@H LKB ERKAOBA QEFKDP (LO QL CFKA LKB QEFKD FK . QEFKDP, VLR E>SB QL @EB@H . QFJBP.) 9LRO "FD / .LQ>QFLK FP .. 4EFP, FK@FABKQ>IIV, FP QEB "FD / .LQ>QFLK QE>Q ABP@OF?BP LRO PFJMIB M>OQF@IB PVPQBJ. )C TB E>SB . M>OQF@IBP, TB E>SB QL ORK >KA AFPMI>V QELPB M>OQF@IBP . QFJBP. .LT, IBQgP QEFKH >?LRQ > DOLRM ?BE>SFLO (PR@E >P CIL@HFKD). &LO BSBOV IC> L?GB@Q, TB E>SB QL @EB@H BSBOV LQEBO IC> L?GB@Q (CLO FQP SBIL@FQV >KA IL@>QFLK). ,BQgP P>V TB E>SB LKB ERKAOBA ?LFAP. &LO ?LFA #1, TB KBBA QL @EB@H LKB ERKAOBA ?LFAP; CLO ?LFA #2, TB KBBA QL @EB@H LKB ERKAOBA ?LFAP, >KA PL LK >KA PL CLOQE. &LO LKB ERKAOBA ?LFAP, TB KBBA QL MBOCLOJ LKB ERKAOBA QFJBP LKB ERKAOBA @EB@HP, LO QBK QELRP>KA. .L MOL?IBJ: @LJMRQBOP >OB C>PQ >KA @>K AL QEFKDP QBK QELRP>KA QFJBP MOBQQV B>PFIV. ,BQgP QOV LKB QELRP>KA. 1,000 U 1,000 = 1,000,000 @V@IBP. /+, QEFP FP O>QEBO PILT, ?RQ PQFII PLJBTE>Q J>K>DB>?IB. ,BQgP QOV 10,000 BIBJBKQP: 10,000 U 10,000 BIBJBKQP = 100,000,000 @V@IBP. .LT, TBgOB OB>IIV DBQQFKD PILT. 2B>IIV, OB>IIV, OB>IIV PILT. .LQF@B PLJBQEFKD LAA? !P QEB KRJ?BO LC BIBJBKQP FK@OB>PBP ?V > C>@QLO LC 10, QEB KRJ?BO LC OBNRFOBA @V@IBP FK@OB>PBP ?V > C>@QLO LC 100. /O >P QEB KRJ?BO LC BIBJBKQP FK@OB>PBP ?V > C>@QLO LC ., QEB @V@IBP FK@OB>PB ?V > C>@QLO LC . QFJBP .. 4EFP FP HKLTK >P "FD / .LQ>QFLK .3NR>OBA. ) HKLT TE>Q VLR >OB QEFKHFKD. 9LR >OB QEFKHFKD: h.L MOL?IBJ; TFQE CIL@HFKD, TB LKIV KBBA QL @LKPFABO QEB ?LFAP QE>Q >OB @ILPB QL LQEBO ?LFAP. 3L BSBK FC TB E>SB 1,000 ?LFAP, TB @>K GRPQ ILLH >Q, P>V, QEB 5 @ILPBPQ ?LFAP >KA QEBK TB LKIV E>SB 5,000 @V@IBP.i 9LR M>RPB CLO > 315

#E>MQBO 6. !RQLKLJLRP !DBKQP

JLJBKQ, >KA QEBK PQ>OQ QEFKHFKD: h3L CLO B>@E ?LFA ) GRPQ KBBA QL @EB@H >II QEB ?LFAP >KA CFKA QEB CFSB @ILPBPQ LKBP >KA )gJ DLLA!i 3BB QEB @>Q@E-22? %SBK FC TB LKIV T>KQ QL ILLH >Q QEB @ILPB LKBP, QEB LKIV T>V QL HKLT TE>Q QEB @ILPB LKBP >OB TLRIA ?B QL @EB@H >II LC QEBJ. /O FP QEBOB >KLQEBO T>V? ,BQgP Q>HB > KRJ?BO QE>Q TB JFDEQ >@QR>IIV T>KQ QL RPB, ?RQ TLRIA PQFII ORK QLL PILTIV: 2,000 (4,000,000 @V@IBP OBNRFOBA). 7E>Q FC TB @LRIA AFSFAB QEB P@OBBK FKQL > DOFA? 7B TLRIA Q>HB >II 2,000 ?LFAP >KA >PPFDK B>@E ?LFA QL > @BII TFQEFK QE>Q DOFA. 7B TLRIA QEBK ?B >?IB QL ILLH >Q B>@E ?LFA >KA @LJM>OB FQ QL FQP KBFDE?LOP TFQEFK QE>Q @BII >Q >KV DFSBK JLJBKQ. )J>DFKB > 10 U 10 DOFA. )K > PVPQBJ LC 2,000 BIBJBKQP, LK >SBO>DB, >MMOLUFJ>QBIV 20 BIBJBKQP TLRIA ?B CLRKA FK B>@E @BII (20 U 10 U 10 = 2,000). %>@E @BII TLRIA QEBK OBNRFOB 20 U 20 = 400 @V@IBP. 7FQE 100 @BIIP, TBgA E>SB 100 U 400 = 40,000 @V@IBP, > J>PPFSB P>SFKDP LSBO 4,000,000.

F64B?2 6.37

4EFP QB@EKFNRB FP HKLTK >P h?FK-I>QQF@B PM>QF>I PR?AFSFPFLKi >KA FP LRQIFKBA FK JLOB ABQ>FI FK (PROMOFPB, PROMOFPB) 2BVKLIAPgP 2000 M>MBO, h)KQBO>@QFLK TFQE 'OLRMP LC !RQLKLJLRP #E>O>@QBOPi (EQQM://TTT.OBA3A.@LJ/@TO/M>MBOP/2000/MFM.MAC). (LT AL TB FJMIBJBKQ PR@E >K >IDLOFQEJ FK 0OL@BPPFKD? /KB T>V FP QL HBBM JRIQFMIB ALL;S*CMN P. /KB ALL;S*CMN TLRIA HBBM QO>@H LC >II QEB ?LFAP, GRPQ IFHB FK LRO CIL@HFKD BU>JMIB.
ALL;S*CMN< IC>> <IC>M;

)K >AAFQFLK QL QE>Q ALL;S*CMN , TB PQLOB >K >AAFQFLK>I OBCBOBK@B QL B>@E IC> L?GB@Q FK > QTL-AFJBKPFLK>I ALL;S*CMN . &LO B>@E @BII FK QEB DOFA, QEBOB FP >K ALL;S*CMN QE>Q QO>@HP QEB L?GB@QP FK QE>Q @BII.

316

4EB .>QROB LC #LAB (S1.0)


ALL;S*CMN< IC>>8989 ALC>;

)K QEB J>FK >L;Q() ILLM, B>@E IC> L?GB@Q QEBK OBDFPQBOP FQPBIC FK QEB >MMOLMOF>QB @BII >@@LOAFKD QL FQP IL@>QFLK.
CHN =IFOGH = CHN(<IC>.R) / L?MIFONCIH; CHN LIQ = CHN(<IC>.S) /L?MIFONCIH; ALC>8=IFOGH98LIQ9.;>>(<IC>);

4EBK TEBK FQ @LJBP QFJB QL E>SB QEB ?LFAP @EB@H CLO KBFDE?LOP, QEBV @>K ILLH >Q LKIV QELPB FK QEBFO M>OQF@RI>O @BII (FK QORQE, TB >IPL KBBA QL @EB@H KBFDE?LOFKD @BIIP QL AB>I TFQE ?LOABO @>PBP). Example 6.10: Bin-lattice spatial subdivision
CHN =IFOGH = CHN(<IC>.R) / L?MIFONCIH; CHN LIQ = CHN(<IC>.S) /L?MIFONCIH; <IC>.@FI=E(<IC>M); <IC>.@FI=E(ALC>8=IFOGH98LIQ9); Instead of looking at all the boids, just this cell

7BgOB LKIV @LSBOFKD QEB ?>PF@P EBOB; CLO QEB CRII @LAB, @EB@H QEB ?LLHgP TB?PFQB. .LT, QEBOB >OB @BOQ>FKIV CI>TP TFQE QEFP PVPQBJ. 7E>Q FC >II QEB ?LFAP @LKDOBD>QB FK QEB @LOKBO >KA IFSB FK QEB P>JB @BII? 4EBK ALKgQ TB E>SB QL @EB@H >II 2,000 >D>FKPQ >II 2,000? 4EB DLLA KBTP FP QE>Q QEFP KBBA CLO LMQFJFW>QFLK FP > @LJJLK LKB >KA QEBOB >OB > TFAB S>OFBQV LC PFJFI>O QB@EKFNRBP LRQ QEBOB. &LO RP, FQgP IFHBIV QE>Q > ?>PF@ >MMOL>@E TFII ?B DLLA BKLRDE (FK JLPQ @>PBP, VLR TLKgQ KBBA LKB >Q >II.) &LO >KLQEBO, JLOB PLMEFPQF@>QBA >MMOL>@E, @EB@H LRQ QLUF@IF?P' /@QOBB BU>JMIBP (EQQM://QLUF@IF?P.LOD/2010/02/KBT-M>@H>DB-PFJRQFIP/).

6.15 A Few Last Notes: Optimization Tricks


4EFP FP PLJBQEFKD LC > JLJBKQLRP L@@>PFLK. 4EB BKA LC #E>MQBO 6 J>OHP QEB BKA LC LRO PQLOV LC JLQFLK (FK QEB @LKQBUQ LC QEFP ?LLH, QE>Q FP). 7B PQ>OQBA TFQE QEB @LK@BMQ LC > SB@QLO, JLSBA LK QL CLO@BP, ABPFDKBA PVPQBJP LC J>KV BIBJBKQP, BU>JFKBA MEVPF@P IF?O>OFBP, ?RFIQ BKQFQFBP TFQE ELMBP >KA AOB>JP >KA CB>OP, >KA PFJRI>QBA BJBODBK@B. 4EB PQLOV ALBPKgQ BKA EBOB, ?RQ FQ ALBP Q>HB > ?FQ LC > QROK. 4EB KBUQ QTL @E>MQBOP TLKgQ CL@RP LK JLSFKD ?LAFBP, ?RQ O>QEBO LK PVPQBJP LC ORIBP. "BCLOB TB DBQ QEBOB, ) E>SB > CBT NRF@H FQBJP )gA IFHB QL JBKQFLK QE>Q >OB FJMLOQ>KQ TEBK TLOHFKD TFQE QEB BU>JMIBP FK #E>MQBOP 1 QEOLRDE 6. 4EBV >IPL OBI>QB QL LMQFJFWFKD VLRO @LAB, TEF@E CFQP FK TFQE QEB MOBSFLRP PB@QFLK.

317

#E>MQBO 6. !RQLKLJLRP !DBKQP

1) Magnitude squared (or sometimes distance squared)


7E>Q FP J>DKFQRAB PNR>OBA >KA TEBK PELRIA VLR RPB FQ? ,BQgP OBSFPFQ ELT QEB J>DKFQRAB LC > SB@QLO FP @>I@RI>QBA.
@FI;N G;A() U L?NOLH MKLN(R*R + S*S); W

->DKFQRAB OBNRFOBP QEB PNR>OB OLLQ LMBO>QFLK. !KA FQ PELRIA. !CQBO >II, FC VLR T>KQ QEB J>DKFQRAB LC > SB@QLO, QEBK VLRgSB DLQ QL ILLH RM QEB 0VQE>DLOB>K QEBLOBJ >KA @LJMRQB FQ (TB AFA QEFP FK #E>MQBO 1). (LTBSBO, FC VLR @LRIA PLJBELT PHFM RPFKD QEB PNR>OB OLLQ, VLRO @LAB TLRIA ORK C>PQBO. ,BQgP @LKPFABO > PFQR>QFLK TEBOB VLR GRPQ T>KQ QL HKLT QEB OBI>QFSB J>DKFQRAB LC > SB@QLO. &LO BU>JMIB, FP QEB J>DKFQRAB DOB>QBO QE>K QBK? (!PPRJB > .3?=NIL P .)
C@ (P.G;A() > 10) U // "I 0IG?NBCHA! W

7BII, QEFP FP BNRFS>IBKQ QL P>VFKD:


C@ (P.G;A0K() > 100) U // "I 0IG?NBCHA! W

!KA ELT FP J>DKFQRAB PNR>OBA @>I@RI>QBA?


@FI;N G;A0K() U L?NOLH R*R + S*S; W

3>JB >P J>DKFQRAB, ?RQ TFQELRQ QEB PNR>OB OLLQ. )K QEB @>PB LC > PFKDIB .3?=NIL L?GB@Q, QEFP TFII KBSBO J>HB > PFDKFCF@>KQ AFCCBOBK@B LK > 0OL@BPPFKD PHBQ@E. (LTBSBO, FC VLR >OB @LJMRQFKD QEB J>DKFQRAB LC QELRP>KAP LC .3?=NIL L?GB@QP B>@E QFJB QEOLRDE >L;Q() , RPFKD G;A0K() FKPQB>A LC G;A() @LRIA EBIM VLRO @LAB ORK > TBB ?FQ C>PQBO. (.LQB: G;A0K() FP LKIV >S>FI>?IB FK 0OL@BPPFKD 2.0>1 LO I>QBO.)

2) Sine and cosine lookup tables


4EBOBgP > M>QQBOK EBOB. 7E>Q HFKAP LC CRK@QFLKP >OB PILT QL @LJMRQB? 3NR>OB OLLQ. 3FKB. #LPFKB. 4>KDBKQ. !D>FK, FC VLR GRPQ KBBA > PFKB LO @LPFKB S>IRB EBOB LO QEBOB FK VLRO @LAB, VLR >OB KBSBO DLFKD QL ORK FKQL > MOL?IBJ. "RQ TE>Q FC VLR E>A PLJBQEFKD IFHB QEFP?

318

4EB .>QROB LC #LAB (S1.0)


PIC> >L;Q() U @IL (CHN C = 0; C < 10000; C++) U JLCHNFH(MCH(.')); W W

3ROB, QEFP FP > QLQ>IIV OFAF@RILRP @LAB PKFMMBQ QE>Q VLR TLRIA KBSBO TOFQB. "RQ FQ FIIRPQO>QBP > @BOQ>FK MLFKQ. )C VLR >OB @>I@RI>QFKD QEB PFKB LC MF QBK QELRP>KA QFJBP, TEV KLQ GRPQ @>I@RI>QB FQ LK@B, P>SB QE>Q S>IRB, >KA OBCBO QL FQ TEBKBSBO KB@BPP>OV? 4EFP FP QEB MOFK@FMIB ?BEFKA PFKB >KA @LPFKB ILLHRM Q>?IBP. )KPQB>A LC @>IIFKD QEB PFKB >KA @LPFKB CRK@QFLKP FK VLRO @LAB TEBKBSBO VLR KBBA QEBJ, VLR @>K ?RFIA >K >OO>V QE>Q PQLOBP QEB OBPRIQP LC PFKB >KA @LPFKB >Q >KDIBP ?BQTBBK 0 >KA 14-:.' >KA GRPQ ILLH RM QEB S>IRBP TEBK VLR KBBA QEBJ. &LO BU>JMIB, EBOB >OB QTL >OO>VP QE>Q PQLOB QEB PFKB >KA @LPFKB S>IRBP CLO BSBOV >KDIB, 0 QL 359 ABDOBBP.
@FI;N MCHP;FO?M89 = H?Q @FI;N83609; @FI;N =IMP;FO?M89 = H?Q @FI;N83609; @IL (CHN C = 0; C < 360; C++) U MCHP;FO?M8C9 = MCH(L;>C;HM(C)); =IMP;FO?M8C9 = =IM(L;>C;HM(C)); W

.LT, TE>Q FC VLR KBBA QEB S>IRB LC PFKB LC MF?


CHN ;HAF? = CHN(>?AL??M(.')); @FI;N ;HMQ?L = MCHP;FO?M8;HAF?9;

! JLOB PLMEFPQF@>QBA BU>JMIB LC QEFP QB@EKFNRB FP >S>FI>?IB LK QEB 0OL@BPPFKD TFHF (EQQM://TFHF.MOL@BPPFKD.LOD/T/3FK/#LP=ILLH-RM=Q>?IB).

3) Making gajillions of unnecessary PVector objects


) E>SB QL >AJFQ, ) >J MBOE>MP QEB ?FDDBPQ @RIMOFQ LC QEFP I>PQ KLQB. )K C>@Q, FK QEB FKQBOBPQ LC TOFQFKD @IB>O >KA RKABOPQ>KA>?IB BU>JMIBP, ) LCQBK @ELLPB QL J>HB BUQO> .3?=NIL L?GB@QP TEBK ) >?PLIRQBIV AL KLQ KBBA QL. &LO QEB JLPQ M>OQ, QEFP FP KLQ > MOL?IBJ >Q >II. "RQ PLJBQFJBP, FQ @>K ?B. ,BQgP Q>HB > ILLH >Q >K BU>JMIB.
PIC> >L;Q() U @IL (3?BC=F? P : P?BC=F?M) U .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); P.M??E(GIOM?); W W

,BQgP P>V LRO ALL;S*CMN LC SBEF@IBP E>P LKB QELRP>KA SBEF@IBP FK FQ. 7B GRPQ J>AB LKB QELRP>KA KBT .3?=NIL L?GB@QP BSBOV PFKDIB QFJB QEOLRDE >L;Q() . .LT, LK >KV LIg I>MQLM LO ABPHQLM @LJMRQBO VLRgSB MRO@E>PBA FK OB@BKQ QFJBP, VLRO PHBQ@E TFII IFHBIV KLQ OBDFPQBO > @LJMI>FKQ, ORK PILTIV, LO E>SB >KV MOL?IBJP. !CQBO >II, VLRgSB DLQ QLKP LC 2!-, >KA *>S> TFII

319

#E>MQBO 6. !RQLKLJLRP !DBKQP

?B >?IB QL E>KAIB J>HFKD > QELRP>KA LO PL QBJMLO>OV L?GB@QP >KA AFPMLPB LC QEBJ TFQELRQ JR@E LC > MOL?IBJ. )C VLRO KRJ?BOP DOLT I>ODBO (>KA QEBV B>PFIV @LRIA) LO MBOE>MP JLOB IFHBIV, FC VLR >OB TLOHFKD TFQE 0OL@BPPFKD LK !KAOLFA, VLR TFII >IJLPQ @BOQ>FKIV ORK FKQL > MOL?IBJ. )K @>PBP IFHB QEFP VLR T>KQ QL ILLH CLO T>VP QL OBAR@B QEB KRJ?BO LC .3?=NIL L?GB@QP VLR J>HB. !K L?SFLRP CFU CLO QEB >?LSB @LAB FP:
PIC> >L;Q() U .3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6); @IL (3?BC=F? P : P?BC=F?M) U P.M??E(GIOM?); W W

.LT VLRgSB J>AB GRPQ LKB .3?=NIL FKPQB>A LC LKB QELRP>KA. %SBK ?BQQBO, VLR @LRIA QROK QEB .3?=NIL FKQL > DIL?>I S>OF>?IB >KA GRPQ >PPFDK QEB R >KA S S>IRB:
.3?=NIL GIOM? = H?Q .3?=NIL(); PIC> >L;Q() U GIOM?.R = GIOM?5; GIOM?.S = GIOM?6; @IL (3?BC=F? P : P?BC=F?M) U P.M??E(GIOM?); W W

.LT VLR KBSBO J>HB > KBT .3?=NIL ; VLR RPB GRPQ LKB LSBO QEB IBKDQE LC VLRO PHBQ@E! 4EOLRDELRQ QEB ?LLHgP BU>JMIBP, VLR @>K CFKA ILQP LC LMMLOQRKFQFBP QL OBAR@B QEB KRJ?BO LC QBJMLO>OV L?GB@QP. ,BQgP ILLH >Q LKB JLOB. (BOB FP > PKFMMBQ COLJ LRO M??E() CRK@QFLK.
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>); +0;9JEH IJ;;H = +0;9JEH.IK8(:;I?H;:,L;BE9?JO); MN??L.FCGCN(G;R@IL=?); L?NOLH MN??L; Create a new PVector to store the steering force.

3BB ELT TBgSB J>AB QTL .3?=NIL L?GB@QP? &FOPQ, TB CFDROB LRQ QEB ABPFOBA SB@QLO, QEBK TB @>I@RI>QB QEB PQBBOFKD CLO@B. .LQF@B ELT TB @LRIA OBTOFQB QEFP QL @OB>QB LKIV LKB .3?=NIL .
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N, FI=;NCIH); >?MCL?>.HILG;FCT?(); >?MCL?>.GOFN(G;RMJ??>);

320

4EB .>QROB LC #LAB (S1.0)

:;I?H;:.IK8(L;BE9?JO); >?MCL?>.FCGCN(G;R@IL=?); L?NOLH >?MCL?>;

Calculate the steering force in the desired PVector.

7B ALKgQ >@QR>IIV KBBA > PB@LKA .3?=NIL @>IIBA MN??L . 7B @LRIA GRPQ RPB QEB ABPFOBA .3?=NIL L?GB@Q >KA QROK FQ FKQL QEB PQBBOFKD CLO@B ?V PR?QO>@QFKD SBIL@FQV. ) AFAKgQ AL QEFP FK JV BU>JMIB ?B@>RPB FQ FP JLOB @LKCRPFKD QL OB>A. "RQ FK PLJB @>PBP, FQ J>V ?B DOB>QIV JLOB BCCF@FBKQ.

Exercise 6.20
%IFJFK>QB >P J>KV QBJMLO>OV .3?=NIL L?GB@QP COLJ QEB CIL@HFKD BU>JMIB >P MLPPF?IB. !IPL RPB G;A0K() TEBOB MLPPF?IB.

Exercise 6.21
5PB PQBBOFKD ?BE>SFLOP TFQE "LU2$ LO QLUF@IF?P.

321

#E>MQBO 6. !RQLKLJLRP !DBKQP

The Ecosystem Project


Step 6 Exercise: Use the concept of steering forces to drive the behavior of the creatures in your ecosystem. Some possibilities:

Create schools or flocks of creatures. Use a seeking behavior for creatures to search for food (for chasing moving prey, consider pursuit). Use a flow field for the ecosystem environment. For example, how does your system behave if the creatures live in a flowing river? Build a creature with countless steering behaviors (as many as you can reasonably add). Think about ways to vary the weights of these behaviors so that you can dial those behaviors up and down, mixing and matching on the fly. How are creatures initial weights set? What rules drive how the weights change over time? Complex systems can be nested. Can you design a single creature out of a flock of boids? And can you then make a flock of those creatures? Complex systems can have memory (and be adaptive). Can the history of your ecosystem affect the behavior in its current state? (This could be the driving force behind how the creatures adjust their steering force weights.)

322

4EB .>QROB LC #LAB (S1.0)

Chapter 7. Cellular Automata


I*< =9.F 9632 F<B :B@A 5.C2 . 3.6?9F 9.?42 052082?/<.?1 .;1 . =92;A63B9 @B==9F <3 39.A 0<B;A2?@ <3 AD< 0<9<?@. !A 6@ =<@@6/92 A< D<?8 D6A5 =2;069 .;1 4?.=5 =.=2? /BA 6A 6@ :B05 2.@62?, =.?A60B9.?9F 3<? /246;;2?@, A< B@2 0<B;A2?@ .;1 . /<.?1.J Martin Gardner, .7=9BH=:=7 AA9F=75B (October 1970)

)K QEFP @E>MQBO, TBgOB DLFKD QL Q>HB > ?OB>H COLJ Q>IHFKD >?LRQ SB@QLOP >KA JLQFLK. )K C>@Q, QEB OBPQ LC QEB ?LLH TFII JLPQIV CL@RP LK PVPQBJP >KA >IDLOFQEJP (>I?BFQ LKBP QE>Q TB @>K, PELRIA, >KA TFII >MMIV QL JLSFKD ?LAFBP). )K QEB MOBSFLRP @E>MQBO, TB BK@LRKQBOBA LRO CFOPQ 0OL@BPPFKD BU>JMIB LC > @LJMIBU PVPQBJ: CIL@HFKD. 7B ?OFBCIV PQ>QBA QEB @LOB MOFK@FMIBP ?BEFKA @LJMIBU PVPQBJP: JLOB QE>K QEB PRJ LC FQP M>OQP, > @LJMIBU PVPQBJ FP > PVPQBJ LC BIBJBKQP, LMBO>QFKD FK M>O>IIBI, TFQE PELOQ-O>KDB OBI>QFLKPEFMP QE>Q >P > TELIB BUEF?FQ BJBODBKQ ?BE>SFLO. 4EFP BKQFOB @E>MQBO FP DLFKD QL ?B ABAF@>QBA QL ?RFIAFKD >KLQEBO @LJMIBU PVPQBJ PFJRI>QFLK FK 0OL@BPPFKD. /AAIV, TB >OB DLFKD QL Q>HB PLJB PQBMP ?>@HT>OA >KA PFJMIFCV QEB BIBJBKQP LC LRO PVPQBJ. .L ILKDBO >OB QEB FKAFSFAR>I BIBJBKQP DLFKD QL ?B JBJ?BOP LC > MEVPF@P TLOIA; FKPQB>A TB TFII ?RFIA > PVPQBJ LRQ LC QEB PFJMIBPQ AFDFQ>I BIBJBKQ MLPPF?IB, > PFKDIB ?FQ. 4EFP ?FQ FP DLFKD QL ?B @>IIBA > @BII >KA FQP S>IRB (0 LO 1) TFII ?B @>IIBA FQP PQ>QB. 7LOHFKD TFQE PR@E PFJMIB BIBJBKQP TFII EBIM RP RKABOPQ>KA JLOB LC QEB ABQ>FIP ?BEFKA ELT @LJMIBU PVPQBJP TLOH, >KA TBgII >IPL ?B >?IB QL BI>?LO>QB LK PLJB MOLDO>JJFKD QB@EKFNRBP QE>Q TB @>K >MMIV QL @LAB-?>PBA MOLGB@QP.

323

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

7.1 What Is a Cellular Automaton?


&FOPQ, IBQgP DBQ LKB QEFKD PQO>FDEQ. 4EB QBOJ .077@7,= ,@?:8,?, FP MIRO>I. /RO @LAB BU>JMIBP TFII PFJRI>QB GRPQ LKBe> .077@7,= ,@?:8,?:9 , PFKDRI>O. 4L PFJMIFCV LRO IFSBP, TBgII >IPL OBCBO QL @BIIRI>O >RQLJ>Q> >P h#!.i )K #E>MQBOP 1 QEOLRDE 6, LRO L?GB@QP (JLSBO, M>OQF@IB, SBEF@IB, ?LFA) DBKBO>IIV BUFPQBA FK LKIV LKB hPQ>QB.i 4EBV JFDEQ E>SB JLSBA >OLRKA TFQE >AS>K@BA ?BE>SFLOP >KA MEVPF@P, ?RQ RIQFJ>QBIV QEBV OBJ>FKBA QEB P>JB QVMB LC L?GB@Q LSBO QEB @LROPB LC QEBFO AFDFQ>I IFCBQFJB. 7BgSB >IIRABA QL QEB MLPPF?FIFQV QE>Q QEBPB BKQFQFBP @>K @E>KDB LSBO QFJB (CLO BU>JMIB, QEB TBFDEQP LC PQBBOFKD hABPFOBPi @>K S>OV), ?RQ TB E>SBKgQ CRIIV MRQ QEFP FKQL MO>@QF@B. )K QEFP @LKQBUQ, @BIIRI>O >RQLJ>Q> J>HB > DOB>Q CFOPQ PQBM FK ?RFIAFKD > PVPQBJ LC J>KV L?GB@QP QE>Q E>SB S>OVFKD PQ>QBP LSBO QFJB. ! @BIIRI>O >RQLJ>QLK FP > JLABI LC > PVPQBJ LC h@BIIi L?GB@QP TFQE QEB CLIILTFKD @E>O>@QBOFPQF@P. j 4EB @BIIP IFSB LK > 2=4/ . (7BgII PBB BU>JMIBP FK ?LQE LKB >KA QTL AFJBKPFLKP FK QEFP @E>MQBO, QELRDE > @BIIRI>O >RQLJ>QLK @>K BUFPQ FK >KV CFKFQB KRJ?BO LC AFJBKPFLKP.) j %>@E @BII E>P > >?,?0. 4EB KRJ?BO LC PQ>QB MLPPF?FIFQFBP FP QVMF@>IIV CFKFQB. 4EB PFJMIBPQ BU>JMIB E>P QEB QTL MLPPF?FIFQFBP LC 1 >KA 0 (LQEBOTFPB OBCBOOBA QL >P hLKi >KA hLCCi LO h>IFSBi >KA hAB>Ai). j %>@E @BII E>P > 90423-:=3::/. 4EFP @>K ?B ABCFKBA FK >KV KRJ?BO LC T>VP, ?RQ FQ FP QVMF@>IIV > IFPQ LC >AG>@BKQ @BIIP.

F64B?2 7.1

4EB ABSBILMJBKQ LC @BIIRI>O >RQLJ>Q> PVPQBJP FP QVMF@>IIV >QQOF?RQBA QL 3Q>KFP\>T 5I>J >KA *LEK SLK .BRJ>KK, TEL TBOB ?LQE OBPB>O@EBOP >Q QEB ,LP !I>JLP .>QFLK>I ,>?LO>QLOV FK

324

4EB .>QROB LC #LAB (S1.0)

.BT -BUF@L FK QEB 1940P. 5I>J T>P PQRAVFKD QEB DOLTQE LC @OVPQ>IP >KA SLK .BRJ>KK T>P FJ>DFKFKD > TLOIA LC PBIC-OBMIF@>QFKD OL?LQP. 4E>QgP OFDEQ, OL?LQP QE>Q ?RFIA @LMFBP LC QEBJPBISBP. /K@B TB PBB PLJB BU>JMIBP LC #! SFPR>IFWBA, FQgII ?B @IB>O ELT LKB JFDEQ FJ>DFKB JLABIFKD @OVPQ>I DOLTQE; QEB OL?LQP FAB> FP MBOE>MP IBPP L?SFLRP. #LKPFABO QEB ABPFDK LC > OL?LQ >P > M>QQBOK LK > DOFA LC @BIIP (QEFKH LC CFIIFKD FK PLJB PNR>OBP LK > MFB@B LC DO>ME M>MBO). .LT @LKPFABO > PBQ LC PFJMIB ORIBP QE>Q TLRIA >IILT QE>Q M>QQBOK QL @OB>QB @LMFBP LC FQPBIC LK QE>Q DOFA. 4EFP FP BPPBKQF>IIV QEB MOL@BPP LC > #! QE>Q BUEF?FQP ?BE>SFLO PFJFI>O QL ?FLILDF@>I OBMOLAR@QFLK >KA BSLIRQFLK. ()K@FABKQ>IIV, SLK .BRJ>KKgP @BIIP E>A QTBKQV-KFKB MLPPF?IB PQ>QBP.) 6LK .BRJ>KKgP TLOH FK PBIC-OBMIF@>QFLK >KA #! FP @LK@BMQR>IIV PFJFI>O QL TE>Q FP MOL?>?IV QEB JLPQ C>JLRP @BIIRI>O >RQLJ>QLK: QEB h'>JB LC ,FCB,i TEF@E TB TFII AFP@RPP FK ABQ>FI FK PB@QFLK 7.3. 0BOE>MP QEB JLPQ PFDKFCF@>KQ P@FBKQFCF@ (>KA IBKDQEV) TLOH PQRAVFKD @BIIRI>O >RQLJ>Q> >OOFSBA FK 2002: 3QBMEBK 7LICO>JgP 1,280-M>DB A *9K '=B8 C: .7=9B79 (EQQM://TTT.TLICO>JP@FBK@B.@LJ/ KHPLKIFKB/QL@.EQJI). !S>FI>?IB FK FQP BKQFOBQV CLO COBB LKIFKB, 7LICO>JgP ?LLH AFP@RPPBP ELT #! >OB KLQ PFJMIV KB>Q QOF@HP, ?RQ >OB OBIBS>KQ QL QEB PQRAV LC ?FLILDV, @EBJFPQOV, MEVPF@P, >KA >II ?O>K@EBP LC P@FBK@B. 4EFP @E>MQBO TFII ?>OBIV P@O>Q@E QEB PROC>@B LC QEB QEBLOFBP 7LICO>J LRQIFKBP (TB TFII CL@RP LK QEB @LAB FJMIBJBKQ>QFLK) PL FC QEB BU>JMIBP MOLSFABA PM>OH VLRO @ROFLPFQV, VLRgII CFKA MIBKQV JLOB QL OB>A >?LRQ FK EFP ?LLH.

7.2 Elementary Cellular Automata


4EB BU>JMIBP FK QEFP @E>MQBO TFII ?BDFK TFQE > PFJRI>QFLK LC 7LICO>JgP TLOH. 4L RKABOPQ>KA 7LICO>JgP BIBJBKQ>OV #!, TB PELRIA >PH LROPBISBP QEB NRBPQFLK: h7E>Q FP QEB PFJMIBPQ @BIIRI>O >RQLJ>QLK TB @>K FJ>DFKB?i 7E>QgP BU@FQFKD >?LRQ QEFP NRBPQFLK >KA FQP >KPTBO FP QE>Q BSBK TFQE QEB PFJMIBPQ #! FJ>DFK>?IB, TB TFII PBB QEB MOLMBOQFBP LC @LJMIBU PVPQBJP >Q TLOH. ,BQgP ?RFIA 7LICO>JgP BIBJBKQ>OV #! COLJ P@O>Q@E. #LK@BMQP CFOPQ, QEBK @LAB. 7E>Q >OB QEB QEOBB HBV BIBJBKQP LC > #!? 1) G=4/. 4EB PFJMIBPQ DOFA TLRIA ?B LKB-AFJBKPFLK>I: > IFKB LC @BIIP.

F64B?2 7.2

2) '?,?0> . 4EB PFJMIBPQ PBQ LC PQ>QBP (?BVLKA E>SFKD LKIV LKB PQ>QB) TLRIA ?B QTL PQ>QBP: 0 LO 1.

F64B?2 7.3

325

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

3) #0423-:=3::/. 4EB PFJMIBPQ KBFDE?LOELLA FK LKB AFJBKPFLK CLO >KV DFSBK @BII TLRIA ?B QEB @BII FQPBIC >KA FQP QTL >AG>@BKQ KBFDE?LOP: LKB QL QEB IBCQ >KA LKB QL QEB OFDEQ.

F64B?2 7.4: A ;2645/<?5<<1 6@ A5?22 0299@.

3L TB ?BDFK TFQE > IFKB LC @BIIP, B>@E TFQE >K FKFQF>I PQ>QB (IBQgP P>V FQ FP O>KALJ), >KA B>@E TFQE QTL KBFDE?LOP. 7BgII E>SB QL CFDROB LRQ TE>Q TB T>KQ QL AL TFQE QEB @BIIP LK QEB BADBP (PFK@B QELPB E>SB LKIV LKB KBFDE?LO B>@E), ?RQ QEFP FP PLJBQEFKD TB @>K PLOQ LRQ I>QBO.

F64B?2 7.5: *52 2142 0299 <;9F 5.@ . ;2645/<?5<<1 <3 AD<.

7B E>SBKgQ VBQ AFP@RPPBA, ELTBSBO, TE>Q FP MBOE>MP QEB JLPQ FJMLOQ>KQ ABQ>FI LC ELT @BIIRI>O >RQLJ>Q> TLOHeH=A9 . 7BgOB KLQ OB>IIV Q>IHFKD >?LRQ OB>I-TLOIA QFJB EBOB, ?RQ >?LRQ QEB #! IFSFKD LSBO > MBOFLA LC H=A9, TEF@E @LRIA >IPL ?B @>IIBA > 2090=,?4:9 >KA, FK LRO @>PB, TFII IFHBIV OBCBO QL QEB 1=,80 .:@9? LC >K >KFJ>QFLK. 4EB CFDROBP >?LSB PELT RP QEB #! >Q QFJB BNR>IP 0 LO DBKBO>QFLK 0. 4EB NRBPQFLKP TB E>SB QL >PH LROPBISBP >OB: $CK 8C K9 7CADIH9 H<9 GH5H9G :CF 5@@ 79@@G 5H ;9B9F5H=CB 1? AB8 ;9B9F5H=CB 2? !KA PL LK >KA PL CLOQE.

F64B?2 7.6

,BQgP P>V TB E>SB >K FKAFSFAR>I @BII FK QEB #!, >KA IBQgP @>II FQ #%,,. 4EB CLOJRI> CLO @>I@RI>QFKD #%,,gP PQ>QB >Q >KV DFSBK QFJB N FP >P CLIILTP:
C!'' IJ7J; 7J J?C; J = <(C!'' D;?=>8EH>EE: 7J J?C; J - 1)

)K LQEBO TLOAP, > @BIIgP KBT PQ>QB FP > CRK@QFLK LC >II QEB PQ>QBP FK QEB @BIIgP KBFDE?LOELLA >Q QEB MOBSFLRP JLJBKQ FK QFJB (LO AROFKD QEB MOBSFLRP DBKBO>QFLK). 7B @>I@RI>QB > KBT PQ>QB S>IRB ?V ILLHFKD >Q >II QEB MOBSFLRP KBFDE?LO PQ>QBP.

326

4EB .>QROB LC #LAB (S1.0)

F64B?2 7.7

.LT, FK QEB TLOIA LC @BIIRI>O >RQLJ>Q>, QEBOB >OB J>KV T>VP TB @LRIA @LJMRQB > @BIIgP PQ>QB COLJ > DOLRM LC @BIIP. #LKPFABO ?IROOFKD >K FJ>DB. ('RBPP TE>Q? )J>DB MOL@BPPFKD TLOHP TFQE #!-IFHB ORIBP.) ! MFUBIgP KBT PQ>QB (F.B. FQP @LILO) FP QEB >SBO>DB LC >II LC FQP KBFDE?LOPg @LILOP. 7B @LRIA >IPL P>V QE>Q > @BIIgP KBT PQ>QB FP QEB PRJ LC >II LC FQP KBFDE?LOPg PQ>QBP. 7FQE 7LICO>JgP BIBJBKQ>OV #!, ELTBSBO, TB @>K >@QR>IIV AL PLJBQEFKD > ?FQ PFJMIBO >KA PBBJFKDIV >?PROA: 7B @>K ILLH >Q >II QEB MLPPF?IB @LKCFDRO>QFLKP LC > @BII >KA FQP KBFDE?LO >KA ABCFKB QEB PQ>QB LRQ@LJB CLO BSBOV MLPPF?IB @LKCFDRO>QFLK. )Q PBBJP OFAF@RILRPeTLRIAKgQ QEBOB ?B T>V QLL J>KV MLPPF?FIFQFBP CLO QEFP QL ?B MO>@QF@>I? ,BQgP DFSB FQ > QOV. 7B E>SB QEOBB @BIIP, B>@E TFQE > PQ>QB LC 0 LO 1. (LT J>KV MLPPF?IB T>VP @>K TB @LKCFDROB QEB PQ>QBP? )C VLR ILSB ?FK>OV, VLRgII KLQF@B QE>Q QEOBB @BIIP ABCFKB > 3-?FQ KRJ?BO, >KA ELT EFDE @>K VLR @LRKQ TFQE 3 ?FQP? 5M QL 8. ,BQgP E>SB > ILLH.

F64B?2 7.8

/K@B TB E>SB ABCFKBA >II QEB MLPPF?IB KBFDE?LOELLAP, TB KBBA QL ABCFKB >K LRQ@LJB (KBT PQ>QB S>IRB: 0 LO 1) CLO B>@E KBFDE?LOELLA @LKCFDRO>QFLK.

F64B?2 7.9

4EB PQ>KA>OA 7LICO>J JLABI FP QL PQ>OQ DBKBO>QFLK 0 TFQE >II @BIIP E>SFKD > PQ>QB LC 0 BU@BMQ CLO QEB JFAAIB @BII, TEF@E PELRIA E>SB > PQ>QB LC 1.

F64B?2 7.10

327

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

2BCBOOFKD QL QEB ORIBPBQ >?LSB, IBQgP PBB ELT > DFSBK @BII (TBgII MF@H QEB @BKQBO LKB) TLRIA @E>KDB COLJ DBKBO>QFLK 0 QL DBKBO>QFLK 1.

F64B?2 7.11

4OV >MMIVFKD QEB P>JB ILDF@ QL >II LC QEB @BIIP >?LSB >KA CFII FK QEB BJMQV @BIIP. .LT, IBQgP DL M>PQ GRPQ LKB DBKBO>QFLK >KA @LILO QEB @BIIP e0 JB>KP TEFQB, 1 JB>KP ?I>@He>KA PQ>@H QEB DBKBO>QFLKP, TFQE B>@E KBT DBKBO>QFLK >MMB>OFKD ?BILT QEB MOBSFLRP LKB.

F64B?2 7.12: (B92 90

4EB ILT-OBPLIRQFLK PE>MB TBgOB PBBFKD >?LSB FP QEB h3FBOMF]PHF QOF>KDIB.i .>JBA >CQBO QEB 0LIFPE J>QEBJ>QF@F>K 7>@\>T 3FBOMF]PHF, FQgP > CO>@Q>I M>QQBOK QE>Q TBgII BU>JFKB FK QEB KBUQ @E>MQBO. 4E>QgP OFDEQ: QEFP FK@OBAF?IV PFJMIB PVPQBJ LC 0P >KA 1P, TFQE IFQQIB KBFDE?LOELLAP LC QEOBB @BIIP, @>K DBKBO>QB > PE>MB >P PLMEFPQF@>QBA >KA ABQ>FIBA >P QEB 3FBOMF]PHF QOF>KDIB. ,BQgP ILLH >Q FQ >D>FK, LKIV TFQE B>@E @BII > PFKDIB MFUBI TFAB PL QE>Q QEB OBPLIRQFLK FP JR@E EFDEBO.

328

4EB .>QROB LC #LAB (S1.0)

F64B?2 7.13: (B92 90

4EFP M>OQF@RI>O OBPRIQ AFAKgQ E>MMBK ?V >@@FABKQ. ) MF@HBA QEFP PBQ LC ORIBP ?B@>RPB LC QEB M>QQBOK FQ DBKBO>QBP. 4>HB > ILLH >Q &FDROB 7.8 LKB JLOB QFJB. .LQF@B ELT QEBOB >OB BFDEQ MLPPF?IB KBFDE?LOELLA @LKCFDRO>QFLKP; TB QEBOBCLOB ABCFKB > hORIBPBQi >P > IFPQ LC 8 ?FQP. 3L QEFP M>OQF@RI>O ORIB @>K ?B FIIRPQO>QBA >P CLIILTP:

F64B?2 7.14: (B92 90

%FDEQ 0P >KA 1P JB>KP >K 8-?FQ KRJ?BO. (LT J>KV @LJ?FK>QFLKP LC BFDEQ 0P >KA 1P >OB QEBOB? 256. 4EFP FP GRPQ IFHB ELT TB ABCFKB QEB @LJMLKBKQP LC >K 2'" @LILO. 7B DBQ 8 ?FQP CLO OBA, DOBBK, >KA ?IRB, JB>KFKD TB J>HB @LILOP TFQE S>IRBP COLJ 0 QL 255 (256 MLPPF?FIFQFBP). )K QBOJP LC > 7LICO>J BIBJBKQ>OV #!, TB E>SB KLT AFP@LSBOBA QE>Q QEBOB >OB 256 MLPPF?IB ORIBPBQP. 4EB >?LSB ORIBPBQ FP @LJJLKIV OBCBOOBA QL >P h2RIB 90i ?B@>RPB FC VLR @LKSBOQ QEB ?FK>OV PBNRBK@Be01011010eQL > AB@FJ>I KRJ?BO, VLRgII DBQ QEB FKQBDBO 90. ,BQgP QOV ILLHFKD >Q QEB OBPRIQP LC >KLQEBO ORIBPBQ.

329

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

F64B?2 7.15: (B92 222

!P TB @>K KLT PBB, QEB PFJMIB >@Q LC @OB>QFKD > #! >KA ABCFKFKD > ORIBPBQ ALBP KLQ DR>O>KQBB SFPR>IIV FKQBOBPQFKD OBPRIQP. /RQ LC >II 256 ORIBPBQP, LKIV > E>KACRI MOLAR@B @LJMBIIFKD LRQ@LJBP. (LTBSBO, FQgP NRFQB FK@OBAF?IB QE>Q BSBK LKB LC QEBPB ORIBPBQP CLO > LKB-AFJBKPFLK>I #! TFQE LKIV QTL MLPPF?IB PQ>QBP @>K MOLAR@B QEB M>QQBOKP TB PBB BSBOV A>V FK K>QROB (PBB &FDROB 7.16), >KA FQ ABJLKPQO>QBP ELT S>IR>?IB QEBPB PVPQBJP @>K ?B FK PFJRI>QFLK >KA M>QQBOK DBKBO>QFLK. "BCLOB TB DL QLL C>O ALTK QEB OL>A LC ELT 7LICO>J @I>PPFCFBP QEB OBPRIQP LC S>OVFKD ORIBPBQP, IBQgP ILLH >Q ELT TB >@QR>IIV ?RFIA > 0OL@BPPFKD PHBQ@E QE>Q DBKBO>QBP QEB 7LICO>J #! >KA SFPR>IFWBP FQ LKP@OBBK.
F64B?2 7.16: A *2EA692 C<;2 );.69 (C<;B@ A2EA692), C<1 <92, G?2.A B.??62? (223, AB@A?.96., 7 AB4B@A 2005. '5<A<4?.=52?: (605.?1 #6;4 ?605.?1@?2@2.?05.0.;<;.0<:..B

7.3 How to Program an Elementary CA


9LR J>V ?B QEFKHFKD: h/+, )gSB DLQ QEFP @BII QEFKD. !KA QEB @BII QEFKD E>P PLJB MOLMBOQFBP, IFHB > PQ>QB, TE>Q DBKBO>QFLK FQgP LK, TEL FQP KBFDE?LOP >OB, TEBOB FQ IFSBP MFUBI-TFPB LK QEB P@OBBK. !KA J>V?B FQ E>P PLJB CRK@QFLKP: FQ @>K AFPMI>V FQPBIC, FQ @>K DBKBO>QB FQP KBT PQ>QB, BQ@.i 4EFP IFKB LC QEFKHFKD FP >K BU@BIIBKQ LKB >KA TLRIA IFHBIV IB>A VLR QL TOFQB PLJB @LAB IFHB QEFP:
=F;MM !?FF U W

330

4EB .>QROB LC #LAB (S1.0)

4EFP IFKB LC QEFKHFKD, ELTBSBO, FP KLQ QEB OL>A TB TFII CFOPQ QO>SBI. ,>QBO FK QEFP @E>MQBO, TB TFII AFP@RPP TEV >K L?GB@Q-LOFBKQBA >MMOL>@E @LRIA MOLSB S>IR>?IB FK ABSBILMFKD > #! PFJRI>QFLK, ?RQ QL ?BDFK, TB @>K TLOH TFQE > JLOB BIBJBKQ>OV A>Q> PQOR@QROB. !CQBO >II, TE>Q FP >K BIBJBKQ>OV #! ?RQ > IFPQ LC 0P >KA 1P? #BOQ>FKIV, TB @LRIA ABP@OF?B QEB CLIILTFKD #! DBKBO>QFLK RPFKD >K >OO>V:

F64B?2 7.17
CHN89 =?FFM = U1,0,1,0,0,0,0,1,0,1,1,1,0,0,0,1,1,1,0,0W;

4L AO>T QE>Q >OO>V, TB PFJMIV @EB@H FC TBgSB DLQ > 0 LO > 1 >KA @OB>QB > CFII >@@LOAFKDIV.
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U C@ (=?FFM8C9 == 0) @CFF(255); ?FM? @CFF(0); MNLIE?(0); L?=N(C*50,0,50,50); W Create a fill based on its state (0 or 1). Loop through every cell.

.LT QE>Q TB E>SB QEB >OO>V QL ABP@OF?B QEB @BII PQ>QBP LC > DFSBK DBKBO>QFLK (TEF@E TBgII RIQFJ>QBIV @LKPFABO QEB h@ROOBKQi DBKBO>QFLK), TB KBBA > JB@E>KFPJ ?V TEF@E QL @LJMRQB QEB KBUQ DBKBO>QFLK. ,BQgP QEFKH >?LRQ QEB MPBRAL@LAB LC TE>Q TB >OB ALFKD >Q QEB JLJBKQ. For every cell in the array: j (,60 , 7::6 ,? ?30 90423-:=3::/ >?,?0>: 701?, 84//70, =423?. j !::6 @; ?30 90B A,7@0 1:= ?30 .077 >?,?0 ,..:=/492 ?: >:80 =@70>0?. j '0? ?30 .077H> >?,?0 ?: ?3,? 90B A,7@0. 4EFP J>V IB>A VLR QL TOFQB PLJB @LAB IFHB QEFP:
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U For every cell in the array...

CHN F?@N = =?FF8C-19; CHN GC>>F? = =?FF8C9; CHN LCABN = =?FF8C+19;

...take a look at the neighborhood.

CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);

Look up the new value according to the rules.

331

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

=?FF8C9 = H?QMN;N?; W

Set the cells state to the new value.

7BgOB C>FOIV @ILPB QL DBQQFKD QEFP OFDEQ, ?RQ TBgSB J>AB LKB JFKLO ?IRKABO >KA LKB J>GLO ?IRKABO FK QEB >?LSB @LAB. ,BQgP Q>IH >?LRQ TE>Q TBgSB ALKB TBII PL C>O. .LQF@B ELT B>PV FQ FP QL ILLH >Q > @BIIgP KBFDE?LOP. "B@>RPB >K >OO>V FP >K LOABOBA IFPQ LC A>Q>, TB @>K RPB QEB C>@Q QE>Q QEB FKAF@BP >OB KRJ?BOBA QL HKLT TEF@E @BIIP >OB KBUQ QL TEF@E @BIIP. 7B HKLT QE>Q @BII KRJ?BO 15, CLO BU>JMIB, E>P @BII 14 QL FQP IBCQ >KA 16 QL FQP OFDEQ. -LOB DBKBO>IIV, TB @>K P>V QE>Q CLO >KV @BII C , FQP KBFDE?LOP >OB C-1 >KA C+1 . 7BgOB >IPL C>OJFKD LRQ QEB @>I@RI>QFLK LC > KBT PQ>QB S>IRB QL PLJB CRK@QFLK @>IIBA LOF?M() . /?SFLRPIV, TBgOB DLFKD QL E>SB QL TOFQB QEFP CRK@QFLK LROPBISBP, ?RQ QEB MLFKQ TBgOB J>HFKD EBOB FP JLARI>OFQV. 7B E>SB > ?>PF@ CO>JBTLOH CLO QEB #! FK QEFP CRK@QFLK, >KA FC TB I>QBO T>KQ QL @E>KDB ELT QEB ORIBP LMBO>QB, TB ALKgQ E>SB QL QLR@E QE>Q CO>JBTLOH; TB @>K PFJMIV OBTOFQB QEB LOF?M() CRK@QFLK QL @LJMRQB QEB KBT PQ>QBP AFCCBOBKQIV. 3L TE>Q E>SB TB ALKB TOLKD? ,BQgP Q>IH QEOLRDE ELT QEB @LAB TFII BUB@RQB. &FOPQ, TB ILLH >Q @BII FKABU C BNR>IP 0. .LT IBQgP ILLH >Q 0gP KBFDE?LOP. ,BCQ FP FKABU -1. -FAAIB FP FKABU 0. !KA OFDEQ FP FKABU 1. (LTBSBO, LRO >OO>V ?V ABCFKFQFLK ALBP KLQ E>SB >K BIBJBKQ TFQE QEB FKABU -1. )Q PQ>OQP TFQE 0. 4EFP FP > MOL?IBJ TBgSB >IIRABA QL ?BCLOB: QEB BADB @>PBP. (LT AL TB AB>I TFQE QEB @BIIP LK QEB BADB TEL ALKgQ E>SB > KBFDE?LO QL ?LQE QEBFO IBCQ >KA QEBFO OFDEQ? (BOB >OB QEOBB MLPPF?IB PLIRQFLKP QL QEFP MOL?IBJ: 1. E/20> =08,49 .:9>?,9?. 4EFP FP MBOE>MP QEB PFJMIBPQ PLIRQFLK. 7B KBSBO ?LQEBO QL BS>IR>QB QEB BADBP >KA >IT>VP IB>SB QEBFO PQ>QB S>IRB @LKPQ>KQ (0 LO 1). 2. E/20> B=,; ,=:@9/. 4EFKH LC QEB #! >P > PQOFM LC M>MBO >KA QROK QE>Q PQOFM LC M>MBO FKQL > OFKD. 4EB @BII LK QEB IBCQ BADB FP > KBFDE?LO LC QEB @BII LK QEB OFDEQ >KA SF@B SBOP>. 4EFP @>K @OB>QB QEB >MMB>O>K@B LC >K FKCFKFQB DOFA >KA FP MOL?>?IV QEB JLPQ RPBA PLIRQFLK. 3. E/20> 3,A0 /4110=09? 90423-:=3::/> ,9/ =@70>. )C TB T>KQBA QL, TB @LRIA QOB>Q QEB BADB @BIIP AFCCBOBKQIV >KA @OB>QB ORIBP CLO @BIIP QE>Q E>SB > KBFDE?LOELLA LC QTL FKPQB>A LC QEOBB. 9LR J>V T>KQ QL AL QEFP FK PLJB @FO@RJPQ>K@BP, ?RQ FK LRO @>PB, FQgP DLFKD QL ?B > ILQ LC BUQO> IFKBP LC @LAB CLO IFQQIB ?BKBCFQ. 4L J>HB QEB @LAB B>PFBPQ QL OB>A >KA RKABOPQ>KA OFDEQ KLT, TBgII DL TFQE LMQFLK #1 >KA GRPQ PHFM QEB BADB @>PBP, IB>SFKD QEBFO S>IRBP @LKPQ>KQ. 4EFP @>K ?B >@@LJMIFPEBA ?V PQ>OQFKD QEB ILLM LKB @BII I>QBO >KA BKAFKD LKB @BII B>OIFBO:

332

4EB .>QROB LC #LAB (S1.0)

<EH (?DJ ? = 1; ? < 9;BBI.B;D=J>-1; ?++) Q CHN F?@N = =?FF8C-19; CHN GC>>F? = =?FF8C9; CHN LCABN = =?FF8C+19; CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN); =?FF8C9 = H?QMN;N?; W

A loop that ignores the first and last cell

4EBOBgP LKB JLOB MOL?IBJ TB E>SB QL CFU ?BCLOB TBgOB ALKB. )QgP PR?QIB >KA VLR TLKgQ DBQ > @LJMFI>QFLK BOOLO; QEB #! GRPQ TLKgQ MBOCLOJ @LOOB@QIV. (LTBSBO, FABKQFCVFKD QEFP MOL?IBJ FP >?PLIRQBIV CRKA>JBKQ>I QL QEB QB@EKFNRBP ?BEFKA MOLDO>JJFKD #! PFJRI>QFLKP. )Q >II IFBP FK QEFP IFKB LC @LAB:
=?FF8C9 = H?QMN;N?;

4EFP PBBJP IFHB > MBOCB@QIV FKKL@BKQ IFKB. !CQBO >II, TBgSB @LJMRQBA QEB KBT PQ>QB S>IRB >KA TBgOB PFJMIV DFSFKD QEB @BII FQP KBT PQ>QB. "RQ FK QEB KBUQ FQBO>QFLK, VLRgII AFP@LSBO > J>PPFSB ?RD. ,BQgP P>V TBgSB GRPQ @LJMRQBA QEB KBT PQ>QB CLO @BII #5. 7E>Q AL TB AL KBUQ? 7B @>I@RI>QB QEB KBT PQ>QB S>IRB CLO @BII #6. C9@@ #6, ;9B9F5H=CB 0 = GCA9 GH5H9, 0 CF 1 C9@@ #6, ;9B9F5H=CB 1 = 5 :IB7H=CB C: GH5H9G :CF .077 #5, 79@@ #6, 5B8 79@@ #7 5H *;9B9F5H=CB 0* .LQF@B ELT TB KBBA QEB S>IRB LC @BII #5 >Q DBKBO>QFLK 0 FK LOABO QL @>I@RI>QB @BII #6gP KBT PQ>QB >Q DBKBO>QFLK 1? ! @BIIgP KBT PQ>QB FP > CRK@QFLK LC QEB MOBSFLRP KBFDE?LO PQ>QBP. $L TB HKLT @BII #5gP S>IRB >Q DBKBO>QFLK 0? 2BJBJ?BO, 0OL@BPPFKD GRPQ BUB@RQBP QEFP IFKB LC @LAB CLO = = 5.
=?FF8C9 = H?QMN;N?;

/K@B QEFP E>MMBKP, TB KL ILKDBO E>SB >@@BPP QL @BII #5gP PQ>QB >Q DBKBO>QFLK 0, >KA @BII FKABU 5 FP PQLOFKD QEB S>IRB CLO DBKBO>QFLK 1. 7B @>KKLQ LSBOTOFQB QEB S>IRBP FK QEB >OO>V TEFIB TB >OB MOL@BPPFKD QEB >OO>V, ?B@>RPB TB KBBA QELPB S>IRBP QL @>I@RI>QB QEB KBT S>IRBP. ! PLIRQFLK QL QEFP MOL?IBJ FP QL E>SB QTL >OO>VP, LKB QL PQLOB QEB @ROOBKQ DBKBO>QFLK PQ>QBP >KA LKB CLO QEB KBUQ DBKBO>QFLK PQ>QBP.
?DJ45 D;M9;BBI = D;M ?DJ49;BBI.B;D=J>5; @IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U CHN F?@N = =?FF8C-19; CHN GC>>F? = =?FF8C9; CHN LCABN = =?FF8C+19; CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN); D;M9;BBI4?5 = D;MIJ7J;; W Saving the new state in the new array Look at the states from the current array. Another array to store the states for the next generation.

333

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

/K@B QEB BKQFOB >OO>V LC S>IRBP FP MOL@BPPBA, TB @>K QEBK AFP@>OA QEB LIA >OO>V >KA PBQ FQ BNR>I QL QEB KBT >OO>V LC PQ>QBP.
9;BBI = D;M9;BBI; The new generation becomes the current generation.

7BgOB >IJLPQ ALKB. 4EB >?LSB @LAB FP @LJMIBQB BU@BMQ CLO QEB C>@Q QE>Q TB E>SBKgQ VBQ TOFQQBK QEB LOF?M() CRK@QFLK QE>Q @LJMRQBP QEB KBT PQ>QB S>IRB ?>PBA LK QEB KBFDE?LOELLA (IBCQ, JFAAIB, >KA OFDEQ @BIIP). 7B HKLT QE>Q CRK@QFLK KBBAP QL OBQROK >K FKQBDBO (0 LO 1) >P TBII >P OB@BFSB QEOBB >ODRJBKQP (CLO QEB QEOBB KBFDE?LOP).
CHN LOF?M (CHN ;, CHN <, CHN =) U Function receives 3 ints and returns 1.

.LT, QEBOB >OB J>KV T>VP TB @LRIA TOFQB QEFP CRK@QFLK, ?RQ )gA IFHB QL PQ>OQ TFQE > ILKDTFKABA LKB QE>Q TFII ELMBCRIIV MOLSFAB > @IB>O FIIRPQO>QFLK LC TE>Q TB >OB ALFKD. ,BQgP CFOPQ BPQ>?IFPE ELT TB >OB PQLOFKD QEB ORIBPBQ. 4EB ORIBPBQ, FC VLR OBJBJ?BO COLJ QEB MOBSFLRP PB@QFLK, FP > PBOFBP LC 8 ?FQP (0 LO 1) QE>Q ABCFKBP QE>Q LRQ@LJB CLO BSBOV MLPPF?IB KBFDE?LOELLA @LKCFDRO>QFLK.

F64B?2 7.14 (?2=2.A21)

7B @>K PQLOB QEFP ORIBPBQ FK 0OL@BPPFKD >P >K >OO>V.


CHN89 LOF?M?N = U0,1,0,1,1,0,1,0W;

!KA QEBK P>V:


C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;

)C IBCQ, JFAAIB, >KA OFDEQ >II E>SB QEB PQ>QB 1, QEBK QE>Q J>Q@EBP QEB @LKCFDRO>QFLK 111 >KA QEB KBT PQ>QB PELRIA ?B BNR>I QL QEB CFOPQ S>IRB FK QEB ORIBPBQ >OO>V. 7B @>K KLT ARMIF@>QB QEFP PQO>QBDV CLO >II BFDEQ MLPPF?FIFQFBP.

334

4EB .>QROB LC #LAB (S1.0)


CHN LOF?M C@ ?FM? C@ ?FM? C@ ?FM? C@ ?FM? C@ ?FM? C@ ?FM? C@ ?FM? C@ (CHN ;, (; == 1 (; == 1 (; == 1 (; == 1 (; == 0 (; == 0 (; == 0 (; == 0 CHN <, CHN =) U && < == 1 && = == && < == 1 && = == && < == 0 && = == && < == 0 && = == && < == 1 && = == && < == 1 && = == && < == 0 && = == && < == 0 && = ==

1) 0) 1) 0) 1) 0) 1) 0)

L?NOLH L?NOLH L?NOLH L?NOLH L?NOLH L?NOLH L?NOLH L?NOLH

LOF?M?N809; LOF?M?N819; LOF?M?N829; LOF?M?N839; LOF?M?N849; LOF?M?N859; LOF?M?N869; LOF?M?N879; For this function to be valid, we have to make sure something is returned in cases where the states do not match one of the eight possibilities. We know this is impossible given the rest of our code, but Processing does not.

L?NOLH 0; W

) IFHB E>SFKD QEB BU>JMIB TOFQQBK >P >?LSB ?B@>RPB FQ ABP@OF?BP IFKB ?V IFKB BU>@QIV TE>Q FP E>MMBKFKD CLO B>@E KBFDE?LOELLA @LKCFDRO>QFLK. (LTBSBO, FQgP KLQ > DOB>Q PLIRQFLK. !CQBO >II, TE>Q FC TB ABPFDK > #! QE>Q E>P 4 MLPPF?IB PQ>QBP (0-3) >KA PRAABKIV TB E>SB 64 MLPPF?IB KBFDE?LOELLA @LKCFDRO>QFLKP? 7FQE 10 MLPPF?IB PQ>QBP, TB E>SB 1,000 @LKCFDRO>QFLKP. #BOQ>FKIV TB ALKgQ T>KQ QL QVMB FK 1,000 IFKBP LC @LAB! !KLQEBO PLIRQFLK, QELRDE MBOE>MP > ?FQ JLOB AFCCF@RIQ QL CLIILT, FP QL @LKSBOQ QEB KBFDE?LOELLA @LKCFDRO>QFLK (> 3-?FQ KRJ?BO) FKQL > OBDRI>O FKQBDBO >KA RPB QE>Q S>IRB >P QEB FKABU FKQL QEB ORIBPBQ >OO>V. 4EFP @>K ?B ALKB FK *>S> IFHB PL.
CHN LOF?M (CHN ;, CHN <, CHN =) U 0NLCHA M = "" + ; + < + =; A quick way to join three bits into a String

CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2); L?NOLH LOF?M?N8CH>?R9; W

The second argument 2 indicates that we intend to parse a binary number (base 2).

4EBOBgP LKB QFKV MOL?IBJ TFQE QEFP PLIRQFLK, ELTBSBO. ,BQgP P>V TB >OB FJMIBJBKQFKD ORIB 222:
CHN89 LOF?M?N = U1,1,0,1,1,1,1,0W; Rule 222

!KA TB E>SB QEB KBFDE?LOELLA h111i. 4EB OBPRIQFKD PQ>QB FP BNR>I QL ORIBPBQ FKABU 0, >P TB PBB FK QEB CFOPQ T>V TB TOLQB QEB CRK@QFLK.
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;

)C TB @LKSBOQ h111i QL > AB@FJ>I KRJ?BO, TB DBQ 7. "RQ TB ALKgQ T>KQ ORIBPBQ;7<; TB T>KQ ORIBPBQ;0<. &LO QEFP QL TLOH, TB KBBA QL TOFQB QEB ORIBPBQ TFQE QEB ?FQP FK OBSBOPB LOABO, F.B.

335

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

CHN89 LOF?M?N = U0,1,1,1,1,0,1,1W;

Rule 222 in reverse order

3L C>O FK QEFP PB@QFLK, TBgSB TOFQQBK BSBOVQEFKD TB KBBA QL @LJMRQB QEB DBKBO>QFLKP CLO > 7LICO>J BIBJBKQ>OV #!. ,BQgP Q>HB > JLJBKQ QL LOD>KFWB QEB >?LSB @LAB FKQL > @I>PP, TEF@E TFII RIQFJ>QBIV EBIM FK QEB ABPFDK LC LRO LSBO>II PHBQ@E.
=F;MM !A U CHN89 =?FFM; CHN89 LOF?M?N; !A() U =?FFM = H?Q CHN8QC>NB9; LOF?M?N = U0,1,0,1,1,0,1,0W; @IL (CHN C = 0; C < =?FFM.F?HANB; C++) U =?FFM8C9 = 0; W =?FFM8=?FFM.F?HANB/29 = 1; W PIC> A?H?L;N?() U CHN89 H?RNA?H = H?Q CHN8=?FFM.F?HANB9; @IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U CHN F?@N = =?FFM8C-19; CHN G? = =?FFM8C9; CHN LCABN = =?FFM8C+19; H?RNA?H8C9 = LOF?M(F?@N, G?, LCABN); W =?FFM = H?RNA?H; W CHN LOF?M (CHN ;, CHN <, CHN =) U 0NLCHA M = "" + ; + < + =; CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2); L?NOLH LOF?M?N8CH>?R9; W W Look up a new state from the ruleset. Compute the next generation. All cells start with state 0, except the center cell has state 1. Arbitrarily starting with rule 90 We need an array for the cells and one for the rules.

7.4 Drawing an Elementary CA


7E>QgP JFPPFKD? 0OBPRJ>?IV, FQgP LRO FKQBKQFLK QL AFPMI>V @BIIP >KA QEBFO PQ>QBP FK SFPR>I CLOJ. !P TB P>T B>OIFBO, QEB PQ>KA>OA QB@EKFNRB CLO ALFKD QEFP FP QL PQ>@H QEB DBKBO>QFLKP LKB LK QLM LC B>@E LQEBO >KA AO>T > OB@Q>KDIB QE>Q FP ?I>@H (CLO PQ>QB 1) LO TEFQB (CLO PQ>QB 0).

336

4EB .>QROB LC #LAB (S1.0)

F64B?2 7.12 (?2=2.A21)

"BCLOB TB FJMIBJBKQ QEFP M>OQF@RI>O SFPR>IFW>QFLK, )gA IFHB QL MLFKQ LRQ QTL QEFKDP. /KB, QEFP SFPR>I FKQBOMOBQ>QFLK LC QEB A>Q> FP @LJMIBQBIV IFQBO>I. )QgP RPBCRI CLO ABJLKPQO>QFKD QEB >IDLOFQEJP >KA OBPRIQP LC 7LICO>JgP BIBJBKQ>OV #!, ?RQ FQ PELRIAKgQ KB@BPP>OFIV AOFSB VLRO LTK MBOPLK>I TLOH. )QgP O>QEBO RKIFHBIV QE>Q VLR >OB ?RFIAFKD > MOLGB@Q QE>Q KBBAP MOB@FPBIV QEFP >IDLOFQEJ TFQE QEFP SFPR>I PQVIB. 3L TEFIB IB>OKFKD QL AO>T QEB #! FK QEFP T>V TFII EBIM VLR RKABOPQ>KA >KA FJMIBJBKQ #! PVPQBJP, QEFP PHFII PELRIA BUFPQ LKIV >P > CLRKA>QFLK. 3B@LKA, QEB C>@Q QE>Q TB >OB SFPR>IFWFKD > LKB-AFJBKPFLK>I #! TFQE > QTL-AFJBKPFLK>I FJ>DB @>K ?B @LKCRPFKD. )QgP SBOV FJMLOQ>KQ QL OBJBJ?BO QE>Q QEFP FP KLQ > 2$ #!. 7B >OB PFJMIV @ELLPFKD QL PELT > EFPQLOV LC >II QEB DBKBO>QFLKP PQ>@HBA SBOQF@>IIV. 4EFP QB@EKFNRB @OB>QBP > QTL-AFJBKPFLK>I FJ>DB LRQ LC J>KV FKPQ>K@BP LC LKB-AFJBKPFLK>I A>Q>. "RQ QEB PVPQBJ FQPBIC FP LKB-AFJBKPFLK>I. ,>QBO, TB >OB DLFKD QL ILLH >Q >K >@QR>I 2$ #! (QEB '>JB LC ,FCB) >KA AFP@RPP ELT TB JFDEQ @ELLPB QL AFPMI>V PR@E > PVPQBJ. 4EB DLLA KBTP FP QE>Q AO>TFKD QEB #! FP KLQ M>OQF@RI>OIV AFCCF@RIQ. ,BQgP ?BDFK ?V ILLHFKD >Q ELT TB TLRIA OBKABO > PFKDIB DBKBO>QFLK. !PPRJB TB E>SB > 0OL@BPPFKD TFKALT 600 MFUBIP TFAB >KA TB T>KQ B>@E @BII QL ?B > 10U10 PNR>OB. 7B QEBOBCLOB E>SB > #! TFQE 60 @BIIP. /C @LROPB, TB @>K @>I@RI>QB QEFP S>IRB AVK>JF@>IIV.
CHN Q = 10; CHN89 =?FFM = H?Q CHN8QC>NB/Q9; How many cells fit across given a certain width

!PPRJFKD TBgSB DLKB QEOLRDE QEB MOL@BPP LC DBKBO>QFKD QEB @BII PQ>QBP (TEF@E TB AFA FK QEB MOBSFLRP PB@QFLK), TB @>K KLT ILLM QEOLRDE QEB BKQFOB >OO>V LC @BIIP, AO>TFKD > ?I>@H @BII TEBK QEB PQ>QB FP 1 >KA > TEFQB LKB TEBK QEB PQ>QB FP 0.
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U C@ (=?FFM8C9 == 1) @CFF(0); ?FM? @CFF(255); Black or white fill?

337

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

L?=N(C*Q, 0, Q, Q); W

Notice how the x-location is the cell index times the cell width. In the above scenario, this would give us cells located at x equals 0, 10, 20, 30, all the way up to 600.

)K QORQE, TB @LRIA LMQFJFWB QEB >?LSB ?V E>SFKD > TEFQB ?>@HDOLRKA >KA LKIV AO>TFKD TEBK QEBOB FP > ?I>@H @BII (P>SFKD RP QEB TLOH LC AO>TFKD J>KV TEFQB PNR>OBP), ?RQ FK JLPQ @>PBP QEFP PLIRQFLK FP DLLA BKLRDE (>KA KB@BPP>OV CLO LQEBO JLOB PLMEFPQF@>QBA ABPFDKP TFQE S>OVFKD @LILOP, BQ@.) !IPL, FC TB T>KQBA B>@E @BII QL ?B OBMOBPBKQBA >P > PFKDIB MFUBI, TB TLRIA KLQ T>KQ QL RPB 0OL@BPPFKDgP L?=N() CRK@QFLK, ?RQ O>QEBO >@@BPP QEB MFUBI >OO>V AFOB@QIV. )K QEB >?LSB @LAB, VLRgII KLQF@B QEB V-IL@>QFLK CLO B>@E OB@Q>KDIB FP 0. )C TB T>KQ QEB DBKBO>QFLKP QL ?B AO>TK KBUQ QL B>@E LQEBO, TFQE B>@E OLT LC @BIIP J>OHFKD > KBT DBKBO>QFLK, TBgII >IPL KBBA QL @LJMRQB > V-IL@>QFLK ?>PBA LK ELT J>KV FQBO>QFLKP LC QEB #! TBgSB BUB@RQBA. 7B @LRIA >@@LJMIFPE QEFP ?V >AAFKD > hDBKBO>QFLKi S>OF>?IB (>K FKQBDBO) QL LRO #! @I>PP >KA FK@OBJBKQFKD FQ B>@E QFJB QEOLRDE A?H?L;N?() . 7FQE QEBPB >AAFQFLKP, TB @>K KLT ILLH >Q QEB #! @I>PP TFQE >II QEB CB>QROBP CLO ?LQE @LJMRQFKD >KA AO>TFKD QEB #!.

Example 7.1: Wolfram elementary cellular automata


=F;MM !A U CHN89 =?FFM; CHN89 LOF?M?N; CHN Q = 10; CHN A?H?L;NCIH = 0; !A() U =?FFM = H?Q CHN8QC>NB/Q9; LOF?M?N = U0,1,0,1,1,0,1,0W; =?FFM8=?FFM.F?HANB/29 = 1; W The CA should keep track of how many generations.

338

4EB .>QROB LC #LAB (S1.0)

PIC> A?H?L;N?() U CHN89 H?RNA?H = H?Q CHN8=?FFM.F?HANB9; @IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U CHN F?@N = =?FFM8C-19; CHN G? = =?FFM8C9; CHN LCABN = =?FFM8C+19; H?RNA?H8C9 = LOF?M(F?@N, G?, LCABN); W =?FFM = H?RNA?H; A?H?L;NCIH++; W CHN LOF?M(CHN ;, CHN <, CHN =) U 0NLCHA M = "" + ; + < + =; CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2); L?NOLH LOF?M?N8CH>?R9; W @IL (CHN C = 0; C < =?FFM.F?HANB; C++) U C@ (=?FFM8C9 == 1) @CFF(0); ?FM? @CFF(255); L?=N(C*Q, A?H?L;NCIH*Q, Q, Q); W W

Function to compute the next generation

Increment the generation counter.

Set the y-location according to the generation.

Exercise 7.1
%UM>KA %U>JMIB 7.1 QL E>SB QEB CLIILTFKD CB>QROB: TEBK QEB #! OB>@EBP QEB ?LQQLJ LC QEB 0OL@BPPFKD TFKALT, QEB #! PQ>OQP LSBO TFQE > KBT, O>KALJ ORIBPBQ.

Exercise 7.2
%U>JFKB TE>Q M>QQBOKP L@@RO FC VLR FKFQF>IFWB QEB CFOPQ DBKBO>QFLK TFQE B>@E @BII E>SFKD > O>KALJ PQ>QB.

Exercise 7.3
6FPR>IFWB QEB #! FK > KLK-QO>AFQFLK>I T>V. "OB>H >II QEB ORIBP VLR @>K; ALKgQ CBBI QFBA QL RPFKD PNR>OBP LK > MBOCB@Q DOFA TFQE ?I>@H >KA TEFQB.

339

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

Exercise 7.4
#OB>QB > SFPR>IFW>QFLK LC QEB #! QE>Q P@OLIIP RMT>OAP >P QEB DBKBO>QFLKP FK@OB>PB PL QE>Q VLR @>K SFBT QEB DBKBO>QFLKP QL hFKCFKFQV.i (FKQ: FKPQB>A LC HBBMFKD QO>@H LC LKIV LKB DBKBO>QFLK >Q > QFJB, VLRgII KBBA QL PQLOB > EFPQLOV LC DBKBO>QFLKP, >IT>VP >AAFKD > KBT LKB >KA ABIBQFKD QEB LIABPQ LKB FK B>@E CO>JB.

7.5 Wolfram Classification


"BCLOB TB JLSB LK QL ILLHFKD >Q #! FK QTL AFJBKPFLKP, FQgP TLOQE Q>HFKD > ?OFBC ILLH >Q 7LICO>JgP @I>PPFCF@>QFLK CLO @BIIRI>O >RQLJ>Q>. !P TB KLQBA B>OIFBO, QEB S>PQ J>GLOFQV LC BIBJBKQ>OV #! ORIBPBQP MOLAR@B RKFKPMFOFKD OBPRIQP, TEFIB PLJB OBPRIQ FK TLKAOLRPIV @LJMIBU M>QQBOKP IFHB QELPB CLRKA FK K>QROB. 7LICO>J E>P AFSFABA RM QEB O>KDB LC LRQ@LJBP FKQL CLRO @I>PPBP:

F64B?2 7.18: (B92 222

C7,>> 1: )941:=84?D. #I>PP 1 #!P BKA RM, >CQBO PLJB KRJ?BO LC DBKBO>QFLKP, TFQE BSBOV @BII @LKPQ>KQ. 4EFP FP KLQ QBOOF?IV BU@FQFKD QL T>Q@E. 2RIB 222 (>?LSB) FP > @I>PP 1 #!; FC VLR ORK FQ CLO BKLRDE DBKBO>QFLKP, BSBOV @BII TFII BSBKQR>IIV ?B@LJB >KA OBJ>FK ?I>@H.

F64B?2 7.19: (B92 190

340

4EB .>QROB LC #LAB (S1.0)

C7,>> 2: &0;0?4?4:9. ,FHB @I>PP 1 #!P, @I>PP 2 #!P OBJ>FK PQ>?IB, ?RQ QEB @BII PQ>QBP >OB KLQ @LKPQ>KQ. 2>QEBO, QEBV LP@FII>QB FK PLJB OBDRI>O M>QQBOK ?>@H >KA CLOQE COLJ 0 QL 1 QL 0 QL 1 >KA PL LK. )K ORIB 190 (>?LSB), B>@E @BII CLIILTP QEB PBNRBK@B 11101110111011101110.

F64B?2 7.20: (B92 30

C7,>> 3: &,9/:8. #I>PP 3 #!P >MMB>O O>KALJ >KA E>SB KL B>PFIV AFP@BOKF?IB M>QQBOK. )K C>@Q, ORIB 30 (>?LSB) FP RPBA >P > O>KALJ KRJ?BO DBKBO>QLO FK 7LICO>JgP ->QEBJ>QF@> PLCQT>OB. !D>FK, QEFP FP > JLJBKQ TEBOB TB @>K CBBI >J>WBA QE>Q PR@E > PFJMIB PVPQBJ TFQE PFJMIB ORIBP @>K ABP@BKA FKQL > @E>LQF@ >KA O>KALJ M>QQBOK.

F64B?2 7.21: (B92 110

C7,>> 4: C:8;70C4?D. #I>PP 4 #!P @>K ?B QELRDEQ LC >P > JFU ?BQTBBK @I>PP 2 >KA @I>PP 3. /KB @>K CFKA OBMBQFQFSB, LP@FII>QFKD M>QQBOKP FKPFAB QEB #!, ?RQ TEBOB >KA TEBK QEBPB M>QQBOKP >MMB>O FP RKMOBAF@Q>?IB >KA PBBJFKDIV O>KALJ. #I>PP 4 #!P BUEF?FQ QEB MOLMBOQFBP LC @LJMIBU PVPQBJP QE>Q TB ABP@OF?BA B>OIFBO FK QEFP @E>MQBO >KA FK #E>MQBO 6. )C > @I>PP 3 #! TLTBA VLR, QEBK > @I>PP 4 IFHB 2RIB 110 >?LSB PELRIA OB>IIV ?ILT VLRO JFKA.

Exercise 7.5
#OB>QB > 0OL@BPPFKD PHBQ@E QE>Q P>SBP >K FJ>DB CLO BSBOV MLPPF?IB ORIBPBQ. #>K VLR @I>PPFCV QEBJ?

341

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

7.6 The Game of Life


4EB KBUQ PQBM TB >OB DLFKD QL Q>HB FP QL JLSB COLJ > LKB-AFJBKPFLK>I #! QL > QTLAFJBKPFLK>I LKB. 4EFP TFII FKQOLAR@B PLJB >AAFQFLK>I @LJMIBUFQV; B>@E @BII TFII E>SB > ?FDDBO KBFDE?LOELLA, ?RQ QE>Q TFII LMBK RM QEB ALLO QL > O>KDB LC MLPPF?IB >MMIF@>QFLKP. !CQBO >II, JLPQ LC TE>Q TB AL FK @LJMRQBO DO>MEF@P IFSBP FK QTL AFJBKPFLKP, >KA QEFP @E>MQBO TFII ABJLKPQO>QB ELT QL >MMIV #! QEFKHFKD QL TE>Q TB AO>T FK LRO 0OL@BPPFKD PHBQ@EBP. )K 1970, ->OQFK '>OAKBO TOLQB >K >OQF@IB FK .7=9BH=:=7 AA9F=75B QE>Q AL@RJBKQBA J>QEBJ>QF@F>K *LEK #LKT>VgP KBT h'>JB LC ,FCB,i ABP@OF?FKD FQ >P hOB@OB>QFLK>Ii J>QEBJ>QF@P >KA PRDDBPQFKD QE>Q QEB OB>ABO DBQ LRQ > @EBPP?L>OA >KA PLJB @EB@HBOP >KA hMI>V.i 7EFIB QEB '>JB LC ,FCB E>P ?B@LJB PLJBQEFKD LC > @LJMRQ>QFLK>I @IF@E[ (J>HB KLQB LC QEB JVOF>A MOLGB@QP QE>Q AFPMI>V QEB '>JB LC ,FCB LK ,%$P, P@OBBKP, MOLGB@QFLK PROC>@BP, BQ@.), FQ FP PQFII FJMLOQ>KQ CLO RP QL ?RFIA FQ COLJ P@O>Q@E. &LO LKB, FQ MOLSFABP > DLLA LMMLOQRKFQV QL MO>@QF@B LRO PHFIIP TFQE QTL-AFJBKPFLK>I >OO>VP, L?GB@Q LOFBKQ>QFLK, BQ@. "RQ MBOE>MP JLOB FJMLOQ>KQIV, FQP @LOB MOFK@FMIBP >OB QFBA AFOB@QIV QL LRO @LOB DL>IPePFJRI>QFKD QEB K>QRO>I TLOIA TFQE @LAB. 4ELRDE TB J>V T>KQ QL >SLFA PFJMIV ARMIF@>QFKD FQ TFQELRQ > DOB>Q AB>I LC QELRDEQ LO @>OB, QEB >IDLOFQEJ >KA FQP QB@EKF@>I FJMIBJBKQ>QFLK TFII MOLSFAB RP TFQE QEB FKPMFO>QFLK >KA CLRKA>QFLK QL ?RFIA PFJRI>QFLKP QE>Q BUEF?FQ QEB @E>O>@QBOFPQF@P >KA ?BE>SFLOP LC ?FLILDF@>I PVPQBJP LC OBMOLAR@QFLK. 5KIFHB SLK .BRJ>KK, TEL @OB>QBA >K BUQO>LOAFK>OFIV @LJMIBU PVPQBJ LC PQ>QBP >KA ORIBP, #LKT>V T>KQBA QL >@EFBSB > PFJFI>O hIFCBIFHBi OBPRIQ TFQE QEB PFJMIBPQ PBQ LC ORIBP MLPPF?IB. ->OQFK '>OAKBO LRQIFKBA #LKT>VgP DL>IP >P CLIILTP:
I1. *52?2 @5<B91 /2 ;< 6;6A6.9 =.AA2?; 3<? D5605 A52?2 6@ . @6:=92 =?<<3 A5.A A52 =<=B9.A6<; 0.; 4?<D D6A5<BA 96:6A. 2. *52?2 @5<B91 /2 6;6A6.9 =.AA2?;@ A5.A .==.?2;A9F 1< 4?<D D6A5<BA 96:6A. 3. *52?2 @5<B91 /2 @6:=92 6;6A6.9 =.AA2?;@ A5.A 4?<D .;1 05.;42 3<? . 0<;@612?./92 =2?6<1 <3 A6:2 /23<?2 0<:6;4 A< .; 2;1 6; A5?22 =<@@6/92 D.F@: 3.16;4 .D.F 0<:=92A29F (3?<: <C2?0?<D16;4 <? /20<:6;4 A<< @=.?@2), @2AA96;4 6;A< . @A./92 0<;364B?.A6<; A5.A ?2:.6;@ B;05.;421 A52?2.3A2?, <? 2;A2?6;4 .; <@0699.A6;4 =5.@2 6; D5605 A52F ?2=2.A .; 2;192@@ 0F092 <3 AD< <? :<?2 =2?6<1@.J H$.?A6; G.?1;2?, Scientific American (http://www.ibiblio.org/lifepatterns/ october1970.html) 223 (&0A</2? 1970): 120-123.

4EB >?LSB JFDEQ PLRKA > ?FQ @OVMQF@, ?RQ FQ BPPBKQF>IIV ABP@OF?BP > 7LICO>J @I>PP 4 #!. 4EB #! PELRIA ?B M>QQBOKBA ?RQ RKMOBAF@Q>?IB LSBO QFJB, BSBKQR>IIV PBQQIFKD FKQL > RKFCLOJ LO LP@FII>QFKD PQ>QB. )K LQEBO TLOAP, QELRDE #LKT>V AFAKgQ RPB QEFP QBOJFKLILDV, FQ PELRIA E>SB >II QELPB MOLMBOQFBP LC > 7CAD@9L GMGH9A QE>Q TB HBBM JBKQFLKFKD. ,BQgP ILLH >Q ELT QEB '>JB LC ,FCB TLOHP. )Q TLKgQ Q>HB RM QLL JR@E QFJB LO PM>@B, PFK@B TBgSB @LSBOBA QEB ?>PF@P LC #! >IOB>AV.

342

4EB .>QROB LC #LAB (S1.0)

&FOPQ, FKPQB>A LC > IFKB LC @BIIP, TB KLT E>SB > QTL-AFJBKPFLK>I J>QOFU LC @BIIP. !P TFQE QEB BIBJBKQ>OV #!, QEB MLPPF?IB PQ>QBP >OB 0 LO 1. /KIV FK QEFP @>PB, PFK@B TBgOB Q>IHFKD >?LRQ hIFCB," 0 JB>KP AB>A >KA 1 JB>KP >IFSB. 4EB @BIIgP KBFDE?LOELLA E>P >IPL BUM>KABA. )C > KBFDE?LO FP >K >AG>@BKQ @BII, > KBFDE?LOELLA FP KLT KFKB @BIIP FKPQB>A LC QEOBB. 7FQE QEOBB @BIIP, TB E>A > 3-?FQ KRJ?BO LO BFDEQ MLPPF?IB @LKCFDRO>QFLKP. 7FQE KFKB @BIIP, TB E>SB 9 ?FQP, LO 512 MLPPF?IB F64B?2 7.22 KBFDE?LOELLAP. )K JLPQ @>PBP, FQ TLRIA ?B FJMO>@QF@>I QL ABCFKB >K LRQ@LJB CLO BSBOV PFKDIB MLPPF?FIFQV. 4EB '>JB LC ,FCB DBQP >OLRKA QEFP MOL?IBJ ?V ABCFKFKD > PBQ LC ORIBP >@@LOAFKD QL DBKBO>I @E>O>@QBOFPQF@P LC QEB KBFDE?LOELLA. )K LQEBO TLOAP, FP QEB KBFDE?LOELLA LSBOMLMRI>QBA TFQE IFCB? 3ROOLRKABA ?V AB>QE? /O GRPQ OFDEQ? (BOB >OB QEB ORIBP LC IFCB. 1. D0,?3. )C > @BII FP >IFSB (PQ>QB = 1) FQ TFII AFB (PQ>QB ?B@LJBP 0) RKABO QEB CLIILTFKD @FO@RJPQ>K@BP. $A0=;:;@7,?4:9: )C QEB @BII E>P CLRO LO JLOB >IFSB KBFDE?LOP, FQ AFBP. !:907490>>: )C QEB @BII E>P LKB LO CBTBO >IFSB KBFDE?LOP, FQ AFBP. 2. B4=?3. )C > @BII FP AB>A (PQ>QB = 0) FQ TFII @LJB QL IFCB (PQ>QB ?B@LJBP 1) FC FQ E>P BU>@QIV QEOBB >IFSB KBFDE?LOP (KL JLOB, KL IBPP). 3. '?,>4>. )K >II LQEBO @>PBP, QEB @BII PQ>QB ALBP KLQ @E>KDB. 4L ?B QELOLRDE, IBQgP ABP@OF?B QELPB P@BK>OFLP. '?,D492 A74A0: )C > @BII FP >IFSB >KA E>P BU>@QIV QTL LO QEOBB IFSB KBFDE?LOP, FQ PQ>VP >IFSB. '?,D492 D0,/: )C > @BII FP AB>A >KA E>P >KVQEFKD LQEBO QE>K QEOBB IFSB KBFDE?LOP, FQ PQ>VP AB>A. ,BQgP ILLH >Q > CBT BU>JMIBP.

343

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

F64B?2 7.23

7FQE QEB BIBJBKQ>OV #!, TB TBOB >?IB QL ILLH >Q >II QEB DBKBO>QFLKP KBUQ QL B>@E LQEBO, PQ>@HBA >P OLTP FK > 2$ DOFA. 7FQE QEB '>JB LC ,FCB, ELTBSBO, QEB #! FQPBIC FP FK QTL AFJBKPFLKP. 7B @LRIA QOV @OB>QFKD >K BI>?LO>QB 3$ SFPR>IFW>QFLK LC QEB OBPRIQP >KA PQ>@H >II QEB DBKBO>QFLKP FK > @R?B PQOR@QROB (>KA FK C>@Q, VLR JFDEQ T>KQ QL QOV QEFP >P >K BUBO@FPB). .BSBOQEBIBPP, QEB QVMF@>I T>V QEB '>JB LC ,FCB FP AFPMI>VBA FP QL QOB>Q B>@E DBKBO>QFLK >P > PFKDIB CO>JB FK >K >KFJ>QFLK. 3L FKPQB>A LC SFBTFKD >II QEB DBKBO>QFLKP >Q LK@B, TB PBB QEBJ LKB >Q > QFJB, >KA QEB OBPRIQ OBPBJ?IBP O>MFAIV DOLTFKD ?>@QBOF> FK > MBQOF AFPE. /KB LC QEB BU@FQFKD >PMB@QP LC QEB '>JB LC ,FCB FP QE>Q QEBOB >OB FKFQF>I M>QQBOKP QE>Q VFBIA FKQOFDRFKD OBPRIQP. &LO BU>JMIB, PLJB OBJ>FK PQ>QF@ >KA KBSBO @E>KDB.

F64B?2 7.24

4EBOB >OB M>QQBOKP QE>Q LP@FII>QB ?>@H >KA CLOQE ?BQTBBK QTL PQ>QBP.

F64B?2 7.25

344

4EB .>QROB LC #LAB (S1.0)

!KA QEBOB >OB >IPL M>QQBOKP QE>Q COLJ DBKBO>QFLK QL DBKBO>QFLK JLSB >?LRQ QEB DOFA. ()QgP FJMLOQ>KQ QL KLQB QE>Q QEB @BIIP QEBJPBISBP >OBKgQ >@QR>IIV JLSFKD, >IQELRDE TB PBB QEB >MMB>O>K@B LC JLQFLK FK QEB OBPRIQ >P QEB @BIIP QROK LK >KA LCC.)

F64B?2 7.26

)C VLR >OB FKQBOBPQBA FK QEBPB M>QQBOKP, QEBOB >OB PBSBO>I DLLA hLRQ LC QEB ?LUi '>JB LC ,FCB ABJLKPQO>QFLKP LKIFKB QE>Q >IILT VLR QL @LKCFDROB QEB #!gP FKFQF>I PQ>QB >KA T>Q@E FQ ORK >Q S>OVFKD PMBBAP. 4TL BU>JMIBP VLR JFDEQ T>KQ QL BU>JFKB >OB: j %UMILOFKD %JBODBK@B (EQQM://IIH.JBAF>.JFQ.BAR/MOLGB@QP/BJBODBK@B/) ?V -FQ@EBI 2BPKF@H >KA "OF>K 3FISBOJ>K, ,FCBILKD +FKABOD>OQBK 'OLRM, -)4 -BAF> ,>?LO>QLOV j #LKT>VgP '>JB LC ,FCB (EQQM://PQBSBKHIFPB.DFQER?.@LJ/#LKT>VP'>JB/C,FCB) ?V 3QBSBK +IFPB (RPBP 0OL@BPPFKD.GP!) &LO QEB BU>JMIB TBgII ?RFIA COLJ P@O>Q@E FK QEB KBUQ PB@QFLK, FQ TFII ?B B>PFBO QL PFJMIV O>KALJIV PBQ QEB PQ>QBP CLO B>@E @BII.

7.7 Programming the Game of Life


.LT TB GRPQ KBBA QL BUQBKA LRO @LAB COLJ QEB 7LICO>J #! QL QTL AFJBKPFLKP. 7B RPBA > LKB-AFJBKPFLK>I >OO>V QL PQLOB QEB IFPQ LC @BII PQ>QBP ?BCLOB, >KA CLO QEB '>JB LC ,FCB, TB @>K RPB > QTL-AFJBKPFLK>I >OO>V (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/2A>OO>V/).
CHN8989 <I;L> = H?Q CHN8=IFOGHM98LIQM9;

7BgII ?BDFK ?V FKFQF>IFWFKD B>@E @BII LC QEB ?L>OA TFQE > O>KALJ PQ>QB: 0 LO 1.
@IL (CHN R = 0; R < =IFOGHM; R++) U @IL (CHN S = 0; S < LIQM; S++) U =OLL?HN8R98S9 = CHN(L;H>IG(2)); W W Initialize each cell with a 0 or 1.

345

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

!KA QL @LJMRQB QEB KBUQ DBKBO>QFLK, GRPQ >P ?BCLOB, TB KBBA > COBPE 2$ >OO>V QL TOFQB QL >P TB >K>IVWB B>@E @BIIgP KBFDE?LOELLA >KA @>I@RI>QB > KBT PQ>QB.
CHN8989 H?RN = H?Q CHN8=IFOGHM98LIQM9; @IL (CHN R = 0; R < =IFOGHM; R++) U @IL (CHN S = 0; S < LIQM; S++) U H?RN8R98S9 = :::::::::::::::?; W W We need a new state for each cell.

/+. "BCLOB TB @>K PLOQ LRQ ELT QL >@QR>IIV @>I@RI>QB QEB KBT PQ>QB, TB KBBA QL HKLT ELT TB @>K OBCBOBK@B B>@E @BIIgP KBFDE?LO. )K QEB @>PB LC QEB 1$ #!, QEFP T>P PFJMIB: FC > @BII FKABU T>P C , FQP KBFDE?LOP TBOB F-1 >KA F+1. (BOB B>@E @BII ALBPKgQ E>SB > PFKDIB FKABU, ?RQ O>QEBO > @LIRJK >KA OLT FKABU: U,V. !P PELTK FK &FDROB 7.27, TB @>K PBB QE>Q FQP KBFDE?LOP >OB: (L-1,M-1) (L,M-1), (L+1,M-2), (L-1,M), (L+1,M), (L-1,M+1), (L,M+1), >KA (L+1,M+1) . !II LC QEB '>JB LC ,FCB ORIBP LMBO>QB ?V HKLTFKD ELT J>KV KBFDE?LOP >OB >IFSB. 3L FC TB @OB>QB > KBFDE?LO @LRKQBO S>OF>?IB >KA FK@OBJBKQ FQ B>@E QFJB TB CFKA > KBFDE?LO TFQE > PQ>QB LC 1, TBgII E>SB QEB QLQ>I LC IFSB KBFDE?LOP.
CHN H?CAB<ILM = 0; C@ (<I;L>8R-198S-19 == 1) H?CAB<ILM++; C@ (<I;L>8R 98S-19 == 1) H?CAB<ILM++; C@ (<I;L>8R+198S-19 == 1) H?CAB<ILM++; Top row of neighbors

F64B?2 7.27

C@ (<I;L>8R-198S9 C@ (<I;L>8R+198S9

== 1) H?CAB<ILM++; == 1) H?CAB<ILM++;

Middle row of neighbors (note we dont count self)

C@ (<I;L>8R-198S+19 == 1) H?CAB<ILM++; C@ (<I;L>8R 98S+19 == 1) H?CAB<ILM++; C@ (<I;L>8R+198S+19 == 1) H?CAB<ILM++;

Bottom row of neighbors

346

4EB .>QROB LC #LAB (S1.0)

!KA >D>FK, GRPQ >P TFQE QEB 7LICO>J #!, TB CFKA LROPBISBP FK > PFQR>QFLK TEBOB QEB >?LSB FP > RPBCRI >KA @IB>O T>V QL TOFQB QEB @LAB CLO QB>@EFKD MROMLPBP, >IILTFKD RP QL PBB BSBOV PQBM (B>@E QFJB TB CFKA > KBFDE?LO TFQE > PQ>QB LC LKB, TB FK@OB>PB > @LRKQBO). .BSBOQEBIBPP, FQgP > ?FQ PFIIV QL P>V, h)C QEB @BII PQ>QB BNR>IP LKB, >AA LKB QL > @LRKQBOi TEBK TB @LRIA GRPQ P>V, h!AA QEB @BII PQ>QB QL > @LRKQBO.i !CQBO >II, FC QEB PQ>QB FP LKIV > 0 LO 1, QEB PRJ LC >II QEB KBFDE?LOPg PQ>QBP TFII VFBIA QEB QLQ>I KRJ?BO LC IFSB @BIIP. 3FK@B QEB KBFDE?LOP >OB >OO>KDBA FK > JFKF 3U3 DOFA, TB @>K >AA QEBJ >II RM TFQE >KLQEBO ILLM.
@IL (CHN C = -1; C <= 1; C++) U @IL (CHN D = -1; D <= 1; D++) U H?CAB<ILM += <I;L>8R+C98S+D9; W W Add up all the neighbors states.

/C @LROPB, TBgSB J>AB > JFPQ>HB FK QEB @LAB >?LSB. )K QEB '>JB LC ,FCB, QEB @BII FQPBIC ALBP KLQ @LRKQ >P LKB LC QEB KBFDE?LOP. 7B @LRIA RPB > @LKAFQFLK>I QL PHFM >AAFKD QEB PQ>QB TEBK ?LQE C >KA D BNR>I 0, ?RQ >KLQEBO LMQFLK TLRIA ?B QL GRPQ PR?QO>@Q QEB @BII PQ>QB LK@B TBgSB CFKFPEBA QEB ILLM.
H?CAB<ILM -= <I;L>8R98S9; Whoops! Subtract the cells state, which we dont want in the total.

&FK>IIV, LK@B TB HKLT QEB QLQ>I KRJ?BO LC IFSB KBFDE?LOP, TB @>K AB@FAB TE>Q QEB @BIIgP KBT PQ>QB PELRIA ?B >@@LOAFKD QL QEB ORIBP: ?FOQE, AB>QE, LO PQ>PFP.
C@ W ((<I;L>8R98S9 == 1) && (H?CAB<ILM < H?RN8R98S9 = 0; 2)) U If it is alive and has less than 2 live neighbors, it dies from loneliness.

?FM? C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM > H?RN8R98S9 = 0; W

3)) U

If it is alive and has more than 3 live neighbors, it dies from overpopulation.

?FM? C@ ((<I;L>8R98S9 == 0) && (H?CAB<ILM == 3)) U H?RN8R98S9 = 1; W

If it is dead and has exactly 3 live neighbors, it is born!

?FM? U H?RN8R98S9 = <I;L>8R98S9; W

In all other cases, its state remains the same.

0RQQFKD QEFP >II QLDBQEBO, TB E>SB:


CHN8989 H?RN = H?Q CHN8=IFOGHM98LIQM9; The next board

347

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

@IL (CHN R = 1; R < =IFOGHM-1; R++) U @IL (CHN S = 1; S < LIQM-1; S++) U

Looping but skipping the edge cells

CHN H?CAB<ILM = 0; @IL (CHN C = -1; C <= 1; C++) U @IL (CHN D = -1; D <= 1; D++) U H?CAB<ILM += <I;L>8R+C98S+D9; W W H?CAB<ILM -= <I;L>8R98S9;

Add up all the neighbor states to calculate the number of live neighbors.

Correct by subtracting the cell state itself.

The rules of life! C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM < 2)) H?RN8R98S9 = 0; ?FM? C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM > 3)) H?RN8R98S9 = 0; ?FM? C@ ((<I;L>8R98S9 == 0) && (H?CAB<ILM == 3)) H?RN8R98S9 = 1; ?FM? H?RN8R98S9 = <I;L>8R98S9; W W <I;L> = H?RN; The 2D array next is now the current board.

&FK>IIV, LK@B QEB KBUQ DBKBO>QFLK FP @>I@RI>QBA, TB @>K BJMILV QEB P>JB JBQELA TB RPBA QL AO>T QEB 7LICO>J #!e> PNR>OB CLO B>@E PMLQ, TEFQB CLO LCC, ?I>@H CLO LK.

Example 7.2: Game of Life


@IL ( CHN C = 0; C < =IFOGHM;C++) U @IL ( CHN D = 0; D < LIQM;D++) U C@ ((<I;L>8C98D9 == 1)) @CFF(0); Black when state = 1

348

4EB .>QROB LC #LAB (S1.0)

?FM? @CFF(255); MNLIE?(0); L?=N(C*Q, D*Q, Q, Q); W W

White when state = 0

Exercise 7.6
#OB>QB > '>JB LC ,FCB PFJRI>QFLK QE>Q >IILTP VLR QL J>KR>IIV @LKCFDROB QEB DOFA ?V AO>TFKD LO TFQE PMB@FCF@ HKLTK M>QQBOKP.

Exercise 7.7
)JMIBJBKQ hTO>M->OLRKAi CLO QEB '>JB LC ,FCB PL QE>Q @BIIP LK QEB BADBP E>SB KBFDE?LOP LK QEB LMMLPFQB PFAB LC QEB DOFA.

Exercise 7.8
7EFIB QEB >?LSB PLIRQFLK (%U>JMIB 7.2) FP @LKSBKFBKQ, FQ FP KLQ M>OQF@RI>OIV JBJLOVBCCF@FBKQ. )Q @OB>QBP > KBT 2$ >OO>V CLO BSBOV CO>JB LC >KFJ>QFLK! 4EFP J>QQBOP SBOV IFQQIB CLO > 0OL@BPPFKD ABPHQLM >MMIF@>QFLK, ?RQ FC VLR TBOB FJMIBJBKQFKD QEB '>JB LC ,FCB LK > JF@OL@LKQOLIIBO LO JL?FIB ABSF@B, VLRgA T>KQ QL ?B JLOB @>OBCRI. /KB PLIRQFLK FP QL E>SB LKIV QTL >OO>VP >KA @LKPQ>KQIV PT>M QEBJ, TOFQFKD QEB KBUQ PBQ LC PQ>QBP FKQL TEF@EBSBO LKB FPKgQ QEB @ROOBKQ >OO>V. )JMIBJBKQ QEFP M>OQF@RI>O PLIRQFLK.

7.8 Object-Oriented Cells


/SBO QEB @LROPB LC QEB MOBSFLRP PFU @E>MQBOP, TBgSB PILTIV ?RFIQ BU>JMIBP LC PVPQBJP LC C6>97HG TFQE MOLMBOQFBP QE>Q JLSB >?LRQ QEB P@OBBK. !KA FK QEFP @E>MQBO, >IQELRDE TBgSB ?BBK Q>IHFKD >?LRQ > h@BIIi >P FC FQ TBOB >K L?GB@Q, TB >@QR>IIV E>SBKgQ ?BBK RPFKD >KV L?GB@Q LOFBKQ>QFLK FK LRO @LAB (LQEBO QE>K > @I>PP QL ABP@OF?B QEB #! PVPQBJ >P > TELIB). 4EFP E>P TLOHBA ?B@>RPB > @BII FP PR@E >K BKLOJLRPIV PFJMIB L?GB@Q (> PFKDIB ?FQ). (LTBSBO, FK > JLJBKQ, TB >OB DLFKD QL AFP@RPP PLJB FAB>P CLO CROQEBO ABSBILMFKD #! PVPQBJP, J>KV LC TEF@E FKSLISB HBBMFKD QO>@H LC JRIQFMIB MOLMBOQFBP CLO B>@E @BII. &LO BU>JMIB, TE>Q FC > @BII KBBABA QL OBJBJ?BO FQP I>PQ QBK PQ>QBP? /O TE>Q FC TB T>KQBA QL >MMIV PLJB LC LRO JLQFLK >KA MEVPF@P QEFKHFKD QL > #! >KA E>SB QEB @BIIP JLSB >?LRQ QEB TFKALT, AVK>JF@>IIV @E>KDFKD QEBFO KBFDE?LOP COLJ CO>JB QL CO>JB?

349

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

4L >@@LJMIFPE >KV LC QEBPB FAB>P (>KA JLOB), FQ TLRIA ?B EBIMCRI QL PBB ELT TB JFDEQ QOB>Q > @BII >P >K L?GB@Q TFQE JRIQFMIB MOLMBOQFBP, O>QEBO QE>K >P > PFKDIB 0 LO 1. 4L PELT QEFP, IBQgP GRPQ OB@OB>QB QEB '>JB LC ,FCB PFJRI>QFLK. /KIV FKPQB>A LC:
CHN8989 <I;L>;

,BQgP E>SB:
!?FF8989 <I;L>;

TEBOB !?FF FP > @I>PP TB TFII TOFQB. 7E>Q >OB QEB MOLMBOQFBP LC > !?FF L?GB@Q? )K LRO '>JB LC ,FCB BU>JMIB, B>@E @BII E>P > IL@>QFLK >KA PFWB, >P TBII >P > PQ>QB.
=F;MM !?FF U @FI;N R, S; @FI;N Q; Location and size

CHN MN;N?;

What is the cells state?

)K QEB KLK-//0 SBOPFLK, TB RPBA > PBM>O>QB 2$ >OO>V QL HBBM QO>@H LC QEB PQ>QBP CLO QEB @ROOBKQ >KA KBUQ DBKBO>QFLK. "V J>HFKD > @BII >K L?GB@Q, ELTBSBO, B>@E @BII @LRIA HBBM QO>@H LC ?LQE PQ>QBP. )K QEFP @>PB, TBgII QEFKH LC QEB @BII >P OBJBJ?BOFKD FQP MOBSFLRP PQ>QB (CLO TEBK KBT PQ>QBP KBBA QL ?B @LJMRQBA).
CHN JL?PCIOM; What was its previous state?

4EFP >IILTP RP QL SFPR>IFWB JLOB FKCLOJ>QFLK >?LRQ TE>Q QEB PQ>QB FP ALFKD. &LO BU>JMIB, TB @LRIA @ELLPB QL @LILO > @BII AFCCBOBKQIV FC FQP PQ>QB E>P @E>KDBA. &LO BU>JMIB:

Example 7.3: Game of Life OOP


PIC> >CMJF;S() U

350

4EB .>QROB LC #LAB (S1.0)

C@ (JL?PCIOM == 0 && MN;N? == 1) @CFF(0,0,255); ?FM? C@ (MN;N? == 1) @CFF(0);

If the cell is born, color it blue!

?FM? C@ (JL?PCIOM == 1 && MN;N? == 0) @CFF(255,0,0); ?FM? @CFF(255); L?=N(R, S, Q, Q); W If the cell dies, color it red!

.LQ JR@E BIPB >?LRQ QEB @LAB (>Q IB>PQ CLO LRO MROMLPBP EBOB) E>P QL @E>KDB. 4EB KBFDE?LOP @>K PQFII ?B @LRKQBA QEB P>JB T>V; QEB AFCCBOBK@B FP QE>Q TB KLT KBBA QL OBCBO QL QEB L?GB@QgP PQ>QB S>OF>?IBP >P TB ILLM QEOLRDE QEB 2$ >OO>V.
@IL (CHN R = 1; R < =IFOGHM-1; R++) U @IL (CHN S = 1; S < LIQM-1; S++) U CHN H?CAB<ILM = 0; @IL (CHN C = -1; C <= 1; C++) U @IL (CHN D = -1; D <= 1; D++) U D;?=>8EHI += 8E7H:4N+?54O+@5.FH;L?EKI; W W H?CAB<ILM -= <I;L>8R98S9.JL?PCIOM; Use the previous state when tracking neighbors.

We are calling a function newState() to assign a new state to each cell. C@ ((<I;L>8R98S9.MN;N? == 1) && (H?CAB<ILM < 2)) <I;L>8R98S9.H?Q0N;N?(0); ?FM? C@ ((<I;L>8R98S9.MN;N? == 1) && (H?CAB<ILM > 3)) <I;L>8R98S9.H?Q0N;N?(0); ?FM? C@ ((<I;L>8R98S9.MN;N? == 0) && (H?CAB<ILM == 3)) <I;L>8R98S9.H?Q0N;N?(1); W W else do nothing!

7.9 Variations of Traditional CA


.LT QE>Q TB E>SB @LSBOBA QEB ?>PF@ @LK@BMQP, >IDLOFQEJP, >KA MOLDO>JJFKD PQO>QBDFBP ?BEFKA QEB JLPQ C>JLRP 1$ >KA 2$ @BIIRI>O >RQLJ>Q>, FQgP QFJB QL QEFKH >?LRQ ELT VLR JFDEQ Q>HB QEFP CLRKA>QFLK LC @LAB >KA ?RFIA LK FQ, ABSBILMFKD @OB>QFSB >MMIF@>QFLKP LC #!P FK VLRO LTK TLOH. )K QEFP PB@QFLK, TBgII Q>IH QEOLRDE PLJB FAB>P CLO BUM>KAFKD QEB CB>QROBP LC QEB #! BU>JMIBP. %U>JMIB >KPTBOP QL B>@E LC QEBPB BUBO@FPBP @>K ?B CLRKA LK QEB ?LLH TB?PFQB. 1) #:9-=0.?,92@7,= G=4/> . 4EBOBgP KL M>OQF@RI>O OB>PLK TEV VLR PELRIA IFJFQ VLROPBIC QL E>SFKD VLRO @BIIP LK > OB@Q>KDRI>O DOFA. 7E>Q E>MMBKP FC VLR ABPFDK > #! TFQE >KLQEBO QVMB LC PE>MB?

351

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

Exercise 7.9
#OB>QB > #! RPFKD > DOFA LC EBU>DLKP (>P ?BILT), B>@E TFQE PFU KBFDE?LOP.

2) %=:-,-474>?4. . 4EB ORIBP LC > #! ALKgQ KB@BPP>OFIV E>SB QL ABCFKB >K BU>@Q LRQ@LJB.

Exercise 7.10
2BTOFQB QEB '>JB LC ,FCB ORIBP >P CLIILTP: /SBOMLMRI>QFLK: )C QEB @BII E>P CLRO LO JLOB >IFSB KBFDE?LOP, FQ E>P > 80% @E>K@B LC AVFKD. ,LKBIFKBPP: )C QEB @BII E>P LKB LO CBTBO >IFSB KBFDE?LOP, FQ E>P > 60% @E>K@B LC AVFKD. %Q@. 3) C:9?49@:@>. 7BgSB ILLHBA >Q BU>JMIBP TEBOB QEB @BIIgP PQ>QB @>K LKIV ?B > 1 LO > 0. "RQ TE>Q FC QEB @BIIgP PQ>QB T>P > CIL>QFKD MLFKQ KRJ?BO ?BQTBBK 0 >KA 1?

Exercise 7.11
!A>MQ 7LICO>J BIBJBKQ>OV #! QL E>SB QEB PQ>QB ?B > CIL>Q. 9LR @LRIA ABCFKB ORIBP PR@E >P, h)C QEB PQ>QB FP DOB>QBO QE>K 0.5i LO hkIBPP QE>K 0.2.i 4) I8,20 %=:.0>>492. 7B ?OFBCIV QLR@EBA LK QEFP B>OIFBO, ?RQ J>KV FJ>DB-MOL@BPPFKD >IDLOFQEJP LMBO>QB LK #!-IFHB ORIBP. "IROOFKD >K FJ>DB FP @OB>QFKD > KBT MFUBI LRQ LC QEB >SBO>DB LC > KBFDE?LOELLA LC MFUBIP. 3FJRI>QFLKP LC FKH AFPMBOPFKD LK M>MBO LO T>QBO OFMMIFKD LSBO >K FJ>DB @>K ?B >@EFBSBA TFQE #! ORIBP.

352

4EB .>QROB LC #LAB (S1.0)

Exercise 7.12
#OB>QB > #! FK TEF@E > MFUBI FP > @BII >KA > @LILO FP FQP PQ>QB. 5) H4>?:=4.,7. )K QEB '>JB LC ,FCB L?GB@Q-LOFBKQBA BU>JMIB, TB RPBA QTL S>OF>?IBP QL HBBM QO>@H LC FQP PQ>QB: @ROOBKQ >KA MOBSFLRP. 7E>Q FC VLR RPB >K >OO>V QL HBBM QO>@H LC > @BIIgP PQ>QB EFPQLOV? 4EFP OBI>QBP QL QEB FAB> LC > h@LJMIBU >A>MQFSB PVPQBJ,i LKB QE>Q E>P QEB >?FIFQV QL >A>MQ >KA @E>KDB FQP ORIBP LSBO QFJB ?V IB>OKFKD COLJ FQP EFPQLOV. 7BgII PBB >K BU>JMIB LC QEFP FK #E>MQBO 10: .BRO>I .BQTLOHP.

Exercise 7.13
6FPR>IFWB QEB '>JB LC ,FCB ?V @LILOFKD B>@E @BII >@@LOAFKD QL ELT ILKD FQgP ?BBK >IFSB LO AB>A. #>K VLR >IPL RPB QEB @BIIgP EFPQLOV QL FKCLOJ QEB ORIBP? 6) ":A492 .077> . )K QEBPB ?>PF@ BU>JMIBP, @BIIP E>SB > CFUBA MLPFQFLK LK > DOFA, ?RQ VLR @LRIA ?RFIA > #! TFQE @BIIP QE>Q E>SB KL CFUBA MLPFQFLK >KA FKPQB>A JLSB >?LRQ QEB P@OBBK.

Exercise 7.14
5PB #! ORIBP FK > CIL@HFKD PVPQBJ. 7E>Q FC B>@E ?LFA E>A > PQ>QB (QE>Q MBOE>MP FKCLOJP FQP PQBBOFKD ?BE>SFLOP) >KA FQP KBFDE?LOELLA @E>KDBA COLJ CO>JB QL CO>JB >P FQ JLSBA @ILPBO QL LO CROQEBO COLJ LQEBO ?LFAP? 7) #0>?492 . !KLQEBO CB>QROB LC @LJMIBU PVPQBJP FP QE>Q QEBV @>K ?B KBPQBA. /RO TLOIA QBKAP QL TLOH QEFP T>V: > @FQV FP > @LJMIBU PVPQBJ LC MBLMIB, > MBOPLK FP > @LJMIBU PVPQBJ LC LOD>KP, >K LOD>K FP > @LJMIBU PVPQBJ LC @BIIP, >KA PL LK >KA PL CLOQE.

Exercise 7.15
$BPFDK > #! FK TEF@E B>@E @BII FQPBIC FP > PJ>IIBO #! LO > PVPQBJ LC ?LFAP.

353

#E>MQBO 7. #BIIRI>O !RQLJ>Q>

The Ecosystem Project


Step 7 Exercise: Incorporate cellular automata into your ecosystem. Some possibilities:

Give each creature a state. How can that state drive their behavior? Taking inspiration from CA, how can that state change over time according to its neighbors states? Consider the ecosystems world to be a CA. The creatures move from tile to tile. Each tile has a stateis it land? water? food? Use a CA to generate a pattern for the design of a creature in your ecosystem.

354

4EB .>QROB LC #LAB (S1.0)

Chapter 8. Fractals
I'.A5<9<460.9 :<;@A2?@! 0?621 A52 A2??63621 :.A52:.A606.; EC2?F <;2 <3 A52: . @=96;A2? 6; :F 2F2 ! 5.A2 A52 '2.;< )=.02 .;1 A52 "<05 CB?C2 ! 32.? A52 C.;A<? *2?;.?F )2A *52 )62?=6;@86 G.@82A :.82@ :2 D.;;. 0?F A;1 . :6996<; :692@ .D.F . /BAA2?39F 39.==21 6A@ D6;4@ &; . 0<91 %<C2:/2? 1.F . :.; ;.:21 B2;<6A $.;129/?<A D.@ /<?;J Jonathan Coulton, lyrics from Mandelbrot Set

/K@B RMLK > QFJB, ) QLLH > @LROPB FK EFDE P@ELLI @>IIBA h'BLJBQOV.i 0BOE>MP VLR AFA QLL. 9LR IB>OKBA >?LRQ PE>MBP FK LKB AFJBKPFLK, QTL AFJBKPFLKP, >KA J>V?B BSBK QEOBB. 7E>Q FP QEB @FO@RJCBOBK@B LC > @FO@IB? 4EB >OB> LC > OB@Q>KDIB? 4EB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB? #LJB QL QEFKH LC FQ, TBgSB ?BBK PQRAVFKD DBLJBQOV >II >ILKD FK QEFP ?LLH, RPFKD SB@QLOP QL ABP@OF?B QEB JLQFLK LC ?LAFBP FK #>OQBPF>K PM>@B. 4EFP PLOQ LC DBLJBQOV FP DBKBO>IIV OBCBOOBA QL >P %R@IFAB>K DBLJBQOV, >CQBO QEB 'OBBH J>QEBJ>QF@F>K %R@IFA.

F64B?2 8.1

355

#E>MQBO 8. &O>@Q>IP

&LO RP K>QROB @LABOP, TB E>SB QL >PH QEB NRBPQFLK: #>K TB ABP@OF?B LRO TLOIA TFQE %R@IFAB>K DBLJBQOV? 4EB ,#$ P@OBBK )gJ PQ>OFKD >Q OFDEQ KLT PROB ILLHP IFHB > OB@Q>KDIB. !KA QEB MIRJ ) >QB QEFP JLOKFKD FP @FO@RI>O. "RQ TE>Q FC ) TBOB QL ILLH CROQEBO, >KA @LKPFABO QEB QOBBP QE>Q IFKB QEB PQOBBQ, QEB IB>SBP QE>Q E>KD LCC QELPB QOBBP, QEB IFDEQKFKD COLJ I>PQ KFDEQgP QERKABOPQLOJ, QEB @>RIFCILTBO ) >QB CLO AFKKBO, QEB ?ILLA SBPPBIP FK JV ?LAV, >KA QEB JLRKQ>FKP >KA @L>PQIFKBP QE>Q @LSBO I>KA ?BVLKA .BT 9LOH #FQV? -LPQ LC QEB PQRCC VLR CFKA FK K>QROB @>KKLQ ?B ABP@OF?BA ?V QEB FAB>IFWBA DBLJBQOF@>I CLOJP LC %R@IFAB>K DBLJBQOV. 3L FC TB T>KQ QL PQ>OQ ?RFIAFKD @LJMRQ>QFLK>I ABPFDKP TFQE M>QQBOKP ?BVLKA QEB PFJMIB PE>MBP ?FFCJM?() , L?=N() , >KA FCH?() , FQgP QFJB CLO RP QL IB>OK >?LRQ QEB @LK@BMQP ?BEFKA >KA QB@EKFNRBP CLO PFJRI>QFKD QEB DBLJBQOV LC K>QROB: CO>@Q>IP.

8.1 What Is a Fractal?


4EB QBOJ 1=,.?,7 (COLJ QEB ,>QFK :F57HIG, JB>KFKD h?OLHBKi) T>P @LFKBA ?V QEB J>QEBJ>QF@F>K "BKLFQ ->KABI?OLQ FK 1975. )K EFP PBJFK>I TLOH h4EB &O>@Q>I 'BLJBQOV LC .>QROB,i EB ABCFKBP > CO>@Q>I >P h> OLRDE LO CO>DJBKQBA DBLJBQOF@ PE>MB QE>Q @>K ?B PMIFQ FKQL M>OQP, B>@E LC TEF@E FP (>Q IB>PQ >MMOLUFJ>QBIV) > OBAR@BA-PFWB @LMV LC QEB TELIB.i

F64B?2 8.2: &;2 <3 A52 :<@A D299-8;<D; .;1 ?20<4;6G./92 3?.0A.9 =.AA2?;@ 6@ ;.:21 3<? B2;<6A $.;129/?<A 56:@293. G2;2?.A6;4 A52 $.;129/?<A @2A 6;C<9C2@ A2@A6;4 A52 =?<=2?A62@ <3 0<:=92E ;B:/2?@ .3A2? A52F .?2 =.@@21 A5?<B45 .; 6A2?.A6C2 3B;0A6<;. D< A52F A2;1 A< 6;36;6AF? D< A52F @A.F /<B;121? ,5692 . 3.@06;.A6;4 :.A52:.A60.9 16@0B@@6<;, A56@ I2@0.=2-A6:2J .94<?6A5: 6@ . 92@@ =?.0A60.9 :2A5<1 3<? 42;2?.A6;4 3?.0A.9@ A5.; A52 ?20B?@6C2 A205;6>B2@ D2K99 2E.:6;2 6; A56@ 05.=A2?. <D2C2?, .; 2E.:=92 3<? 42;2?.A6;4 A52 $.;129/?<A @2A 6@ 6;09B121 6; A52 0<12 2E.:=92@.

,BQgP FIIRPQO>QB QEFP ABCFKFQFLK TFQE QTL PFJMIB BU>JMIBP. &FOPQ, IBQgP QEFKH >?LRQ > QOBB ?O>K@EFKD PQOR@QROB (CLO TEF@E TBgII TOFQB QEB @LAB I>QBO):

356

4EB .>QROB LC #LAB (S1.0)

F64B?2 8.3

.LQF@B ELT QEB QOBB FK &FDROB 8.3 E>P > PFKDIB OLLQ TFQE QTL ?O>K@EBP @LKKB@QBA >Q FQP BKA. %>@E LKB LC QELPB ?O>K@EBP E>P QTL ?O>K@EBP >Q FQP BKA >KA QELPB ?O>K@EBP E>SB QTL ?O>K@EBP >KA PL LK >KA PL CLOQE. 7E>Q FC TB TBOB QL MIR@H LKB ?O>K@E COLJ QEB QOBB >KA BU>JFKB FQ LK FQP LTK?

F64B?2 8.4

,LLHFKD @ILPBIV >Q > DFSBK PB@QFLK LC QEB QOBB, TB CFKA QE>Q QEB PE>MB LC QEFP ?O>K@E OBPBJ?IBP QEB QOBB FQPBIC. 4EFP FP HKLTK >P >071->4847,=4?D ; >P ->KABI?OLQ PQ>QBA, B>@E M>OQ FP > hOBAR@BAPFWB @LMV LC QEB TELIB.i 4EB >?LSB QOBB FP MBOCB@QIV PVJJBQOF@>I >KA QEB M>OQP >OB, FK C>@Q, BU>@Q OBMIF@>P LC QEB TELIB. (LTBSBO, CO>@Q>IP AL KLQ E>SB QL ?B MBOCB@QIV PBIC-PFJFI>O. ,BQgP Q>HB > ILLH >Q > DO>ME LC QEB PQL@H J>OHBQ (>A>MQBA COLJ >@QR>I !MMIB PQL@H A>Q>).

F64B?2 8.5: G?.=5 A

!KA LKB JLOB.

357

#E>MQBO 8. &O>@Q>IP

F64B?2 8.6: G?.=5 B

)K QEBPB DO>MEP, QEB U->UFP FP QFJB >KA QEB V->UFP FP QEB PQL@HgP S>IRB. )QgP KLQ >K >@@FABKQ QE>Q ) LJFQQBA QEB I>?BIP, ELTBSBO. 'O>MEP LC PQL@H J>OHBQ A>Q> >OB BU>JMIBP LC CO>@Q>IP ?B@>RPB QEBV ILLH QEB P>JB >Q >KV P@>IB. !OB QEBPB DO>MEP LC QEB PQL@H LSBO LKB VB>O? /KB A>V? /KB ELRO? 4EBOBgP KL T>V CLO VLR QL HKLT TFQELRQ > I>?BI. ()K@FABKQ>IIV, DO>ME ! PELTP PFU JLKQEPg TLOQE LC A>Q> >KA DO>ME " WLLJP FKQL > QFKV M>OQ LC DO>ME !, PELTFKD PFU ELROP.)

F64B?2 8.7

4EFP FP >K BU>JMIB LC > >?:.3,>?4. CO>@Q>I, JB>KFKD QE>Q FQ FP ?RFIQ LRQ LC MOL?>?FIFQFBP >KA O>KALJKBPP. 5KIFHB QEB ABQBOJFKFPQF@ QOBB-?O>K@EFKD PQOR@QROB, FQ FP PQ>QFPQF@>IIV PBIC-PFJFI>O. !P TB DL QEOLRDE QEB BU>JMIBP FK QEFP @E>MQBO, TB TFII ILLH >Q ?LQE ABQBOJFKFPQF@ >KA PQL@E>PQF@ QB@EKFNRBP CLO DBKBO>QFKD CO>@Q>I M>QQBOKP. 7EFIB PBIC-PFJFI>OFQV FP > HBV QO>FQ LC CO>@Q>IP, FQgP FJMLOQ>KQ QL OB>IFWB QE>Q PBIC-PFJFI>OFQV >ILKB ALBP KLQ J>HB > CO>@Q>I. !CQBO >II, > IFKB FP PBIC-PFJFI>O. ! IFKB ILLHP QEB P>JB >Q >KV P@>IB, >KA @>K ?B QELRDEQ LC >P @LJMOFPFKD ILQP LC IFQQIB IFKBP. "RQ FQgP KLQ > CO>@Q>I. &O>@Q>IP >OB @E>O>@QBOFWBA ?V E>SFKD > CFKB PQOR@QROB >Q PJ>II P@>IBP (HBBM WLLJFKD FKQL QEB PQL@H J>OHBQ DO>ME >KA VLRgII @LKQFKRB QL CFKA CIR@QR>QFLKP) >KA @>KKLQ ?B ABP@OF?BA TFQE %R@IFAB>K DBLJBQOV. )C VLR @>K P>V h)QgP > IFKB!i QEBK FQgP KLQ > CO>@Q>I. !KLQEBO CRKA>JBKQ>I @LJMLKBKQ LC CO>@Q>I DBLJBQOV FP OB@ROPFLK. &O>@Q>IP >II E>SB > OB@ROPFSB ABCFKFQFLK. 7BgII PQ>OQ TFQE OB@ROPFLK ?BCLOB ABSBILMFKD QB@EKFNRBP >KA @LAB BU>JMIBP CLO ?RFIAFKD CO>@Q>I M>QQBOKP FK 0OL@BPPFKD.

8.2 Recursion
,BQgP ?BDFK LRO AFP@RPPFLK LC OB@ROPFLK ?V BU>JFKFKD QEB CFOPQ >MMB>O>K@B LC CO>@Q>IP FK JLABOK J>QEBJ>QF@P. )K 1883, 'BOJ>K J>QEBJ>QF@F>K 'BLODB #>KQLO ABSBILMBA PFJMIB ORIBP QL DBKBO>QB >K FKCFKFQB PBQ:

358

4EB .>QROB LC #LAB (S1.0)

F64B?2 8.8: *52 C.;A<? @2A

4EBOB FP > CBBA?>@H ILLM >Q TLOH EBOB. 4>HB > PFKDIB IFKB >KA ?OB>H FQ FKQL QTL. 4EBK OBQROK QL QELPB QTL IFKBP >KA >MMIV QEB P>JB ORIB, ?OB>HFKD B>@E IFKB FKQL QTL, >KA KLT TBgOB IBCQ TFQE CLRO. 4EBK OBQROK QL QELPB CLRO IFKBP >KA >MMIV QEB ORIB. .LT VLRgSB DLQ BFDEQ. 4EFP MOL@BPP FP HKLTK >P =0.@=>4:9 : QEB OBMB>QBA >MMIF@>QFLK LC > ORIB QL PR@@BPPFSB OBPRIQP. #>KQLO T>P FKQBOBPQBA FK TE>Q E>MMBKP TEBK VLR >MMIV QEBPB ORIBP >K FKCFKFQB KRJ?BO LC QFJBP. 7B, ELTBSBO, >OB TLOHFKD FK > CFKFQB MFUBI PM>@B >KA @>K JLPQIV FDKLOB QEB NRBPQFLKP >KA M>O>ALUBP QE>Q >OFPB COLJ FKCFKFQB OB@ROPFLK. 7B TFII FKPQB>A @LKPQOR@Q LRO @LAB FK PR@E > T>V QE>Q TB AL KLQ >MMIV QEB ORIBP CLOBSBO (TEF@E TLRIA @>RPB LRO MOLDO>J QL COBBWB). "BCLOB TB FJMIBJBKQ QEB #>KQLO PBQ, IBQgP Q>HB > ILLH >Q TE>Q FQ JB>KP QL E>SB OB@ROPFLK FK @LAB. (BOBgP PLJBQEFKD TBgOB RPBA QL ALFKD >II QEB QFJBe@>IIFKD > CRK@QFLK FKPFAB >KLQEBO CRK@QFLK.
PIC> MIG?$OH=NCIH() U <;=EALIOH>(0); W Calling the function background() in the definition of someFunction()

7E>Q TLRIA E>MMBK FC TB @>IIBA QEB CRK@QFLK TB >OB ABCFKFKD TFQEFK QEB CRK@QFLK FQPBIC? #>K MIG?$OH=NCIH() @>II MIG?$OH=NCIH() ?
PIC> MIG?$OH=NCIH() U MIG?$OH=NCIH(); W

)K C>@Q, QEFP FP KLQ LKIV >IILTBA, ?RQ FQgP NRFQB @LJJLK (>KA BPPBKQF>I QL ELT TB TFII FJMIBJBKQ QEB #>KQLO PBQ). &RK@QFLKP QE>Q @>II QEBJPBISBP >OB F97IFG=J9 >KA DLLA CLO PLISFKD @BOQ>FK MOL?IBJP. &LO BU>JMIB, @BOQ>FK J>QEBJ>QF@>I @>I@RI>QFLKP >OB FJMIBJBKQBA OB@ROPFSBIV; QEB JLPQ @LJJLK BU>JMIB FP :57HCF=5@ . 4EB C>@QLOF>I LC >KV KRJ?BO K, RPR>IIV TOFQQBK >P K!, FP ABCFKBA >P:
D! = D * D W 1 * . . . . * 3 * 2 * 1 0! = 1

(BOB TBgII TOFQB > CRK@QFLK FK 0OL@BPPFKD QE>Q RPBP > @IL ILLM QL @>I@RI>QB C>@QLOF>I:
CHN @;=NILC;F(CHN H) U CHN @ = 1;

359

#E>MQBO 8. &O>@Q>IP

@IL (CHN C = 0; C < H; C++) U @ = @ * (C+1); W L?NOLH @; W

Using a regular loop to compute factorial

5MLK @ILPB BU>JFK>QFLK, VLRgII KLQF@B PLJBQEFKD FKQBOBPQFKD >?LRQ ELT C>@QLOF>I TLOHP. ,BQgP ILLH >Q 4! >KA 3!
4! = 4 * 3 * 2 * 1 3! = 3 * 2 * 1

?30=01:=0. . .
4! = 4 * 3!

)K JLOB DBKBO>I QBOJP, CLO >KV MLPFQFSB FKQBDBO K:


D! = D * (D-1)! 1! = 1

7OFQQBK LRQ: 4EB :57HCF=5@ LC H FP ABCFKBA >P H QFJBP QEB :57HCF=5@ LC H-1 . 4EB ABCFKFQFLK LC 1,.?:=4,7 FK@IRABP 1,.?:=4,7?! )QgP HFKA LC IFHB ABCFKFKD hQFOBA" >P hQEB CBBIFKD VLR DBQ TEBK VLR >OB QFOBA.i 4EFP @LK@BMQ LC PBIC-OBCBOBK@B FK CRK@QFLKP FP >K BU>JMIB LC OB@ROPFLK. !KA TB @>K RPB FQ QL TOFQB > C>@QLOF>I CRK@QFLK QE>Q @>IIP FQPBIC.
CHN @;=NILC;F(CHN H) U C@ (H == 1) U L?NOLH 1; W ?FM? U L?NOLH H * @;=NILC;F(H-1); W W

)Q J>V ILLH @O>WV, ?RQ FQ TLOHP. (BOB >OB QEB PQBMP QE>Q E>MMBK TEBK @;=NILC;F(4) FP @>IIBA.

360

4EB .>QROB LC #LAB (S1.0)

F64B?2 8.9

7B @>K >MMIV QEB P>JB MOFK@FMIB QL DO>MEF@P TFQE FKQBOBPQFKD OBPRIQP, >P TB TFII PBB FK J>KV BU>JMIBP QEOLRDELRQ QEFP @E>MQBO. 4>HB > ILLH >Q QEFP OB@ROPFSB CRK@QFLK.

Example 8.1: Recursive Circles I


PIC> >L;Q!CL=F?(CHN R, CHN S, @FI;N L;>COM) U ?FFCJM?(R, S, L;>COM, L;>COM); C@(L;>COM > 2) U L;>COM *= 0.75@; >L;Q!CL=F?(R, S, L;>COM); W W The drawCircle() function is calling itself recursively.

>L;Q!CL=F?() AO>TP >K BIIFMPB ?>PBA LK > PBQ LC M>O>JBQBOP QE>Q FQ OB@BFSBP >P >ODRJBKQP. )Q

QEBK @>IIP FQPBIC TFQE QELPB P>JB M>O>JBQBOP, >AGRPQFKD QEBJ PIFDEQIV. 4EB OBPRIQ FP > PBOFBP LC @FO@IBP, B>@E LC TEF@E FP AO>TK FKPFAB QEB MOBSFLRP @FO@IB.

361

#E>MQBO 8. &O>@Q>IP

.LQF@B QE>Q QEB >?LSB CRK@QFLK LKIV OB@ROPFSBIV @>IIP FQPBIC FC QEB O>AFRP FP DOB>QBO QE>K 2. 4EFP FP > @OR@F>I MLFKQ. !P TFQE FQBO>QFLK, 5@@ F97IFG=J9 :IB7H=CBG AIGH <5J9 5B 9L=H 7CB8=H=CB! 9LR IFHBIV >OB >IOB>AV >T>OB QE>Q >II @IL >KA QBCF? ILLMP JRPQ FK@IRAB > ?LLIB>K BUMOBPPFLK QE>Q BSBKQR>IIV BS>IR>QBP QL C>IPB, QERP BUFQFKD QEB ILLM. 7FQELRQ LKB, QEB MOLDO>J TLRIA @O>PE, @>RDEQ FKPFAB LC >K FKCFKFQB ILLM. 4EB P>JB @>K ?B P>FA >?LRQ OB@ROPFLK. )C > OB@ROPFSB CRK@QFLK @>IIP FQPBIC CLOBSBO >KA BSBO, VLRgII ?B JLPQ IFHBIV ?B QOB>QBA QL > KF@B COLWBK P@OBBK. 4EFP @FO@IBP BU>JMIB FP O>QEBO QOFSF>I; FQ @LRIA B>PFIV ?B >@EFBSBA QEOLRDE PFJMIB FQBO>QFLK. (LTBSBO, CLO P@BK>OFLP FK TEF@E > CRK@QFLK @>IIP FQPBIC JLOB QE>K LK@B, OB@ROPFLK ?B@LJBP TLKABOCRIIV BIBD>KQ. ,BQgP J>HB >L;Q!CL=F?() > ?FQ JLOB @LJMIBU. &LO BSBOV @FO@IB AFPMI>VBA, AO>T > @FO@IB E>IC FQP PFWB QL QEB IBCQ >KA OFDEQ LC QE>Q @FO@IB.

Example 8.2: Recursion twice


PIC> M?NOJ() U MCT?(640,360); W PIC> >L;Q() U <;=EALIOH>(255); >L;Q!CL=F?(QC>NB/2,B?CABN/2,200); W PIC> >L;Q!CL=F?(@FI;N R, @FI;N S, @FI;N L;>COM) U MNLIE?(0); HI$CFF(); ?FFCJM?(R, S, L;>COM, L;>COM); C@(L;>COM > 2) U >L;Q!CL=F?(R + L;>COM/2, S, L;>COM/2); >L;Q!CL=F?(R - L;>COM/2, S, L;>COM/2); drawCircle() calls itself twice, creating a branching effect. For every circle, a smaller circle is drawn to the left and the right.

W W

362

4EB .>QROB LC #LAB (S1.0)

7FQE GRPQ > IFQQIB JLOB @LAB, TB @LRIA >IPL >AA > @FO@IB >?LSB >KA ?BILT B>@E @FO@IB.

Example 8.3: Recursion four times


PIC> >L;Q!CL=F?(@FI;N R, @FI;N S, @FI;N L;>COM) U ?FFCJM?(R, S, L;>COM, L;>COM); C@(L;>COM > 8) U >L;Q!CL=F?(R + L;>COM/2, S, L;>COM/2); >L;Q!CL=F?(R - L;>COM/2, S, L;>COM/2); >L;Q!CL=F?(R, S + L;>COM/2, L;>COM/2); >L;Q!CL=F?(R, S - L;>COM/2, L;>COM/2); W W

4OV OBMOLAR@FKD QEFP PHBQ@E TFQE FQBO>QFLK FKPQB>A LC OB@ROPFLKe) A>OB VLR!

8.3 The Cantor Set with a Recursive Function


.LT TBgOB OB>AV QL SFPR>IFWB QEB #>KQLO PBQ FK 0OL@BPPFKD RPFKD > OB@ROPFSB CRK@QFLK. 7EBOB AL TB ?BDFK? 7BII, TB HKLT QE>Q QEB #>KQLO PBQ ?BDFKP TFQE > IFKB. 3L IBQgP PQ>OQ QEBOB >KA TOFQB > CRK@QFLK QE>Q AO>TP > IFKB.
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U FCH?(R,S,R+F?H,S); W

4EB >?LSB =;HNIL() CRK@QFLK AO>TP > IFKB QE>Q PQ>OQP >Q MFUBI @LLOAFK>QB (L,M) TFQE > IBKDQE LC F?H . (4EB IFKB FP AO>TK ELOFWLKQ>IIV EBOB, ?RQ QEFP FP >K >O?FQO>OV AB@FPFLK.) 3L FC TB @>IIBA QE>Q CRK@QFLK, P>VFKD:
=;HNIL(10, 20, QC>NB-20);

TBgA DBQ QEB CLIILTFKD:

363

#E>MQBO 8. &O>@Q>IP

F64B?2 8.10

.LT, QEB #>KQLO ORIB QBIIP RP QL BO>PB QEB JFAAIB QEFOA LC QE>Q IFKB, TEF@E IB>SBP RP TFQE QTL IFKBP, LKB COLJ QEB ?BDFKKFKD LC QEB IFKB QL QEB LKB-QEFOA J>OH, >KA LKB COLJ QEB QTL-QEFOAP J>OH QL QEB BKA LC QEB IFKB. 7B @>K KLT >AA QTL JLOB IFKBP LC @LAB QL AO>T QEB PB@LKA M>FO LC IFKBP, JLSFKD QEB V-IL@>QFLK ALTK > ?RK@E LC MFUBIP PL QE>Q TB @>K PBB QEB OBPRIQ ?BILT QEB LOFDFK>I IFKB.
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U FCH?(R,S,R+F?H,S); S += 20; B?D;(N,O,N+B;D/3,O); B?D;(N+B;D*2/3,O,N+B;D,O); W From start to 1/3rd From 2/3rd to end

F64B?2 8.11

F64B?2 8.12

7EFIB QEFP FP > CFKB PQ>OQ, PR@E > J>KR>I >MMOL>@E LC @>IIFKD FCH?() CLO B>@E IFKB FP KLQ TE>Q TB T>KQ. )Q TFII DBQ RKTFBIAV SBOV NRF@HIV, >P TBgA KBBA CLRO, QEBK BFDEQ, QEBK PFUQBBK @>IIP QL FCH?() . 9BP, > @IL ILLM FP LRO RPR>I T>V >OLRKA PR@E > MOL?IBJ, ?RQ DFSB QE>Q > QOV >KA VLRgII PBB QE>Q TLOHFKD LRQ QEB J>QE CLO B>@E FQBO>QFLK NRF@HIV MOLSBP FKLOAFK>QBIV @LJMIF@>QBA. (BOB FP TEBOB OB@ROPFLK @LJBP >KA OBP@RBP RP. 4>HB > ILLH >Q TEBOB TB AO>T QE>Q CFOPQ IFKB COLJ QEB PQ>OQ QL QEB LKB-QEFOA J>OH.
FCH?(R,S,R+F?H/3,S);

)KPQB>A LC @>IIFKD QEB FCH?() CRK@QFLK AFOB@QIV, TB @>K PFJMIV @>II QEB =;HNIL() CRK@QFLK FQPBIC. !CQBO >II, TE>Q ALBP QEB =;HNIL() CRK@QFLK AL? )Q AO>TP > IFKB >Q >K (L,M) IL@>QFLK TFQE > DFSBK IBKDQE! !KA PL:
FCH?(R,S,R+F?H/3,S); <?=IG?M -------> =;HNIL(R,S,F?H/3);

364

4EB .>QROB LC #LAB (S1.0)

!KA CLO QEB PB@LKA IFKB:


FCH?(R+F?H*2/3,S,R+F?H,S); <?=IG?M -------> =;HNIL(R+F?H*2/3,S,F?H/3);

,B>SFKD RP TFQE:
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U FCH?(R,S,R+F?H,S); S += 20; =;HNIL(R,S,F?H/3); =;HNIL(R+F?H*2/3,S,F?H/3); W

!KA PFK@B QEB =;HNIL() CRK@QFLK FP @>IIBA OB@ROPFSBIV, QEB P>JB ORIB TFII ?B >MMIFBA QL QEB KBUQ IFKBP >KA QL QEB KBUQ >KA QL QEB KBUQ >P =;HNIL() @>IIP FQPBIC >D>FK >KA >D>FK! .LT, ALKgQ DL >KA ORK QEFP @LAB VBQ. 7BgOB JFPPFKD QE>Q @OR@F>I BIBJBKQ: >K BUFQ @LKAFQFLK. 7BgII T>KQ QL J>HB PROB TB PQLM >Q PLJB MLFKQeCLO BU>JMIB, FC QEB IBKDQE LC QEB IFKB BSBO FP IBPP QE>K 1 MFUBI.

Example 8.4: Cantor set


PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U C@ (F?H >= 1) U FCH?(R,S,R+F?H,S); S += 20; =;HNIL(R,S,F?H/3); =;HNIL(R+F?H*2/3,S,F?H/3); W W Stop at 1 pixel!

365

#E>MQBO 8. &O>@Q>IP

Exercise 8.1
5PFKD >L;Q!CL=F?() >KA QEB #>KQLO PBQ >P JLABIP, DBKBO>QB VLRO LTK M>QQBOK TFQE OB@ROPFLK. (BOB FP > P@OBBKPELQ LC LKB QE>Q RPBP IFKBP.

8.4 The Koch Curve and the ArrayList Technique


7OFQFKD > CRK@QFLK QE>Q OB@ROPFSBIV @>IIP FQPBIC FP LKB QB@EKFNRB CLO DBKBO>QFKD > CO>@Q>I M>QQBOK LK P@OBBK. (LTBSBO, TE>Q FC VLR T>KQBA QEB IFKBP FK QEB >?LSB #>KQLO PBQ QL BUFPQ >P FKAFSFAR>I L?GB@QP QE>Q @LRIA ?B JLSBA FKABMBKABKQIV? 4EB OB@ROPFSB CRK@QFLK FP PFJMIB >KA BIBD>KQ, ?RQ FQ ALBP KLQ >IILT VLR QL AL JR@E ?BPFABP PFJMIV DBKBO>QFKD QEB M>QQBOK FQPBIC. (LTBSBO, QEBOB FP >KLQEBO T>V TB @>K >MMIV OB@ROPFLK FK @LJ?FK>QFLK TFQE >K ALL;S*CMN QE>Q TFII >IILT RP QL KLQ LKIV DBKBO>QB > CO>@Q>I M>QQBOK, ?RQ HBBM QO>@H LC >II FQP FKAFSFAR>I M>OQP >P L?GB@QP. 4L ABJLKPQO>QB QEFP QB@EKFNRB, IBQgP ILLH >Q >KLQEBO C>JLRP CO>@Q>I M>QQBOK, AFP@LSBOBA FK 1904 ?V 3TBAFPE J>QEBJ>QF@F>K (BIDB SLK +L@E. (BOB >OB QEB ORIBP. (.LQB QE>Q FQ PQ>OQP QEB P>JB T>V >P QEB #>KQLO PBQ, TFQE > PFKDIB IFKB.)

366

4EB .>QROB LC #LAB (S1.0)

F64B?2 8.13

4EB OBPRIQ ILLHP IFHB:

F64B?2 8.14

The Monster Curve


4EB +L@E @ROSB >KA LQEBO CO>@Q>I M>QQBOKP >OB LCQBK @>IIBA hJ>QEBJ>QF@>I JLKPQBOP.i 4EFP FP ARB QL >K LAA M>O>ALU QE>Q BJBODBP TEBK VLR >MMIV QEB OB@ROPFSB ABCFKFQFLK >K FKCFKFQB KRJ?BO LC QFJBP. )C QEB IBKDQE LC QEB LOFDFK>I PQ>OQFKD IFKB FP LKB, QEB CFOPQ FQBO>QFLK LC QEB +L@E @ROSB TFII VFBIA > IFKB LC IBKDQE CLRO-QEFOAP (B>@E PBDJBKQ FP LKBQEFOA QEB IBKDQE LC QEB PQ>OQFKD IFKB). $L FQ >D>FK >KA VLR DBQ > IBKDQE LC PFUQBBK-KFKQEP. !P VLR FQBO>QB QLT>OAP FKCFKFQV, QEB IBKDQE LC QEB +L@E @ROSB >MMOL>@EBP FKCFKFQV. 9BQ FQ CFQP FK QEB QFKV CFKFQB PM>@B MOLSFABA OFDEQ EBOB LK QEFP M>MBO (LO P@OBBK)! 3FK@B TB >OB TLOHFKD FK QEB 0OL@BPPFKD I>KA LC CFKFQB MFUBIP, QEFP QEBLOBQF@>I M>O>ALU TLKgQ ?B > C>@QLO CLO RP. 7BgII E>SB QL IFJFQ QEB KRJ?BO LC QFJBP TB OB@ROPFSBIV >MMIV QEB +L@E ORIBP PL QE>Q LRO MOLDO>J TLKgQ ORK LRQ LC JBJLOV LO @O>PE.

7B @LRIA MOL@BBA FK QEB P>JB J>KKBO >P TB AFA TFQE QEB #>KQLO PBQ, >KA TOFQB > OB@ROPFSB CRK@QFLK QE>Q FQBO>QFSBIV >MMIFBP QEB +L@E ORIBP LSBO >KA LSBO. .BSBOQEBIBPP, TB >OB DLFKD QL Q>@HIB QEFP MOL?IBJ FK > AFCCBOBKQ J>KKBO ?V QOB>QFKD B>@E PBDJBKQ LC QEB +L@E @ROSB >P >K FKAFSFAR>I L?GB@Q. 4EFP TFII LMBK RM PLJB ABPFDK MLPPF?FIFQFBP. &LO BU>JMIB, FC B>@E PBDJBKQ FP

367

#E>MQBO 8. &O>@Q>IP

>K L?GB@Q, TB @LRIA >IILT B>@E PBDJBKQ QL JLSB FKABMBKABKQIV COLJ FQP LOFDFK>I IL@>QFLK >KA M>OQF@FM>QB FK > MEVPF@P PFJRI>QFLK. )K >AAFQFLK, TB @LRIA RPB > O>KALJ @LILO, IFKB QEF@HKBPP, BQ@. QL AFPMI>V B>@E PBDJBKQ AFCCBOBKQIV. )K LOABO QL >@@LJMIFPE LRO DL>I LC QOB>QFKD B>@E PBDJBKQ >P >K FKAFSFAR>I L?GB@Q, TB JRPQ CFOPQ AB@FAB TE>Q QEFP L?GB@Q PELRIA ?B FK QEB CFOPQ MI>@B. 7E>Q A>Q> PELRIA FQ PQLOB? 7E>Q CRK@QFLKP PELRIA FQ E>SB? 4EB +L@E @ROSB FP > PBOFBP LC @LKKB@QBA IFKBP, >KA PL TB TFII QEFKH LC B>@E PBDJBKQ >P > h+L@E,FKB.i %>@E )I=B*CH? L?GB@Q E>P > PQ>OQ MLFKQ (h>i) >KA >K BKA MLFKQ (h?i). 4EBPB MLFKQP >OB .3?=NIL L?GB@QP, >KA QEB IFKB FP AO>TK TFQE 0OL@BPPFKDgP FCH?() CRK@QFLK.
=F;MM )I=B*CH? U .3?=NIL MN;LN; .3?=NIL ?H>; )I=B*CH?(.3?=NIL ;, .3?=NIL <) U MN;LN = ;.A?N(); ?H> = <.A?N(); W PIC> >CMJF;S() U MNLIE?(0); FCH?(MN;LN.R, MN;LN.S, ?H>.R, ?H>.S); W W Draw the line from PVector start to end. A line between two points: start and end

.LT QE>Q TB E>SB LRO )I=B*CH? @I>PP, TB @>K DBQ PQ>OQBA LK QEB J>FK MOLDO>J. 7BgII KBBA > A>Q> PQOR@QROB QL HBBM QO>@H LC TE>Q TFII BSBKQR>IIV ?B@LJB J>KV )I=B*CH? L?GB@QP, >KA >K ALL;S*CMN (PBB #E>MQBO 4 CLO > OBSFBT LC ALL;S*CMN P) TFII AL GRPQ CFKB.
ALL;S*CMN<)I=B*CH?> FCH?M;

)K M?NOJ() , TBgII T>KQ QL @OB>QB QEB ALL;S*CMN >KA >AA QEB CFOPQ IFKB PBDJBKQ QL FQ, > IFKB QE>Q PQOBQ@EBP COLJ 0 QL QEB TFAQE LC QEB PHBQ@E.
PIC> M?NOJ() U MCT?(600, 300); FCH?M = H?Q ALL;S*CMN<)I=B*CH?>(); Create the ArrayList.

.3?=NIL MN;LN = H?Q .3?=NIL(0, 200); .3?=NIL ?H> = H?Q .3?=NIL(QC>NB, 200);

Left side of window Right side of window

368

4EB .>QROB LC #LAB (S1.0)

FCH?M.;>>(H?Q )I=B*CH?(MN;LN, ?H>)); W

The first KochLine object

4EBK FK >L;Q() , >II )I=B*CH? L?GB@QP (GRPQ LKB OFDEQ KLT) @>K ?B AFPMI>VBA FK > ILLM.
PIC> >L;Q() U <;=EALIOH>(255); @IL ()I=B*CH? F : FCH?M) U F.>CMJF;S(); W W

4EFP FP LRO CLRKA>QFLK. ,BQgP OBSFBT TE>Q TB E>SB PL C>O: j :.3!490 .7,>>: ! @I>PP QL HBBM QO>@H LC > IFKB COLJ MLFKQ ! QL ".

j A==,D!4>?: ! IFPQ LC >II )I=B*CH? L?GB@QP. 7FQE QEB >?LSB BIBJBKQP, ELT >KA TEBOB AL TB >MMIV +L@E ORIBP >KA MOFK@FMIBP LC OB@ROPFLK? 2BJBJ?BO QEB '>JB LC ,FCB @BIIRI>O >RQLJ>Q>? )K QE>Q PFJRI>QFLK, TB >IT>VP HBMQ QO>@H LC QTL DBKBO>QFLKP: @ROOBKQ >KA KBUQ. 7EBK TB TBOB CFKFPEBA @LJMRQFKD QEB KBUQ DBKBO>QFLK, KBUQ ?B@>JB @ROOBKQ >KA TB JLSBA LK QL @LJMRQFKD QEB KBT KBUQ DBKBO>QFLK. 7B >OB DLFKD QL >MMIV > PFJFI>O QB@EKFNRB EBOB. 7B E>SB >K ALL;S*CMN QE>Q HBBMP QO>@H LC QEB @ROOBKQ PBQ LC )I=B*CH? L?GB@QP (>Q QEB PQ>OQ LC QEB MOLDO>J, QEBOB FP LKIV LKB). 7B TFII KBBA > PB@LKA ALL;S*CMN (IBQgP @>II FQ hKBUQi) TEBOB TB TFII MI>@B >II QEB KBT )I=B*CH? L?GB@QP QE>Q >OB DBKBO>QBA COLJ >MMIVFKD QEB +L@E ORIBP. &LO BSBOV )I=B*CH? L?GB@Q FK QEB @ROOBKQ ALL;S*CMN , CLRO KBT )I=B*CH? L?GB@QP >OB >AABA QL QEB KBUQ ALL;S*CMN . 7EBK TBgOB ALKB, QEB KBUQ ALL;S*CMN ?B@LJBP QEB @ROOBKQ LKB.

F64B?2 8.15

369

#E>MQBO 8. &O>@Q>IP

(BOBgP ELT QEB @LAB TFII ILLH:


PIC> A?H?L;N?() U ALL;S*CMN H?RN = H?Q ALL;S*CMN<)I=B*CH?>(); Create the next ArrayList...

@IL ()I=B*CH? F : FCH?M) U

...for every current line.

H?RN.;>>(H?Q H?RN.;>>(H?Q H?RN.;>>(H?Q H?RN.;>>(H?Q W FCH?M = H?RN; W

)I=B*CH?(???,???)); )I=B*CH?(???,???)); )I=B*CH?(???,???)); )I=B*CH?(???,???));

Add four new lines. (We need to figure out how to compute the locations of these lines!)

The new ArrayList is now the one we care about!

"V @>IIFKD A?H?L;N?() LSBO >KA LSBO >D>FK (CLO BU>JMIB, B>@E QFJB QEB JLRPB FP MOBPPBA), TB OB@ROPFSBIV >MMIV QEB +L@E @ROSB ORIBP QL QEB BUFPQFKD PBQ LC )I=B*CH? L?GB@QP. /C @LROPB, QEB >?LSB LJFQP QEB OB>I hTLOHi EBOB, TEF@E FP CFDROFKD LRQ QELPB ORIBP. (LT AL TB ?OB>H LKB IFKB PBDJBKQ FKQL CLRO >P ABP@OF?BA ?V QEB ORIBP? 7EFIB QEFP @>K ?B >@@LJMIFPEBA TFQE PLJB PFJMIB >OFQEJBQF@ >KA QOFDLKLJBQOV, PFK@B LRO )I=B*CH? L?GB@Q RPBP .3?=NIL , QEFP FP > KF@B LMMLOQRKFQV CLO RP QL MO>@QF@B LRO SB@QLO J>QE. ,BQgP BPQ>?IFPE ELT J>KV MLFKQP TB KBBA QL @LJMRQB CLO B>@E )I=B*CH? L?GB@Q.

F64B?2 8.16

!P VLR @>K PBB COLJ QEB >?LSB CFDROB, TB KBBA CFSB MLFKQP (>, ?, @, A, >KA B) QL DBKBO>QB QEB KBT )I=B*CH? L?GB@QP >KA J>HB QEB KBT IFKB PBDJBKQP (>?, @?, @A, >KA AB).
H?RN.;>>(H?Q H?RN.;>>(H?Q H?RN.;>>(H?Q H?RN.;>>(H?Q )I=B*CH?(;,<)); )I=B*CH?(<,=)); )I=B*CH?(=,>)); )I=B*CH?(>,?));

7EBOB AL TB DBQ QEBPB MLFKQP? 3FK@B TB E>SB > )I=B*CH? L?GB@Q, TEV KLQ >PH QEB )I=B*CH? L?GB@Q QL @LJMRQB >II QEBPB MLFKQP CLO RP?
PIC> A?H?L;N?() U ALL;S*CMN H?RN = H?Q ALL;S*CMN<)I=B*CH?>(); @IL ()I=B*CH? F : FCH?M) U

370

4EB .>QROB LC #LAB (S1.0)

.3?=NIL .3?=NIL .3?=NIL .3?=NIL .3?=NIL

; < = > ?

= = = = =

F.EI=BA(); F.EI=B (); F.EI=B!(); F.EI=B"(); F.EI=B#(); )I=B*CH?(;, )I=B*CH?(<, )I=B*CH?(=, )I=B*CH?(>, <)); =)); >)); ?));

The KochLine object has five functions, each of which return a PVector according to the Koch rules.

H?RN.;>>(H?Q H?RN.;>>(H?Q H?RN.;>>(H?Q H?RN.;>>(H?Q W FCH?M = H?RN; W

.LT TB GRPQ KBBA QL TOFQB CFSB KBT CRK@QFLKP FK QEB )I=B*CH? @I>PP, B>@E LKB OBQROKFKD > .3?=NIL >@@LOAFKD QL &FDROB 8.16 (PBB M>DB 370) >?LSB. ,BQgP HKL@H LCC EI=BA() >KA EI=B#() CFOPQ, TEF@E >OB PFJMIV QEB PQ>OQ >KA BKA MLFKQP LC QEB LOFDFK>I IFKB.
.3?=NIL EI=BA() U L?NOLH MN;LN.A?N(); W .3?=NIL EI=B#() U L?NOLH ?H>.A?N(); W Note the use of get(), which returns a copy of the PVector. As was noted in Chapter 6, section 14, we want to avoid making copies whenever possible, but here we will need a new PVector in case we want the segments to move independently of each other.

.LT IBQgP JLSB LK QL MLFKQP " >KA $. " FP LKB-QEFOA LC QEB T>V >ILKD QEB IFKB PBDJBKQ >KA $ FP QTL-QEFOAP. (BOB TB @>K J>HB > .3?=NIL QE>Q MLFKQP COLJ PQ>OQ QL BKA >KA PEOFKH FQ QL LKB-QEFOA QEB IBKDQE CLO " >KA QTL-QEFOAP QEB IBKDQE CLO $ QL CFKA QEBPB MLFKQP.

F64B?2 8.17
.3?=NIL EI=B () U .3?=NIL P = .3?=NIL.MO<(?H>, MN;LN); P.>CP(3); PVector from start to end One-third the length

371

#E>MQBO 8. &O>@Q>IP

P.;>>(MN;LN); L?NOLH P; W .3?=NIL EI=B"() U .3?=NIL P = .3?=NIL.MO<(?H>, MN;LN); P.GOFN(2/3.0); P.;>>(MN;LN); L?NOLH P; W

Add that PVector to the beginning of the line to find the new point.

Same thing here, only we need to move two-thirds along the line instead of onethird.

4EB I>PQ MLFKQ, #, FP QEB JLPQ AFCCF@RIQ LKB QL CFKA. (LTBSBO, FC VLR OB@>II QE>Q QEB >KDIBP LC >K BNRFI>QBO>I QOF>KDIB >OB >II PFUQV ABDOBBP, QEFP J>HBP FQ > IFQQIB ?FQ B>PFBO. )C TB HKLT ELT QL CFKA MLFKQ " TFQE > .3?=NIL LKB-QEFOA QEB IBKDQE LC QEB IFKB, TE>Q FC TB TBOB QL OLQ>QB QE>Q P>JB .3?=NIL PFUQV ABDOBBP >KA JLSB >ILKD QE>Q SB@QLO COLJ MLFKQ "? 7BgA ?B >Q MLFKQ #!

F64B?2 8.18
.3?=NIL EI=B!() U .3?=NIL ; = MN;LN.A?N(); .3?=NIL P = .3?=NIL.MO<(?H>, MN;LN); P.>CP(3); ;.;>>(P); P.LIN;N?(-L;>C;HM(60)); ;.;>>(P); L?NOLH ;; W Rotate above the line 60 degrees. Move along that vector to point C. Move 1/3rd of the way to point B. Start at the beginning.

0RQQFKD FQ >II QLDBQEBO, FC TB @>II A?H?L;N?() CFSB QFJBP FK M?NOJ() , TBgII PBB QEB CLIILTFKD OBPRIQ.

372

4EB .>QROB LC #LAB (S1.0)

Example 8.5: Koch curve


ALL;S*CMN<)I=B*CH?> FCH?M; PIC> M?NOJ() U MCT?(600, 300); <;=EALIOH>(255); FCH?M = H?Q ALL;S*CMN<)I=B*CH?>(); .3?=NIL MN;LN = H?Q .3?=NIL(0, 200); .3?=NIL ?H> = H?Q .3?=NIL(QC>NB, 200); FCH?M.;>>(H?Q )I=B*CH?(MN;LN, ?H>)); @IL (CHN C = 0; C < 5; C++) U A?H?L;N?(); W W Arbitrarily apply the Koch rules five times.

Exercise 8.2
$O>T QEB +L@E PKLTCI>HB (LO PLJB LQEBO S>OF>QFLK LC QEB +L@E @ROSB).

373

#E>MQBO 8. &O>@Q>IP

Exercise 8.3
4OV >KFJ>QFKD QEB +L@E @ROSB. &LO BU>JMIB, @>K VLR AO>T FQ COLJ IBCQ QL OFDEQ? #>K VLR S>OV QEB SFPR>I ABPFDK LC QEB IFKB PBDJBKQP? #>K VLR JLSB QEB IFKB PBDJBKQP RPFKD QB@EKFNRBP COLJ B>OIFBO @E>MQBOP? 7E>Q FC B>@E IFKB PBDJBKQ TBOB J>AB FKQL > PMOFKD (QLUF@IF?P) LO GLFKQ ("LU2$)?

Exercise 8.4
2BTOFQB QEB #>KQLO PBQ BU>JMIB RPFKD L?GB@QP >KA >K ALL;S*CMN .

Exercise 8.5
$O>T QEB 3FBOMF]PHF QOF>KDIB (>P PBBK FK 7LICO>J BIBJBKQ>OV #!) RPFKD OB@ROPFLK.

8.5 Trees
4EB CO>@Q>IP TB E>SB BU>JFKBA FK QEFP @E>MQBO PL C>O >OB ABQBOJFKFPQF@, JB>KFKD QEBV E>SB KL O>KALJKBPP >KA TFII >IT>VP MOLAR@B QEB FABKQF@>I LRQ@LJB B>@E QFJB QEBV >OB ORK. 4EBV >OB BU@BIIBKQ ABJLKPQO>QFLKP LC @I>PPF@ CO>@Q>IP >KA QEB MOLDO>JJFKD QB@EKFNRBP ?BEFKA AO>TFKD QEBJ, ?RQ >OB QLL MOB@FPB QL CBBI B5HIF5@ . )K QEFP KBUQ M>OQ LC QEB @E>MQBO, ) T>KQ QL BU>JFKB PLJB QB@EKFNRBP ?BEFKA DBKBO>QFKD > PQL@E>PQF@ (LO KLK-ABQBOJFKFPQF@) CO>@Q>I. 4EB BU>JMIB TBgII RPB FP > ?O>K@EFKD QOBB. ,BQgP CFOPQ T>IH QEOLRDE QEB PQBMP QL @OB>QB > ABQBOJFKFPQF@ SBOPFLK. (BOB >OB LRO MOLAR@QFLK ORIBP:

374

4EB .>QROB LC #LAB (S1.0)

F64B?2 8.19

!D>FK, TB E>SB > KF@B CO>@Q>I TFQE > OB@ROPFSB ABCFKFQFLK: ! ?O>K@E FP > IFKB TFQE QTL ?O>K@EBP @LKKB@QBA QL FQ.

4EB M>OQ QE>Q FP > ?FQ JLOB AFCCF@RIQ QE>K LRO MOBSFLRP CO>@Q>IP IFBP FK QEB RPB LC QEB TLOA FCH5H9 FK QEB CO>@Q>IgP ORIBP. %>@E KBT ?O>K@E JRPQ OLQ>QB OBI>QFSB QL QEB MOBSFLRP ?O>K@E, TEF@E FP OLQ>QBA OBI>QFSB QL >II FQP MOBSFLRP ?O>K@EBP. ,R@HFIV CLO RP, 0OL@BPPFKD E>P > JB@E>KFPJ QL HBBM QO>@H LC OLQ>QFLKP CLO RPeQEB ?=,9>1:=8,?4:9 8,?=4C. )C VLR >OBKgQ C>JFIF>O TFQE QEB CRK@QFLKP JOMB+;NLCR() >KA JIJ+;NLCR() , ) PRDDBPQ VLR OB>A QEB LKIFKB 0OL@BPPFKD QRQLOF>I 2$ 4O>KPCLOJ>QFLKP (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/QO>KPCLOJ2A/), TEF@E TFII @LSBO QEB @LK@BMQP VLRgII KBBA CLO QEFP M>OQF@RI>O BU>JMIB. ,BQgP ?BDFK ?V AO>TFKD > PFKDIB ?O>K@E, QEB QORKH LC QEB QOBB. 3FK@B TB >OB DLFKD QL FKSLISB QEB LIN;N?() CRK@QFLK, TBgII KBBA QL J>HB PROB TB >OB @LKQFKRLRPIV QO>KPI>QFKD >ILKD QEB ?O>K@EBP TEFIB TB AO>T QEB QOBB. !KA PFK@B QEB OLLQ PQ>OQP >Q QEB ?LQQLJ LC QEB TFKALT (PBB >?LSB), QEB CFOPQ PQBM OBNRFOBP QO>KPI>QFKD QL QE>Q PMLQ:
NL;HMF;N?(QC>NB/2,B?CABN);

375

#E>MQBO 8. &O>@Q>IP

kCLIILTBA ?V AO>TFKD > IFKB RMT>OAP (&FDROB 8.20):

F64B?2 8.20

FCH?(0,0,0,-100);

/K@B TBgSB CFKFPEBA QEB OLLQ, TB GRPQ KBBA QL QO>KPI>QB QL QEB BKA >KA OLQ>QB FK LOABO QL AO>T QEB KBUQ ?O>K@E. (%SBKQR>IIV, TBgOB DLFKD QL KBBA QL M>@H>DB RM TE>Q TBgOB ALFKD OFDEQ KLT FKQL > OB@ROPFSB CRK@QFLK, ?RQ IBQgP PLOQ LRQ QEB PQBMP CFOPQ.)

F64B?2 8.21

2BJBJ?BO, TEBK TB OLQ>QB FK 0OL@BPPFKD, TB >OB >IT>VP OLQ>QFKD >OLRKA QEB MLFKQ LC LOFDFK, PL EBOB QEB MLFKQ LC LOFDFK JRPQ >IT>VP ?B QO>KPI>QBA QL QEB BKA LC LRO @ROOBKQ ?O>K@E.
NL;HMF;N?(0,-100); LIN;N?(.'/6); FCH?(0,0,0,-100);

.LT QE>Q TB E>SB > ?O>K@E DLFKD QL QEB OFDEQ, TB KBBA LKB DLFKD QL QEB IBCQ. 7B @>K RPB JOMB+;NLCR() QL P>SB QEB QO>KPCLOJ>QFLK PQ>QB ?BCLOB TB OLQ>QB, IBQQFKD RP @>II JIJ+;NLCR() QL OBPQLOB QE>Q PQ>QB >KA AO>T QEB ?O>K@E QL QEB IBCQ. ,BQgP ILLH >Q >II QEB @LAB QLDBQEBO.

376

4EB .>QROB LC #LAB (S1.0)

F64B?2 8.22

F64B?2 8.23

NL;HMF;N?(QC>NB/2,B?CABN); FCH?(0,0,0,-100); NL;HMF;N?(0,-100); JOMB+;NLCR(); LIN;N?(.'/6); FCH?(0,0,0,-100); JIJ+;NLCR(); LIN;N?(-.'/6); FCH?(0,0,0,-100); Branch to the left Branch to the right The root

)C VLR QEFKH LC B>@E @>II QL QEB CRK@QFLK FCH?() >P > h?O>K@E,i VLR @>K PBB COLJ QEB @LAB >?LSB QE>Q TB E>SB FJMIBJBKQBA LRO ABCFKFQFLK LC ?O>K@EFKD >P > IFKB QE>Q E>P QTL IFKBP @LKKB@QBA QL FQP BKA. 7B @LRIA HBBM >AAFKD JLOB >KA JLOB @>IIP QL FCH?() CLO JLOB >KA JLOB ?O>K@EBP, ?RQ GRPQ >P TFQE QEB #>KQLO PBQ >KA +L@E @ROSB, LRO @LAB TLRIA ?B@LJB FK@OBAF?IV @LJMIF@>QBA >KA RKTFBIAV. )KPQB>A, TB @>K RPB QEB >?LSB ILDF@ >P LRO CLRKA>QFLK CLO TOFQFKD > OB@ROPFSB CRK@QFLK, OBMI>@FKD QEB AFOB@Q @>IIP QL FCH?() TFQE LRO LTK CRK@QFLK @>IIBA <L;H=B() . ,BQgP Q>HB > ILLH. Example 8.6: Recursive tree
PIC> <L;H=B() U FCH?(0, 0, 0, -100); NL;HMF;N?(0, -100); JOMB+;NLCR(); Draw the branch itself. Translate to the end.

377

#E>MQBO 8. &O>@Q>IP

LIN;N?(.'/6); <L;H=B(); JIJ+;NLCR(); JOMB+;NLCR(); LIN;N?(-.'/6); <L;H=B(); JIJ+;NLCR(); W

Rotate to the right and branch again.

Rotate to the left and branch again.

.LQF@B ELT FK QEB >?LSB @LAB TB RPB JOMB+;NLCR() >KA JIJ+;NLCR() >OLRKA B>@E PR?PBNRBKQ @>II QL <L;H=B() . 4EFP FP LKB LC QELPB BIBD>KQ @LAB PLIRQFLKP QE>Q CBBIP >IJLPQ IFHB J>DF@. %>@E @>II QL <L;H=B() Q>HBP > JLJBKQ QL OBJBJ?BO QEB IL@>QFLK LC QE>Q M>OQF@RI>O ?O>K@E. )C VLR QROK VLROPBIC FKQL 0OL@BPPFKD CLO > JLJBKQ >KA QOV QL CLIILT QEB OB@ROPFSB CRK@QFLK TFQE MBK@FI >KA M>MBO, VLRgII KLQF@B QE>Q FQ AO>TP >II LC QEB ?O>K@EBP QL QEB OFDEQ CFOPQ. 7EBK FQ DBQP QL QEB BKA, JIJ+;NLCR() TFII MLM RP ?>@H >ILKD >II LC QEB ?O>K@EBP TBgSB AO>TK >KA PQ>OQ PBKAFKD ?O>K@EBP LRQ QL QEB IBCQ.

Exercise 8.6

%JRI>QB QEB 0OL@BPPFKD @LAB FK %U>JMIB 8.6 (PBB M>DB 377) >KA KRJ?BO QEB ?O>K@EBP FK QEB >?LSB AF>DO>J FK QEB LOABO QE>Q 0OL@BPPFKD TLRIA >@QR>IIV AO>T B>@E LKB. 9LR J>V E>SB KLQF@BA QE>Q QEB OB@ROPFSB CRK@QFLK TB GRPQ TOLQB TLRIA KLQ >@QR>IIV AO>T QEB >?LSB QOBB. !CQBO >II, FQ E>P KL BUFQ @LKAFQFLK >KA TLRIA DBQ PQR@H FK FKCFKFQB OB@ROPFSB @>IIP QL FQPBIC. 9LRgII >IPL MOL?>?IV KLQF@B QE>Q QEB ?O>K@EBP LC QEB QOBB DBQ PELOQBO >Q B>@E IBSBI. ,BQgP ILLH >Q ELT TB @>K PEOFKH QEB IBKDQE LC QEB IFKBP >P QEB QOBB FP AO>TK, >KA PQLM ?O>K@EFKD LK@B QEB IFKBP E>SB ?B@LJB QLL PELOQ.

378

4EB .>QROB LC #LAB (S1.0)

PIC> <L;H=B(@FI;N F?H) U FCH?(0, 0, 0, -F?H); NL;HMF;N?(0, -F?H); F?H *= 0.66; C@ (F?H > 2) U JOMB+;NLCR(); LIN;N?(NB?N;); <L;H=B(F?H); JIJ+;NLCR(); JOMB+;NLCR(); LIN;N?(-NB?N;); <L;H=B(F?H); JIJ+;NLCR(); W W

Each branch now receives its length as an argument.

Each branchs length shrinks by two-thirds.

Subsequent calls to branch() include the length argument.

7BgSB >IPL FK@IRABA > S>OF>?IB CLO QEBQ> QE>Q >IILTP RP, TEBK TOFQFKD QEB OBPQ LC QEB @LAB FK M?NOJ() >KA >L;Q() , QL S>OV QEB ?O>K@EFKD >KDIB >@@LOAFKD QL, P>V, QEB GIOM?5 IL@>QFLK.

Example 8.7: Recursive tree


@FI;N NB?N;; PIC> M?NOJ() U MCT?(300, 200); W PIC> >L;Q() U <;=EALIOH>(255);

379

#E>MQBO 8. &O>@Q>IP

NB?N; = G;J(GIOM?5,0,QC>NB,0,.'/2);

Pick an angle according to the mouse location. The first branch starts at the bottom of the window.

NL;HMF;N?(QC>NB/2, B?CABN); MNLIE?(0); <L;H=B(60); W

Exercise 8.7
6>OV QEB MNLIE?4?CABN() CLO B>@E ?O>K@E. ->HB QEB OLLQ QEF@H >KA B>@E PR?PBNRBKQ ?O>K@E QEFKKBO.

Exercise 8.8
4EB QOBB PQOR@QROB @>K >IPL ?B DBKBO>QBA RPFKD QEB ALL;S*CMN QB@EKFNRB ABJLKPQO>QBA TFQE QEB +L@E @ROSB. 2B@OB>QB QEB QOBB RPFKD > L;H=B L?GB@Q >KA >K ALL;S*CMN QL HBBM QO>@H LC QEB ?O>K@EBP. (FKQ: VLRgII T>KQ QL HBBM QO>@H LC QEB ?O>K@E AFOB@QFLKP >KA IBKDQEP RPFKD SB@QLO J>QE FKPQB>A LC 0OL@BPPFKD QO>KPCLOJ>QFLKP.

Exercise 8.9
/K@B VLR E>SB QEB QOBB ?RFIQ TFQE >K ALL;S*CMN LC L;H=B L?GB@QP, >KFJ>QB QEB QOBBgP DOLTQE. #>K VLR AO>T IB>SBP >Q QEB BKA LC QEB ?O>K@EBP? 4EB OB@ROPFSB QOBB CO>@Q>I FP > KF@B BU>JMIB LC > P@BK>OFL FK TEF@E >AAFKD > IFQQIB ?FQ LC O>KALJKBPP @>K J>HB QEB QOBB ILLH JLOB K>QRO>I. 4>HB > ILLH LRQPFAB >KA VLRgII KLQF@B QE>Q ?O>K@E IBKDQEP >KA >KDIBP S>OV COLJ ?O>K@E QL ?O>K@E, KLQ QL JBKQFLK QEB C>@Q QE>Q ?O>K@EBP ALKgQ >II E>SB BU>@QIV QEB P>JB KRJ?BO LC PJ>IIBO ?O>K@EBP. &FOPQ, IBQgP PBB TE>Q E>MMBKP

380

4EB .>QROB LC #LAB (S1.0)

TEBK TB PFJMIV S>OV QEB >KDIB >KA IBKDQE. 4EFP FP > MOBQQV B>PV LKB, DFSBK QE>Q TB @>K GRPQ >PH 0OL@BPPFKD CLO > O>KALJ KRJ?BO B>@E QFJB TB AO>T QEB QOBB.

PIC> <L;H=B(@FI;N F?H) U @FI;N NB?N; = L;H>IG(0,.'/3); FCH?(0, 0, 0, -F?H); NL;HMF;N?(0, -F?H); F?H *= 0.66; C@ (F?H > 2) U JOMB+;NLCR(); LIN;N?(NB?N;); <L;H=B(F?H); JIJ+;NLCR(); JOMB+;NLCR(); LIN;N?(-NB?N;); <L;H=B(F?H); JIJ+;NLCR(); W W Start by picking a random angle for each branch.

)K QEB >?LSB CRK@QFLK, TB >IT>VP @>II <L;H=B() QTF@B. "RQ TEV KLQ MF@H > O>KALJ KRJ?BO LC ?O>K@EBP >KA @>II <L;H=B() QE>Q KRJ?BO LC QFJBP?

381

#E>MQBO 8. &O>@Q>IP

Example 8.8: Stochastic tree


PIC> <L;H=B(@FI;N F?H) U FCH?(0, 0, 0, -F?H); NL;HMF;N?(0, -F?H); C@ (F?H > 2) U CHN H = CHN(L;H>IG(1,4)); @IL (CHN C = 0; C < H; C++) U @FI;N NB?N; = L;H>IG(-.'/2, .'/2); JOMB+;NLCR(); LIN;N?(NB?N;); <L;H=B(B); JIJ+;NLCR(); W W Each branch gets its own random angle. Call branch() a random number of times.

Exercise 8.10
3BQ QEB >KDIBP LC QEB ?O>K@EBP LC QEB QOBB >@@LOAFKD QL 0BOIFK KLFPB S>IRBP. !AGRPQ QEB KLFPB S>IRBP LSBO QFJB QL >KFJ>QB QEB QOBB. 3BB FC VLR @>K DBQ FQ QL >MMB>O >P FC FQ FP ?ILTFKD FK QEB TFKA.

Exercise 8.11
5PB QLUF@IF?P QL PFJRI>QB QOBB MEVPF@P. %>@E ?O>K@E LC QEB QOBB PELRIA ?B QTL M>OQF@IBP @LKKB@QBA TFQE > PMOFKD. (LT @>K VLR DBQ QEB QOBB QL PQ>KA RM >KA KLQ C>II ALTK?

8.6 L-systems
)K 1968, (RKD>OF>K ?LQ>KFPQ !OFPQFA ,FKABKJ>VBO ABSBILMBA > DO>JJ>O-?>PBA PVPQBJ QL JLABI QEB DOLTQE M>QQBOKP LC MI>KQP. ,-PVPQBJP (PELOQ CLO ,FKABKJ>VBO PVPQBJP) @>K ?B RPBA QL DBKBO>QB >II LC QEB OB@ROPFSB CO>@Q>I M>QQBOKP TBgSB PBBK PL C>O FK QEFP @E>MQBO. 7B ALKgQ KBBA ,-PVPQBJP QL AL QEB HFKA LC TLOH TBgOB ALFKD EBOB; ELTBSBO, QEBV >OB FK@OBAF?IV RPBCRI ?B@>RPB QEBV MOLSFAB > JB@E>KFPJ CLO HBBMFKD QO>@H LC CO>@Q>I PQOR@QROBP QE>Q OBNRFOB @LJMIBU >KA JRIQF-C>@BQBA MOLAR@QFLK ORIBP.

382

4EB .>QROB LC #LAB (S1.0)

)K LOABO QL @OB>QB >K BU>JMIB QE>Q FJMIBJBKQP ,-PVPQBJP FK 0OL@BPPFKD, TB >OB DLFKD QL E>SB QL ?B @LJCLOQ>?IB TFQE TLOHFKD TFQE (>) OB@ROPFLK, (?) QO>KPCLOJ>QFLK J>QOF@BP, >KA (@) PQOFKDP. 3L C>O TBgSB TLOHBA TFQE OB@ROPFLK >KA QO>KPCLOJ>QFLKP, ?RQ PQOFKDP >OB KBT EBOB. 7B TFII >PPRJB QEB ?>PF@P, ?RQ FC QE>Q FP KLQ @LJCLOQ>?IB CLO VLR, ) TLRIA PRDDBPQ Q>HFKD > ILLH >Q QEB 0OL@BPPFKD QRQLOF>I 3QOFKDP >KA $O>TFKD 4BUQ (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/QBUQ/). !K ,-PVPQBJ FKSLISBP QEOBB J>FK @LJMLKBKQP: j A7;3,-0?. !K ,-PVPQBJgP >IME>?BQ FP @LJMOFPBA LC QEB S>IFA @E>O>@QBOP QE>Q @>K ?B FK@IRABA. &LO BU>JMIB, TB @LRIA P>V QEB >IME>?BQ FP h!"#,i JB>KFKD QE>Q >KV S>IFA hPBKQBK@Bi (> PQOFKD LC @E>O>@QBOP) FK >K ,-PVPQBJ @>K LKIV FK@IRAB QEBPB QEOBB @E>O>@QBOP. j AC4:8. 4EB >UFLJ FP > PBKQBK@B (J>AB RM TFQE @E>O>@QBOP COLJ QEB >IME>?BQ) QE>Q ABP@OF?BP QEB FKFQF>I PQ>QB LC QEB PVPQBJ. &LO BU>JMIB, TFQE QEB >IME>?BQ h!"#,i PLJB BU>JMIB >UFLJP >OB h!!!i LO h"i LO h!#"!".i j &@70>. 4EB ORIBP LC >K ,-PVPQBJ >OB >MMIFBA QL QEB >UFLJ >KA QEBK >MMIFBA OB@ROPFSBIV, DBKBO>QFKD KBT PBKQBK@BP LSBO >KA LSBO >D>FK. !K ,-PVPQBJ ORIB FK@IRABP QTL PBKQBK@BP, > hMOBAB@BPPLOi >KA > hPR@@BPPLO.i &LO BU>JMIB, TFQE QEB 2RIB h! e> !"i, TEBKBSBO >K h!i FP CLRKA FK > PQOFKD, FQ FP OBMI>@BA TFQE h!".i ,BQgP ?BDFK TFQE > SBOV PFJMIB ,-PVPQBJ. (4EFP FP, FK C>@Q, ,FKABKJ>VBOgP LOFDFK>I ,-PVPQBJ CLO JLABIFKD QEB DOLTQE LC >ID>B.)
ABF>78;J: A B AN?EC: A ,KB;I: (A X AB) (B X A)

!P TFQE LRO OB@ROPFSB CO>@Q>I PE>MBP, TB @>K @LKPFABO B>@E PR@@BPPFSB >MMIF@>QFLK LC QEB ,-PVPQBJ ORIBP QL ?B > DBKBO>QFLK. 'BKBO>QFLK 0 FP, ?V ABCFKFQFLK, QEB >UFLJ. ,BQgP ILLH >Q ELT TB JFDEQ @OB>QB QEBPB DBKBO>QFLKP TFQE @LAB. 7BgII PQ>OQ ?V RPFKD > 0NLCHA L?GB@Q QL PQLOB QEB >UFLJ.
F64B?2 8.24: A;1 @< <; .;1 @< 3<?A5...

0NLCHA =OLL?HN = "A";

!KA LK@B >D>FK, GRPQ >P TB AFA TFQE QEB '>JB LC ,FCB >KA QEB +L@E @ROSB ALL;S*CMN BU>JMIBP, TB TFII KBBA >K BKQFOBIV PBM>O>QB PQOFKD QL HBBM QO>@H LC QEB hKBUQi DBKBO>QFLK.

383

#E>MQBO 8. &O>@Q>IP
0NLCHA H?RN = "";

.LT FQgP QFJB QL >MMIV QEB ORIBP QL QEB @ROOBKQ DBKBO>QFLK >KA MI>@B QEB OBPRIQP FK QEB KBUQ.
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U =B;L = = =OLL?HN.=B;LAN(C); C@ (= == 'A') U H?RN += "A "; W ?FM? C@ (= == ' ') U H?RN += "A"; W W Production rule B --> A Production rule A --> AB

!KA TEBK TBgOB ALKB, @ROOBKQ @>K ?B@LJB KBUQ.


=OLL?HN = H?RN;

4L ?B PROB QEFP FP TLOHFKD, IBQgP M>@H>DB FQ FKQL > CRK@QFLK >KA >KA @>II FQ BSBOV QFJB QEB JLRPB FP MOBPPBA.

Example 8.9: Simple L-system sentence generation


0NLCHA =OLL?HN = "A"; CHN =IOHN = 0; PIC> M?NOJ() U JLCHNFH("%?H?L;NCIH " + =IOHN + ": " + =OLL?HN); W PIC> >L;Q() U W PIC> GIOM?.L?MM?>() U 0NLCHA H?RN = ""; Start with an axiom. Lets keep track of how many generations.

384

4EB .>QROB LC #LAB (S1.0)

@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U =B;L = = =OLL?HN.=B;LAN(C); C@ (= == 'A') U H?RN += "A "; W ?FM? C@ (= == ' ') U H?RN += "A"; W W =OLL?HN = H?RN; =IOHN++; JLCHNFH("%?H?L;NCIH " + =IOHN + ": " + =OLL?HN); W

Traverse the current String and make the new one.

3FK@B QEB ORIBP >OB >MMIFBA OB@ROPFSBIV QL B>@E DBKBO>QFLK, QEB IBKDQE LC QEB PQOFKD DOLTP BUMLKBKQF>IIV. "V DBKBO>QFLK #11, QEB PBKQBK@B FP 233 @E>O>@QBOP ILKD; ?V DBKBO>QFLK #22, FQ FP LSBO 46,000 @E>O>@QBOP ILKD. 4EB *>S> 0NLCHA @I>PP, TEFIB @LKSBKFBKQ QL RPB, FP > DOLPPIV FKBCCF@FBKQ A>Q> PQOR@QROB CLO @LK@>QBK>QFKD I>ODB PQOFKDP. ! 0NLCHA L?GB@Q FP hFJJRQ>?IB,i TEF@E JB>KP LK@B QEB L?GB@Q FP @OB>QBA FQ @>K KBSBO ?B @E>KDBA. 7EBKBSBO VLR >AA LK QL QEB BKA LC > 0NLCHA L?GB@Q, *>S> E>P QL J>HB > ?O>KA KBT 0NLCHA L?GB@Q (BSBK FC VLR >OB RPFKD QEB P>JB S>OF>?IB K>JB).
0NLCHA M = "<F;B"; M += ";>> MIG? GIL? MNO@@";

)K JLPQ @>PBP, QEFP FP CFKB, ?RQ TEV ARMIF@>QB > 46,000-@E>O>@QBO PQOFKD FC VLR ALKgQ E>SB QL? &LO ?BQQBO BCCF@FBK@V FK LRO ,-PVPQBJ BU>JMIBP, TBgII RPB QEB 0NLCHA O@@?L @I>PP, TEF@E FP LMQFJFWBA CLO QEFP QVMB LC Q>PH >KA @>K B>PFIV ?B @LKSBOQBA FKQL > PQOFKD >CQBO @LK@>QBK>QFLK FP @LJMIBQB.
0NLCHA O@@?L H?RN = H?Q 0NLCHA O@@?L(); @IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U =B;L = = =OLL?HN.=B;LAN(C); C@ (= == 'A') U H?RN.;JJ?H>("A "); W ?FM? C@ (= == ' ') U H?RN.;JJ?H>("A"); W W =OLL?HN = H?RN.NI0NLCHA(); StringBuffer can easily be converted back to a String. append() instead of += A StringBuffer for the next sentence

9LR J>V CFKA VLROPBIC TLKABOFKD OFDEQ >?LRQ KLT: TE>Q BU>@QIV FP QEB MLFKQ LC >II QEFP? !CQBO >II, FPKgQ QEFP > @E>MQBO >?LRQ 8F5K=B; CO>@Q>I M>QQBOKP? 9BP, QEB OB@ROPFSB K>QROB LC QEB ,-PVPQBJ PBKQBK@B PQOR@QROB PBBJP OBIBS>KQ QL QEB AFP@RPPFLK, ?RQ ELT BU>@QIV ALBP QEFP JLABI MI>KQ DOLTQE FK > SFPR>I T>V?

385

#E>MQBO 8. &O>@Q>IP

7E>Q TBgSB IBCQ RKP>FA RKQFI KLT FP QE>Q BJ?BAABA FKQL QEBPB ,-PVPQBJ PBKQBK@BP >OB FKPQOR@QFLKP CLO AO>TFKD. ,BQgP PBB ELT QEFP TLOHP TFQE >KLQEBO BU>JMIB.
ABF>78;J: A B AN?EC: A ,KB;I: (A X ABA) (B X BBB)

4L OB>A > PBKQBK@B, TBgII QO>KPI>QB FQ FK QEB CLIILTFKD T>V:


A: H7M 7 B?D; <EHM7H:.

B: (EL; <EHM7H: M?J>EKJ :H7M?D=.

,BQgP ILLH >Q QEB PBKQBK@B LC B>@E DBKBO>QFLK >KA FQP SFPR>I LRQMRQ.
#;D;H7J?ED 0: A #;D;H7J?ED 1: ABA #;D;H7J?ED 2: ABABBBABA #;D;H7J?ED 3: ABABBBABABBBBBBBBBABABBBABA

,LLH C>JFIF>O? 4EFP FP QEB #>KQLO PBQ DBKBO>QBA TFQE >K ,-PVPQBJ.

F64B?2 8.25

4EB CLIILTFKD >IME>?BQ FP LCQBK RPBA TFQE ,-PVPQBJP: h&'+-;<i, JB>KFKD:


": H7M 7 B?D; 7D: CEL; <EHM7H:

#: (EL; <EHM7H: (M?J>EKJ :H7M?D= 7 B?D;) +: .KHD H?=>J -: .KHD B;<J 4: -7L; 9KHH;DJ BE97J?ED 5: ,;IJEH; FH;L?EKI BE97J?ED

4EFP QVMB LC AO>TFKD CO>JBTLOH FP LCQBK OBCBOOBA QL >P h4ROQIB DO>MEF@Pi (COLJ QEB LIA A>VP LC ,/'/ MOLDO>JJFKD). )J>DFKB > QROQIB PFQQFKD LK VLRO @LJMRQBO P@OBBK QL TEF@E VLR @LRIA FPPRB > PJ>II PBQ LC @LJJ>KAP: QROK IBCQ, QROK OFDEQ, AO>T > IFKB, BQ@. 0OL@BPPFKD FPKgQ PBQ RM QL LMBO>QB QEFP T>V ?V ABC>RIQ, ?RQ ?V RPFKD NL;HMF;N?() , LIN;N?() , >KA FCH?() , TB @>K BJRI>QB > 4ROQIB DO>MEF@P BKDFKB C>FOIV B>PFIV.

386

4EB .>QROB LC #LAB (S1.0)

(BOBgP ELT TB TLRIA QO>KPI>QB QEB >?LSB ,-PVPQBJ >IME>?BQ FKQL 0OL@BPPFKD @LAB.
": B?D;(0,0,0,B;D); JH7DIB7J;(0,B;D); #: JH7DIB7J;(0,B;D); +: HEJ7J;(7D=B;); -: HEJ7J;(-7D=B;); 4: FKI>(7JH?N(); 5: FEF(7JH?N();

!PPRJFKD TB E>SB > PBKQBK@B DBKBO>QBA COLJ QEB ,-PVPQBJ, TB @>K T>IH QEOLRDE QEB PBKQBK@B @E>O>@QBO ?V @E>O>@QBO >KA @>II QEB >MMOLMOF>QB CRK@QFLK >P LRQIFKBA >?LSB.
@IL (CHN C = 0; C < M?HN?H=?.F?HANB(); C++) U =B;L = = M?HN?H=?.=B;LAN(C); Looking at each character one at a time

C@ (= == '$') U FCH?(0,0,F?H,0); NL;HMF;N?(F?H,0); W ?FM? C@ (= == '$') NL;HMF;N?(F?H,0); W ?FM? C@ (= == '+') LIN;N?(NB?N;); W ?FM? C@ (= == '-') LIN;N?(-NB?N;); W ?FM? C@ (= == '8') JOMB+;NLCR(); W ?FM? C@ (= == '9') JIJ+;NLCR(); W W

U U U U U

Performing the correct task for each character. This could also be written with a case statement, which might be nicer to look at, but leaving it as an if/else if structure helps readers not familiar with case statements.

4EB KBUQ BU>JMIB TFII AO>T > JLOB BI>?LO>QB PQOR@QROB TFQE QEB CLIILTFKD ,-PVPQBJ.
ABF>78;J: "#+-45 AN?EC: " ,KB;I: " -X ""+4+"-"-"5-4-"+"+"5

4EB BU>JMIB >S>FI>?IB CLO ALTKIL>A LK QEB ?LLHgP TB?PFQB Q>HBP >II LC QEB ,-PVPQBJ @LAB MOLSFABA FK QEFP PB@QFLK >KA LOD>KFWBP FQ FKQL QEOBB @I>PPBP: j ,KB; : ! @I>PP QE>Q PQLOBP QEB MOBAB@BPPLO >KA PR@@BPPLO PQOFKDP CLO >K ,-PVPQBJ ORIB. j '-OIJ;C : ! @I>PP QL FQBO>QB > KBT ,-PVPQBJ DBKBO>QFLK (>P ABJLKPQO>QBA TFQE QEB 0NLCHA O@@?L QB@EKFNRB). j .KHJB; : ! @I>PP QL J>K>DB OB>AFKD QEB ,-PVPQBJ PBKQBK@B >KA CLIILTFKD FQP FKPQOR@QFLKP QL AO>T LK QEB P@OBBK.

387

#E>MQBO 8. &O>@Q>IP

7B TLKgQ TOFQB LRQ QEBPB @I>PPBP EBOB PFK@B QEBV PFJMIV ARMIF@>QB QEB @LAB TBgSB >IOB>AV TLOHBA LRQ FK QEFP @E>MQBO. (LTBSBO, IBQgP PBB ELT QEBV >OB MRQ QLDBQEBO FK QEB J>FK Q>?.

Example 8.10: LSystem


*0SMN?G FMSM; 1OLNF? NOLNF?; PIC> M?NOJ() U MCT?(600,600); /OF?89 LOF?M?N = H?Q /OF?819; LOF?M?N809 = H?Q /OF?('$',"$$+8+$-$-$9-8-$+$+$9"); FMSM = H?Q *0SMN?G("$",LOF?M?N); The L-system is created with an axiom and a ruleset. A ruleset is an array of Rule objects.

NOLNF? = H?Q 1OLNF?(FMSM.A?N0?HN?H=?(),QC>NB/4,L;>C;HM(25)); W PIC> >L;Q() U <;=EALIOH>(255); NL;HMF;N?(QC>NB/2,B?CABN); NOLNF?.L?H>?L(); W PIC> GIOM?.L?MM?>() U FMSM.A?H?L;N?(); NOLNF?.M?N1I"I(FMSM.A?N0?HN?H=?()); NOLNF?.=B;HA?*?H(0.5); W Generate a new sentence when the mouse is pressed. The Turtle graphics renderer is given a sentence, a starting length, and an angle for rotations. Start at the bottom of the window and draw.

The length shrinks each generation.

388

4EB .>QROB LC #LAB (S1.0)

Exercise 8.12
5PB >K ,-PVPQBJ >P > PBQ LC FKPQOR@QFLKP CLO @OB>QFKD L?GB@QP PQLOBA FK >K ALL;S*CMN . 5PB QOFDLKLJBQOV >KA SB@QLO J>QE QL MBOCLOJ QEB OLQ>QFLKP FKPQB>A LC J>QOFU QO>KPCLOJ>QFLKP (JR@E IFHB TB AFA FK QEB +L@E @ROSB BU>JMIB).

Exercise 8.13
4EB PBJFK>I TLOH FK ,-PVPQBJP >KA MI>KQ PQOR@QROBP, /<9 A@;CF=H<A=7 B95IHM C: ,@5BHG ?V 0OWBJVP\>T 0ORPFKHFBTF@W >KA !OFPQFA ,FKABKJ>VBO, T>P MR?IFPEBA FK 1990. )Q FP >S>FI>?IB CLO COBB FK FQP BKQFOBQV LKIFKB (EQQM://>IDLOFQEJF@?LQ>KV.LOD/M>MBOP/#>?LM). #E>MQBO 1 ABP@OF?BP J>KV PLMEFPQF@>QBA ,-PVPQBJP TFQE >AAFQFLK>I AO>TFKD ORIBP >KA >S>FI>?IB >IME>?BQ @E>O>@QBOP. )K >AAFQFLK, FQ ABP@OF?BP PBSBO>I JBQELAP CLO DBKBO>QFKD PQL@E>PQF@ ,-PVPQBJP. %UM>KA QEB ,-PVPQBJ BU>JMIB QL FK@IRAB LKB LO JLOB >AAFQFLK>I CB>QROBP ABP@OF?BA ?V 0ORPFKHFBTF@W >KA ,FKABKJ>VBO.

Exercise 8.14
)K QEFP @E>MQBO, TB BJME>PFWBA RPFKD CO>@Q>I >IDLOFQEJP CLO DBKBO>QFKD SFPR>I M>QQBOKP. (LTBSBO, CO>@Q>IP @>K ?B CLRKA FK LQEBO @OB>QFSB JBAFRJP. &LO BU>JMIB, CO>@Q>I M>QQBOKP >OB BSFABKQ FK *LE>KK 3B?>PQF>K ">@EgP #BIIL 3RFQB KL. 3. 4EB PQOR@QROB LC $>SFA &LPQBO 7>II>@BgP KLSBI %B:=B=H9 &9GH T>P FKPMFOBA ?V CO>@Q>IP. #LKPFABO RPFKD QEB BU>JMIBP FK QEFP @E>MQBO QL DBKBO>QB >RAFL LO QBUQ.

The Ecosystem Project


Step 8 Exercise: Incorporate fractals into your ecosystem. Some possibilities:

Add plant-like creatures to the ecosystem environment. Lets say one of your plants is similar to a tree. Can you add leaves or flowers to the end of the branches? What if the leaves can fall off the tree (depending on a wind force)? What if you add fruit that can be picked and eaten by the creatures? Design a creature with a fractal pattern. Use an L-system to generate instructions for how a creature should move or behave.

389

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

Chapter 9. The Evolution of Code


I*52 3.0A A5.A 9632 2C<9C21 <BA <3 ;2.?9F ;<A56;4, @<:2 10 /6996<; F2.?@ .3A2? A52 B;6C2?@2 2C<9C21 <BA <3 96A2?.99F ;<A56;4, 6@ . 3.0A @< @A.442?6;4 A5.A ! D<B91 /2 :.1 A< .AA2:=A D<?1@ A< 1< 6A 7B@A602.J Richard Dawkins

,BQgP Q>HB > JLJBKQ QL QEFKH ?>@H QL > PFJMIBO QFJB, TEBK VLR TOLQB VLRO CFOPQ 0OL@BPPFKD PHBQ@EBP >KA IFCB T>P COBB >KA B>PV. 7E>Q FP LKB LC MOLDO>JJFKDgP CRKA>JBKQ>I @LK@BMQP QE>Q VLR IFHBIV RPBA FK QELPB CFOPQ PHBQ@EBP >KA @LKQFKRB QL RPB LSBO >KA LSBO >D>FK? 15F=56@9G. 6>OF>?IBP >IILT VLR QL P>SB A>Q> >KA OBRPB QE>Q A>Q> TEFIB > MOLDO>J ORKP. 4EFP, LC @LROPB, FP KLQEFKD KBT QL RP. )K C>@Q, TB E>SB JLSBA C>O ?BVLKA > PHBQ@E TFQE GRPQ LKB LO QTL S>OF>?IBP >KA LK QL JLOB @LJMIBU A>Q> PQOR@QROBPeS>OF>?IBP J>AB COLJ @RPQLJ QVMBP (L?GB@QP) QE>Q FK@IRAB ?LQE A>Q> >KA CRK@QFLK>IFQV. 7BgSB J>AB LRO LTK IFQQIB TLOIAP LC JLSBOP >KA M>OQF@IBP >KA SBEF@IBP >KA @BIIP >KA QOBBP. )K B>@E >KA BSBOV BU>JMIB FK QEFP ?LLH, QEB S>OF>?IBP LC QEBPB L?GB@QP E>SB QL ?B FKFQF>IFWBA. 0BOE>MP VLR J>AB > TELIB ?RK@E LC M>OQF@IBP TFQE O>KALJ @LILOP >KA PFWBP LO > IFPQ LC SBEF@IBP >II PQ>OQFKD >Q QEB P>JB R ,S IL@>QFLK LK P@OBBK. "RQ FKPQB>A LC >@QFKD >P hFKQBIIFDBKQ ABPFDKBOPi >KA >PPFDKFKD QEB MOLMBOQFBP LC LRO L?GB@QP QEOLRDE O>KALJKBPP LO QELRDEQCRI @LKPFABO>QFLK, TB @>K IBQ > MOL@BPP CLRKA FK K>QROBe9JC@IH=CBeAB@FAB CLO RP. #>K TB QEFKH LC QEB S>OF>?IBP LC >K L?GB@Q >P FQP $.!? #>K L?GB@QP J>HB LQEBO L?GB@QP >KA M>PP ALTK QEBFO $.! QL > KBT DBKBO>QFLK? #>K LRO PFJRI>QFLK BSLISB?

390

4EB .>QROB LC #LAB (S1.0)

4EB >KPTBO QL >II QEBPB NRBPQFLKP FP VBP. !CQBO >II, TB TLRIAKgQ ?B >?IB QL C>@B LROPBISBP FK QEB JFOOLO >P K>QROB-LC-@LABOP TFQELRQ Q>@HIFKD > PFJRI>QFLK LC LKB LC QEB JLPQ MLTBOCRI >IDLOFQEJF@ MOL@BPPBP CLRKA FK K>QROB FQPBIC. 4EFP @E>MQBO FP ABAF@>QBA QL BU>JFKFKD QEB MOFK@FMIBP ?BEFKA ?FLILDF@>I BSLIRQFLK >KA CFKAFKD T>VP QL >MMIV QELPB MOFK@FMIBP FK @LAB.

9.1 Genetic Algorithms: Inspired by Actual Events


)QgP FJMLOQ>KQ CLO RP QL @I>OFCV QEB DL>IP LC QEFP @E>MQBO. 7B TFII KLQ DL FKQL ABMQE >?LRQ QEB P@FBK@B LC DBKBQF@P >KA BSLIRQFLK >P FQ E>MMBKP FK QEB OB>I TLOIA. 7B TLKgQ ?B J>HFKD 0RKKBQQ PNR>OBP (PLOOV QL AFP>MMLFKQ) >KA QEBOB TFII ?B KL AFP@RPPFLK LC KR@IBLQFABP, MOLQBFK PVKQEBPFP, 2.!, >KA LQEBO QLMF@P OBI>QBA QL QEB >@QR>I ?FLILDF@>I MOL@BPPBP LC BSLIRQFLK. )KPQB>A, TB >OB DLFKD QL ILLH >Q QEB @LOB MOFK@FMIBP ?BEFKA $>OTFKF>K BSLIRQFLK>OV QEBLOV >KA ABSBILM > PBQ LC >IDLOFQEJP =BGD=F98 ?V QEBPB MOFK@FMIBP. 7B ALKgQ @>OB PL JR@E >?LRQ >K >@@RO>QB PFJRI>QFLK LC BSLIRQFLK; O>QEBO, TB @>OB >?LRQ JBQELAP CLO >MMIVFKD BSLIRQFLK>OV PQO>QBDFBP FK PLCQT>OB. 4EFP FP KLQ QL P>V QE>Q > MOLGB@Q TFQE JLOB P@FBKQFCF@ ABMQE TLRIAKgQ E>SB S>IRB, >KA ) BK@LRO>DB OB>ABOP TFQE > M>OQF@RI>O FKQBOBPQ FK QEFP QLMF@ QL BUMILOB MLPPF?FIFQFBP CLO BUM>KAFKD QEB BU>JMIBP MOLSFABA TFQE >AAFQFLK>I BSLIRQFLK>OV CB>QROBP. .BSBOQEBIBPP, CLO QEB P>HB LC HBBMFKD QEFKDP J>K>DB>?IB, TBgOB DLFKD QL PQF@H QL QEB ?>PF@P, TEF@E TFII ?B MIBKQV @LJMIBU >KA BU@FQFKD. 4EB QBOJ hDBKBQF@ >IDLOFQEJi OBCBOP QL > PMB@FCF@ >IDLOFQEJ FJMIBJBKQBA FK > PMB@FCF@ T>V QL PLISB PMB@FCF@ PLOQP LC MOL?IBJP. 7EFIB QEB CLOJ>I DBKBQF@ >IDLOFQEJ FQPBIC TFII PBOSB >P QEB CLRKA>QFLK CLO QEB BU>JMIBP TB @OB>QB FK QEFP @E>MQBO, TB KBBAKgQ TLOOV >?LRQ FJMIBJBKQFKD QEB >IDLOFQEJ TFQE MBOCB@Q >@@RO>@V, DFSBK QE>Q TB >OB ILLHFKD CLO @OB>QFSB RPBP LC BSLIRQFLK>OV QEBLOFBP FK LRO @LAB. 4EFP @E>MQBO TFII ?B ?OLHBK ALTK FKQL QEB CLIILTFKD QEOBB M>OQP (TFQE QEB J>GLOFQV LC QEB QFJB PMBKQ LK QEB CFOPQ). 1. (=,/4?4:9,7 G090?4. A72:=4?38. 7BgII ?BDFK TFQE QEB QO>AFQFLK>I @LJMRQBO P@FBK@B DBKBQF@ >IDLOFQEJ. 4EFP >IDLOFQEJ T>P ABSBILMBA QL PLISB MOL?IBJP FK TEF@E QEB PLIRQFLK PM>@B FP PL S>PQ QE>Q > h?ORQB CLO@Bi >IDLOFQEJ TLRIA PFJMIV Q>HB QLL ILKD. (BOBgP >K BU>JMIB: )gJ QEFKHFKD LC > KRJ?BO. ! KRJ?BO ?BQTBBK LKB >KA LKB ?FIIFLK. (LT ILKD TFII FQ Q>HB CLO VLR QL DRBPP FQ? 3LISFKD > MOL?IBJ TFQE h?ORQB CLO@Bi OBCBOP QL QEB MOL@BPP LC @EB@HFKD BSBOV MLPPF?IB PLIRQFLK. )P FQ LKB? )P FQ QTL? )P FQ QEOBB? )P FQ CLRO? !KA PL >KA >KA PL CLOQE. 4ELRDE IR@H ALBP MI>V > C>@QLO EBOB, TFQE ?ORQB CLO@B TB TLRIA LCQBK CFKA LROPBISBP M>QFBKQIV T>FQFKD CLO VB>OP TEFIB VLR @LRKQ QL LKB ?FIIFLK. (LTBSBO, TE>Q FC ) @LRIA QBII VLR FC >K >KPTBO VLR D>SB T>P DLLA LO ?>A? 7>OJ LO @LIA? 6BOV T>OJ? (LQ? 3RMBO, PRMBO @LIA? )C VLR @LRIA BS>IR>QB ELT hCFQi > DRBPP FP, VLR @LRIA MF@H LQEBO KRJ?BOP @ILPBO QL QE>Q DRBPP >KA >OOFSB >Q QEB >KPTBO JLOB NRF@HIV. 9LRO >KPTBO @LRIA BSLISB. 2. I9?0=,.?4A0 '070.?4:9. /K@B TB BPQ>?IFPE QEB QO>AFQFLK>I @LJMRQBO P@FBK@B >IDLOFQEJ, TBgII ILLH >Q LQEBO >MMIF@>QFLKP LC DBKBQF@ >IDLOFQEJP FK QEB SFPR>I >OQP. )KQBO>@QFSB PBIB@QFLK OBCBOP QL QEB MOL@BPP LC BSLISFKD PLJBQEFKD (LCQBK >K @LJMRQBO-DBKBO>QBA FJ>DB) QEOLRDE RPBO FKQBO>@QFLK. ,BQgP P>V VLR T>IH FKQL > JRPBRJ D>IIBOV >KA PBB 391

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

QBK M>FKQFKDP. 7FQE FKQBO>@QFSB PBIB@QFLK, VLR TLRIA MF@H VLRO C>SLOFQBP >KA >IILT >K >IDLOFQEJF@ MOL@BPP QL DBKBO>QB (LO hBSLISBi) KBT M>FKQFKDP ?>PBA LK VLRO MOBCBOBK@BP. 3. E.:>D>?08 '48@7,?4:9. 4EB QO>AFQFLK>I @LJMRQBO P@FBK@B DBKBQF@ >IDLOFQEJ >KA FKQBO>@QFSB PBIB@QFLK QB@EKFNRB >OB TE>Q VLR TFII IFHBIV CFKA FC VLR PB>O@E LKIFKB LO OB>A > QBUQ?LLH >?LRQ >OQFCF@F>I FKQBIIFDBK@B. "RQ >P TBgII PLLK PBB, QEBV ALKgQ OB>IIV PFJRI>QB QEB MOL@BPP LC BSLIRQFLK >P FQ E>MMBKP FK QEB OB>I TLOIA. )K QEFP @E>MQBO, ) T>KQ QL >IPL BUMILOB QB@EKFNRBP CLO PFJRI>QFKD QEB MOL@BPP LC BSLIRQFLK FK >K B@LPVPQBJ LC MPBRAL-IFSFKD ?BFKDP. (LT @>K LRO L?GB@QP QE>Q JLSB >?LRQ QEB P@OBBK JBBQ B>@E LQEBO, J>QB, >KA M>PP QEBFO DBKBP LK QL > KBT DBKBO>QFLK? 4EFP TLRIA >MMIV AFOB@QIV QL QEB %@LPVPQBJ 0OLGB@Q LRQIFKBA >Q QEB BKA LC B>@E @E>MQBO.

9.2 Why Use Genetic Algorithms?


7EFIB @LJMRQBO PFJRI>QFLKP LC BSLIRQFLK>OV MOL@BPPBP A>QB ?>@H QL QEB 1950P, JR@E LC TE>Q TB QEFKH LC >P DBKBQF@ >IDLOFQEJP (>IPL HKLTK >P h'!Pi) QLA>V T>P ABSBILMBA ?V *LEK (LII>KA, > MOLCBPPLO >Q QEB 5KFSBOPFQV LC -F@EFD>K, TELPB ?LLH A85DH5H=CB =B *5HIF5@ 5B8 AFH=:=7=5@ .MGH9AG MFLKBBOBA '! OBPB>O@E. 4LA>V, JLOB DBKBQF@ >IDLOFQEJP >OB M>OQ LC > TFABO CFBIA LC OBPB>O@E, LCQBK OBCBOOBA QL >P "%SLIRQFLK>OV #LJMRQFKD." 4L EBIM FIIRPQO>QB QEB QO>AFQFLK>I DBKBQF@ >IDLOFQEJ, TB >OB DLFKD QL PQ>OQ TFQE JLKHBVP. .L, KLQ LRO BSLIRQFLK>OV >K@BPQLOP. 7BgOB DLFKD QL PQ>OQ TFQE PLJB CF@QFLK>I JLKHBVP QE>Q ?>KD >T>V LK HBV?L>OAP TFQE QEB DL>I LC QVMFKD LRQ QEB @LJMIBQB TLOHP LC 3E>HBPMB>OB.

F64B?2 9.1

392

4EB .>QROB LC #LAB (S1.0)

4EB hFKCFKFQB JLKHBV QEBLOBJi FP PQ>QBA >P CLIILTP: ! JLKHBV EFQQFKD HBVP O>KALJIV LK > QVMBTOFQBO TFII BSBKQR>IIV QVMB QEB @LJMIBQB TLOHP LC 3E>HBPMB>OB (DFSBK >K FKCFKFQB >JLRKQ LC QFJB). 4EB MOL?IBJ TFQE QEFP QEBLOV FP QE>Q QEB MOL?>?FIFQV LC P>FA JLKHBV >@QR>IIV QVMFKD 3E>HBPMB>OB FP PL ILT QE>Q BSBK FC QE>Q JLKHBV PQ>OQBA >Q QEB "FD ">KD, FQgP RK?BIFBS>?IV RKIFHBIV TBgA BSBK E>SB $5A@9H >Q QEFP MLFKQ. ,BQgP @LKPFABO > JLKHBV K>JBA 'BLODB. 'BLODB QVMBP LK > OBAR@BA QVMBTOFQBO @LKQ>FKFKD LKIV QTBKQV-PBSBK @E>O>@QBOP: QTBKQV-PFU IBQQBOP >KA LKB PM>@B ?>O. 3L QEB MOL?>?FIFQV LC 'BLODB EFQQFKD >KV DFSBK HBV FP LKB FK QTBKQV-PBSBK. ,BQgP @LKPFABO QEB MEO>PB hQL ?B LO KLQ QL ?B QE>Q FP QEB NRBPQFLKi (TBgOB PFJMIFCVFKD FQ COLJ QEB LOFDFK>I h4L ?B, LO KLQ QL ?B: QE>Q FP QEB NRBPQFLKi). 4EB MEO>PB FP 39 @E>O>@QBOP ILKD. )C 'BLODB PQ>OQP QVMFKD, QEB @E>K@B EBgII DBQ QEB CFOPQ @E>O>@QBO OFDEQ FP 1 FK 27. 3FK@B QEB MOL?>?FIFQV EBgII DBQ QEB PB@LKA @E>O>@QBO OFDEQ FP >IPL 1 FK 27, EB E>P > 1 FK 27*27 @E>K@B LC I>KAFKD QEB CFOPQ QTL @E>O>@QBOP FK @LOOB@Q LOABOeTEF@E CLIILTP AFOB@QIV COLJ LRO AFP@RPPFLK LC "BSBKQ MOL?>?FIFQV" FK QEB )KQOLAR@QFLK (PBB M>DB 7). 4EBOBCLOB, QEB MOL?>?FIFQV QE>Q 'BLODB TFII QVMB QEB CRII MEO>PB FP: (1/27) JRIQFMIFBA ?V FQPBIC 39 QFJBP, F.B. (1/27)39 TEF@E BNR>IP > 1 FK 66,555,937,033,867,822,607,895,549,241,096,482,953,017,615,834,735,226,163 @E>K@B LC DBQQFKD FQ OFDEQ! .BBAIBPP QL P>V, BSBK EFQQFKD GRPQ QEFP LKB MEO>PB, KLQ QL JBKQFLK >K BKQFOB MI>V, FP EFDEIV RKIFHBIV. %SBK FC 'BLODB FP > @LJMRQBO PFJRI>QFLK >KA @>K QVMB LKB JFIIFLK O>KALJ MEO>PBP MBO PB@LKA, CLO 'BLODB QL E>SB > 99% MOL?>?FIFQV LC BSBKQR>IIV DBQQFKD FQ OFDEQ, EB TLRIA E>SB QL QVMB CLO 9,719,096,182,010,563,073,125,591,133,903,305,625,605,017 VB>OP. (.LQB QE>Q QEB >DB LC QEB RKFSBOPB FP BPQFJ>QBA QL ?B > JBOB 13,750,000,000 VB>OP.) 4EB MLFKQ LC >II QEBPB RKC>QELJ>?IV I>ODB KRJ?BOP FP KLQ QL DFSB VLR > EB>A>@EB, ?RQ QL ABJLKPQO>QB QE>Q > ?ORQB CLO@B >IDLOFQEJ (QVMFKD BSBOV MLPPF?IB O>KALJ MEO>PB) FP KLQ > OB>PLK>?IB PQO>QBDV CLO >OOFSFKD O>KALJIV >Q hQL ?B LO KLQ QL ?B QE>Q FP QEB NRBPQFLKi. %KQBO DBKBQF@ >IDLOFQEJP, TEF@E TFII PELT QE>Q TB @>K PQFII PQ>OQ TFQE O>KALJ MEO>PBP >KA CFKA QEB PLIRQFLK QEOLRDE PFJRI>QBA BSLIRQFLK. .LT, FQgP TLOQE KLQFKD QE>Q QEFP MOL?IBJ (5FF=J9 5H H<9 D<F5G9 THC 69 CF BCH HC 69 H<5H =G H<9 EI9GH=CBU) FP > OFAF@RILRP LKB. 3FK@B TB HKLT QEB >KPTBO, >II TB KBBA QL AL FP QVMB FQ. (BOBgP > 0OL@BPPFKD PHBQ@E QE>Q PLISBP QEB MOL?IBJ.
MNLCHA M = "1I <? IL HIN NI <? NB;N CM NB? KO?MNCIH"; JLCHNFH(M);

.BSBOQEBIBPP, QEB MLFKQ EBOB FP QE>Q PLISFKD > MOL?IBJ TFQE > HKLTK >KPTBO >IILTP RP QL B>PFIV QBPQ LRO @LAB. /K@B TBgSB PR@@BPPCRIIV PLISBA QEB MOL?IBJ, TB @>K CBBI JLOB @LKCFABKQ FK RPFKD DBKBQF@ >IDLOFQEJP QL AL PLJB >@QR>I RPBCRI TLOH: PLISFKD MOL?IBJP TFQE RKHKLTK >KPTBOP. 3L QEFP CFOPQ BU>JMIB PBOSBP KL OB>I MROMLPB LQEBO QE>K QL ABJLKPQO>QB ELT DBKBQF@

393

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

>IDLOFQEJP TLOH. )C TB QBPQ QEB '! OBPRIQP >D>FKPQ QEB HKLTK >KPTBO >KA DBQ hQL ?B LO KLQ QL ?Bi, QEBK TBgSB PR@@BBABA FK TOFQFKD LRO DBKBQF@ >IDLOFQEJ.

Exercise 9.1
#OB>QB > PHBQ@E QE>Q DBKBO>QBP O>KALJ PQOFKDP. 7BgII KBBA QL HKLT ELT QL AL QEFP FK LOABO QL FJMIBJBKQ QEB DBKBQF@ >IDLOFQEJ BU>JMIB QE>Q TFII PELOQIV CLIILT. (LT ILKD ALBP FQ Q>HB CLO 0OL@BPPFKD QL O>KALJIV DBKBO>QB QEB PQOFKD h@>Qi? (LT @LRIA VLR >A>MQ QEFP QL DBKBO>QB > O>KALJ ABPFDK RPFKD 0OL@BPPFKDgP PE>MB-AO>TFKD CRK@QFLKP?

9.3 Darwinian Natural Selection


"BCLOB TB ?BDFK T>IHFKD QEOLRDE QEB DBKBQF@ >IDLOFQEJ, IBQgP Q>HB > JLJBKQ QL ABP@OF?B QEOBB @LOB MOFK@FMIBP LC $>OTFKF>K BSLIRQFLK QE>Q TFII ?B OBNRFOBA >P TB FJMIBJBKQ LRO PFJRI>QFLK. )K LOABO CLO K>QRO>I PBIB@QFLK QL L@@RO >P FQ ALBP FK K>QROB, >II QEOBB LC QEBPB BIBJBKQP JRPQ ?B MOBPBKQ. 1. H0=0/4?D. 4EBOB JRPQ ?B > MOL@BPP FK MI>@B ?V TEF@E @EFIAOBK OB@BFSB QEB MOLMBOQFBP LC QEBFO M>OBKQP. )C @OB>QROBP IFSB ILKD BKLRDE QL OBMOLAR@B, QEBK QEBFO QO>FQP >OB M>PPBA ALTK QL QEBFO @EFIAOBK FK QEB KBUQ DBKBO>QFLK LC @OB>QROBP. 2. *,=4,?4:9. 4EBOB JRPQ ?B > S>OFBQV LC QO>FQP MOBPBKQ FK QEB MLMRI>QFLK LO > JB>KP TFQE TEF@E QL FKQOLAR@B S>OF>QFLK. &LO BU>JMIB, IBQgP P>V QEBOB FP > MLMRI>QFLK LC ?BBQIBP FK TEF@E >II QEB ?BBQIBP >OB BU>@QIV QEB P>JB: P>JB @LILO, P>JB PFWB, P>JB TFKDPM>K, P>JB BSBOVQEFKD. 7FQELRQ >KV S>OFBQV FK QEB MLMRI>QFLK, QEB @EFIAOBK TFII >IT>VP ?B FABKQF@>I QL QEB M>OBKQP >KA QL B>@E LQEBO. .BT @LJ?FK>QFLKP LC QO>FQP @>K KBSBO L@@RO >KA KLQEFKD @>K BSLISB. 3. '070.?4:9. 4EBOB JRPQ ?B > JB@E>KFPJ ?V TEF@E PLJB JBJ?BOP LC > MLMRI>QFLK E>SB QEB LMMLOQRKFQV QL ?B M>OBKQP >KA M>PP ALTK QEBFO DBKBQF@ FKCLOJ>QFLK >KA PLJB AL KLQ. 4EFP FP QVMF@>IIV OBCBOOBA QL >P hPROSFS>I LC QEB CFQQBPQ.i &LO BU>JMIB, IBQgP P>V > MLMRI>QFLK LC D>WBIIBP FP @E>PBA ?V IFLKP BSBOV A>V. 4EB C>PQBO D>WBIIBP >OB JLOB IFHBIV QL BP@>MB QEB IFLKP >KA >OB QEBOBCLOB JLOB IFHBIV QL IFSB ILKDBO >KA E>SB > @E>K@B QL OBMOLAR@B >KA M>PP QEBFO DBKBP ALTK QL QEBFO @EFIAOBK. 4EB QBOJ :=HH9GH , ELTBSBO, @>K ?B > ?FQ JFPIB>AFKD. 'BKBO>IIV, TB QEFKH LC FQ >P JB>KFKD ?FDDBO, C>PQBO, LO PQOLKDBO. 7EFIB QEFP J>V ?B QEB @>PB FK PLJB FKPQ>K@BP, K>QRO>I PBIB@QFLK LMBO>QBP LK QEB MOFK@FMIB QE>Q PLJB QO>FQP >OB ?BQQBO >A>MQBA CLO QEB @OB>QROBgP BKSFOLKJBKQ >KA QEBOBCLOB MOLAR@B > DOB>QBO IFHBIFELLA LC PROSFSFKD >KA OBMOLAR@FKD. )Q E>P KLQEFKD QL AL TFQE > DFSBK @OB>QROB ?BFKD h?BQQBOi (>CQBO >II, QEFP FP > PR?GB@QFSB QBOJ) LO JLOB hMEVPF@>IIV CFQ.i )K QEB @>PB LC LRO QVMFKD JLKHBVP, CLO BU>JMIB, > JLOB hCFQi JLKHBV FP LKB QE>Q E>P QVMBA > MEO>PB @ILPBO QL hQL ?B LO KLQ QL ?Bi.

394

4EB .>QROB LC #LAB (S1.0)

.BUQ )gA IFHB QL T>IH QEOLRDE QEB K>OO>QFSB LC QEB DBKBQF@ >IDLOFQEJ. 7BgII AL QEFP FK QEB @LKQBUQ LC QEB QVMFKD JLKHBV. 4EB >IDLOFQEJ FQPBIC TFII ?B AFSFABA FKQL QTL M>OQP: > PBQ LC @LKAFQFLKP CLO FKFQF>IFW>QFLK (F.B. 0OL@BPPFKDgP M?NOJ() ) >KA QEB PQBMP QE>Q >OB OBMB>QBA LSBO >KA LSBO >D>FK (F.B. 0OL@BPPFKDgP >L;Q() ) RKQFI TB >OOFSB >Q QEB @LOOB@Q >KPTBO.

9.4 The Genetic Algorithm, Part I: Creating a Population


)K QEB @LKQBUQ LC QEB QVMFKD JLKHBV BU>JMIB, TB TFII @OB>QB > MLMRI>QFLK LC MEO>PBP. (.LQB QE>Q TB >OB RPFKD QEB QBOJ hMEO>PBi O>QEBO ILLPBIV, JB>KFKD > PQOFKD LC @E>O>@QBOP.) 4EFP ?BDP QEB NRBPQFLK: (LT AL TB @OB>QB QEFP MLMRI>QFLK? (BOB FP TEBOB QEB $>OTFKF>K MOFK@FMIB LC A,=4,?4:9 >MMIFBP. ,BQgP P>V, CLO PFJMIF@FQV, QE>Q TB >OB QOVFKD QL BSLISB QEB MEO>PB h@>Qi >KA QE>Q TB E>SB > MLMRI>QFLK LC QEOBB MEO>PBP.
>K= H?: MED

3ROB, QEBOB FP S>OFBQV FK QEB QEOBB MEO>PBP >?LSB, ?RQ QOV QL JFU >KA J>Q@E QEB @E>O>@QBOP BSBOV TEF@E T>V >KA VLR TFII KBSBO DBQ 75H . 4EBOB FP KLQ 9BCI;< S>OFBQV EBOB QL BSLISB QEB LMQFJ>I PLIRQFLK. (LTBSBO, FC TB E>A > MLMRI>QFLK LC QELRP>KAP LC MEO>PBP, >II DBKBO>QBA O>KALJIV, @E>K@BP >OB QE>Q >Q IB>PQ LKB JBJ?BO LC QEB MLMRI>QFLK TFII E>SB > 7 >P QEB CFOPQ @E>O>@QBO, LKB TFII E>SB >K 5 >P QEB PB@LKA, >KA LKB > H >P QEB QEFOA. ! I>ODB MLMRI>QFLK TFII JLPQ IFHBIV DFSB RP BKLRDE S>OFBQV QL DBKBO>QB QEB ABPFOBA MEO>PB (>KA FK 0>OQ 2 LC QEB >IDLOFQEJ, TBgII E>SB >KLQEBO LMMLOQRKFQV QL FKQOLAR@B BSBK JLOB S>OF>QFLK FK @>PB QEBOB FPKgQ BKLRDE FK QEB CFOPQ MI>@B). 3L TB @>K ?B JLOB PMB@FCF@ FK ABP@OF?FKD 3QBM 1 >KA P>V:

#OB>QB > MLMRI>QFLK LC O>KALJIV DBKBO>QBA BIBJBKQP.

4EFP ?OFKDP RM >KLQEBO FJMLOQ>KQ NRBPQFLK. 7E>Q FP QEB BIBJBKQ FQPBIC? !P TB JLSB QEOLRDE QEB BU>JMIBP FK QEFP @E>MQBO, TBgII PBB PBSBO>I AFCCBOBKQ P@BK>OFLP; TB JFDEQ E>SB > MLMRI>QFLK LC FJ>DBP LO > MLMRI>QFLK LC SBEF@IBP Z I> #E>MQBO 6 (PBB M>DB 308). 4EB HBV, >KA QEB M>OQ QE>Q FP KBT CLO RP FK QEFP @E>MQBO, FP QE>Q B>@E JBJ?BO LC QEB MLMRI>QFLK E>P > SFOQR>I h$.!,i > PBQ LC MOLMBOQFBP (TB @>K @>II QEBJ hDBKBPi) QE>Q ABP@OF?B ELT > DFSBK BIBJBKQ ILLHP LO ?BE>SBP. )K QEB @>PB LC QEB QVMFKD JLKHBV, CLO BU>JMIB, QEB $.! FP PFJMIV > PQOFKD LC @E>O>@QBOP. )K QEB CFBIA LC DBKBQF@P, QEBOB FP >K FJMLOQ>KQ AFPQFK@QFLK ?BQTBBK QEB @LK@BMQP ;9BCHMD9 >KA D<9BCHMD9. 4EB >@QR>I DBKBQF@ @LABeFK LRO @>PB, QEB AFDFQ>I FKCLOJ>QFLK FQPBICeFP >K BIBJBKQgP 209:?D;0 . 4EFP FP TE>Q DBQP M>PPBA ALTK COLJ DBKBO>QFLK QL DBKBO>QFLK. 4EB ;309:?D;0 , ELTBSBO, FP QEB BUMOBPPFLK LC QE>Q A>Q>. 4EFP AFPQFK@QFLK FP HBV QL ELT VLR TFII RPB DBKBQF@ >IDLOFQEJP FK VLRO LTK TLOH. 7E>Q >OB QEB L?GB@QP FK VLRO TLOIA? (LT TFII VLR ABPFDK QEB

395

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

DBKLQVMB CLO VLRO L?GB@QP (QEB A>Q> PQOR@QROB QL PQLOB B>@E L?GB@QgP MOLMBOQFBP) >P TBII >P QEB MEBKLQVMB (TE>Q >OB MCI RPFKD QEBPB S>OF>?IBP QL BUMOBPP?) 7B AL QEFP >II QEB QFJB FK DO>MEF@P MOLDO>JJFKD. 4EB PFJMIBPQ BU>JMIB FP MOL?>?IV @LILO.

Genotype
FKQ @ = 255; FKQ @ = 127; FKQ @ = 0;

Phenotype

!P TB @>K PBB, QEB DBKLQVMB FP QEB AFDFQ>I FKCLOJ>QFLK. %>@E @LILO FP > S>OF>?IB QE>Q PQLOBP >K FKQBDBO >KA TB @ELLPB QL BUMOBPP QE>Q FKQBDBO >P > @LILO. "RQ ELT TB @ELLPB QL BUMOBPP QEB A>Q> FP >O?FQO>OV. )K > AFCCBOBKQ >MMOL>@E, TB @LRIA E>SB RPBA QEB FKQBDBO QL ABP@OF?B QEB IBKDQE LC > IFKB, QEB TBFDEQ LC > CLO@B, BQ@.

Same Genotype
FKQ @ = 255; FKQ @ = 127; FKQ @ = 0;

Different Phenotype (line length)

4EB KF@B QEFKD >?LRQ LRO JLKHBV-QVMFKD BU>JMIB FP QE>Q QEBOB FP KL AFCCBOBK@B ?BQTBBK DBKLQVMB >KA MEBKLQVMB. 4EB $.! A>Q> FQPBIC FP > PQOFKD LC @E>O>@QBOP >KA QEB BUMOBPPFLK LC QE>Q A>Q> FP QE>Q SBOV PQOFKD. 3L, TB @>K CFK>IIV BKA QEB AFP@RPPFLK LC QEFP CFOPQ PQBM >KA ?B JLOB PMB@FCF@ TFQE FQP ABP@OFMQFLK, P>VFKD:

#OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE O>KALJIV DBKBO>QBA $.!.

396

4EB .>QROB LC #LAB (S1.0)

9.5 The Genetic Algorithm, Part II: Selection


(BOB FP TEBOB TB >MMIV QEB $>OTFKF>K MOFK@FMIB LC G9@97H=CB. 7B KBBA QL BS>IR>QB QEB MLMRI>QFLK >KA ABQBOJFKB TEF@E JBJ?BOP >OB CFQ QL ?B PBIB@QBA >P M>OBKQP CLO QEB KBUQ DBKBO>QFLK. 4EB MOL@BPP LC PBIB@QFLK @>K ?B AFSFABA FKQL QTL PQBMP. 1) EA,7@,?0 14?90>>. &LO LRO DBKBQF@ >IDLOFQEJ QL CRK@QFLK MOLMBOIV, TB TFII KBBA QL ABPFDK TE>Q FP OBCBOOBA QL >P > 14?90>> 1@9.?4:9 . 4EB CRK@QFLK TFII MOLAR@B > KRJBOF@ P@LOB QL ABP@OF?B QEB CFQKBPP LC > DFSBK JBJ?BO LC QEB MLMRI>QFLK. 4EFP, LC @LROPB, FP KLQ ELT QEB OB>I TLOIA TLOHP >Q >II. #OB>QROBP >OB KLQ DFSBK > P@LOB; QEBV PFJMIV PROSFSB LO KLQ. "RQ FK QEB @>PB LC QEB QO>AFQFLK>I DBKBQF@ >IDLOFQEJ, TEBOB TB >OB QOVFKD QL BSLISB >K LMQFJ>I PLIRQFLK QL > MOL?IBJ, TB KBBA QL ?B >?IB QL KRJBOF@>IIV BS>IR>QB >KV DFSBK MLPPF?IB PLIRQFLK. ,BQgP BU>JFKB LRO @ROOBKQ BU>JMIB, QEB QVMFKD JLKHBV. !D>FK, IBQgP PFJMIFCV QEB P@BK>OFL >KA P>V TB >OB >QQBJMQFKD QL BSLISB QEB TLOA h@>Qi. 7B E>SB QEOBB JBJ?BOP LC QEB MLMRI>QFLK: <IH, 75F , >KA 6CL. C5F FP L?SFLRPIV QEB JLPQ CFQ, DFSBK QE>Q FQ E>P QTL @LOOB@Q @E>O>@QBOP, <IH E>P LKIV LKB, >KA 6CL E>P WBOL. !KA QEBOB FQ FP, LRO CFQKBPP CRK@QFLK:
<?JD;II = J>; DKC8;H E< 9EHH;9J 9>7H79J;HI

DNA ERQ @>O ?LU

Fitness 1 2 0

7B TFII BSBKQR>IIV T>KQ QL ILLH >Q BU>JMIBP TFQE JLOB PLMEFPQF@>QBA CFQKBPP CRK@QFLKP, ?RQ QEFP FP > DLLA MI>@B QL PQ>OQ. 2) C=0,?0 , 8,?492 ;::7. /K@B QEB CFQKBPP E>P ?BBK @>I@RI>QBA CLO >II JBJ?BOP LC QEB MLMRI>QFLK, TB @>K QEBK PBIB@Q TEF@E JBJ?BOP >OB CFQ QL ?B@LJB M>OBKQP >KA MI>@B QEBJ FK > J>QFKD MLLI. 4EBOB >OB PBSBO>I AFCCBOBKQ >MMOL>@EBP TB @LRIA Q>HB EBOB. &LO BU>JMIB, TB @LRIA BJMILV TE>Q FP HKLTK >P QEB 074?4>? JBQELA >KA P>V, h7EF@E QTL JBJ?BOP LC QEB MLMRI>QFLK P@LOBA QEB EFDEBPQ? 9LR QTL TFII J>HB >II QEB @EFIAOBK CLO QEB KBUQ DBKBO>QFLK.i 4EFP FP MOL?>?IV LKB LC QEB B>PFBO JBQELAP QL MOLDO>J; ELTBSBO, FQ CIFBP FK QEB C>@B LC QEB MOFK@FMIB LC S>OF>QFLK. )C QTL JBJ?BOP LC QEB MLMRI>QFLK (LRQ LC MBOE>MP QELRP>KAP) >OB QEB LKIV LKBP >S>FI>?IB QL OBMOLAR@B, QEB KBUQ DBKBO>QFLK TFII E>SB IFQQIB S>OFBQV >KA QEFP J>V PQRKQ QEB BSLIRQFLK>OV MOL@BPP. 7B @LRIA FKPQB>A J>HB > J>QFKD MLLI LRQ LC > I>ODBO KRJ?BOeCLO BU>JMIB, QEB QLM 50% LC QEB

397

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

MLMRI>QFLK, 500 LRQ LC 1,000. 4EFP FP >IPL GRPQ >P B>PV QL MOLDO>J, ?RQ FQ TFII KLQ MOLAR@B LMQFJ>I OBPRIQP. )K QEFP @>PB, QEB EFDE-P@LOFKD QLM BIBJBKQP TLRIA E>SB QEB P>JB @E>K@B LC ?BFKD PBIB@QBA >P > M>OBKQ >P QEB LKBP QLT>OA QEB JFAAIB. !KA TEV PELRIA BIBJBKQ KRJ?BO 500 E>SB > PLIFA PELQ LC OBMOLAR@FKD, TEFIB BIBJBKQ KRJ?BO 501 E>P KL PELQ? ! ?BQQBO PLIRQFLK CLO QEB J>QFKD MLLI FP QL RPB > ;=:-,-474>?4. JBQELA, TEF@E TBgII @>II QEB hTEBBI LC CLOQRKBi (>IPL HKLTK >P QEB hOLRIBQQB TEBBIi). 4L FIIRPQO>QB QEFP JBQELA, IBQgP @LKPFABO > PFJMIB BU>JMIB TEBOB TB E>SB > MLMRI>QFLK LC CFSB BIBJBKQP, B>@E TFQE > CFQKBPP P@LOB.

Element ! " # $ %

Fitness 3 4 0.5 1.5 1

4EB CFOPQ QEFKD TBgII T>KQ QL AL FP 9:=8,74E0 >II QEB P@LOBP. 2BJBJ?BO KLOJ>IFWFKD > SB@QLO? 4E>Q FKSLISBA Q>HFKD >K SB@QLO >KA PQ>KA>OAFWFKD FQP IBKDQE, PBQQFKD FQ QL 1. 7EBK TB KLOJ>IFWB > PBQ LC CFQKBPP P@LOBP, TB >OB PQ>KA>OAFWFKD QEBFO O>KDB QL ?BQTBBK 0 >KA 1, >P > MBO@BKQ>DB LC QLQ>I CFQKBPP. ,BQgP >AA RM >II QEB CFQKBPP P@LOBP. QLQ>I CFQKBPP = 3 + 4 + 0.5 + 1.5 + 1 = 10 4EBK IBQgP AFSFAB B>@E P@LOB ?V QEB QLQ>I CFQKBPP, DFSFKD RP QEB KLOJ>IFWBA CFQKBPP.

Element ! " # $ %

Fitness 3 4 0.5 1.5 1

Normalized Fitness 0.3 0.4 0.05 0.15 0.1

Expressed as a Percentage 30% 40% 5% 15% 10%

398

4EB .>QROB LC #LAB (S1.0)

.LT FQgP QFJB CLO QEB TEBBI LC CLOQRKB.

F64B?2 9.2

3MFK QEB TEBBI >KA VLRgII KLQF@B QE>Q %IBJBKQ " E>P QEB EFDEBPQ @E>K@B LC ?BFKD PBIB@QBA, CLIILTBA ?V !, QEBK $, QEBK %, >KA CFK>IIV #. 4EFP MOL?>?FIFQV-?>PBA PBIB@QFLK >@@LOAFKD QL CFQKBPP FP >K BU@BIIBKQ >MMOL>@E. /KB, FQ DR>O>KQBBP QE>Q QEB EFDEBPQ-P@LOFKD BIBJBKQP TFII ?B JLPQ IFHBIV QL OBMOLAR@B. 4TL, FQ ALBP KLQ BKQFOBIV BIFJFK>QB >KV S>OF>QFLK COLJ QEB MLMRI>QFLK. 5KIFHB TFQE QEB BIFQFPQ JBQELA, BSBK QEB ILTBPQ-P@LOFKD BIBJBKQ (FK QEFP @>PB #) E>P > @E>K@B QL M>PP FQP FKCLOJ>QFLK ALTK QL QEB KBUQ DBKBO>QFLK. )QgP NRFQB MLPPF?IB (>KA LCQBK QEB @>PB) QE>Q BSBK ILT-P@LOFKD BIBJBKQP E>SB > QFKV KRDDBQ LC DBKBQF@ @LAB QE>Q FP QORIV RPBCRI >KA PELRIA KLQ BKQFOBIV ?B BIFJFK>QBA COLJ QEB MLMRI>QFLK. &LO BU>JMIB, FK QEB @>PB LC BSLISFKD hQL ?B LO KLQ QL ?Bi, TB JFDEQ E>SB QEB CLIILTFKD BIBJBKQP.
A: JE 8; EH DEJ JE =E B: JE 8; EH DEJ JE F? C: NNNNNNNNNNNNNNNN8;

!P VLR @>K PBB, BIBJBKQP ! >KA " >OB @IB>OIV QEB JLPQ CFQ >KA TLRIA E>SB QEB EFDEBPQ P@LOB. "RQ KBFQEBO @LKQ>FKP QEB @LOOB@Q @E>O>@QBOP CLO QEB BKA LC QEB MEO>PB. %IBJBKQ #, BSBK QELRDE FQ TLRIA OB@BFSB > SBOV ILT P@LOB, E>MMBKP QL E>SB QEB DBKBQF@ A>Q> CLO QEB BKA LC QEB MEO>PB. !KA PL TEFIB TB TLRIA T>KQ ! >KA " QL ?B MF@HBA QL DBKBO>QB QEB J>GLOFQV LC QEB KBUQ DBKBO>QFLK, TB TLRIA PQFII T>KQ # QL E>SB > PJ>II @E>K@B QL M>OQF@FM>QB FK QEB OBMOLAR@QFSB MOL@BPP.

9.6 The Genetic Algorithm, Part III: Reproduction


.LT QE>Q TB E>SB > PQO>QBDV CLO MF@HFKD M>OBKQP, TB KBBA QL CFDROB LRQ ELT QL RPB F9DFC8I7H=CB QL J>HB QEB MLMRI>QFLKgP KBUQ DBKBO>QFLK, HBBMFKD FK JFKA QEB $>OTFKF>K MOFK@FMIB LC EBOBAFQVeQE>Q @EFIAOBK FKEBOFQ MOLMBOQFBP COLJ QEBFO M>OBKQP. !D>FK, QEBOB >OB > KRJ?BO LC AFCCBOBKQ QB@EKFNRBP TB @LRIA BJMILV EBOB. &LO BU>JMIB, LKB OB>PLK>?IB (>KA B>PV QL MOLDO>J) PQO>QBDV FP >PBUR>I OBMOLAR@QFLK, JB>KFKD TB MF@H GRPQ LKB M>OBKQ >KA @OB>QB >

399

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

@EFIA QE>Q FP >K BU>@Q @LMV LC QE>Q M>OBKQ. 4EB PQ>KA>OA >MMOL>@E TFQE DBKBQF@ >IDLOFQEJP, ELTBSBO, FP QL MF@H QTL M>OBKQP >KA @OB>QB > @EFIA >@@LOAFKD QL QEB CLIILTFKD PQBMP. 1) C=:>>:A0=. #OLPPLSBO FKSLISBP @OB>QFKD > @EFIA LRQ LC QEB DBKBQF@ @LAB LC QTL M>OBKQP. )K QEB @>PB LC QEB JLKHBV-QVMFKD BU>JMIB, IBQgP >PPRJB TBgSB MF@HBA QTL MEO>PBP COLJ QEB J>QFKD MLLI (>P LRQIFKBA FK LRO PBIB@QFLK PQBM).
+7H;DJ A: "*,& +7H;DJ B: +'A3

)QgP KLT RM QL RP QL J>HB > @EFIA MEO>PB COLJ QEBPB QTL. 0BOE>MP QEB JLPQ L?SFLRP T>V (IBQgP @>II QEFP QEB 50/50 JBQELA) TLRIA ?B QL Q>HB QEB CFOPQ QTL @E>O>@QBOP COLJ ! >KA QEB PB@LKA QTL COLJ ", IB>SFKD RP TFQE:

F64B?2 9.3

! S>OF>QFLK LC QEFP QB@EKFNRB FP QL MF@H > O>KALJ JFAMLFKQ. )K LQEBO TLOAP, TB ALKgQ E>SB QL MF@H BU>@QIV E>IC LC QEB @LAB COLJ B>@E M>OBKQ. 7B @LRIA PLJBQFJBP BKA RM TFQE &,!9, >KA PLJBQFJBP TFQE &/29. 4EFP FP MOBCBO>?IB QL QEB 50/50 >MMOL>@E, PFK@B TB FK@OB>PB QEB S>OFBQV LC MLPPF?FIFQFBP CLO QEB KBUQ DBKBO>QFLK.

F64B?2 9.4: '6086;4 . ?.;1<: :61=<6;A

400

4EB .>QROB LC #LAB (S1.0)

!KLQEBO MLPPF?FIFQV FP QL O>KALJIV PBIB@Q > M>OBKQ CLO B>@E @E>O>@QBO FK QEB @EFIA PQOFKD. 9LR @>K QEFKH LC QEFP >P CIFMMFKD > @LFK CLRO QFJBP: EB>AP Q>HB COLJ M>OBKQ !, Q>FIP COLJ M>OBKQ ". (BOB TB @LRIA BKA RM TFQE J>KV AFCCBOBKQ OBPRIQP PR@E >P: 0,29, &,2+, &,29, &/29, BQ@.

F64B?2 9.5: C<6;-396==6;4 .==?<.05

4EFP PQO>QBDV TFII MOLAR@B BPPBKQF>IIV QEB P>JB OBPRIQP >P QEB O>KALJ JFAMLFKQ JBQELA; ELTBSBO, FC QEB LOABO LC QEB DBKBQF@ FKCLOJ>QFLK MI>VP PLJB OLIB FK BUMOBPPFKD QEB MEBKLQVMB, VLR J>V MOBCBO LKB PLIRQFLK LSBO QEB LQEBO. 2) "@?,?4:9. /K@B QEB @EFIA $.! E>P ?BBK @OB>QBA SF> @OLPPLSBO, TB >MMIV LKB CFK>I MOL@BPP ?BCLOB >AAFKD QEB @EFIA QL QEB KBUQ DBKBO>QFLKe8@?,?4:9. -RQ>QFLK FP >K LMQFLK>I PQBM, >P QEBOB >OB PLJB @>PBP FK TEF@E FQ FP RKKB@BPP>OV. (LTBSBO, FQ BUFPQP ?B@>RPB LC QEB $>OTFKF>K MOFK@FMIB LC S>OF>QFLK. 7B @OB>QBA >K FKFQF>I MLMRI>QFLK O>KALJIV, J>HFKD PROB QE>Q TB PQ>OQ TFQE > S>OFBQV LC BIBJBKQP. (LTBSBO, QEBOB @>K LKIV ?B PL JR@E S>OFBQV TEBK PBBAFKD QEB CFOPQ DBKBO>QFLK, >KA JRQ>QFLK >IILTP RP QL FKQOLAR@B >AAFQFLK>I S>OFBQV QEOLRDELRQ QEB BSLIRQFLK>OV MOL@BPP FQPBIC. -RQ>QFLK FP ABP@OF?BA FK QBOJP LC > F5H9 . ! DFSBK DBKBQF@ >IDLOFQEJ JFDEQ E>SB > JRQ>QFLK O>QB LC 5% LO 1% LO 0.1%, BQ@. ,BQgP >PPRJB TB GRPQ CFKFPEBA TFQE @OLPPLSBO >KA BKABA RM TFQE QEB @EFIA &/29. )C TB E>SB > JRQ>QFLK O>QB LC 1%, QEFP JB>KP QE>Q CLO B>@E @E>O>@QBO FK QEB MEO>PB DBKBO>QBA COLJ @OLPPLSBO, QEBOB FP > 1% @E>K@B QE>Q FQ TFII F64B?2 9.6 JRQ>QB. 7E>Q ALBP FQ JB>K CLO > @E>O>@QBO QL JRQ>QB? )K QEFP @>PB, TB ABCFKB JRQ>QFLK >P MF@HFKD > KBT O>KALJ @E>O>@QBO. ! 1% MOL?>?FIFQV FP C>FOIV ILT, >KA JLPQ LC QEB QFJB JRQ>QFLK TFII KLQ L@@RO >Q >II FK > CLRO-@E>O>@QBO PQOFKD (96% LC QEB QFJB QL ?B JLOB MOB@FPB). (LTBSBO, TEBK FQ ALBP, QEB JRQ>QBA @E>O>@QBO FP OBMI>@BA TFQE > O>KALJIV DBKBO>QBA LKB (PBB &FDROB 9.6).

401

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB

!P TBgII PBB FK PLJB LC QEB BU>JMIBP, QEB JRQ>QFLK O>QB @>K DOB>QIV >CCB@Q QEB ?BE>SFLO LC QEB PVPQBJ. #BOQ>FKIV, > SBOV EFDE JRQ>QFLK O>QB (PR@E >P, P>V, 80%) TLRIA KBD>QB QEB BSLIRQFLK>OV MOL@BPP FQPBIC. )C QEB J>GLOFQV LC > @EFIAgP DBKBP >OB DBKBO>QBA O>KALJIV, QEBK TB @>KKLQ DR>O>KQBB QE>Q QEB JLOB hCFQi DBKBP L@@RO TFQE DOB>QBO COBNRBK@V TFQE B>@E PR@@BPPFSB DBKBO>QFLK. 4EB MOL@BPP LC PBIB@QFLK (MF@HFKD QTL M>OBKQP) >KA OBMOLAR@QFLK (@OLPPLSBO >KA JRQ>QFLK) FP >MMIFBA LSBO >KA LSBO >D>FK , QFJBP RKQFI TB E>SB > KBT MLMRI>QFLK LC , BIBJBKQP. !Q QEFP MLFKQ, QEB KBT MLMRI>QFLK LC @EFIAOBK ?B@LJBP QEB @ROOBKQ MLMRI>QFLK >KA TB ILLM ?>@H QL BS>IR>QB CFQKBPP >KA MBOCLOJ PBIB@QFLK >KA OBMOLAR@QFLK >D>FK. .LT QE>Q TB E>SB ABP@OF?BA >II QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ FK ABQ>FI, FQgP QFJB QL QO>KPI>QB QEBPB PQBMP FKQL 0OL@BPPFKD @LAB. "B@>RPB QEB MOBSFLRP ABP@OFMQFLK T>P > ?FQ ILKDTFKABA, IBQgP ILLH >Q >K LSBOSFBT LC QEB >IDLOFQEJ CFOPQ. 7BgII QEBK @LSBO B>@E LC QEB QEOBB PQBMP FK FQP LTK PB@QFLK, TLOHFKD LRQ QEB @LAB. 'E()%: 3QBM 1: I94?4,74E0. #OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE O>KALJIV DBKBO>QBA $.!. !$$%: 3QBM 2: '070.?4:9. %S>IR>QB QEB CFQKBPP LC B>@E BIBJBKQ LC QEB MLMRI>QFLK >KA ?RFIA > J>QFKD MLLI. 3QBM 3: &0;=:/@.?4:9 . 2BMB>Q . QFJBP: >) 0F@H QTL M>OBKQP TFQE MOL?>?FIFQV >@@LOAFKD QL OBI>QFSB CFQKBPP. ?) #OLPPLSBOe@OB>QB > h@EFIAi ?V @LJ?FKFKD QEB $.! LC QEBPB QTL M>OBKQP. @) -RQ>QFLKeJRQ>QB QEB @EFIAgP $.! ?>PBA LK > DFSBK MOL?>?FIFQV. A) !AA QEB KBT @EFIA QL > KBT MLMRI>QFLK. 3QBM 4. 2BMI>@B QEB LIA MLMRI>QFLK TFQE QEB KBT MLMRI>QFLK >KA OBQROK QL 3QBM 2.

9.7 Code for Creating the Population


Step 1: Initialize Population
)C TBgOB DLFKD QL @OB>QB > MLMRI>QFLK, TB KBBA > A>Q> PQOR@QROB QL PQLOB > IFPQ LC JBJ?BOP LC QEB MLMRI>QFLK. )K JLPQ @>PBP (PR@E >P LRO QVMFKD-JLKHBV BU>JMIB), QEB KRJ?BO LC BIBJBKQP FK QEB MLMRI>QFLK @>K ?B CFUBA, >KA PL TB RPB >K >OO>V. (,>QBO TBgII PBB BU>JMIBP QE>Q FKSLISB > DOLTFKD/PEOFKHFKD MLMRI>QFLK >KA TBgII RPB >K ALL;S*CMN .) "RQ >K >OO>V LC TE>Q? 7B KBBA >K L?GB@Q QE>Q PQLOBP QEB DBKBQF@ FKCLOJ>QFLK CLO > JBJ?BO LC QEB MLMRI>QFLK. ,BQgP @>II FQ D#A.

402

4EB .>QROB LC #LAB (S1.0)


=F;MM ",A U W

4EB MLMRI>QFLK TFII QEBK ?B >K >OO>V LC ",A L?GB@QP.


",A89 JIJOF;NCIH = H?Q ",A81009; A population of 100 DNA objects

"RQ TE>Q PQRCC DLBP FK QEB ",A @I>PP? &LO > QVMFKD JLKHBV, FQP $.! FP QEB O>KALJ MEO>PB FQ QVMBP, > PQOFKD LC @E>O>@QBOP.
=F;MM ",A U 0NLCHA JBL;M?; W

7EFIB QEFP FP MBOCB@QIV OB>PLK>?IB CLO QEFP M>OQF@RI>O BU>JMIB, TBgOB KLQ DLFKD QL RPB >K >@QR>I 0NLCHA L?GB@Q >P QEB DBKBQF@ @LAB. )KPQB>A, TBgII RPB >K >OO>V LC @E>O>@QBOP.
=F;MM ",A U =B;L89 A?H?M = H?Q =B;L8189; W Each "gene" is one element of the array. We need 18 genes because to be or not to be is 18 characters long.

"V RPFKD >K >OO>V, TBgII ?B >?IB QL BUQBKA >II QEB @LAB TB TOFQB FKQL LQEBO BU>JMIBP. &LO BU>JMIB, QEB $.! LC > @OB>QROB FK > MEVPF@P PVPQBJ JFDEQ ?B >K >OO>V LC .3?=NIL PeLO CLO >K FJ>DB, >K >OO>V LC FKQBDBOP (2'" @LILOP). 7B @>K ABP@OF?B >KV PBQ LC MOLMBOQFBP FK >K >OO>V, >KA BSBK QELRDE > PQOFKD FP @LKSBKFBKQ CLO QEFP M>OQF@RI>O PHBQ@E, >K >OO>V TFII PBOSB >P > ?BQQBO CLRKA>QFLK CLO CRQROB BSLIRQFLK>OV BU>JMIBP. /RO DBKBQF@ >IDLOFQEJ AF@Q>QBP QE>Q TB @OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE F5B8CA@M ;9B9F5H98 *A. 4EBOBCLOB, FK QEB L?GB@QgP @LKPQOR@QLO, TB O>KALJIV @OB>QB B>@E @E>O>@QBO LC QEB >OO>V.
=F;MM ",A U =B;L89 A?H?M = H?Q =B;L8189; ",A() U @IL (CHN C = 0; C < A?H?M.F?HANB; C++) U A?H?M8C9 = (=B;L) L;H>IG(32,128); W W W Picking randomly from a range of characters with ASCII values between 32 and 128. For more about ASCII: http://en.wikipedia.org/ wiki/ASCII

.LT QE>Q TB E>SB QEB @LKPQOR@QLO, TB @>K OBQROK QL M?NOJ() >KA FKFQF>IFWB B>@E ",A L?GB@Q FK QEB MLMRI>QFLK >OO>V.

403

#E>MQBO 9. 4EB %SLIRQFLK LC #LAB


",A89 JIJOF;NCIH = H?Q ",A81009; PIC> M?NOJ() U @IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U JIJOF;NCIH8C9 = H?Q ",A(); W W Initializing each member of the population

/RO ",A @I>PP FP KLQ >Q >II @LJMIBQB. 7BgII KBBA QL >AA CRK@QFLKP QL FQ QL MBOCLOJ >II QEB LQEBO Q>PHP FK LRO DBKBQF@ >IDLOFQEJ, TEF@E TBgII AL >P TB T>IH QEOLRDE PQBMP 2 >KA 3.

Step 2: Selection
3QBM 2 OB>AP, T!J5@I5H9 H<9 :=HB9GG C: 957< 9@9A9BH C: H<9 DCDI@5H=CB 5B8 6I=@8 5 A5H=B; DCC@.U ,BQgP CFOPQ BS>IR>QB B>@E L?GB@QgP CFQKBPP. %>OIFBO TB PQ>QBA QE>Q LKB MLPPF?IB CFQKBPP CRK@QFLK CLO LRO QVMBA MEO>PBP FP QEB QLQ>I KRJ?BO LC @LOOB@Q @E>O>@QBOP. ,BQgP OBSFPB QEFP CFQKBPP CRK@QFLK > IFQQIB ?FQ >KA PQ>QB FQ >P QEB MBO@BKQ>DB LC @LOOB@Q @E>O>@QBOPeF.B., QEB QLQ>I KRJ?BO LC @LOOB@Q @E>O>@QBOP AFSFABA ?V QEB QLQ>I @E>O>@QBOP.
"?JD;II = .EJ7B # C>7H79J;HI CEHH;9J/.EJ7B # C>7H79J;HI

7EBOB PELRIA TB @>I@RI>QB QEB CFQKBPP? 3FK@B QEB ",A @I>PP @LKQ>FKP QEB DBKBQF@ FKCLOJ>QFLK (QEB MEO>PB TB TFII QBPQ >D>FKPQ QEB Q>ODBQ MEO>PB), TB @>K TOFQB > CRK@QFLK FKPFAB QEB ",A @I>PP FQPBIC QL P@LOB FQP LTK CFQKBPP. ,BQgP >PPRJB TB E>SB > Q>ODBQ MEO>PB:
0NLCHA N;LA?N = "NI <? IL HIN NI <?";

7B @>K KLT @LJM>OB B>@E hDBKBi >D>FKPQ QEB @LOOBPMLKAFKD @E>O>@QBO FK QEB Q>ODBQ MEO>PB, FK@OBJBKQFKD > @LRKQBO B>@E QFJB TB DBQ > @LOOB@Q @E>O>@QBO.
=F;MM ",A U @FI;N @CNH?MM; We are adding another variable to the DNA class to track fitness. Function to score fitness

PIC> @CNH?MM () U CHN M=IL? = 0; @IL (CHN C = 0;