TRIGONOMETRY FOR ANIMATION



Similar documents
Solutions to Exercises, Section 5.1

With the Tan function, you can calculate the angle of a triangle with one corner of 90 degrees, when the smallest sides of the triangle are given:

Solving Simultaneous Equations and Matrices

Angles and Quadrants. Angle Relationships and Degree Measurement. Chapter 7: Trigonometry

Trigonometry Review with the Unit Circle: All the trig. you ll ever need to know in Calculus

Algebra Geometry Glossary. 90 angle

Trigonometry Hard Problems

How to Graph Trigonometric Functions

Trigonometric Functions and Triangles

Trigonometry. An easy way to remember trigonometric properties is:

PHYSICS 151 Notes for Online Lecture #6

Introduction Assignment

Parallel and Perpendicular. We show a small box in one of the angles to show that the lines are perpendicular.

Chapter 8 Geometry We will discuss following concepts in this chapter.

Geometry Notes PERIMETER AND AREA

Higher Education Math Placement

Trigonometry for AC circuits

CSU Fresno Problem Solving Session. Geometry, 17 March 2012

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

Algebra. Exponents. Absolute Value. Simplify each of the following as much as possible. 2x y x + y y. xxx 3. x x x xx x. 1. Evaluate 5 and 123

Section 1.1. Introduction to R n

Introduction and Mathematical Concepts

SOLVING TRIGONOMETRIC EQUATIONS

Dear Accelerated Pre-Calculus Student:

Extra Credit Assignment Lesson plan. The following assignment is optional and can be completed to receive up to 5 points on a previously taken exam.

Advanced Math Study Guide

6. LECTURE 6. Objectives

Additional Topics in Math

South Carolina College- and Career-Ready (SCCCR) Pre-Calculus

An Application of Analytic Geometry to Designing Machine Parts--and Dresses

New York State Student Learning Objective: Regents Geometry

Grade 7 & 8 Math Circles Circles, Circles, Circles March 19/20, 2013

Biggar High School Mathematics Department. National 5 Learning Intentions & Success Criteria: Assessing My Progress

Right Triangles 4 A = 144 A = A = 64

Algebra and Geometry Review (61 topics, no due date)

Right Triangles A right triangle, as the one shown in Figure 5, is a triangle that has one angle measuring

3.2 Sources, Sinks, Saddles, and Spirals

ANALYTICAL METHODS FOR ENGINEERS

Mathematics on the Soccer Field

The GED math test gives you a page of math formulas that

Expression. Variable Equation Polynomial Monomial Add. Area. Volume Surface Space Length Width. Probability. Chance Random Likely Possibility Odds

4.3 & 4.8 Right Triangle Trigonometry. Anatomy of Right Triangles

6.1 Basic Right Triangle Trigonometry

Chapter 5: Trigonometric Functions of Angles

+ 4θ 4. We want to minimize this function, and we know that local minima occur when the derivative equals zero. Then consider

MATH STUDENT BOOK. 8th Grade Unit 6

Estimated Pre Calculus Pacing Timeline

13. Write the decimal approximation of 9,000,001 9,000,000, rounded to three significant

Trigonometric Functions: The Unit Circle

In mathematics, there are four attainment targets: using and applying mathematics; number and algebra; shape, space and measures, and handling data.

Algebra 1 Course Title

PRE-CALCULUS GRADE 12

Exact Values of the Sine and Cosine Functions in Increments of 3 degrees

alternate interior angles

Overview of the Adobe Flash Professional CS6 workspace

DEFINITIONS. Perpendicular Two lines are called perpendicular if they form a right angle.

INVERSE TRIGONOMETRIC FUNCTIONS. Colin Cox

If A is divided by B the result is 2/3. If B is divided by C the result is 4/7. What is the result if A is divided by C?

Trigonometry LESSON ONE - Degrees and Radians Lesson Notes

Objectives After completing this section, you should be able to:

Common Core Unit Summary Grades 6 to 8

Geometry Notes RIGHT TRIANGLE TRIGONOMETRY

Scientific Graphing in Excel 2010

Functions and their Graphs

Geometry and Measurement

Vector Math Computer Graphics Scott D. Anderson

MovieClip, Button, Graphic, Motion Tween, Classic Motion Tween, Shape Tween, Motion Guide, Masking, Bone Tool, 3D Tool

SketchUp Instructions

