Horse
We plan the horse like this. Note that all parts are cylinders and thus have a radius and a length.
public static final float BASE=1.0f; public static final float BODY_LENGTH=BASE*1.0f; public static final float BODY_RADIUS=BASE*0.3f; public static final float LEG_UPPER_LENGTH=BASE*0.65f; public static final float LEG_LOWER_LENGTH=BASE*0.4f; public static final float LEG_RADIUS=BASE*0.06f; public static final float NECK_LENGTH=BASE*0.65f; public static final float NECK_RADIUS=0.06f; public static final float HEAD_LENGTH=BASE*0.4f; public static final float HEAD_RADIUS=BASE*0.15f; private static final int SLICES=10; private static final int STACKS=10;
and the angels
// the horse know how to walk ? private static final int[] HIP_V = { 0, 5, 10, 15, 20 ,25, 20, 15 ,10,5, 0, -5,-10,-15,-20,-25,-20,-15,-10,-5 }; private static final int[] KNEE_V = {0, 0,0,0,10,20,10,0,0,0, 0, 0,0,0,0,10,20,10,0,0,0}; // dynamic angle for moves of head public float m_neck_angle=0.0f; public float m_head_angle=90.0f; // when we walk: private int movePos=0; private int[]front_left_leg={0,0}; private int[]front_right_leg={0,0}; private int[]back_left_leg={0,0}; private int[]back_right_leg={0,0};
The complete class for the horse, oneHorse:
Animation
We want the horse to start walking when we click a walk-button in the interface, and we want it to continue walking until we click again. We do this by letting the main class, Horse, implement Runnable and we generate a new Thread.
private void jToggleButtonWalkActionPerformed(ActionEvent evt) { if(jToggleButtonWalk.isSelected()) { glr.doWalk(true); isRunning=true; new Thread(new Horse()).start(); } else { glr.doWalk(false); isRunning=false; } }
The run.method is implemented like this:
public void run() { while(isRunning) { try { thePanel.display(); Thread.sleep(200); } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } } }
For this to work we must make the involved elements static, so they are known across threads:
GLRenderer glr; // keep these across threads static boolean isRunning=false; static GLJPanel thePanel=null; public Horse() { initComponents(); setTitle("Simple JOGL Application"); glr=new GLRenderer(); panel.addGLEventListener(glr); if(thePanel==null) thePanel=panel; }
Note that the animator that is introduced in the "standard"-application generated by Netbeans is not of much help here.
Another animal
The Main Class, Horse, does not know what animal we are working with. This is completely administrated by GLRenderer. GLRenderer sets up the horse, rotates it, give it its material and conveys all necessary angles. The complete GLRenderer is like this:
Inspecting this, it should be rather straight-forward to implement a different "pin-figure" with some animation.
- The program project: https://svnit.hiof.no/svn/psource/JOGL/horse