Trig is cool

As many of you know from frequenting my site, source code, tutorials, and books, I use a lot of trigonometry to create various effects. At least I used to to think I used a lot. All I really ever used was sine, cosine, arctangent, and occasionally the law of cosines.

Usually, whenever I write about math or physics, I’m overly careful to point out that I haven’t done any formal study of the stuff. Well, all that’s starting to change. Last fall I did a College Algebra course and right now I’m wrapping up Precalculus. The last couple chapters have been heavy into trig. There’s more trig than I ever considered existed.

The last chapter covered all kinds of trig functions and inverse functions including all those trigonometric identities. Stuff makes my head spin. I think I’m supposed to memorize all those for the final, which kind of scares the crap out of me.

This chapter we’re covering the laws of sines and cosines, and now moving into vectors, dot product, cross product. This is the big payoff for me. I’ve dabbled in the stuff before, but mostly just copying down formulas and plugging in variables. Now I’m starting to understand it. You can do some really cool stuff with that. Just started playing with some of it last night and came up with this:

http://www.bit-101.com/misc/trig/angles.html

The free end of the arm is draggable. You can envision some cool mechanical devices that could be made with this. 

Then I got thinking of a problem I had tried to solve for ages. Basically, you have a ball bouncing around the stage. It goes past one of the boundaries. To make it bounce, you reverse its velocity on the axis of the collison – x or y. But you also have to move the ball so that it’s just touching the wall/floor/ceiling, not extending through it. I’ve always faked it by just adjusting its position on that same axis. But I’ve always known that it wasn’t quite accurate. I know that I should be actually be moving it back along the line of velocity to the point where it is just hitting.

So this morning, I sat down and drew a diagram and suddenly it was all very clear to me. I worked out the various trig formulas that would calculate that exact point. Tested it out. Worked like a charm!

Then I ran two objects side by side, one using the old faked method, one using the new accurate method. Truth is, you can’t really see a difference. If it’s moving really slow, the difference is only a couple of pixels. If it’s moving fast, the difference may be larger, but it’s moving so fast that you can’t see the difference.

So the result is that I’ll probably continue to use the old hack, as it’s more efficient, though less accurate. But it’s great to have worked out the correct method and have that available to pull out if I need it. I’ll probably eventually post it in the lab if anyone wants to see it.

This entry was posted in General. Bookmark the permalink.

5 Responses to Trig is cool

  1. I could be wrong on this (feel free to correct me), but I’m pretty certain the most accurate way to deal with the bouncing ball is to reflect it back along the axis tangential to the surface it strikes by a distance equal to the distance it travelled past the surface. Setting it to be just touching actually ignores the time elapsed between the ball striking the surface, and the time when it was traveling through the surface.

    So, for a collision with the floor (where HEIGHT is the height of the environment):
    if (_y + _height/2 > HEIGHT) {
    _y -= (_y+_height/2-HEIGHT)*2;
    }

    You could of course do the same thing with reflected vectors, but it’s more processor intensive.

    Very cool that you’re learning formal trig though – you’ll have to teach me some basics next time we meet up. I could definitely use it!

  2. PS. Strange… can’t post comments from Safari – I get a connection lost error. FireFox worked though.

  3. kp says:

    Grant, you are absolutely right about that, as far as I can tell. That’s been in the back of my mind, but I always thought it would look more accurate to see it touching the surface, rather than bouncing without touching. Then again I’ve never actually tried it, so it may look just fine.
    Mostly though, it was just cool to sit down and pretty easily figure out that trig problem that used to spin my head before. Looking forward to doing a lot more with vectors now.

  4. Josh Tynjala says:

    Now that you’re learning more cool trig stuff, have you starting making plans to write another awesome book? 😉

  5. Karl says:

    Yeah, I think you did the same thing I did. I made a physics program with a bouncing ball, and, like you said, I just reversed the velocity when it hit a surface. The problem came when I wanted it to bounce off a surface that wasn’t vertical or lateral. I figured it out recently too, the polar coordinate system. It’s like the standard one, but based on angles. Nice progam-thing, by the way.

Leave a Reply