Need mathematical function for computer game I'm programming

planes86

New member
Joined
Oct 13, 2008
Messages
4
I apologize if this is in the wrong section. I am writing a computer game and I need a mathematical function to represent a road. I remember seeing a graph of a function before but I cannot remember the function. Does anyone recognize this graph and it's corresponding function?

I apologize for the poor quality, this is the best I could come up with.
graph.jpg



If not can anyone give me an idea on how I would find out, or some knowledge on how I can create a function to look somewhat similar?

Basically it needs to start at 0,0, but then jump up higher and cycle back and forth before coming back down to 0,y.

Thank you for any help you can give.
 
Re: Identify a graph

How about Sine? Most compilers come with a library of mathematical functions, and in any good ones, sine will be there.
 
Re: Identify a graph

Thanks for the reply.

sine is good for the top part, but I can't have the graph hovering on the x-axis. I need it to start at 0, but quickly get up "out of the way" as there will be other stuff on the x-axis, and then a mirror image of the top function below.
 
Re: Identify a graph

You could do something like:

5/(2pi)*x if 0<=x<2pi
sin(x)+5 if 2pi<=x<=4pi
-5/(2pi)*(x-4Pi)+5 if 4pi<x<=6pi
 
Re: Identify a graph

I was thinking about doing something like that, how do I "compress" the sin wave so that the cycles are closer together?
 
Re: Identify a graph

planes86 said:
I was thinking about doing something like that, how do I "compress" the sin wave so that the cycles are closer together?

Increase the amplitude to make the winds longer and multiply x by a number larger than 1 to "squish it." To raise it higher, change all the 5's to a larger number.

Be aware that this "squishing" will change the period of sine. Divide 2Pi by whatever is multiplying x to get the new bounds to keep the "road" connected. For example sin(2x) will have a period of Pi. So it will look like this:

5/Pi*x , 0<=x<=Pi
Sin(2x)+5, Pi < x < 5Pi
-5/Pi*(x - 5Pi)+5, 5Pi < x <= 6Pi
 
Re: Identify a graph

thanks a lot for your help, I will try it out tomorrow and see how it looks
 
Re: Identify a graph

planes86 said:
thanks a lot for your help, I will try it out tomorrow and see how it looks

No problem. Should work fine... FYI I edited my posts to fix a little error.
 
Re: Identify a graph

you could also try a function like:

f(x) = -x*(x-1)*(x+1)*(x-2)*(x+1.75)*(x-1.5)*(x+1.5)*(x-1.75)

This will give you 4 peaks and make peaks "un-even" - more like a natural road - if that is what you want.
 
Top