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 › Ribbon class that connects to Spring class
Page Index Toggle Pages: 1
Ribbon class that connects to Spring class? (Read 534 times)
Ribbon class that connects to Spring class?
Dec 18th, 2006, 3:24pm
 
Hey all,

I'm using some code I borrowed from Max Kaufman

His code can create basically a piece of cord or string or whatever you want to call it that is affected by gravity as it falls down the screen.  Great for what I wanted to do, which is create three separate ribbons that I can control the length, position, and other properties of.  However, I can't figure out how to make a Ribbon class that connects to this code.  What he was doing before was creating a line using a for statement in the draw function; however, that can't make 3 individual and independently manipulable objects.  

Anyway here is the code.  Any help would be much appreciated.



//VECTOR CLASS/////////////////////////////////////
class Vector
{
 float x,y;
 Vector(float u, float v)
 {
   x = u;
   y = v;
 }
 void add(Vector V)
 {
   x += V.x;
   y += V.y;
 }
 void scale(float Scalar)
 {
   x *= Scalar;
   y *= Scalar;
 }
 void setEqual(Vector V)
 {
   x = V.x;
   y = V.y;
 }
}

//FORCE CLASS/////////////////////////////////////
abstract class Force
{
 abstract Vector forceOn(DynamicNode N);
}

//GRAVITY CLASS/////////////////////////////////////
class Gravity extends Force
{
 float acceleration=0.25f;
 Vector forceOn(DynamicNode N)
 {
   return new Vector(0.0f,acceleration*N.mass);
 }
}

//SPRING CLASS/////////////////////////////////////
class Spring extends Force
{
 float restLength=1.0f, stiffness=1.0f;
 DynamicNode a,b;
 Spring (DynamicNode left, DynamicNode right)
 {
   a = left;
   b = right;
   a.addForce(this);
   b.addForce(this);
 }
 Vector forceOn(DynamicNode N1)
 {
   DynamicNode N2;
   if (N1==a) N2 = b;
   else if (N1 == b) N2 = a;
   else return new Vector(0.0f, 0.0f);
   float dx = N2.position.x-N1.position.x;
   float dy = N2.position.y-N1.position.y;
   float displacement = sqrt(dx*dx+dy*dy);
   if (displacement == 0) return new Vector(0.0f, 0.0f);
   float magnitude = stiffness*(displacement-restLength)/displacement;
   return new Vector(dx*magnitude, dy*magnitude);
 }
}


//POINT CLASS/////////////////////////////////////
class Point
{
 float x,y;
 Point(float u, float v)
 {
   x = u;
   y = v;
 }
 void displace(Vector V)
 {
   x += V.x;
   y += V.y;
 }
}


//DYNAMICNODE CLASS/////////////////////////////////////
class DynamicNode {

 Point position = new Point(0.0f,0.0f);
 float mass = 1.0f;
 boolean pinned;

 float dampening = 1.0f;
 Force[] myForces = new Force[0];
 Vector netForce = new Vector(0.0f,0.0f);
 Vector velocity = new Vector(0.0f,0.0f);
 Vector acceleration = new Vector(0.0f, 0.0f);
 void addForce(Force F)
 {
   Force[] newForces = new Force[myForces.length+1];
   arraycopy(myForces,0,newForces,0,myForces.length);
   newForces[newForces.length-1] = F;
   myForces = newForces;
 }
 void computeForce()
 {
   netForce.x = 0;
   netForce.y = 0;
   for (int i=0; i<myForces.length; i++){
     netForce.add(myForces[i].forceOn(this));
   }
 }


 void update()
 {
   if (!pinned){
     acceleration.setEqual(netForce);
     acceleration.scale(1.0f/mass);
     velocity.add(acceleration);
     velocity.scale(dampening);
     position.displace(velocity);

   }
 }


}
Re: Ribbon class that connects to Spring class?
Reply #1 - Jan 7th, 2007, 3:37pm
 
Got the link to the original code? Once I can see how one ribbon is made I can inform you how a class would be made. All these classes on their own just smacks of a lot of work to illustrate a solution.
Page Index Toggle Pages: 1