Animate a polygon

Started by
1 comment, last by 77outcomes 8 years, 11 months ago

I was able to animate a GL_TRIANGLE, but when I tried to animate a GL_POLYGON using a similar method it did not work. I am programming in Java (with JOGL).

My code:

helloworld.java

circle.java

Here is is the code I used to animate a triangle:

simplescene.java

To test the triangle animation, change


canvas.addGLEventListener(new circle());

to


canvas.addGLEventListener(new simplescene());

in helloworld.java

Advertisement

What is at least wrong inside circle.java is that the variable angle is initialized to zero and then counted up to 360 within drawCircle, but it is never reset to zero. So the condition


while(angle <= 360.0)

will inhibit the generation of a second set of vertices. You want to use a for loop instead of a while loop here, because a for loop is useable to reset angle each time the loop is entered.

BTW: A principle issue you have here is that variables like angle, s and c should be local variables in the scope of the routine drawCircle, because they are used just for the inner workings of that routine. Besides that, short names like s and c may be used for loop variables (often i, j, or n are used for that), but they should never be used as member variables. Instead use expressive names.

What is at least wrong inside circle.java is that the variable angle is initialized to zero and then counted up to 360 within drawCircle, but it is never reset to zero. So the condition


while(angle <= 360.0)

will inhibit the generation of a second set of vertices. You want to use a for loop instead of a while loop here, because a for loop is useable to reset angle each time the loop is entered.

BTW: A principle issue you have here is that variables like angle, s and c should be local variables in the scope of the routine drawCircle, because they are used just for the inner workings of that routine. Besides that, short names like s and c may be used for loop variables (often i, j, or n are used for that), but they should never be used as member variables. Instead use expressive names.

Yes, you are right I forgot to reset the angle. Thanks for pointing it out ^^

This topic is closed to new replies.

Advertisement