Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
CheckingforCollision
byVickiZhangUnderthedirectionofProfessorSusanRodger
DukeUniversityJuly2017
1
CheckingforCollision
Thegoalofthismini-gameistoguidethefloatingbuddysafelytothecoconut.Therefore,twodetectionsareinvolved:1. GhostandCoconut2. GhostandFire
��
2
Collision
TherearetwowaysofcodingcollisiondetectioninAlice3:� tousethein-built“collisiondetection”eventlistenerfeature,or� tomanuallyspecifythresholddistancebetween2+objects
��
3
Togetstarted
•DownloadtheGhostFire_Start Alice3world.• Youwillfindthattheobjectsareinplace,withaghost,fourflamesofdifferentcolors,andacoconut.• Tip:Ifyouwanttochangethecolorofanobject,gotosetupscene,clickontheobject,andontherightsection,finditspaint.
4
Guideline
• InPartI,wewillusethein-builtAlice3collisiondetectorforghostandcoconut.• InPartII,wewillmanuallydeterminethethresholddistanceforcollisionbetweenghostandfire.Here,wewillunderstandtheunderlyingmechanismofthecollisiondetector usedinPartI.
5
PartI:Collisiondetectioneventlistener
SelectInitializeEventListener tabonthetop.AddEventListener>>
Position/Orientation>>CollisionStartListener>>
CustomArray>>CustomArray 6
Commandingthecollisiondetector
• Inthepop-upwindow,add“Coconut”.ClickOk.
• Anidenticalwindowpopsup.Add“Ghost.”
• Inthesetwowindows,youhavespecifiedwhichtwoobjectsthecollisiondetectorshouldkeepaneyeon.Itshouldlooklikethis:
7
pickUpCoconut Procedure
•Whencollisionbetweencoconutandghostisdetected,theghostshallpickthecoconutup.•Forclarity,let’sputthispickupinasceneprocedure(asmultiplepartiesareinvolved).•Add Scene Procedure andnameitpickUpCoconut•Youcouldimproviseasyouwish.Ademoisdisplayedonthenextpage.
8
9
FinishingupCollisionDetector
• First,switchbacktotheInitializeEventListener tabonthetop.•Wehavejustwrittenaproceduredetailingwhatshallhappenwhentheghostandthecoconutcollide.•Weshouldnowdragthatprocedureintothecollisiondetectorblock,soastoexecutethepickupprocedurewhencollisionhappens.
10
• FindyourpickUpCoconutprocedureunder“this”bar.•Dragtheprocedureintothecollisiondetectionblock.
11
Let’senablethekeyboardtocontroltheghost’smovementAdd event listener >> keyboard>> add object move for >> ghostYoucanruntheprogramandguideourfloatingbuddytothecoconut!Remember:itmovesfromitsownpointofview.
12
NextStep– GhostandFire
• Youhavetwooptions,eithertoaddanothercollisiondetectorforghostandfire,ortomanuallysetthethresholddistancebetweenthetwobeyondwhichyouconsiderascollision.• InPartII,weteachthelattermethod.
13
PartII:Manualadjustmentofproximity/distance
•Clickonthescene tabonthetopbarandthenfindtheproperties section.•Clickon“AddSceneProperty”.Wewilltreatthefourflamesasabloc.• Isvariable:constant field (becausethefourflamesareallweneedandunchanging)• Valuetype:Tick“isarray”.Then,clickonGallery
Class – Flames.You’llseethatthefourflamesareautomaticallytickedontheleftcolumn.• (continuedonnextpage) 14
Addingthenewproperty…
• Name:dangers• Initializer:ClickandselectCustom Array,andthenaddallfourflames.
15
Clickokwhenhappy
16
What’supnext
•AghostprocedurecalledjumpBack asitsreactionwhencollidingwithanyfire•Afunctioncalledcollide,which• returnstrueiftheghostcollideswithANYoftheflames• returnsfalseiftheghostcollideswithNONEoftheflames
•Acontinuouslistenerofwhethercollide istrue,and,iftrue,toexecutejumpBack
17
GhostprocedurejumpBack
Clickonthepolygonandfindghost.CreateanewprocedureandcallitjumpBack.Thiswillbeexecutedwhencollisionbetweenghostandfireoccurs.
18
GhostprocedurejumpBack
• Again,youcouldimprovise!
19
What’supnext
•AghostprocedurecalledjumpBack asitsreactionwhencollidingwithanyfire•Afunctioncalledcollide,which• returnstrueiftheghostcollideswithANYoftheflames• returnsfalseiftheghostcollideswithNONEoftheflames
•Acontinuouslistenerofwhethercollide istrue,and,iftrue,toexecutejumpBack
20
Scenefunctiontodeterminecollision
• Switchtothescene tab•Addnewscenefunction• Returntype:boolean (true/false)• Name:collide
•Dragina“foreachin”fromthebottomrow• Itemtype:GalleryClass>>Flames• Itemname:flame• Array:this.dangers (aswesetupearlier)
21
• Now,draginanif/else statementwithinthepreviousblock.Clickonthe“true”conditionalandselectrelational (decimal number) <,andthenanytwoplaceholderssuchas1.0and1.0.
Scenefunctiontodeterminecollision
22
Ourgoalistoturntheconditionalto…
•Goal:GhostdistancetoFlame<(ghost’sdepth/2+flame’sdepth/2)•Hencewereplacethe1.0sonbothsidesrespectively1.0<1.0•Tryfiguringoutonyourownhowtoreachthegoal,asthestepbystepprocesscouldlookintimidating.Remember! Start with theexteriorstructureandworkyourwayinlikepeelinganonion.Skiptopage31todirectlycheckyouranswer.
23
Stepbystep
•Goal:GhostdistancetoFlame<(ghost’sdepth/2+flame’sdepth/2)•Step1:1.0<1.0•Step2:GhostdistancetoFlame<(1.0+1.0)*shownonnexttwopages
24
Tip:Clickonghost,andthentheFunctionstab.You’llfindusefulelementslikegetDepth andgetDistanceTo.
25 26
Stepbystep
•Goal:GhostdistancetoFlame<(ghost’sdepth/2+flame’sdepth/2)•Step3:GhostdistancetoFlame<(1.0/2.0 +1.0/2.0)*shownonnextpage
27
•Change both 1.0 to1.0/2.0
28
Almostdone
•Goal:GhostdistancetoFlame<(ghost’sdepth/2+flame’sdepth/2)• Step4:GhostdistancetoFlame<(ghost’sdepth/2.0 +flames4’sdepth/2.0)•Click on ghostand you’ll find getDepth in its functions.Clickonanyoftheflames,say,flames4(theredfire).FindgetDepth anddragoverthesecond1.0.Weusethisflames4asaplaceholder.
29
Thefinalstepisthecore.Wewanttocheckeveryflame’scollisionstatuswiththeghost,notjustflames4.Andsoweneedtoreplacethespecificcaseofflames4withthetracerflame createdinthe“foreachin”looptocheckeveryflameinthearraydangers,whichcomprisesallfourflames.
30
Theifconditionalshouldlooklikethis:
Biggerfontinthreesegments:
31
Filloutthefunction
•Weneedtoreturnstatements.• The“returntrue”linegoesinsideoftheifstatement.Wecouldreturnapositiveanswerforcollisionprovidedthatanyofflamescollideswiththeghost.• The“returnfalse”linegoesoutsideoftheentireforeachinloop.Wecanonlyreturnthesolidanswer,thatno,theghostdoesnotcollidewithanyoftheflames,afterwehavecheckedeverysingleflame’scollisionstatuswiththeghost.
32
33
Almostdone
•AghostprocedurecalledjumpBack asitsreactionwhencollidingwithanyfire•Afunctioncalledcollide,which• returnstrueiftheghostcollideswithANYoftheflames• returnsfalseiftheghostcollideswithNONEoftheflames
•Acontinuouslistenerofwhethercollide istrue,and,iftrue,toexecutejumpBack
34
Eventlistener
• SwitchbacktotheInitializeEventListeners tab.•Wewanttheprogramtoconstantlycheckwhethertheghostisintouchwithanyoftheflames;andhaveitjumpbackifso.
35
Eventlistener
•Draginawhile fromthebottomandselecttrue.•Andthenwithinthewhile loop,draginanif blockandselecttrue.
36
Eventlistener
•Now,wewanttoconstantlycheckcollisionstatusofghostandfire.•Findthe“collide”functionunder“this”,andreplacethe“true”intheifblock.
37
Ifcollide,thenjumpBack
Clickontheghost,finditsjumpBack procedureanddragoverundertheifconditional.Everytimethatghostcollideswithanyoftheflames,itwilljumpback.
38
Playit!
• Ihavealittlefiredancethatyoucoulduseifyouwantasasceneprocedure.YoucouldfinditinghostFire_Finished.•Havefun!
39