We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › drawing a "trail" using a buffer array
Page Index Toggle Pages: 1
drawing a "trail" using a buffer array (Read 1432 times)
drawing a "trail" using a buffer array
Sep 10th, 2009, 12:00pm
 
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!
Re: drawing a "trail" using a buffer array
Reply #1 - Sep 10th, 2009, 12:19pm
 
Perhaps I read the code too fast, but on first sight, it seems obvious why locBuffer[locBufferSize - 1] and loc are the same.
It is true at initialization time. It is true when you do locBuffer[locBufferSize - 1] = loc.
I don't see why/when they would differ.
If I miss something obvious, please point it. Smiley

BTW, I think you should do:
float d = PVector.dist(locBuffer[locBufferSize - 1], loc);
as I don't see why you duplicate the vectors before measuring their distance. Not critical, but avoiding unnecessary object creation might sometime be performance critical.
Re: drawing a "trail" using a buffer array
Reply #2 - Sep 10th, 2009, 12:27pm
 
Quote:
Perhaps I read the code too fast, but on first sight, it seems obvious why locBuffer[locBufferSize - 1] and loc are the same.
It is true at initialization time. It is true when you do locBuffer[locBufferSize - 1] = loc.
I don't see why/when they would differ.


i set locBuffer to the current loc which is set in the constructor so that there wouldnt be init lines drawn from the top left corner when the program starts. and also in the update method i shift everything to the left so after a few loops all the values in the index should change... right?


Quote:
BTW, I think you should do:
float d = PVector.dist(locBuffer[locBufferSize - 1], loc);
as I don't see why you duplicate the vectors before measuring their distance. Not critical, but avoiding unnecessary object creation might sometime be performance critical.

all that is true... i meant to remove the get()... i wanted to make certain it was returning a PVector...

so please read through everything again. thanks.
Re: drawing a "trail" using a buffer array
Reply #3 - Sep 10th, 2009, 1:49pm
 
Yes, you shift the values, and as I point out, right away you put loc in the last position of locBuffer.
Page Index Toggle Pages: 1