1 / 11

Understanding Movement with Trigonometry in Programming: A Practical Approach

In this lesson, we explore the basics of movement in a 2D space using trigonometry. While moving left, right, up, and down is straightforward, diagonal movement introduces complexity. We delve into the properties of right triangles, including side relationships and angle measurements. Learn how to calculate horizontal (x) and vertical (y) displacements using sine and cosine functions. We'll also discuss radians and how to convert between degrees and radians for accurate calculations in programming. This foundational knowledge is crucial for game development and simulations.

conan
Télécharger la présentation

Understanding Movement with Trigonometry in Programming: A Practical Approach

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lesson 2 By us

  2. Movement • moving left/right/up/down is easy • so are rotations • but if the goal is to move diagonally, there is a problem • the solution is trigonometry

  3. The Right Triangle • every triangle has 3 sides • the sum of internal angles is 180 degrees • a2 + b2 = c2 c b a

  4. Trig • let ‘x’ be an angle • sin(x) = opposite / hypotenuse • cos(x) = adjacent / hypotenuse • tan(x) = opposite / adjacent • you will never use “tan” in programming • “sin” and “cos” are crucial

  5. Trig 2 • sin(z) = opposite / hypotenuse • cos(z) = adjacent / hypotenuse • tan(z) = opposite / adjacent

  6. Trig 3 the scenario: • you have the angle (this._rotation) • you have the hypotenuse (speed) • now you need vertical length and horizontal length

  7. Trig 4 Math.sin (z) = y / velocity  y = Math.sin (z) * velocity Math.cos (z) = x / velocity  x = Math.cos (z) * velocity

  8. Trig 5 • so now that you have ‘x’ (horizontal displacement) • you also have ‘y’ (vertical displacement) ie: this._x += Math.cos (z) * velocity; this._y -= Math.sin (z) * velocity;

  9. WAIT A MINUTE • the input for Math.sin () or Math.cos () is in radians • 1 radian = PI / 180 degrees • 1 degree = 180 / PI radians • to get PI, you type: Math.PI

  10. More Radians example: z = z*Math.PI/180; //or z *= Math.PI/180; this._x += Math.cos (z) * velocity; this._y -= Math.sin (z) * velocity;

  11. DONE FOR TODAY!

More Related