Advertisement

Math in game programming

Started by December 24, 2003 07:07 PM
4 comments, last by SkinnyM 21 years, 2 months ago
Hello, I have had some recent enlighenment in math. It finally clicked what cos and sin did I have a few things that need to be clarified though, first, i know how to generate a 2d circle, first you use cos and sin with a spherical coordinate (0-360) and it sends back a value thaat can be used in the coordinate system with x-y. Forgot what it was called. And do that 360 times and map the points it gives you and you got yourself a circle! But the problem is, how do i generate a 3d sphere? Do i use tan? Is that for a z coordinate? I am lost in this area. I have looked at some source code for a skydome, they do things like mutiplying the cos and sin value and use that value for something.. Here is the code: m_fVertices[( n*3 )+0]= fRadius*sinf( fDTOR*iPhi )*cosf( fDTOR*( iTheta+fTheta ) ); m_fVertices[( n*3 )+1]= fRadius*sinf( fDTOR*iPhi )*sinf( fDTOR*( iTheta+fTheta ) ); m_fVertices[( n*3 )+2]= fRadius*cosf( fDTOR*iPhi ); after i saw that, i was completely lost. What is the value being returned by each of those have to do with a sphere, what i mean is, by mutiplying sin and cos togeather, what are you getting? If you could explain that, it would make me very happy Please dont refer me to some tutorial that tells me things like, this part of the right triange is a cos value, and blah. i know all that, i just havent been able to relate it to cos and sin in game programming.. Please explain it Also, if you get the time and the patience, can you explain to me what kind of value a tan value returns? What do you use that value for? Thanks alot for the help!
"What we do in life, echos in eternity" -- Gladiator
The sky dome you are describing is specifying spherical coordinates, then converting them to rectangular coordinates. Its a little easier to visualize in 2D.

2D polar coordinates (The 2D version of spherical coordinates) consist of a pair, a radius (r) and an angle (theta). You can describe the location of any point by distance from the origin (r) and the angle (theta) it forms with the x axis.

To convert from polar to rectangular (x,y) x = r*cos(theta), y=r*sin(theta). The useful thing about polar coordinates is that its very easy to describe a circle. You just hold the radius (r) constant, and vary theta.

With sperical coordinates, you still have a radius (r), an angle (theta), and now an angle (phi) from the perpendicular axis. You can describe a sphere by keeping r constant, and varying the angles theta and phi. You then convert back to rectangular as follows:

x=r*sin(phi)*cos(theta)
y=r*cos(phi)*sin(theta)
z=r*cos(theta)

Conceptually its:
for theta=0 to 2*pi   for phi=0 to 2*pi      createvertex(r, theta, phi) 


Anyways, i don''t know if that helps or if its confusing. Here''s a link that might help visualize things:
http://sosnick.uchicago.edu/spherical_cylindrical_rectangular.html
Advertisement
Those are the standard parametrization formulas for spheres. In cleaner terms they look like this:
x = r sin(phi) cos(theta)
y = r sin(phi) sin(theta)
z = r cos(phi)

The only special thing about those equations is that if you run phi from 0 to 2pi, and theta from 0 to 2pi, that you''ll get all the points on a sphere.

You might ask, how did they come up with those formulas? Well, they are classic formulas, so whoever wrote that code in the first place just looked in a textbook. How did they come up with those formulas in the first place? I don''t know. My guess is that they started with the sphere formula

r = x squared + y squared + z squared

and replaced x,y,z with formulas that would keep the equality true (taking advantage of the fact that sin squared + cos squared cancels to 1). As in, if you replace x,y, and z on the right side of the equation with the above formulas, you''ll get r.

As for the question of "what does it mean to multiply a sin and cos"? Ummmm. You get the result of an equation that satisfies the requirements of this particular problem. I don''t know if there''s much more to say about that.

I think you will have more success in mathematics if you can accept that not every formula has an obvious and transparent purpose.
tan(theta) is equal to sin(theta) / cos(theta). Well umm, you are probably going to say that you already knew that.

Okay, well one thing that tan does is if you have a line that goes at the angle theta, then tan(theta) will give you the slope of that line.
Thanks alot for the fast replys, i read what you guys said and it actually made sense!!
Hehe woo. I have two more questions though, one new one and one from the replys.


In the last reply, he said that tan(angle) = slope of line, it said that on the link the first post gave me. (Good site) How do you get from the value returned by tan(angle) a x and y value?

It is the ratio i know that, but the ratio could be formed by more than 1 combination right? That is one question. The next is, i was reading this book awhile back and they were showing a flag in opengl waving, they made a wave go across the flag and lift up the vertexes in a smooth way, how would they do? Would they use sin(angle) to get the smooth y vallue and then just make the x value from a loop to repeasant the wave? That probley made no sense to you guys but you can understand what the book was ttrying to do. I belive the book was OpenGL Game Programming.

Thanks for the help!!! It made alot of sense to me!
"What we do in life, echos in eternity" -- Gladiator
You would have to multiply it by some value R that you knew beforehand, R being the hypotenuse of a right triangle that had angle theta. A flag waving could be done by the function:

y = R(sin(x + c)) where R is a constant, x is an independent variable and c changes with time (the more c changes, the sine function will appear shifted to the left more if c becomes more positive and produce a wavey motion if gradually increased).

One last note. You should probably know a LOT more than just sine and cosine if you want to program in 3d. I was done with calculus before I understood enough to venture out on my own in 3d programming (ignore the post I made a few minutes ago that was just ignorance lol)

[edited by - uber_n00b on December 25, 2003 12:18:06 AM]
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.

This topic is closed to new replies.

Advertisement