I suggest you change the initial value of loc in your Body’s constructor from loc=new Vec2D(50,50); to this loc=new Vec2D(width/2,height/2);. This will ensure your line starts at the center of the sketch. If you do a test with your arduino, if instead of going forward, you travel backwards, you should be able to see it in your sketch.
I am not familiar with the library So I am not sure if it is right. I would think you can infer orientation from the acc vector normalized to unity. I did a quick search and this might be useful http://www.chrobotics.com/library/accel-position-velocity On a side note, I notice than in update(), you modify the vector loc and diff. In display(), you are still modifying loc, since you are calling loc.addSelf(a).x. This affect the rendering of your final product. You can easily see that the following two codes provide different results:
CODE1: points
void display()
{
strokeWeight(3);
stroke(255);
strokeWeight(5);
point(loc.addSelf(a).x, loc.addSelf(a).y);
stroke(255, 0, 0);
strokeWeight(2);
//line(dif.x, dif.y, loc.x, loc.y);
}
CODE2: lines
void display()
{
strokeWeight(3);
stroke(255);
strokeWeight(5);
//point(loc.addSelf(a).x, loc.addSelf(a).y);
stroke(255, 0, 0);
strokeWeight(2);
line(dif.x, dif.y, loc.x, loc.y);
}
I suggest you check the documentation to addSelf() and make sure you are calling this function properly. I think you should call it only once. However I am not sure at the moment. When you reference a vector, you access its components directly. No need to call addSelf().
Related to the acceleration question, There are two ways to do this, the right way and the what-it-seems-easy and fun way but not the so-right way.
The right way is to study a bit of physics, if you haven’t done so, and understand how you can get position from acceleration. It requires to perform a double integral. I believe this is what you are doing but I am not convinced it is totally right. One needs to look at the documentation of the library you are using to verify if you are doing this properly or what would be the proper way to do it.
The fun way is for you to run two simple experiments where you collect data. Start at the center of an empty room. You start your device and you run forward for a some distance… say 10 meters. A second experience is that you start from the same position and you run backwards 10 meters. If you plot both data sets (this is why I suggested to move the center of your start point in your constructor to the middle of the sketch), is one in opposite direction to the other one? Are they both the same length?
Another different exercise is to run an experiment moving in the same direction, starting from the same position, but moving different distances, say 6, 8 and 12m. If the length of the line that you plot for the first data set half of the third data set? Does the length of the like for the 8 m fall between the length of the line for the 6 m and the 12m experiences?
Kf