A little assistance needed with my code! Help!!

Hi everyone,

I am new to OpenGL and I am just working on my first little project, where I want to create a nice wave like motion of a series of ellipses (stack on one another), to try to simulate a sea weed. And here’s the code for that:

void display()
{
glPushMatrix();
glRotatef(0,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle1,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle1,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle2,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle2,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle1,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle3,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(angle3,0,0,1);
drawEllipse();
glTranslatef(0,0.6,0);
glRotatef(0,0,0,1);
drawEllipse();
glPopMatrix();

}

void idleCB(void)
{

if( g_animate == 1 )
{
	
	if( g_recording == 0 )
		g_time = g_timer.GetElapsedTime() ;
	else
		g_time += 0.033 ; // save at 30 frames per second.
		
	cout << "current time: " << g_time << endl;
	
	angle = 5*sin(g_time);
	angle1 = 5*sin(g_time);
	angle2 = 10*sin(g_time*1.5);
	angle3 = 15*sin(g_time*2);
	
	glutPostRedisplay() ; 
	
	
	
}

}

Here, g_time is a time step. Every time the display function gets called, the glRotatef() takes in small increments of a function of g_time which is defined inside the idleCB function as a form of a sine function.

This kind of gives me the feel I want to see but still does not compeltely replicate the sinusoidal motion. Can anyone give me better ideas?

I have been working on this for hours at a stretch and my brain has literally stopped functioning. No matter which way I try, it won’t work. Please some body with a smarter idea, HELP ME!!!

Thanks much!

when computing your angle variables you need to add an offset (0<= offset <= 2*PI) to g_time, not multiply g_time with a factor.
Multiplying with a factor changes the frequency of the sine wave, so parts of your sea weed will swing faster than others. Adding an offset to g_time will sample the sine curve at different positions and should give you the desired effect.

Carsten,thanks for getting back. I was debugging for the whole time and decided that this approach that I was taking won’t work well. Instead, I found a better approach (using sin and cos interchangeably) but again, I still have a small problem. First have a look at the new code (much simpler i guess) and then I’ll explain what the problem I am facing:

void weed3()
{

glPushMatrix();
glRotatef(0,0,0,1);

		drawEllipse();
		
		glTranslatef(0,0.6,0);
		glRotatef(theta1,0,0,1);
		drawEllipse();

		glTranslatef(0,0.6,0);
		glRotatef(theta2,0,0,1);
		drawEllipse();
	
		glTranslatef(0,0.6,0);
		glRotatef(theta2,0,0,1);
		drawEllipse();
	
		glTranslatef(0,0.6,0);
		glRotatef(theta1,0,0,1);
		drawEllipse();

glTranslatef(0,0.6,0);
glRotatef(theta1,0,0,1);
drawEllipse();

glTranslatef(0,0.6,0);
glRotatef(theta2,0,0,1);
drawEllipse();

glTranslatef(0,0.6,0);
glRotatef(theta2,0,0,1);
drawEllipse();

glTranslatef(0,0.6,0);
glRotatef(theta1,0,0,1);
drawEllipse();

glTranslatef(0,0.6,0);
glRotatef(theta1,0,0,1);
drawEllipse();

glTranslatef(0,0.6,0);
glRotatef(theta2,0,0,1);
drawEllipse();

glPopMatrix();
}

where the angles are

theta1 = 15sin(g_time);
theta2 = 15
cos(g_time);

updated inside idleCB();

Now this actually produces a nice sine curve but the problem is that since the constant factor ‘15’ is same for all the ellipses, the weed sways a lot more than I want…it almost bends way far to the left and right. Now I might think of a fix…just break the weed into 2 parts (each with 5 ellipses). But somehow, I have to make the upper half translate along with the tip of the lower half. So that way, it doesnt bend so much (coz it wont be affected by the angular rotations of the bottom half). I just don’t know how to translate it in the right way, so that it looks as if the entire weed is a single piece like a chain…

It’s 4:00 am and still can’t go to bed without solving it! It’s really frustrating…any help would be great!!!

you could use smaller angles at the bottom of the stalk (where it is thicker and prob. less flexible), i.e. in your calls to glRotate multiply theta{1,2} with a factor that you increase as you move to the tip of the stalk.

yeah u were right…the thing is I already tried that trick but it never gave me the expected effect. After a few hit and trial combinations, it seems to work fine now!

Thanks!