Skip to main content
Engineering LibreTexts

9.29: The Mathematics of the Sine Function

  • Page ID
    14649
  • def getBounceAmount(currentBounce, bounceRate, bounceHeight):
        # Returns the number of pixels to offset based on the bounce.
        # Larger bounceRate means a slower bounce.
        # Larger bounceHeight means a higher bounce.
        # currentBounce will always be less than bounceRate
        return int(math.sin( (math.pi / float(bounceRate)) * currentBounce ) * bounceHeight)
    

    There is a mathematical function (which is similar to functions in programming in that they both "return" or "evaluate" to a number based on their parameters) called sine (pronounced like "sign" and often abbreviated as "sin"). You may have learned about it in math class, but if you haven’t it will be explained here. Python has this mathematic function as a Python function in the math module. You can pass an int or float value to math.sin(), and it will return a float value that is called the "sine value"

    In the interactive shell, let’s see what math.sin() returns for some values:

    >>> import math
    >>> math.sin(1)
    0.8414709848078965
    >>> math.sin(2)
    0.90929742682568171
    >>> math.sin(3)
    0.14112000805986721
    >>> math.sin(4)
    -0.7568024953079282
    >>> math.sin(5)
    -0.95892427466313845

    It seems really hard to predict what value math.sin() is going to return based on what value we pass it (which might make you wonder what math.sin() is useful for). But if we graph the sine values of the integers 1 through 10 on a graph, we would get this:

    Figure 48

    You can kind of see a wavy pattern in the values returned by math.sin(). If you figure out the sine values for more numbers besides integers (for example, 1.5 and 2.5 and so on) and then connect the dots with lines, you can see this wavy pattern more easily:

    Figure 49

    In fact, if you kept adding more and more data points to this graph, you would see that the sine wave looks like this:

    Figure 50

    Notice that math.sin(0) returns 0, then gradually increases until math.sin(3.14 / 2) returns 1, then it begins to decrease until math.sin(3.14) returns 0. The number 3.14 is a special number in mathematics called pi (pronounced the same as delicious "pie"). This value is also stored in the constant variable pi in the math module (which is why line 6 [333] uses the variable, math.pi), which is technically the float value 3.1415926535897931. Since we want a wavy-looking bounce for our squirrel, we’ll only pay attention to the return values of math.sin() for the arguments 0 to 3.14:

    Figure 51

    Let’s take a look at the return value of getBounceAmount() and figure out what it does exactly.

    Remember that on line 21 we set the BOUNCERATE constant to 6. This means that our code will only increment playerObj['bounce'] from 0 to 6 and that we want to split up the range of floating-point values from 0 to 3.14 into 6 parts, which we can do with simple division: 3.14 / 6 = 0.5235. Each of the 6 equal parts of the 3.14 length on the graph for the "sine wave bounce" is 0.5235.

    You can see that when playerObj['bounce'] is at 3 (halfway between 0 and 6), the value passed to the math.sin() call is math.pi / 6 * 3, which is 1.5707 (halfway between 0 and 3.1415). Then math.sin(1.5707) will return 1.0, which is the highest part of the sine wave (and the highest part of the sine wave happens half way through the wave).

    As playerObj['bounce'] gets its value incremented, the getBounceAmount() function will return values that have the same bounce shape that the sine wave has from 0 to 3.14. If you want to make the bounce higher, than increase the BOUNCEHEIGHT constant. If you want to make the bounce slower, than increase the BOUNCERATE constant.

    The sine function is a concept from trigonometry mathematics. If you’d like to learn more about the sine wave, the Wikipedia page has detailed information: http://en.Wikipedia.org/wiki/Sine.

    • Was this article helpful?