Angles that are between parallel lines, but on opposite sides of a transversal.

G C.3 Construct the inscribed and circumscribed circles of a triangle, and prove properties of angles for a quadrilateral inscribed in a circle.

1 TRIGONOMETRY. 1.0 Introduction. 1.1 Sum and product formulae. Objectives

Smart Board Notebook Software A guide for new Smart Board users

One advantage of this algebraic approach is that we can write down

(1.) The air speed of an airplane is 380 km/hr at a bearing of. Find the ground speed of the airplane as well as its

NASA Explorer Schools Pre-Algebra Unit Lesson 2 Student Workbook. Solar System Math. Comparing Mass, Gravity, Composition, & Density

Law of Cosines. If the included angle is a right angle then the Law of Cosines is the same as the Pythagorean Theorem.

Getting Started in Tinkercad

Figure 3.5: Exporting SWF Files

TRIG FOR COMPUTER GRAPHICS

7.2 Quadratic Equations

Exam 1 Sample Question SOLUTIONS. y = 2x

Flash MX 2004 Animation Lesson

SAT Subject Math Level 2 Facts & Formulas

ENGINEERING COUNCIL DYNAMICS OF MECHANICAL SYSTEMS D225 TUTORIAL 1 LINEAR AND ANGULAR DISPLACEMENT, VELOCITY AND ACCELERATION

ab = c a If the coefficients a,b and c are real then either α and β are real or α and β are complex conjugates

TRIGONOMETRY Compound & Double angle formulae

Using Excel to Execute Trigonometric Functions

Solutions to old Exam 1 problems

Plotting and Adjusting Your Course: Using Vectors and Trigonometry in Navigation

Figure 1.1 Vector A and Vector F

Geometry Enduring Understandings Students will understand 1. that all circles are similar.

How To Solve The Pythagorean Triangle

Pre Calculus Math 40S: Explained!

Unit 4 Practice Test: Rotational Motion

Curriculum Map by Block Geometry Mapping for Math Block Testing August 20 to August 24 Review concepts from previous grades.

Working With Animation: Introduction to Flash

Triangle Trigonometry and Circles

Section 7.1 Solving Right Triangles

Graphing Trigonometric Skills

Transcription:

TRIGONOMETRY FOR ANIMATION

What is Trigonometry? Trigonometry is basically the study of triangles and the relationship of their sides and angles. For example, if you take any triangle and make one of the angles larger, the side opposite of that angle will get longer (assuming the other two sides stay the same length). Also, the other two angles will get smaller. A specific type of triangle has one of its angles equal to exactly 90 degrees. This is called a right triangle and is indicated by a little square in the corner of that angle. It happens that in a right triangle, the various relationships are far simpler and quite easy to figure out with some basic formulas.

Angles An angle is simply the shape formed by two intersecting lines, or the space in between those lines. Two intersecting lines will form four angles.

Radians and Degrees The two major systems for measuring angles are degrees and radians. You are probably familiar with degrees. The system of 360 degrees in a circle has become a part of our culture. But it turns out that computers have a lot more affinity for radians when it comes to talking about angles. So, like it or not, you need to know about radians.

Radians and Degrees A radian is equal to approximately 57.2958 degrees. A full circle, or 360 degrees, works out to 6.2832 radians. Still not making any sense? Well, remember pi - that symbol, π? That is equal to about 3.1416, meaning that a circle (6.2832 radians) measures exactly 2 pi. You ll get used to thinking of 360 degrees as 2 pi, 180 degrees as pi, 90 degrees as pi/2 and so on.

Radians and Degrees

Radians and Degrees you ll be using radians from here on out then get used to it and live happily ever after. But you re going to encounter many situations where you ll need to use degrees with radians. For example, say you have a vehicle that needs to turn in the direction of its motion. If you figure out the motion using trig, the angle you get will be in radians, but to rotate the vehicle, you need degrees. Here are the formulas:

Flash s Coordinate System The most common 2D coordinate systems signify horizontal measurements with x and vertical measurements with y.

Flash s Coordinate System Flash, however, is based on a video screen coordinate system, where 0, 0 is at the top left

Flash s Coordinate System Let s talk about measuring angles. In most systems, angles are measured counterclockwise, with 0 degrees being shown as a line extending into the positive x axis.

Flash s Coordinate System Again, Flash has it backward.

