PDA

View Full Version : Cosines



Donut
January 5th, 2011, 06:26 PM
so iv been playing super metroid lately, and as some of you know, there is a weapon called the wave beam in that game. the graphic for it is basically a cosine wave (technically its a sine wave in super metroid, but whatever). it got me thinking about the math behind graphing cosines at an angle for 360 degree wave beam firing. i know how to get the cosine curve straight up or straight forward, but not at an angle.

this is f(x) = cos(x). this is more or less what the wave beam looks like shooting straight forward.
http://i154.photobucket.com/albums/s277/TheKillerDonut/gamestuff/cos2x.png

this is as close to what i want as iv gotten. basically, i just added a linear function to the cosine function where the slope of the linear function is an angle of my choosing
f(x,θ) = cos(x) + (tan(θ)*x) where θ is the angle i want the curve at
http://i154.photobucket.com/albums/s277/TheKillerDonut/gamestuff/lintilt.png
this is both ugly, and not what i want. the green line is the linear function i added

this is what im looking for. i just rotated the first image 45 degrees in gimp. θ is the angle the wave is tilted
http://i154.photobucket.com/albums/s277/TheKillerDonut/gamestuff/cos2xtilt.png


what i want to know is the mathematical function that generates a cosine wave that looks like the third image. i want to implement this into a little sprite game im working on, but i dont know the math to put it at an angle. iv toyed with the idea of using f(x) = cos(x) and just tilting the result, but i dont really know how to program that either, but what im mainly looking for is the math behind a tilted cosine graph.

before anybody lmgtfy's me, i asked google, and google didnt know. i asked one of my teachers, and he didnt know, so HE asked google, and google still didnt know.

PlasbianX
January 5th, 2011, 06:56 PM
Try graphing f(x,θ) = cos(x*P) + (tan(θ)*x)

Where P is say.. 10. I tried 10 and it looked closer to what you wanted. Now mind you, I'm taking a stab in the dark here with this but it did look closer.

Warsaw
January 5th, 2011, 07:41 PM
That is a very good question...

Been toying with it for 20 minutes now, and the basic form I've come to thus far is:

x + cos ((pi - x)

However, still distorted. I suppose you could use a rotation matrix on the standard cosine equation to get the effect you want, but that's messier than having a single function. On the flip side, it's also more flexible.

Fake E: I tried what Plas is doing before coming to the above, and that tangent is really unnecessary to get that particular result.

PlasbianX
January 5th, 2011, 08:13 PM
That is a very good question...

Been toying with it for 20 minutes now, and the basic form I've come to thus far is:

x + cos ((pi - x)

However, still distorted. I suppose you could use a rotation matrix on the standard cosine equation to get the effect you want, but that's messier than having a single function. On the flip side, it's also more flexible.

Fake E: I tried what Plas is doing before coming to the above, and that tangent is really unnecessary to get that particular result.

To be honest, all I did was randomly guess without any thought once so ever to the logic behind it or why it showed what it showed. Ive been looking through my calculus books and haven't found anything that would help yet with this.

Warsaw
January 5th, 2011, 09:57 PM
All it does is put it at the desired angle. What I did is put it at 45 degrees, which is what he is trying to achieve.

Donut
January 5th, 2011, 10:10 PM
well, at any angle really, but 45 is a good start. also, plas, multiplying by 10 shrinks the cosine horizontally. it still doesnt achieve what im going for here. do you remeber the linear equation y=mx+b, where m is the slope? the tan is the m. whatever angle i put in there creates the equation of a line that forms the angle with the x axis. adding that to the cosine moves the points up on the y axis, but that isnt what im looking for.

warsaw, what is this rotation matrix you speak of? also, if it is possible, can we keep away from radians? i dont like radians :saddowns:
E:

http://en.wikipedia.org/wiki/Rotation_matrix
In linear algebra (http://en.wikipedia.org/wiki/Linear_algebra), a rotation matrix is a matrix (http://en.wikipedia.org/wiki/Matrix_%28mathematics%29) that is used to perform a rotation (http://en.wikipedia.org/wiki/Rotation_%28mathematics%29) in Euclidean space (http://en.wikipedia.org/wiki/Euclidean_space)well then. this seems like my best bet. and i have a sneaking suspicion that there isnt a simple function for what im trying to do.

Warsaw
January 6th, 2011, 04:57 AM
It isn't. But think of it this way: you can use it to flip your sine/coside wave however you want.

Also, you'll find that radians are so much easier to work in. It's far easier to memorize your unit circle using them because all you have to do is memorize the first quadrant, then multiply each angle by the next highest number that doesn't go into the numerator evenly for the rest of the angles going around.

Also, technically, radians aren't a measure of angle, but rather a ratio between arc-length and radius. It's not arbitrary like degrees, it actually has meaning.

CtrlAltDestroy
January 6th, 2011, 05:05 AM
the rotated sine wave you're trying to achieve can't be expressed as merely a function of x (refer to your third image, it doesn't pass the vertical line test)

your best bet is to use rotation matrices. for example (if you're keeping to two dimensions):

- your initial function is f(x) = cos(x)
- if you want to rotate your function by angle θ, we define a rotation matrix R:

┌ ┐
R = │ cosθ sinθ │
│ -sinθ cosθ │
└ ┘
- ...and a vector v of the [x,y] coordinates of any given point of your function f(x):

┌ ┐ ┌ ┐
v = │ x │ = │ x │
│ f(x) │ │ cos(x) │
└ ┘ └ ┘
- to get the [x,y] coordinates of any point given by [x,f(x)] rotated by θ, we simply multiply the matrix R by the vector v:

┌ ┐┌ ┐ ┌ ┐
Rv = │ cosθ sinθ ││ x │ = │ xcosθ + cos(x)sinθ │
│ -sinθ cosθ ││ cos(x) │ │ cos(x)cosθ - xsinθ │
└ ┘└ ┘ └ ┘
- thus, we can define g(x,θ) which outputs the coordinates of a rotation of f(x) by angle θ:

g(x,θ) = [xcosθ + cos(x)sinθ,cos(x)cosθ - xsinθ]

hth, wrote this pretty quickly

EDIT: this rotates the function about the origin, but you probably already figured that

Warsaw
January 6th, 2011, 07:04 PM
He can move the origin to any place on the screen he wants, so it works.

Donut
January 6th, 2011, 08:29 PM
oh awesome. thanks cad :iamafag:
E:
g(x,θ) = [xcosθ + cos(x)sinθ,cos(x)cosθ - xsinθ]is that comma supposed to be there? or is that an x,y coordinate?
E2: pretty sure its a comma and an x,y coordinate. thanks again
E3: ok so i made a parametric function (see image), and this is what im looking for, but it still looks a little distorted. is there any way to fix this?
http://i154.photobucket.com/albums/s277/TheKillerDonut/gamestuff/parametric.png
im just using 45 degrees as a test for now
E4: nvm, the distortion is due to my axes. it works just fine :iamafag: