Unit4: Rotations, Angles & Dynamics Mike Chantler, 31/8/2008 - - PDF document

unit4 rotations angles dynamics
SMART_READER_LITE
LIVE PREVIEW

Unit4: Rotations, Angles & Dynamics Mike Chantler, 31/8/2008 - - PDF document

Slides: Unit 4: Rotations, Angles & Dynamics 3D Modelling & Animation Module F21MA Unit4: Rotations, Angles & Dynamics Mike Chantler, 31/8/2008 P35 image from http://www.military-aircraft.org.uk Unit contents Rotating an


slide-1
SLIDE 1

Slides: Unit 4: Rotations, Angles & Dynamics 1

Unit4: Rotations, Angles & Dynamics

Mike Chantler, 31/8/2008 3D Modelling & Animation Module F21MA

P35 image from http://www.military-aircraft.org.uk

Unit contents

  • Rotating an image
  • Mouse follower
  • More dynamics:

the mass-spring-damper

  • Bouncing Off Angles
slide-2
SLIDE 2

Slides: Unit 4: Rotations, Angles & Dynamics 2

Rotating and Image (or other display object) about its centre

3D Modelling & Animation Module F21MA

Coordinates

  • Position (x,y) of a

component is

– measured w.r.t. the top- left corner of its container, – relative the component’s top-left corner. Arrow(x,y) = (0,0)

slide-3
SLIDE 3

Slides: Unit 4: Rotations, Angles & Dynamics 3

Rotation

Rotation angle measured clockwise Rotation about local centre

Performing a Rotation an Object’s centre

slide-4
SLIDE 4

Slides: Unit 4: Rotations, Angles & Dynamics 4

Rotation about its centre

Rotation angle measured clockwise

1.Initial condition

Rotation about its centre

Rotation angle measured clockwise

1.Initial condition 2.Move centre to

  • rigin
slide-5
SLIDE 5

Slides: Unit 4: Rotations, Angles & Dynamics 5

Rotation about its centre

Rotation angle measured clockwise

1.Initial condition 2.Move centre to

  • rigin

3.Rotate

Rotation about its centre

Rotation angle measured clockwise

1.Initial condition 2.Move centre to

  • rigin

3.Rotate 4.Translate

slide-6
SLIDE 6

Slides: Unit 4: Rotations, Angles & Dynamics 6

Code

private function updateMatrix():void{

var cmt:Matrix = new Matrix(); //Move centre of object to (0,0) cmt.translate(-width/2, -height/2); //Rotate clockwise cmt.rotate(Math.PI * (angle / 180)); //Translate back to desired position cmt.translate(xCentre, yCentre); this.transform.matrix=cmt;

}

Mouse Follower (with turning inertia)

3D Modelling & Animation Module F21MA

slide-7
SLIDE 7

Slides: Unit 4: Rotations, Angles & Dynamics 7

Requirements

  • Constant velocity
  • Inertia added to turns

d

Calculate Vector to Mouse

  • Calculate new vector to mouse d2 = (dx, dy)
  • Normalise to unit length d2 = d2/| d2 |

d2

slide-8
SLIDE 8

Slides: Unit 4: Rotations, Angles & Dynamics 8

Calculate Vector to Mouse

  • Calculate new vector to mouse d2 = (dx, dy)
  • Normalise to unit length d2 = d2/| d2 |
  • Add a proportion of it to the previous unit direction

vector d

d d2

Determine new vector

d = (inertia)d + (1- inertia)d2 Use new d to set angle of plane and also move it by vector d

d d2

slide-9
SLIDE 9

Slides: Unit 4: Rotations, Angles & Dynamics 9

Apply new vector

d d2

(inertia)d (1- inertia)d2

Code

//Calculate unit vector from object centre to mouse ss=Math.sqrt(dx*dx + dy*dy); dx=dx/ss; dy=dy/ss; //Add a proportion (1-inertia) of this to the current direction vector dx2=dx2*inertia + dx*(1-inertia); dy2=dy2*inertia + dy*(1-inertia); ss=Math.sqrt(dx2*dx2 + dy2*dy2); dx2=dx2/ss; dy2=dy2/ss; angle=Math.atan2(dy2, dx2)*180/Math.PI; //Rotate and move plane rotateAboutCentre(angle); xCentre+=speed*dx2; yCentre+=speed*dy2;

