sine&cosine
i need some help with the sin() and cos() functions...see, im still in school(8th grade(sweden)) and we havent learned trigonometry yet..so, i did a search on trigonometry, and i read some articles on it. but since i kinda suck on math i just dont get it, and since i see its used alot in opengl/computer graphics i feel i need to learn/understand it.
is there any hope for me? help plz...
sine and cosine functions are pretty easy to understand. Here''s what they are. Draw yourself a circle, and draw a dot in the middle of that circle. Now from the dot, draw a straight line from the dot, straight east util it reaches the circle. This is zero degrees (or 0 radians). Now draw another line from the center of the circle to any single point on the circle. Now, if you imagine a cartesian coordinate system, the distance in the x direction is to the point on the circle is the cosine and the distance y is the sine. Something like this. The easiest way to learn this is just to memorize the values of the special angles (in radians). A radian is defined as an arc on the circle equal to the length of the radius, and guess what, to half a circle, there are 3.14xxx radians, or pi in other words, so a circle is 2pi. To convert degrees to radians, the formula is simply (x*pi)/180 where x is the number of degrees you want to change into radians. The conversion from radians to degrees is simply the same formula upside down, and x is the arc in radians rather than degrees. Now that you have that, simply memorize the sin and cosine at the following points : 0, 1/4, 1/3, 1/6 radians, then move them to the other three quadrants by simply changing the signs and you can do any of the other trig functions. Example:
sin x = 1/csc x csc = 1/sin x
cos x = 1/sec x sec = 1/cos x
tan x = sin x/cos x cot = cos x/sin x
That''s basically the biggest part of trig, other than that it''s just memorizing properties to make trigonometric equations look simpler, and memorizing formulas. Trig is also really good for graphing imaginary numbers, but I''m not sure how much you really need to know that for computer graphics.
sin x = 1/csc x csc = 1/sin x
cos x = 1/sec x sec = 1/cos x
tan x = sin x/cos x cot = cos x/sin x
That''s basically the biggest part of trig, other than that it''s just memorizing properties to make trigonometric equations look simpler, and memorizing formulas. Trig is also really good for graphing imaginary numbers, but I''m not sure how much you really need to know that for computer graphics.
hmm..maybe i didnt..i dont really understand HOW to use the sin and cos, like..for instance, i want to rotate my spaceship 180 degrees, how would i do that with sin/cos?
how can you rotate something, and still know whats the front of that something?
Well, you use vectors. You have a position vector (where you''re ship is at in 3d space), you have a view vector (where you''re point of view is centered in 3d space). Let''s do this in 2d for simplicity. Let''s say every time the player hits "r" the position vector remains unchanged, but the view vector rotates 5 degrees in a counter clockwise direction. To get your new coordinates for your view vector, you simply find the new sin and cos of the angle you start at plus five degrees which is your new angle. Let''s say you''re using opengl and it''s first person camera view, just plug the new coordinates into your gluPerspective function after your rotate function completes and you''ve got it. Once you understand how to do the rotations in 2d, then go on to learn 3d, which is much more difficult. Also, you probably want to learn vectors. A vector is, to put it simply, a coordinate (x, y, z). You can do all kinds of operations on them, and vector math is probably the most important math to learn for computer graphics. It''s not all that difficult either.
I wrote a tutorial on 2D and 3D rotation only the other day. Maybe it''ll be relevant to some here:
http://www.alistairkeys.co.uk/rotation.htm
(It''s based on Delphi but hopefully you''ll be able to follow.)
http://www.alistairkeys.co.uk/rotation.htm
(It''s based on Delphi but hopefully you''ll be able to follow.)
cool, that makes alot of sense, thx again
[edit]oh thx to u 2 alimonster, ill check it out
[edited by - branhield on August 9, 2002 2:05:37 PM]
[edit]oh thx to u 2 alimonster, ill check it out
[edited by - branhield on August 9, 2002 2:05:37 PM]
hmm..i could use some more help here..see, i made it rotate and move towards the current view vector, but if you move it away from the center of the screen a bit, it will rotate in a circle around the center instead of rotating around itself..umm yea..u get the point, huh?
I know this is in your tutorial Alimonster, but i still need some help, that delphi code is messin up my mind, so here goes my code...someone, plz help me..
I know this is in your tutorial Alimonster, but i still need some help, that delphi code is messin up my mind, so here goes my code...someone, plz help me..
#include <windows.h>#include <gl\gl.h>#include <gl\glu.h>#include <cstdio>#include <cstdlib>#include "main.h"#define PI 3.14HDC g_hdc;bool keys[256];float xView, yView;float angle;float radians = angle*PI / 180;float xPos, yPos, zPos;void Initialize(){ glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);}float speed = 0.0;void Render(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); xView = cos(radians); xPos = xView * speed; glRotatef(angle, 0.0, 0.0, xView); glTranslatef(0.0, xPos, -8.0); glBegin(GL_TRIANGLES); glColor3f(1.0, 1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glColor3f(0.3, 0.0, 0.2); glVertex3f(-1.0, -1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); SwapBuffers(g_hdc);}void Resize(int &width, int &height, LPARAM &lParam){ height = HIWORD(lParam); width = LOWORD(lParam); if(height == 0) height = 1; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (float)width/(float)height, 0.1, 10000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}// **********************// FPS CALCULATOR// **********************void Fps(HWND c_hwnd){ static float framesPerSecond = 0.0f; static float lastTime = 0.0f; static char strFrameRate[50] = {0}; float currentTime = GetTickCount() * 0.001f; ++framesPerSecond; if( currentTime - lastTime > 1.0f ) { lastTime = currentTime; sprintf(strFrameRate, "%d", int(framesPerSecond)); SetWindowText(c_hwnd, strFrameRate); framesPerSecond = 0; }}// **********************// KEYBOARD// **********************void Keyboard(WPARAM wParam){ keys[wParam] = true; if(keys[VK_ESCAPE]) PostQuitMessage(0); if(keys[VK_UP]) speed+=0.03; if(keys[VK_DOWN]) speed-=0.03; if(keys[VK_LEFT]) angle++; if(keys[VK_RIGHT]) angle--;}
I didn''t look at your code, but think about it this way:
When you rotate a point using your method, you''re rotating around the origin, irrespective of the location of the point. So, you need to translate the point to the origin you want, rotate it using your formulae, and then translate it back.
Ask if you need clarification.
Peace,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
When you rotate a point using your method, you''re rotating around the origin, irrespective of the location of the point. So, you need to translate the point to the origin you want, rotate it using your formulae, and then translate it back.
Ask if you need clarification.
Peace,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[twitter]warrenm[/twitter]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement