Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Trigonometry for Game Programming – SpriteKit and Swift Tutorial: Part 2/2

Learn trigonometry for game programming using SpriteKit and Swift. You will learn the theory and then practice using trigonometry by making a space shooter game!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5505-trigonometry-for-game-programming-spritekit-and-swift-tutorial-part-2-2

When I use the orbit code, the object that I want to orbit around the circle does not move. What am I doing wrong?

@cinnastix Most likely that you aren’t using a component that changes as time passes. If you look at the updateOrbiter method in the tutorial, you’ll notice it take a parameter that increases as time passes. That value is used in the calculation to change the angle over time. Without this value changing, the object would remain in one spot.

This is great. Except, when I go the interactive circle link, it throws everything I learned off. Putting it at a 45 degree angle, it displays the cosine as the Blue line, and the sine as the green line. From what I thought after looking at the part 1 photos, when you use cosine, it returns the value of the opposite side. And as for sine, it returns the value of the adjacent side.

So which is right? Is the cosine the value of the blue line as it is in the interactive link, or is it the value of the opposite line like the photos say here?

Of course, when you Google you get the formula cos() = adj / hypotenuse

Go back and look again. :slight_smile:

For the figure with the sin, cos, and tan defined, the graphic is showing the two sides being used in orange, and the remaining unused side in blue.

sin(angle) = opposite / hypotenuse (opposite side and hypotenuse are orange)
cos(angle) = adjacent / hypotenuse (adjacent side and hypotenuse are orange)
tan(angle) = opposite / adjacent (opposite side and adjacent side are orange)

The graphic is a bit unusual; usually the hypotenuse is not emphasized like that. It is the case, though, that cos goes with adjacent, or the blue line in the interactive circle. You can also think of it as cos gives the X value, and sin gives the Y value, as long as the triangle is in the standard position.

I’ve wrapped my head around it. I get it, and it’s actually quite interesting! Thank you for replying.

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!