CS3505/5020 Software Practice II Teams reminder Finish rotation - - PowerPoint PPT Presentation

cs3505 5020 software practice ii
SMART_READER_LITE
LIVE PREVIEW

CS3505/5020 Software Practice II Teams reminder Finish rotation - - PowerPoint PPT Presentation

CS3505/5020 Software Practice II Teams reminder Finish rotation example Sound CS 3505 L05 - 1 Upcoming projects Project 4 requires teams of two You should have your team formed by the middle of next week. No individual solutions


slide-1
SLIDE 1

CS3505/5020 Software Practice II

Teams reminder Finish rotation example Sound

CS 3505 L05 - 1

slide-2
SLIDE 2

Upcoming projects

Project 4 requires teams of two

– You should have your team formed by the middle

  • f next week.

– No individual solutions accepted! – I will post a link to help uncommitted students pair up.

slide-3
SLIDE 3

Transformations in XNA

Classroom demos

– Consider a rotating flipper again:

  • My sprite
  • A reasonable representation
  • (not quite rotated the same as mine)
slide-4
SLIDE 4

Transformations in XNA

Classroom demos

– Consider a rotating flipper again: » Use circles and line segments to define boundaries

slide-5
SLIDE 5

Transformations in XNA

Classroom demos

– Consider a rotating flipper again: » Define elements using a sprite origin and sprite coordinate system » Actual world/screen coordinates irrelevant for now

  • x
  • -y
slide-6
SLIDE 6

Transformations in XNA

Classroom demos

– Consider a rotating flipper again: » Define elements using a sprite origin and sprite coordinate system » If using my line segment code, define line points in clockwise direction around outside of shape

  • x
  • -y
  • (0, -10)
  • (35, -2)
slide-7
SLIDE 7

Transformations in XNA

Classroom demos

– Consider a rotating flipper again: » Define elements using a sprite origin and sprite coordinate system » Etc.

  • x
  • -y
  • (35, 5) radius=7
slide-8
SLIDE 8

Transformations in XNA

Classroom demos

– In preparation for drawing the sprite » Find the rotation origin in the graphic (using the graphic file’s coordinate system)

  • My sprite
  • spriteOrigin = (12, 13)
  • (0, 0)
slide-9
SLIDE 9

Transformations in XNA

Classroom demos

– In preparation for drawing the sprite » Determine the screen location, scale (usually 1.0), and rotation (in radians) that you want to use for drawing the sprite

  • Examples only:
  • screenLoc = (250, 575)
  • spriteScale = 1
  • spriteRot = -0.25
slide-10
SLIDE 10

Transformations in XNA

Classroom demos

– Draw it using the overloaded SpriteBatch draw:

spriteBatch.Draw ( leftFlipperTexture, spritePos, null, Color.WHITE, spriteRot, spriteOrigin, spriteScale, SpriteEffects.NONE, 0);

slide-11
SLIDE 11

Computing collisions

» Sprite defined in local coordinate system, drawn sprite may be translated, rotated, and scaled on the screen. » You need to convert each line or circle from sprite coordinates to screen/world coordinates

Transformations in XNA

  • x
  • y
  • (0, 10)
slide-12
SLIDE 12

Computing collisions

» Simple: Create a transformation matrix to perform the coordinate space conversion:

Transformations in XNA

Matrix transform; transform = Matrix.Identity; transform = transform * Matrix.CreateRotationZ(spriteRot); transform = transform * Matrix.CreateScale(spriteScale); transform = transform * Matrix.CreateTranslation(spritePos.X, spritePos.Y, 0);

slide-13
SLIDE 13

Computing collisions

» Build new lines / circles by taking the sprite lines/circles and converting the coordinates » Transform each point using the transformation matrix to convert it to screen space. » Hint – you will need to convert your 2D points to 3D points to transform them, then convert them back to 2D

Transformations in XNA

Vector3 screenLoc, spriteLoc = someSpriteLoc; screenLoc = Vector3.Transform(spriteLoc, transform);

slide-14
SLIDE 14

Computing collisions

