Skip to main content

Moving with Trigonometry

These are some thoughts on Trigonometry that I wanted to write down, and someone, somewhere may want to read. But probably not. If you're not into it, turn back immediately.

In a top-down view on a two dimensional plane, you can face any direction and move in a forward direction by moving on the x-axis by the cosine of your rotation and moving on the y-axis by the sine of your rotation.

This seems to require a physical orientation in the world, but this physical limitation is only on the surface. We can take one step back and use these abilities in more subtle ways. Imagine a side view on a plane in which you can move left and right, and jump up and fall back down to the ground.

Let us first think of the left and right movement in terms of trigonometry. Facing right means 0 degrees and so we can move on x by the cosine of our rotation. Facing left is 180 degrees, and we move on x by the cosine of our rotation. Let's maintain a logical rotation factor called xf. When standing still (not moving at all) set xf to pi / 2, ie. 90 degrees. And now, constantly move on the x-axis by the cosine of this rotation. Yes, even when standing still, apply movement. As expected, we won't actually move because the cosine of 90 degrees is 0. When we move right, decrease the rotation xf by some constant (we'll call it an acceleration factor) until the rotation is 0 degrees. Likewise, when moving left increase xf by the acceleration factor until the rotation is 180 degrees.

We have implemented horizontal movement in terms of cosine with a built in acceleration and deceleration. This should seem pretty amazing. It does to me, at least.

Now we can think of jumping up and falling down in the same terms. Let us maintain another logical rotation, we'll call it yf. Any time that you hit the ground, set yf to 180 degrees. Constantly apply movement on the y-axis by the sine of yf. At the same time, constantly increase yf to 270 degrees if it is ever less. To jump, set yf to 90 degrees.

You may realize that this not only implements jumping, but also applies a deceleration to the upward movement, and becomes a falling acceleration up to a terminal velocity.

Finally, I want to mention walking on a slope. This is done by standing on the slope (how to do this is a topic of collision detection) and then determine the angle of the slope using the arctangent function. Then, it's as simple as moving on x by the cosine of this angle, and on y by the sine.

Comments

Popular posts from this blog

Water Pea Care and Instructions

Please treat your water pea like a real pet fish. Feed daily! Click here to get the Water Pea android app. You will receive a single water pea egg. Drop the egg in the water and your baby pea will hatch. If your water pea is alive after 1 da y, it will produce an egg that you can harvest . Care and Instructions Birth When you place the water pea egg in water, it will take a few seconds for the egg to dissolve. Initially, the newborn water pea will be tiny and hard to see. Take care not to accidentally pick it up and drop it out of the tank. Water peas live for about 3 days if properly fed. Feeding Your water pea must be fed every day, or it will die. To be safe, feed it once in the morning and again at night. Water peas recycle vitamin B by producing caecal pellets . Wait for your water pea to produce the pellet and then collect it so you don't run out of food. This is their only source of nutrition! To collect the pellets, drag them to the top of the ...

Momentum transfer

This image shows different possible paths that result in a collision. Intuitively, we know which way the object should continue along the wall. Determining this mathematically is a bit more challenging. The object could go in one of two directions. That is, it could follow the wall one way or the other. But why should it prefer one direction over another? We need a generalized rule that applies for every wall and every angle of approach. The rule is that momentum is transferred along the path of least resistance . Least resistance is the difference between the wall angle and the angle of approach that is less than 90 degrees. This raises the issue that a line's angle is determined by its direction. This means that the end point we start with determines the angle of the line. See the line below. Is it a 45 degree line, or a 225 degree line? It depends on which point you start from. But, for a generalized rule of momentum transfer, the answer shouldn't matter. ...

Mean Peas

I did something silly. I'm finishing up some behavioral changes for an aquatic creature that I'm about to release, called a water pea. For fun, I've been playing with different ideas to show the player interesting behaviors. One of those was teaching water peas to be territorial. The way to teach a program to "learn" to do anything is called training. It simply means having the program recognize incorrect (undesired) behaviors, and reinforcing positive behaviors. Over time, the program starts combining a multitude of desired behaviors to produce results that were never explicitly programmed in. These results can be...surprising. I eventually had peas that would fight to protect "their" area of the water tank. But water peas also form friendships, and they won't fight their friends. At least, that was the plan. Most territorial peas simply stayed in their area, along with their friends, and any poor pea that happened to wander by would be killed....