Trig 101

ORBZORBZ Member Posts: 1,303
edited November -1 in Introductions
Just a little math tip for those that are interested:

You can think of circles and triangles and waves as all inter-related.

triangles can be used to draw circles by fixing a point in the middle and then rotating around the outside edge. In this way you can sort of say that trig is the study of the relationship between triangles and circles.

you can go around and around a circle always ending where you started.
you can similarly go up and down in a wave like motion always ending up where you started.

Now, there are a LOT of maths to learn about all the ins and outs of trig.

But one thing that I always found super useful is the incredible neat sin and cos functions.

with these functions you give them an input and they always return a value between -1 and 1. Neat huh? You can clip that to be just 0 and 1 if you wrap your trig function in the abs() function. It depends on your needs. Remember, 0 is another word for false, and generally speaking, NOT 0 is another word for true (i.e. 1) so we can create a simple flip-flop by just doing abs(sin(game.time)) this will oscillate between true and false at regular intervals. Neat huh? No timers. Additionally we get a fine grain of precision because of all the infinite values between 0 and 1.

This relationship has to do with circles and the nature of waves.

I'm not going to go deep into the maths behind it because i'm not an expert.

But whenever you are confronted with a situation where you would like something to smoothly move from point a to point b or oscillate between two states with a smooth "Wave like" or "Circular" motion you could try attacking your problem using the sin and cos functions.
Examples:

Platform moving back and forth smoothly
Object moving in a perfect circle
Repeatedly fading in and out smoothly
Repeatedly closing and opening something again.
Monsters attacking in a wave like pattern.

Basically, anything that has a repeating, circular, or wave like motion.

When plotting circles the sin function will give you Y, similarly the cos will give you X.
y will be the sin of the angle times the radius. x will be the cos of the angle times the radius.

When dealing with state flipping sin is usually used, but cos can be used instead for similar effect. cos is the wave right behind the sin wave. They are complementary to each other.

For more details I suggest Khan Academy on YouTube to learn all your math needs. :)

Note: Trig does NOT take TIME into account. For that, see calculus and physics.

Disclaimer: I'm not a math expert, but I have found this little bit of trig to be super useful.

Hope this helps someone :)

Comments

  • QuinnZoneStudiosQuinnZoneStudios Member Posts: 452
    It burns...... Owwww!!!

    Heh all I need is some cut-n-paste formulas for all the standard behaviors. I don't need to know how it works lol! But that's because I'm left-brain dead.

    Thanks Orbz
  • design219design219 Member Posts: 2,273
    This sounds interesting, but I'm not a math person at all. Is what you are saying a clue to improvement in our GameSalad behaviors?
  • BeyondtheTechBeyondtheTech Member Posts: 809
    I wanna divide by zero to see what GameSalad will do. Maybe it'll unlock the Pro features.
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    @BtT, it says NAN.
  • ORBZORBZ Member Posts: 1,303
    what i was explaining is that a lot of the problems trying to be solved with rules can also be solved by just using some simple trig functions. Oscillation, rotation, flip-flop between two states, etc.

    Often it can reduce a set of complicated rules down into a single line of trig expressions.

    This yields faster games and simpler code bases.

    I hope it helps :)

    I've found it to be useful myself. And no, i'm not a really good math person either. The concepts aren't that hard to understand once you get the hang of it. I highlighted the most useful functions sin and cos for me. Other functions like tangent are great for determining line-of-sight.

    But don't worry, none of this stuff is required to use GS, it's just handy for doing some common tasks I see posted on the forums here.

    The other nice thing about trig functions is that they give a "smooth" transition between states, so things slow down before stopping and reversing, etc.

    try this:

    put an actor on a scene, give it a Move behavior with the speed set to: sin(game.time * 100) * 100

    Watch what happens. Now go figure out why ;)
  • design219design219 Member Posts: 2,273
    Dude, you might be a good person to know.
  • BeyondtheTechBeyondtheTech Member Posts: 809
    Thanks, CodeMonkey. I love that kind of bread.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,987
    awesome post......khan academy may need to be visited as my little mind just half melted....

    been using sin and cos, but been annoyed that i dont understand it.... this was a step in the right direction... so cheers
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    ummhh...

    nan...

    nothing like flatbread from tandoor!
  • design219design219 Member Posts: 2,273
    and Pakoras!
  • JGary321JGary321 Member Posts: 1,246
    ORBZ - Care to give a couple of example that you used it in? I think it would help a lot of people to see it in action (myself included) & see some examples. Seeing things in context always makes more sense.
  • synthesissynthesis Member Posts: 1,693
    @JGary: Read this thread...shows the difference between a rule sequence and Orbz clever little secret he explains here (near the end of the thread).
    http://gamesalad.com/forums/topic.php?id=4439

    @ ORBZ
    VERY GOOD TIP!!! KEEP THEM COMING!!! I will add that Cosine (cos) is the INVERSE of sin. When overlayed on top of each other...they look like a DNA helix in the movies...zig zagging at different rates back and forth (between -1 and 1) across their center axis...which is 0.

    His point is that if you use a trig function in combination with a timer, you can create very smooth visual effects in your game.

    My only criticism is that Trig and Constrained behaviors are resource intensive for GS at the present time. USE IN MODERATION AND VERY STRATEGICALLY!!!

    I would recommend that if you need a trig function...set it up to run in a background math calculator actor (with any other complex math operations for use in the game). Then tap into the running oscillation at any time from any actor. Then have that actor reference the base sin wave motion value with any sort of magnifier or modification to the base sin wave value within the actor's unique expression. This will give you a running oscillation in the background to spread around throughout the game...rather than have the trig function in multiple actors. The trig function can also be within a game switch...which other actors can then turn on and off when they need a trig value (or any constrained math calculation for that matter).

    @ the rest of you all
    PAY ATTENTION IN CLASS....THIS IS VERY GOOD STUFF HERE!!! You don't have to be a math wiz to understand basic trig. It (combined with basic geometry...although they are really the same thing) can be very, very useful with game development.
  • ORBZORBZ Member Posts: 1,303
  • rebumprebump Member Posts: 1,058
    Well, if you want some backgrounder info on the "maths", I found the following link pretty helpful from an animation engine point-of-view:

    http://www.sidefx.com/docs/houdini9.1/ref/expression_cookbook

    It's for another application but their write-up gives you some useful info and maybe some ideas.

    Enjoy.
  • SlowboySlowboy Member, PRO Posts: 329
    Great posts peeps, thanks for taking the time to share.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,987
    just bumping this thread... as I remember reading it the first time round...

    so much good info in here.
  • JackBQuickJackBQuick Member Posts: 524
    Thanks for bumping this! I'm going make this thread one of my favourites so I don't lose it.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,987
    just bumping again to share with the masses...
Sign In or Register to comment.