10
Checking for Collision by Vicki Zhang Under the direction of Professor Susan Rodger Duke University July 2017 1 Checking for Collision The goal of this mini- game is to guide the floating buddy safely to the coconut. Therefore, two detections are involved: 1. Ghost and Coconut 2. Ghost and Fire 2 Collision There are two ways of coding collision detection in Alice 3: to use the in-built “collision detection” event listener feature, or to manually specify threshold distance between 2+ objects 3 To get started Download the GhostFire_Start Alice 3 world. You will find that the objects are in place, with a ghost, four flames of different colors, and a coconut. Tip: If you want to change the color of an object, go to set up scene, click on the object, and on the right section, find its paint. 4

GhostFire Tutorial Checking for Collision

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: GhostFire Tutorial Checking for Collision

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

Page 2: GhostFire Tutorial Checking for Collision

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

Page 3: GhostFire Tutorial Checking for Collision

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

Page 4: GhostFire Tutorial Checking for Collision

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

Page 5: GhostFire Tutorial Checking for Collision

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

Page 6: GhostFire Tutorial Checking for Collision

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

Page 7: GhostFire Tutorial Checking for Collision

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

Page 8: GhostFire Tutorial Checking for Collision

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

Page 9: GhostFire Tutorial Checking for Collision

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

Page 10: GhostFire Tutorial Checking for Collision

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