im trying to do this in a bigger program but ive taken daniel shiffmans program called hellomotion from his nature of code class and made some changes to use it as an example. you can also go to the storing input example at examples -> basics -> input -> storinginput
you can download the code at http://www.shiffman.net/itp/classes/nature/week02_s09/motion101/
i made changes to the "thing" class which ill paste below
Code:
// Simple Motion with PVector
// Daniel Shiffman <http://www.shiffman.net>
// The Nature of Code, Spring 2009
// Demonstrates simple motion with a Thing class
// A class to describe a thing in our world
// Has variables for location, velocity, and acceleration
class Thing {
PVector loc;
PVector vel;
PVector acc;
int locBufferSize = 100;
PVector[] locBuffer;
//The Constructor (called when the object is first created)
Thing(PVector a, PVector v, PVector l) {
acc = a.get();
vel = v.get();
loc = l.get();
locBuffer = new PVector[locBufferSize];
//add value to all the indexes in the location buffer array so they don't return voids
for (int i = 0; i < locBufferSize; i++)
locBuffer[i] = loc;
}
//main function to operate object)
void go() {
update();
borders();
render();
}
//function to update location
void update() {
vel.add(acc);
loc.add(vel);
//float d = locBuffer[locBufferSize - 1].dist(loc); // same as below
float d = PVector.dist(locBuffer[locBufferSize - 1].get(), loc.get()); //d returns 0 because locBuffer[locBufferSize - 1] and loc have the same value
println("from "+locBuffer[locBufferSize - 1]+ " to " +loc+" is "+d); // why do locBuffer[locBufferSize - 1] and loc have the same value when locBuffer[locBufferSize - 1] is never updated...
//if the distance between locBuffer[locBufferSize - 1] (last location) and loc (current location) is great than 10 add it to the location buffer to be redrawn as a "trail"
//if(d > 10) {
println(d);
//update location buffer by moving everything 1 index to the left (to the start of the array) and adding the new loc at locBufferSize - 1 which is the last index in the array
for (int i = 1; i < locBufferSize; i++)
locBuffer[i - 1] = locBuffer[i];
//add the new loc at locBufferSize - 1 which is the last index in the array
println(locBuffer[locBufferSize - 1]+ " to " +loc);
locBuffer[locBufferSize - 1] = loc;
//}
}
void borders() {
if (loc.x > width ) loc.x = 0;
if (loc.x < 0 ) loc.x = width;
if (loc.y > height) loc.y = 0;
if (loc.x < 0 ) loc.y = height;
}
//function to display
void render() {
rectMode(CENTER);
stroke(0);
fill(175);
//loop though locBuffer and draw all the locations as ellipses
for (int i = 0; i < locBufferSize; i++)
ellipse(locBuffer[i].x, locBuffer[i].y, 10, 10);
//ellipse(loc.x,loc.y,16,16);
if (showVectors) {
drawVector(vel,loc,10);
}
}
}
all the problems are related to the locBuffer variable i've added which stores all the locations which are looped through and drawn as ellipses in the render method. the problem is that the loc (current location) and the locBuffer[locBufferSize - 1] (last location) are return the same values... and so all the indexes in the array are the same value... why is this happening? ive done this many times before and havent had this problem so it must be something small that im not seeing.
thanks!