Code Samples

Here I will be posting code snippets that I am particularly happy with. This does NOT mean they will be super magnificent works of codemanship that perfectly balance the aspects of clean coding and software engineering principles. These will be samples where in the end I went: "Ah ha! This works just as I wanted!", more of personal eureka moments that brought a moment of achievement. :-)
*Until I figure out a nicer way of displaying them, they will be presented as they are now*

--------------------------------------------------------------------------------------------------------------------------------------------------
This is the simple smoothing algorithm that I made to handle the accelerometer input data. Basically what it does is: using two previous input values, it creates a line through them, predicts a third value, then scales down the prediction with the current accelerometer data to get a new smoothed value. The scaling factor (in this case 0.33333) can be changed to either smooth it more (0.2) or allow for more jitter (0.5). Depending how it is scaled, the input values would need to be adjusted. If the scale is decreased, the controls will lag behind the smoothed value, to fix this, exaggerate the input values. On the other hand, if the scale is increased, the controls will be exaggerated and the input would need to be scaled down.


                // Smooth the LeftRightRotation value
                // Create two points from each of the previous values
                Vector2 a = new Vector2(0, previousLRRot[0]);
                Vector2 b = new Vector2(1, previousLRRot[1]);
                // Find the slope of the line created by the two points
                float slope = (b.Y - a.Y) / (b.X - a.X);
                // Find the y-intersect
                float init = a.Y - (slope * a.X);   // y = slope * x + init
                // Find the predicted y value at the next time step
                float predictedY = slope * 2 + init;
                // Calculated the smoothed leftRightRotation value using our predicted y value
                leftRightRotation = (leftRightRotation + predictedY) * 0.33333;
                // Shift the previous values and add the new one
                previousLRRot[0] = previousLRRot[1];
                previousLRRot[1] = leftRightRotation;

                // Smooth the UpDown value
                // Create two points from each of the previous values
                a = new Vector2(0, previousUDRot[0]);
                b = new Vector2(1, previousUDRot[1]);
                // Find the slope of the line created by the two points
                slope = (b.Y - a.Y) / (b.X - a.X);
                // FInd the y-intersect
                init = a.Y - (slope * a.X);   // y = slope * x + init
                // Find the predicted y value at the next time step
                predictedY = slope * 2 + init;
                // Calculated the smoothed leftRightRotation value using our predicted y value
                upDownRotation = (upDownRotation + predictedY) * 0.33333;
                // Shift the previous values and add the new one
                previousUDRot[0] = previousUDRot[1];
                previousUDRot[1] = upDownRotation;

--------------------------------------------------------------------------------------------------------------------------------------------------