From 9d2f034864f147f30c9bb823c951348eb572e9f3 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 10 Jan 2014 17:15:15 +0100 Subject: Walk animation WIP DumbWalkAnimator implementation is for testing purposes. Its constants are hard-coded. --- src/DumbWalkAnimation.java | 53 +++++++++++++++++++++++++++++++++++++ src/Robot.java | 32 ++++++++++++++++++----- src/WalkAnimation.java | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 src/DumbWalkAnimation.java create mode 100644 src/WalkAnimation.java diff --git a/src/DumbWalkAnimation.java b/src/DumbWalkAnimation.java new file mode 100644 index 0000000..7ff44d1 --- /dev/null +++ b/src/DumbWalkAnimation.java @@ -0,0 +1,53 @@ + +/** + * A WalkAnimation that does not result in a smooth transition. The formula and + * constants are chosen at random. + * + * @author Peter Wu + */ +public class DumbWalkAnimation implements WalkAnimation { + + private double robot_pos_meters; + + /** + * Sets the new position for the robot. + */ + public void updatePosition(double pos) { + this.robot_pos_meters = pos; + } + + private double getTime() { + return robot_pos_meters / 5; + } + + @Override + public double getLegAngleLeft() { + return Math.sin(-getTime()) * 30.0; + } + + @Override + public double getLegAngleRight() { + return -getLegAngleLeft(); + } + + @Override + public double getKneeAngleLeft() { + return 75.0 + Math.abs(Math.cos(getTime()) * 90.0); + } + + public double getKneeAngleRight() { + return getKneeAngleLeft(); + } + + @Override + public double getArmAngleLeft() { + // static non-moving arms. + return 0; + } + + @Override + public double getArmAngleRight() { + // static non-moving arms. + return 0; + } +} diff --git a/src/Robot.java b/src/Robot.java index 3b9c0cb..3eeb1af 100644 --- a/src/Robot.java +++ b/src/Robot.java @@ -63,6 +63,8 @@ class Robot extends BetterBase { */ private final static double TRACK_LENGTH = 76.0; + private final WalkAnimation walkAnim; + /** * Constructs the robot with initial parameters. */ @@ -85,6 +87,7 @@ class Robot extends BetterBase { this.boneSize = 0.02f; this.depth = 0.24f; this.laneNo = laneNo; + this.walkAnim = new DumbWalkAnimation(); } /** @@ -105,6 +108,9 @@ class Robot extends BetterBase { // stands on the XY plane. gl.glTranslatef(0, 0, torsoHeight / 2 + legLength + footHeight); + // calculate rotation angles and positions for movements. + walkAnim.updatePosition(robot_pos_meters); + // Draw the robot, everything is relative to the center of torso. // Static parts (that do not animate): @@ -132,11 +138,11 @@ class Robot extends BetterBase { // Parts that should be animated: // draw left and right legs - drawLeg(false); - drawLeg(true); + drawLeg(false, walkAnim.getLegAngleLeft(), walkAnim.getKneeAngleLeft()); + drawLeg(true, walkAnim.getLegAngleRight(), walkAnim.getKneeAngleRight()); // draw left and right arms - drawArm(false); - drawArm(true); + drawArm(false, walkAnim.getArmAngleLeft()); + drawArm(true, walkAnim.getArmAngleRight()); // // The following function call exist to make a point clear. Adding @@ -369,8 +375,10 @@ class Robot extends BetterBase { /** * Draws the upper and lower legs and feet of the robot. * @param isRight True if at the robot's right (from the robot POV). + * @param hip_angle Angle at the hip in degrees. + * @param knee_angle Angle behind knee in degrees. */ - private void drawLeg(boolean isRight) { + private void drawLeg(boolean isRight, double hip_angle, double knee_angle) { // save center position gl.glPushMatrix(); @@ -382,6 +390,9 @@ class Robot extends BetterBase { gl.glTranslatef(leg_top_x, 0f, -torsoHeight / 2); drawJoint(); + // rotate upper leg around hips + gl.glRotated(hip_angle, 1, 0, 0); + // upper leg half gl.glTranslatef(0, 0, -legLength / 4); drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY, false); @@ -390,6 +401,9 @@ class Robot extends BetterBase { gl.glTranslatef(0, 0, -legLength / 4); drawJoint(); + // rotate lower leg around back of knee (clock-wise "180 - knee angle") + gl.glRotated(knee_angle - 180, 1, 0, 0); + // lower leg half gl.glTranslatef(0, 0, -legLength / 4); drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY, false); @@ -410,10 +424,11 @@ class Robot extends BetterBase { } /** - * Draw both arms and both hands of the robot. + * Draw both arms and both hands of the robot. * @param isRight True if at the robot's right (from the robot POV). + * @param armAngle angle of the arm in degrees. */ - private void drawArm(boolean isRight) { + private void drawArm(boolean isRight, double armAngle) { // Push the translation matrix so we can return to the origin gl.glPushMatrix(); @@ -426,6 +441,9 @@ class Robot extends BetterBase { gl.glTranslatef(arm_x, 0, torsoHeight / 2); drawJoint(); + // rotate arm around shoulder + gl.glRotated(armAngle, 1, 0, 0); + // here is your arm (start drawing from the elbow) gl.glTranslatef(0, 0, -armLength / 2); drawBeam(armWidth, armWidth, armLength, Direction.Z, Colors.ARM_GRAY_COLOR, false); diff --git a/src/WalkAnimation.java b/src/WalkAnimation.java new file mode 100644 index 0000000..f66e7df --- /dev/null +++ b/src/WalkAnimation.java @@ -0,0 +1,65 @@ + +/** + * Provides details for animating a walk. + * + * @author Peter Wu + */ +interface WalkAnimation { + + /** + * Sets the new position for the robot. + * + * @param pos Position in meters. + */ + void updatePosition(double pos); + + /** + * Finds the angle between the left upper leg and the rotated upper leg. If + * the robot does not move, the angle is probably 0. When the leg is + * positioned behind the robot, the angle is negative. Similarly, when the + * robot is positioned forward, the angle is positive. + * + * @return angle in degrees. + */ + public double getLegAngleLeft(); + + /** + * Finds the angle between the right upper leg and the rotated upper leg. If + * the robot does not move, the angle is probably 0. When the leg is + * positioned behind the robot, the angle is negative. Similarly, when the + * robot is positioned forward, the angle is positive. + * + * @return angle in degrees. + */ + public double getLegAngleRight(); + + /** + * Finds the angle behind the left knee. + * + * @return angle in degrees. + */ + public double getKneeAngleLeft(); + + /** + * Finds the angle behind the right knee. + * + * @return angle in degrees. + */ + public double getKneeAngleRight(); + + /** + * Finds the angle between the left arm and the rotated arm. Similar to + * getLegAngleLeft(). + * + * @return angle in degrees. + */ + public double getArmAngleLeft(); + + /** + * Finds the angle between the right arm and the rotated arm. Similar to + * getLegAngleRight(). + * + * @return angle in degrees. + */ + public double getArmAngleRight(); +} -- cgit v1.2.1