This week we looked at a representation of a robot arm. The body of our robot is allowed to slide horizontally and the arm can pivot at the shoulder (where the upper arm attaches to the body) and at the elbow (where the lower arm attaches to the upper arm).
An important point about this project was to show how transformations add up together. Transformations include translations (moving in straight line), rotations (turning about a pivot point) and scaling (changing size). We’ve used translations and rotations for our robot.
In P5, the origin, or place where the x and y coordinates are both zero is at the upper left-hand corner of the screen. The x coordinate gets bigger as we move right and the y coordinate gets bigger as we move down.
When we translate, we move the origin to somewhere else. This is handy for a few reasons but if we are performing rotations. All rotations happen around the origin, wherever that happens to be at the time.
This diagram shows all the transformations we use for our robot:
- Translate to move the origin to the centre of the robot body
- Translate to move the origin to the shoulder
- Upper arm rotation at the shoulder
- Translate to move the origin to the elbow
- Lower arm rotation at the elbow
Because these transformations stack up on top of each other, this is what each part experiences:
- Body – Transformations: 1
- Upper Arm – Transformations: 1, 2, 3
- Lower Arm – Transformations: 1, 2, 3, 4, 5
The body is only affected by one, but the lower arm is affected by all five.
To move the robot, we set up three variables:
- bodyPos to store the body position (our first transformation)
- upperArmAngle to store the rotation at the shoulder (our third transformation)
- lowerArmAngle to store the rotation at the elbow (our fifth transformation)
We created a function called handleInput() called from the draw() function (which is called every frame). In that we used the keyIsDown() function from P5 to check for keys being held down. We made the left/right arrow keys move the body horizontally, the up/down arrow keys rotate at the shoulder and the Z/X keys to rotate at the elbow.
As always, the code can be downloaded from our GitHub repository.