21. PV Intro

Slides with introduction to position-velocity struct exercises.

Pick a model

  • A ball moves around the screen.
  • It keeps going in the direction it starts.
  • What could the model be?

Define the model

Keep the names short; we will type them a lot.


Define the model

Keep the names short; we will type them a lot.

(define-struct pv (pos vel))

Example needed

Create a pv structure called ex1.

  • Position (100,50)
  • Velocity (-5,8)

Sketch

(make-pv (make-posn 50 200)
         (make-posn 10 -30))

update-pos

  • Write update-pos: pv -> pv.
  • Use velocity to change position, one time step.

scale-velocity

  • scale-velocity: pv(orig) num(scale) -> pv
  • Multiply both x and y components of velocity of orig by scale.

turn-180

What would this mean? Sketch first.


Bouncing off walls 1

Should you bounce off of a wall?

Sketch three true and three false.

should-bounce-r? : pv num(xwall) -> boolean

The wall on the right is at x=xwall.


Bouncing off walls 2

should-bounce-r? : pv num(xwall) -> boolean

The wall on the right is at x=xwall.

Now actually write the function.


Bouncing off walls 3

bounce-r-velocity: pv -> posn(new-velocity)

Given that you bounced, what should your new velocity be?

Draw a sketch!

Last modified August 18, 2023: 2022-2023 End State (7352e87)