unit4 rotations angles dynamics
play

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


  1. 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 image • Mouse follower • More dynamics: the mass-spring-damper • Bouncing Off Angles 1

  2. Slides: Unit 4: Rotations, Angles & Dynamics 3D Modelling & Animation Module F21MA Rotating and Image (or other display object) about its centre 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) 2

  3. Slides: Unit 4: Rotations, Angles & Dynamics Rotation Rotation about local centre Rotation angle measured clockwise Performing a Rotation an Object’s centre 3

  4. Slides: Unit 4: Rotations, Angles & Dynamics Rotation about its centre 1.Initial condition Rotation angle measured clockwise Rotation about its centre 1.Initial condition Rotation angle 2.Move centre to measured origin clockwise 4

  5. Slides: Unit 4: Rotations, Angles & Dynamics Rotation about its centre 1.Initial condition Rotation angle 2.Move centre to measured origin clockwise 3.Rotate Rotation about its centre 1.Initial condition Rotation angle 2.Move centre to measured origin clockwise 3.Rotate 4.Translate 5

  6. Slides: Unit 4: Rotations, Angles & Dynamics 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; } 3D Modelling & Animation Module F21MA Mouse Follower (with turning inertia) 6

  7. Slides: Unit 4: Rotations, Angles & Dynamics Requirements • Constant velocity • Inertia added to turns d Calculate Vector to Mouse • Calculate new vector to mouse d 2 = (dx, dy) • Normalise to unit length d 2 = d 2 /| d 2 | d 2 7

  8. Slides: Unit 4: Rotations, Angles & Dynamics Calculate Vector to Mouse • Calculate new vector to mouse d 2 = (dx, dy) • Normalise to unit length d 2 = d 2 /| d 2 | • Add a proportion of it to the previous unit direction vector d d d 2 Determine new vector d = (inertia) d + (1- inertia) d 2 Use new d to set angle of plane and also move it by vector d d d 2 8

  9. Slides: Unit 4: Rotations, Angles & Dynamics Apply new vector (1- inertia) d 2 (inertia) d d d 2 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; 9

  10. Slides: Unit 4: Rotations, Angles & Dynamics 3D Modelling & Animation Module F21MA More dynamics: the mass-spring-damper Mass-spring-damper • Basic 2 nd order dynamics • Applicable to lots of situations • Bouncing ball = 2 nd dynamics 10

  11. Slides: Unit 4: Rotations, Angles & Dynamics Mass-spring-damper: forces Mass: f a =ma x Damper friction: f f =-c fricion v x Spring: f s =-c spring x C spring c fricion m x measured from setpooint setpoint Mass-spring-damper: forces f a = f f + f s a x =(c fricion v x + c spring x)/m C spring c fricion m setpoint 11

  12. Slides: Unit 4: Rotations, Angles & Dynamics Mass-spring-damper: forces Previously used the simplification: v:=v+a; x:=x+v; a x =(c fricion v x + c spring x)/m C spring c fricion m setpoint 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 12

  13. Slides: Unit 4: Rotations, Angles & Dynamics Code: spring1.getForce() public function getForce():Number { return c*(setPoint - rhPosition); } m setpoint Code: mass.advance( ) public function advance(): void { ax=(f-friction*vx)/m; vx += ax; //Calculate velocity x += vx; //Calculate position } c fricion m setpoint 13

  14. Slides: Unit 4: Rotations, Angles & Dynamics 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); } 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; } 14

  15. Slides: Unit 4: Rotations, Angles & Dynamics <ns1:colourBall Effects of varying parameters 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);"/> 3D Modelling & Animation Module F21MA Bouncing Off Angles 15

  16. Slides: Unit 4: Rotations, Angles & Dynamics 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; (100, 200) 30 o 16

  17. Slides: Unit 4: Rotations, Angles & Dynamics 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 17

  18. Slides: Unit 4: Rotations, Angles & Dynamics Detect Possible Collision // 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” x1 angle y1 18

  19. Slides: Unit 4: Rotations, Angles & Dynamics Rotation & the Coord-system • In the first x 1 = r sin θ quadrant x both x 1 and y 1 are θ positive r y 1 = r cos θ ( x 1 , y 1 ) y Rotation & the Coord-system x 1 = r sin θ x 1 = r sin θ y 1 = r cos θ negative positive negative x y 1 = r cos θ θ positive r ( x 1 , y 1 ) y 19

  20. Slides: Unit 4: Rotations, Angles & Dynamics 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. Detect Possible Collision Calculate y rotated = cos(angle)*y1 - sin(angle)*x1; x1 y1 20

  21. Slides: Unit 4: Rotations, Angles & Dynamics Detect Possible Collision Note that y rotated is measured positive down the way from the bounce line x1 y positive y1 Detect Possible Collision Test y rotated if (yRotated + ball.height/2 > 0) x1 y1 21

  22. Slides: Unit 4: Rotations, Angles & Dynamics Collision Detected Test y rotated if (yRotated + ball.height/2 > 0) Collision! x1 y1 Reset ball to touch line yRotated = -ball.height / 2; x1 y1 22

  23. Slides: Unit 4: Rotations, Angles & Dynamics Calculate other rotations xRotated = cos*x1 + sin*y1; vxRotated = cos*ball.vx + sin*ball.vy; vyRotated = cos*ball.vy - sin*ball.vx; x1 x y1 y Bounce the direction of travel vyRotated *= -ball.bounceFactor; x1 y1 23

  24. Slides: Unit 4: Rotations, Angles & Dynamics 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; y1 3D Modelling & Animation Module F21MA Lab: ex4.1 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend