In our last project, we improved our Minecraft controller code to make it simpler and more readable (and also more extensible). But, with the current code, our controller is awkward because it requires us to rotate it away from us (which is difficult to do) in order for our character to look down.
In this project, we're going to improve that — we're going to modify the tilt functionality so that our character is looking down while the controller is in the default position (horizontal or "flat on a table") and then we only need to rotate the controller towards us (which is easy) to have him look upwards.
For this project, the changes to the code will be in the
while
loop, specifically around the control.look()
function, which defines where the character is looking.
In the previous project, we used the X and Y values of the accelerometer to control which direction we looked:
And that works... But there are two things we can improve on:
Too hard to look down: To look down, you have to point the controller down, and it can be difficult to twist your wrists enough to tilt it fully down. We can adjust this.
Controller is not sensitive enough: Currently, when you tilt the controller a little, Steve moves a little and when you tilt a lot, Steve moves a lot. That works reasonably well, but the current code doesn't take that far enough.
Here is what the code for this project should look like at this point (with new changes highlighted with arrows):
The above highlighted changes are:
On Line 24, we decrease the Y value by a small amount (0.3). Now, when we hold the kit at a more comfortable angle (with Y pointing a little bit up) Steve won't move at all.
On Line 25, we modify the accelerometer input we receive. Previously, we converted the
accelerometer force to a reasonable value using the equation 50*x
that could
be inserted into the look()
(and likewise for y
). That made a small
value of x
move Steve a little, and a large value move him a lot. We can
increase this effect by using x-squared — a small value will still move him a little, and
a large value will move him even more. To use x-squared, you would expect the the code
to be x*x
, but instead we use x*abs(x)
. The reason is that
x*x
changes negative numbers to positive, which we don't want. Our code that uses
abs()
keeps negative x
values negative.
Also on Line 25, we've adjusted the multiplier from 50 to 150. There is nothing scientific about this change — this just seemed to work better based on our experimentation!
Go ahead and give your modified controller a try...
We increased the sensitivity of the controller by using x-squared instead of x. Can you increase this more by using x-cubed?
The controller is now more comfortable to handle, because the default position is to keep it slightly tilted up. One issue, however, is that when you set the kit on a table, Steve will look down at the ground. Can you detect when the CREATOR Kit is placed flat on a table, and have Steve look straight ahead?