minim
YaBB Newbies
Offline
Posts: 3
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); } } }