Triangle Sides Now talking about right triangles, where one of the angles is 90 degrees. The two sides that touch the 90-degree angle are called legs. The opposite side is called the hypotenuse. The hypotenuse is always the longest side.

Trig Functions The interesting thing about triangles is the relationships between the measurements of the sides and the measurements of the angles. ActionScript has trig functions for calculating the various triangle relationships Sine Cosine Tangent Arcsine Arccosine Arctangent

Sine The sine of an angle is the ratio of the angle s opposite leg to the hypotenuse. In ActionScript, you can use the function Math.sin(angle). For example, the sine of 30 degrees. The opposite leg has a measurement of 1, and the hypotenuse has a measurement of 2. The ratio is thus ½ or 0.5.

Sine You can test this in Flash like so: That trace out which is not even close to 0.5 Can you spot the error?

Sine We forgot to convert to radians. Here s the corrected code, with the conversion: Now let s move it into the world of the Flash coordinate system. So, we say that the sine of 30 degrees is 0.5.

Cosine You can access cosine in Flash with Math.cos(angle). Cosine is defined as the ratio of the adjacent leg of an angle to the hypotenuse. The cosine of -30 degrees is thus 1.73/2, or 0.865.

Cosine Everything has been taken from the lower-left angle. What if you looked at things from the viewpoint of the topright angle? You d need to reorient the triangle so that the angle in question aligns with the coordinate system the sine of 60 degree is its opposite leg over the hypotenuse, or 1.73/2 (0.865). And the cosine is the adjacent over the hypotenuse, 1/2, or 0.5.

Tangent This is the relationship of the opposite leg to the adjacent leg. The ratio works out to 1/1.73, or 0.578. For more accuracy, check it directly in Flash:

Arcsine and Arccosine All these do is the reverse of sine and cosine. In other words, you feed in a ratio, and you get back an angle (in radians). The ActionScript functions are Math.asin(ratio) and Math.acos(ratio). OK, you learned that the sine of 30 degrees is 0.5. Thus, the arcsine of 0.5 should be 30 degrees and the cosine of 30 degrees is roughly 0.865. Try these

Arctangent Arctangent is the opposite of tangent. For example, the tangent of 30 degrees is 0.577. Try this you have another function to check arctangent. But why?

Arctangent Four different triangles: A, B, C, and D. Triangles A and B have a positive x value Triangles C and D extend into negative x dimensions.

Arctangent triangles A and D are in the negative y space, while triangles B and C have positive y measurements. So, for the ratios of the four inner angles, you get this: A: 1/2 or 0.5 B: 1/2 or 0.5 C: 1/ 2 or 0.5 D: 1/ 2 or 0.5 So, say you divide your opposite leg by your adjacent leg and come up with a ratio of 0.5. You feed that in with Math.atan(0.5), convert it to degrees, and you get approximately 26.57. But which triangle are you talking about now: B or D?

Arctangent Welcome Math.atan2(y, x). This function takes two values: the measurement of the opposite side and the measurement of the adjacent side. Go ahead and try this This should give you the angle, 26.565051177078, which is correct for triangle B

Arctangent Try another This gives you the result of -153.434948822922.

Rotation Here is the challenge: You want to rotate a sprite or movie clip so that it always points to the mouse. Rotation is a very useful tool. It can be used in games, mouse trailers, those eyes that follow the mouse around the screen, interface elements, and so on. First, you need a something to rotate. This will be a sprite with an arrow drawn in it. In fact, since you might use this arrow sprite again, you ll make it in a class that extends the Sprite class:

Rotation

Rotation Now, whenever you need an arrow, you just say new Arrow() The most important thing to remember when drawing any content made to rotate like this is to make sure that it is pointing to the right, or positive x axis. because this is how it will look when rotated to 0 degrees. You ll be creating an instance of the Arrow class, placing it in the center of the stage and having it point at the mouse.

Rotation The mouse position can always be read with the mousex and mousey properties. You can get the location of the arrow with x and y properties. Subtracting these, you get the length of the two triangle legs. Now, you need to use Math.atan2(dy, dx) to find the angle. Then convert it to degrees and set the arrow s rotation property to the result.

Rotation Of course, to get animation, you need to set up a loop. Make sure that you have the Arrow.as file in the same directory as RotateToMouse.as file and build the SWF with RotateToMouse as the document class.

