animation -...

32
Piech, CS106A, Stanford University Animation Chris Piech CS106A, Stanford University This is Method Man. He is part of the Wu Tang Clan. J

Upload: others

Post on 08-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

AnimationChris Piech

CS106A, Stanford University

This is Method Man. He is part of the Wu Tang Clan. J

Page 2: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Learning Goals1. Feel more confident debugging

2. Write animated programs

Page 3: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

You will be able to write Bouncing Ball

Page 4: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Page 5: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Goal

Page 6: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Great foundation

Page 7: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Move to Center

Page 8: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Animation Loop

private void run() {// setup

while(true) {// update world

// pausepause(DELAY);

}}

Page 9: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Animation Loop

private void run() {// setup

while(true) {// update world

// pausepause(DELAY);

}}

Make all the variables you need. Add graphics to the screen.

Page 10: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Animation Loop

private void run() {// setup

while(true) {// update world

// pausepause(DELAY);

}}

The animation loop is a repetition of heartbeats

Page 11: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Animation Loop

private void run() {// setup

while(true) {// update world

// pausepause(DELAY);

}}

Each heart-beat, update the world forward one frame

Page 12: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Animation Loop

private void run() {// setup

while(true) {// update world

// pausepause(DELAY);

}}

If you don’t pause, humans won’t be able to see it

Page 13: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Move To Center

private void run() {// setupGRect r = makeRect();while(!isPastCenter(r))

// update worldr.move(1, 0);// pausepause(DELAY);

}}

Page 14: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Page 15: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Milestone #1

Page 16: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

First heartbeat

Velocity: how much the ball position changes each heartbeat

Page 17: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

First heartbeat

vx

vy

The GOval move method takes in a change in x and a change in y

Page 18: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Second heartbeat

vx

vy

Page 19: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Third heartbeat

vx

vy

Page 20: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

What happens when we hit a wall?

Page 21: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

We have this velocity

vy

vx

Page 22: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Our new velocity

When reflecting vertically: vy = -vy

vxvy

Page 23: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Seventh heartbeat

vxvy

Page 24: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Eighth heartbeat

vxvy

Page 25: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Ninth heartbeat

vxvy

Page 26: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

We want this!

Page 27: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

vxvy

This was our old velocity

Page 28: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

When reflecting horizontally: vx = -vx

vx

vy

This is our new velocity

Page 29: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

When reflecting horizontally: vx = -vx

vx

vy

Tenth heartbeat

Page 30: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Bouncing Ball

Page 31: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

A Sticky Situation

This is our new velocity

The ball is bellow the bottom so we reverse its vy

The ball is above the bottom so we reverse its vy

Page 32: Animation - web.stanford.eduweb.stanford.edu/.../cs106a.1186/lectures/9-Animation/9-Animation.… · Animation Chris Piech CS106A, Stanford University This is Method Man. He is part

Piech, CS106A, Stanford University

Learning Goals1. Feel more confident writing methods

2. Write animated programs