slide-10
SLIDE 10

Slides: Unit 4: Rotations, Angles & Dynamics 10

More dynamics: the mass-spring-damper

3D Modelling & Animation Module F21MA

Mass-spring-damper

  • Basic 2nd order dynamics
  • Applicable to lots of situations
  • Bouncing ball = 2nd dynamics
slide-11
SLIDE 11

Slides: Unit 4: Rotations, Angles & Dynamics 11

m

Mass-spring-damper: forces

Mass: fa=max Damper friction: ff=-cfricionvx Spring: fs=-cspringx

Cspring

cfricion

setpoint

x measured from setpooint

m

Mass-spring-damper: forces

fa= ff + fs ax=(cfricionvx + cspringx)/m

Cspring

cfricion

setpoint

slide-12
SLIDE 12

Slides: Unit 4: Rotations, Angles & Dynamics 12

m

Mass-spring-damper: forces

ax=(cfricionvx + cspringx)/m

Cspring

cfricion

setpoint

Previously used the simplification: v:=v+a; x:=x+v;

Code: main.mxml

private function advance(e:Event):void{ //Pass force from spring to mass Mass.setForce(spring1.getForce()); //Advance position of mass one frame Mass.advance(); //Set spring's right-end to follow mass spring1.setPosition(Mass.x); }

m

slide-13
SLIDE 13

Slides: Unit 4: Rotations, Angles & Dynamics 13

Code: spring1.getForce()

public function getForce():Number { return c*(setPoint - rhPosition); }

m

setpoint

Code: mass.advance( )

m

setpoint

public function advance():void { ax=(f-friction*vx)/m; vx += ax; //Calculate velocity x += vx; //Calculate position }

cfricion

slide-14
SLIDE 14

Slides: Unit 4: Rotations, Angles & Dynamics 14

Code

private function advance(e:Event):void{ //Pass force from spring to mass Mass.setForce(spring1.getForce()); //Advance position of mass one frame Mass.advance(); //Set spring's right-end to follow mass spring1.setPosition(Mass.x); }

Main.mxml

X and Y: ball2ndOrder.mxml

public function advance(e:Event):void { //Calculate forces in x and y directions ax=(springC*(setPointX-x) -vx*frictionC)/mass; ay=(springC*(setPointY-y) -vy*frictionC)/mass; vx += ax; vy += ay; //Calculate velocity x += vx; y += vy; //Calculate position } private function mouseMoveHandler(e:MouseEvent): setPointX=parent.mouseX-width/2; setPointY=parent.mouseY-height/2; }

slide-15
SLIDE 15

Slides: Unit 4: Rotations, Angles & Dynamics 15

Effects of varying parameters

<ns1:colourBall frictionC="0.1" red="0" x="184" y="207.55" width="50" height="50" id="b1" mouseDown="mouseDownHandler(b1.mass, b1.springC, b1.frictionC);"/> <ns1:colourBall blue="0" x="224.5" y="67" width="100" height="100" id="b2" mouseDown="mouseDownHandler(b2.mass, b2.springC, b2.frictionC);"/> <ns1:colourBall springC="0.3" frictionC="0.05" blue="0" green="0" x="22" y="192" width="100" height="100" id="b3" mouseDown="mouseDownHandler(b3.mass, b3.springC, b3.frictionC);"/> <ns1:colourBall x="64" y="354" width="25" height="25" id="b4" mouseDown="mouseDownHandler(b4.mass, b4.springC, b4.frictionC);"/> <ns1:colourBall springC="0.3" frictionC="1.5" green="0" x="174.5" y="279" width="100" height="100" id="b5" mouseDown="mouseDownHandler(b5.mass, b5.springC, b5.frictionC);"/>

Bouncing Off Angles

3D Modelling & Animation Module F21MA

slide-16
SLIDE 16

Slides: Unit 4: Rotations, Angles & Dynamics 16

Outline

  • Create the line
  • Detect if x in bounding box
  • Rotate coordinates to give simple

horizontal line case

  • Implement bounce
  • Rotate back

Creating the Line

<mx:HRule x="100" y="200" id="line1" width="300"/> line1.rotation = 30;

30o

(100, 200)

slide-17
SLIDE 17

Slides: Unit 4: Rotations, Angles & Dynamics 17

Detect Possible Collision

var bounds:Rectangle = line1.getBounds(this); If (ball.x-ball.width > bounds.left && ball.x < bounds.right)

Detect Possible Collision

// get position of ball centre relative to line x1 = ball.x + halfBallWidth - line1.x; y1 = ball.y + halfBallHeight - line1.y;

x1 y1

slide-18
SLIDE 18

Slides: Unit 4: Rotations, Angles & Dynamics 18

Detect Possible Collision

x1 y1

// get position of ball, relative to line x1 = ball.x-line1.x; y1 = ball.y-line1.y;

x1 y1

Detect Possible Collision

Rotate anticlockwise by “angle”

angle

slide-19
SLIDE 19

Slides: Unit 4: Rotations, Angles & Dynamics 19

Rotation & the Coord-system

θ (x1,y1)

x y

x1 = rsinθ y1 = rcosθ r

  • In the first

quadrant both x1 and y1 are positive

Rotation & the Coord-system

θ (x1,y1)

x y

x1 = rsinθ positive y1 = rcosθ

positive

r x1 = rsinθ negative y1 = rcosθ

negative

slide-20
SLIDE 20

Slides: Unit 4: Rotations, Angles & Dynamics 20

Notes on Implementation

  • flash.geom.Point class

Point object: a location (x, y) e.g.

import flash.geom.Point; private var point2:Point = new Point(6, 8);

Its not all that great for us – but its what I’d build upon to create a point class for representing positions, velocities, and accelerations. I’d add better polar, rotation and other transformation functions.

x1 y1

Detect Possible Collision

Calculate yrotated

= cos(angle)*y1 - sin(angle)*x1;

slide-21
SLIDE 21

Slides: Unit 4: Rotations, Angles & Dynamics 21

x1 y1

Detect Possible Collision

y positive

Note that yrotated is measured positive down the way from the bounce line

x1 y1

Detect Possible Collision

Test yrotated

if(yRotated + ball.height/2 > 0)

slide-22
SLIDE 22

Slides: Unit 4: Rotations, Angles & Dynamics 22

x1 y1

Collision Detected

Test yrotated

if(yRotated + ball.height/2 > 0)

Collision!

x1 y1

Reset ball to touch line

yRotated = -ball.height / 2;

slide-23
SLIDE 23

Slides: Unit 4: Rotations, Angles & Dynamics 23

x1 y1

Calculate other rotations

xRotated = cos*x1 + sin*y1; vxRotated = cos*ball.vx + sin*ball.vy; vyRotated = cos*ball.vy - sin*ball.vx;

y x

x1 y1

Bounce the direction of travel

vyRotated *= -ball.bounceFactor;

slide-24
SLIDE 24

Slides: Unit 4: Rotations, Angles & Dynamics 24

y1

Rotate everything back

x1 = cos * xRotated - sin * yRotated; y1 = cos * yRotated + sin * xRotated; ball.vx = cos * vxRotated - sin * vyRotated; ball.vy = cos * vyRotated + sin * vxRotated; ball.x = line1.x + x1; ball.y = line1.y + y1;

Lab: ex4.1

3D Modelling & Animation Module F21MA

slide-25
SLIDE 25

Slides: Unit 4: Rotations, Angles & Dynamics 25

angleBounceMain: find two bugs and fix

Lab: ex4.2

3D Modelling & Animation Module F21MA

slide-26
SLIDE 26

Slides: Unit 4: Rotations, Angles & Dynamics 26

Create a Test

  • For a stationary ball and a draggable ball

if(blueBall.hitTestObject(blackBall)) {…….

Perform hitTest in event handler for Event.ENTER_FRAME

End

3D Modelling & Animation Module F21MA