Date post: | 16-Jan-2016 |
Category: |
Documents |
Upload: | alexandra-malone |
View: | 216 times |
Download: | 0 times |
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”);
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();
Robots Example
Examine code in
Robots1.pde
www.scs.carleton.ca/~gail_c/comp1005/Robots1.zip
Robot
Robot
Robot
Beacon
Beacon
Beacon
Pros and cons?
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
crossProduct = (r'x - rx)(gy - ry) - (r'y - ry)(gx - rx)
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
Robot
Robot
Robot
Beacon
Beacon
Implement multiple robots per beacon.
Bird Animation Example
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?
Where can data be shared?
Implement sharing so each bird points to the same set of animation frames.
Separating Shared Data
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 (int i=0; i < personList.length; i++){ personListCopy[i] = personList[i];}
personListCopy[0].name = “Mallory”;
// Prints MallorySystem.out.println(personList[0].name);
Person[] personListCopy = new Person[2];
for (int i=0; i < personList.length; i++){ personListCopy[i] = new Person(personList[i].name);}
personListCopy[1].name = “Harry”;
// Still prints Henry since it’s unchangedSystem.out.println(personList[1].name);
Shallow Copies
vs
Deep Copies