360 likes | 515 Views
Shared Data. Gail Carmichael gail_c@scs.carleton.ca. Please Download:. www.scs.carleton.ca /~gail_c/comp1005/Robots1.zip www.scs.carleton.ca/~gail_c/comp1005/Birds1.zip. About Me. Sharing Resources. http://www.flickr.com/photos/marcosbessa/6260425373/. familyCar = new Car(“black”);
E N D
Shared Data Gail Carmichael gail_c@scs.carleton.ca
Please Download: www.scs.carleton.ca/~gail_c/comp1005/Robots1.zip www.scs.carleton.ca/~gail_c/comp1005/Birds1.zip
Sharing Resources http://www.flickr.com/photos/marcosbessa/6260425373/
familyCar = new Car(“black”); mom.car = familyCar; dad.car = familyCar; teen.car = familyCar;
teen.car.crash(); // Now the family car is not drivable! if (!mom.car.drivable()) mom.isStranded(); if (!dad.car.drivable()) dad.isStranded();
Examine code in Robots1.pde www.scs.carleton.ca/~gail_c/comp1005/Robots1.zip
Beacon Beacon Beacon Robot Robot Robot
Let’s add movement (demo first)
Step 1: Trigonometry to move forward in current direction x = x + speed * cos(direction) y = y + speed * sin(direction)
Step 2: Change direction to head for beacon
Step 3: Add random error for realism amountToTurn = θ + random(θ/4) - (θ/4)/2
Try implementing the move() method… x = x + speed * cos(direction) y = y + speed * sin(direction) crossProduct = (r'x- rx)(gy- ry) - (r'y- ry)(gx- rx) positive → left hand turn negative → right hand turn amountToTurn= θ + random(θ/4) - (θ/4)/2
Let’s add drag-and-drop for the beacons (demo first) www.scs.carleton.ca/~gail_c/comp1005/Robots2.zip
What if we want multiple robots per beacon? www.scs.carleton.ca/~gail_c/comp1005/Robots3.zip
Beacon Beacon Robot Robot Robot
Please Download: www.scs.carleton.ca/~gail_c/comp1005/Birds1.zip Study the code, run the program.
How is the data in the program set up? Pros and cons?
Implement sharing so each bird points to the same set of animation frames.
Person molly = new Person(“Molly”); Person henry = new Person(“Henry”); Person[] personList = new Person[2]; personList[0] = molly; personList[1] = henry;
Person[] personListCopy = new Person[2]; for (inti=0; i < personList.length; i++) { personListCopy[i] = personList[i]; }
personListCopy[0].name = “Mallory”; // Prints Mallory System.out.println(personList[0].name);
Person[] personListCopy = new Person[2]; for (inti=0; i < personList.length; i++) { personListCopy[i] = new Person(personList[i].name); }
personListCopy[1].name = “Harry”; // Still prints Henry since it’s unchanged System.out.println(personList[1].name);
Shallow Copies vs Deep Copies
Thanks! Gail Carmichael gail_c@scs.carleton.ca