» If your original boundaries matched your original sprite, and if you constructed the transform correctly, the converted boundaries will match the transformed sprite! Yay. » Note – my numbers above are guesses.

Transformations in XNA

  • x
  • y

(35, -2) -> (284, 574) (0, -10) -> (252, 568)

slide-15
SLIDE 15

Remember to build copies of your line

segments

– Don’t modify the originals, you’ll need them for the next transform. – Don’t attempt to make incremental transforms in screen coordinate space » Floating point errors will quickly accumulate destroying your result » Always re-transform from the original sprite space to the screen/world space

Transformations in XNA

slide-16
SLIDE 16

The ‘net’ velocity between the ball and the flipper

should be used in reflections.

Since the flipper is anchored (stationary) at one end,

the velocity of the flipper varies

How to ‘hit’ the ball with the flipper

slide-17
SLIDE 17

First, compute the point of impact. Second, compute the velocity at that point. Third, subtract it from the ball’s velocity

How to ‘hit’ the ball with the flipper

slide-18
SLIDE 18

(Remember – use screen space coordinates

– use your transformation.)

Point of impact for a line:

slide-19
SLIDE 19

Compute vectors V and N using coordinates

  • f line and circle

Point of impact for a line:

  • V
  • N
  • D
slide-20
SLIDE 20

Normalize N, then find out how much of V is

in N’s direction. Compute D.

|D| = Ň•V D = Ň * |D|

Point of impact for a line:

  • V
  • Ň
  • D
slide-21
SLIDE 21

Add D to the starting point to get the point of

impact.

Point of impact for a line:

  • V
  • N
  • D
  • D

P

slide-22
SLIDE 22

(Nearly) every point on the flipper is moving

differently around the axis of rotation. Compute the velocity at P.

Velocity at that point:

P

slide-23
SLIDE 23

Find how far P is from the axis of rotation.

Call this distance k.

Velocity at that point:

P k

slide-24
SLIDE 24

The perimeter of the circle is 2πk pixels.

Velocity at that point:

P k 2πk

slide-25
SLIDE 25

Velocity is pixels per second. Determine

your radial velocity in radians, something like -π/4 radians/second.

Velocity at that point:

P k 2πk π/4

slide-26
SLIDE 26

Use unit analysis to do the rest!

Velocity at that point:

P k 2πk pixels 2π radians π/4 radians / second

slide-27
SLIDE 27

velocity = (pixels/radian) * (radians/second) velocity = pixels / second

Velocity at that point:

P k 2πk pixels 2π radians π/4 radians / second

slide-28
SLIDE 28

We know how fast the point is moving, next

compute the direction. Use the vector K that you created to compute k. |K| = k, right?

Direction at that point:

P k 2πk pixels 2π radians π/4 radians / second K

slide-29
SLIDE 29

K is just some pair (Δx, Δy). The point is

moving perpendicular to K, either (-Δy, Δx)

  • r (Δy, -Δx) depending on rotation direction.

Direction at that point:

P 2πk pixels 2π radians π/4 radians / second K K’ = (Δy, -Δx)

slide-30
SLIDE 30

K’ is our velocity vector for the point.

Normalize it (to make it’s length 1) and multiply it by our speed.

Velocity vector at that point:

P K K’ = (Δy, -Δx)

slide-31
SLIDE 31

Done! Vel vector at P = normalize(K’) * velocity

Velocity vector at that point:

P K K’ = (Δy, -Δx)

slide-32
SLIDE 32

What about collisions with moving circles?

– Same basic ideas, we need the impact point, its velocity, and its normal vector.

Issues

slide-33
SLIDE 33

What about glancing blows?

– Need to adjust for collision angle not matching normal angle. (Most of the time you won’t see this, but once in a while things will look strange.)

Issues

slide-34
SLIDE 34

What about glancing blows?

– Simple enough – multiply the velocity vector by the cosine of the angle between velocity normal with the impact normal. (Use a dot product on normalized vectors!)

Issues

  • ө
slide-35
SLIDE 35

Sound in XNA

I will do an in-class example of this

– If you missed class, use the on-line tutorials – You may want to look up how to bend pitch and volume.