Rotation Now, suppose that you didn t have Math.atan2. You could get the ratio of opposite to adjacent angle by dividing dy by dx and pass that in to Math.atan. All you need to dois change the Math.atan2 line as follow

Rotation Try that one, and you ll see the problem. If the mouse is to the left of the arrow, the arrow will not point to it, but directly away from it. Can you figure out what is going on?

Sine Wave Have you ever heard the term sine wave before? It is the graph of the results of the sine function, when fed in all the angles from 0 to 360 (or 0 to 2 pi in radians).

Smooth Up and Down Motion So what can you use Math.sin(angle) for? Have you ever needed to move something up and down or back and forth smoothly? Instead of just going from 0 to 1 and 1 and back to 0, and stopping there, multiply those values by some higher value, say 100, you have a stream of values that goes from 100 to -100, back and forth.

Smooth Up and Down Motion The next document class, Ball.as You can pass in a radius and a color if you want. If not, it will use the default arguments of 40 for the radius and red for the color.

Smooth Up and Down Motion The Bobbing.as creates an instance of the Ball class, adds it to the stage, and sets up an enterframe listener that causes the ball to bob up and down:

Smooth Up and Down Motion

Smooth Up and Down Motion First, create an angle property and initialize it to 0. In the onenterframe method, you take the sine of that angle and multiply it by 50. This will give you a range of values from 50 to -50. Add that to the y position of the ball. Then add 0.1 to the angle for the next time around. You get a nice smooth up and down motion. You ll notice that changing the 0.1 to another value changes the speed of the motion. Changing the 50 changes how far the ball moves. changing the stage.stageheight / 2 to some other value changes the point that it oscillates around.

Smooth Up and Down Motion You could abstract the values into variables like so By keeping the numbers out of the code, preferably up at the top of the listing, you know exactly where all your variables are.

Linear Vertical Motion In the Wave1.as file, I added linear vertical motion, just to give you some inspiration for your own animation.

Pulsing Motion In the Pulse.as file, We used the values to affect the scale of the ball instead.

Waves with Two Angles In the Random.as class, it takes two angles and applies one to the ball s x position and the other to the y position.

Waves with the Drawing API

Cosine Wave Sine and cosine basically end up being the inverse of each other.

Circular Movement you can use cosine in place of sine where all you need is an oscillating motion. But cosine actually has a much more common and useful function in coordination with sine.

Circular Movement If you were to take the circle in Figure 3-20 and turn it so you were looking straight at its edge from the right, you would just see the object going up and down. In this case, sine would be the appropriate function to use, because if you look at the triangle formed, you are calculating the length of y. If you are looking at the circle from its bottom edge instead. In this view, you see the object moving back and forth, left to right. This time, you are calculating the length of x, so you should be using cosine.

Circular Movement

Elliptical Movement sometimes a perfect circle isn t exactly what you need. What you might be looking for is more of an oval or ellipse. To make a more oval shape, all you need to do is use different values for the radius when you calculate the x and y positions. I call them radiusx and radiusy.

Elliptical Movement

Pythagorean Theorem The theorem says A squared + B squared = C squared. The sum of the squares of the two legs of a right triangle is equal to the square of the hypotenuse. The most common situation is where you know the lengths of the two legs and you want to know the hypotenuse. Specifically, you want to find the distance between two points.

Distance Between Two Points Say you have two sprites on stage and you want to find out how far apart they are. You have the x and y positions of each of sprite. Let s call the position of one x1, y1, and the other x2, y2.

Distance Between Two Points Turn it into right triangle

Distance Between Two Points dx is the distance between the two sprites on the x axis, and dy is the distance on the y axis. If you square both of these and add them together, you ll get the square of the distance. In other words 6 2 + 8 2 = dist 2. And from that, you can easily figure out that the distance between the two clips is 10.

Distance Between Two Points

Distance Between Two Points Compile the movie, and you ll get the distance between the two sprites. Each time you run it, the two sprites will be in different positions.

Distance Between Two Points

Distance Between Two Points dx and dy are calculated by subtracting the current mouse position from sprite1 s position. Test the file and move your mouse around.

Important Formulas in This Chapter

Important Formulas in This Chapter

Important Formulas in This Chapter

Summary for now, you know about sine, cosine, and tangent and their opposites: arcsine, arccosine, and arctangent, as well as the ActionScript methods to calculate each one. The next chapter covers some of the more common rendering techniques for getting graphics on the screen, including the allimportant drawing API.