Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 225 lines (174 sloc) 5.352 kb
8b48c762 »
2011-03-23 first commit
1 /*
2
3
4 vincent[at]flyingoctopus.com
5 http://flyingoctopus.net/
6
7 Built with Processing (Beta) v0135
8 uses Geomerative (Alpha) v004
9 ----------------------------------------------------------------
10
11 Created 24 February 2008
12 * Revised to 24 February 2008
13 * uses Geomerative (Alpha) v034
14
15 ----------------------------------------------------------------
16 Elastic-Type
17 ----------------------------------------------------------------
18
19 */
20
21 import traer.physics.*;
22 import java.util.Vector;
23 import geomerative.*;
24
25 RGroup group;
26
27 RFont f;
28
29 Vector tendrils;
30 ParticleSystem physics;
31 Particle mouse;
32
33 //------------------------ Runtime properties ----------------------------------
34 // Save each frame
35 boolean SAVEVIDEO;
36 boolean SAVEFRAME;
37 //------------------------------------------------------------------------------
38
39 //------------------------ Drawing properties ----------------------------------
40 // Text to be drawn
41 String STRNG;
42
43 // Font to be used
44 String FONT;
45
46 // Margin from the borders
47 float MARGIN;
48 //------------------------------------------------------------------------------
49
50
51 String newString = "";
52
53
54 void setup(){
55 size(1000,560);
56 frameRate(30);
57
58 SAVEVIDEO = false;
59 SAVEFRAME = false;
60
61 STRNG = "vincent";
62 FONT = "DroidSansMono.ttf";
63 MARGIN = 7;
64
65 try{
66 smooth();
67 }
68 catch(Exception e){
69 }
70 stroke( 0 );
71 background(255);
72 cursor( CROSS );
73
74
75 RCommand.setSegmentator(RCommand.UNIFORMLENGTH);
76 RCommand.setSegmentLength(10);
77
78 initialize();
79 }
80
81 void draw(){
82 background( 255 );
83 translate(width/2, height/2);
84
85 if(mousePressed){
86 mouse.position().set( mouseX-width/2, mouseY-height/2, 0 );
87 for ( int i = 0; i < tendrils.size(); ++i )
88 {
89 T3ndril t = (T3ndril)tendrils.get( i );
90 t.firstspring.turnOn();
91 }
92 }else{
93 for ( int i = 0; i < tendrils.size(); ++i )
94 {
95 T3ndril t = (T3ndril)tendrils.get( i );
96 t.firstspring.turnOff();
97 }
98 }
99
100 physics.tick( 1.0f );
101
102 stroke(0);
103
104 for ( int i = 0; i < tendrils.size(); ++i )
105 {
106 T3ndril t = (T3ndril)tendrils.get( i );
107 drawElastic( t );
108 }
109
110 if(SAVEVIDEO) saveFrame(STRNG+"video-####.tga");
111 }
112
113 void drawElastic( T3ndril t )
114 {
115 float lastStretch = 1;
116 for ( int i = 0; i < t.particles.size()-1; ++i )
117 {
118 Vector3D firstPoint = ((Particle)t.particles.get( i )).position();
119 Vector3D firstAnchor = i < 1 ? firstPoint : ((Particle)t.particles.get( i-1 )).position();
120 Vector3D secondPoint = i+1 < t.particles.size() ? ((Particle)t.particles.get( i+1 )).position() : firstPoint;
121 Vector3D secondAnchor = i+2 < t.particles.size() ? ((Particle)t.particles.get( i+2 )).position() : secondPoint;
122
123 //float springStretch = 2.5f/((Spring)t.springs.get( i )).stretch();
124 Spring s = (Spring)t.springs.get( i );
125 float springStretch = 2.5*s.restLength()/s.currentLength();
126
127 strokeWeight( (float)((springStretch + lastStretch)/2.0f) ); // smooth out the changes in stroke width with filter
128 lastStretch = springStretch;
129
130 curve( firstAnchor.x(), firstAnchor.y(),
131 firstPoint.x(), firstPoint.y(),
132 secondPoint.x(), secondPoint.y(),
133 secondAnchor.x(), secondAnchor.y() );
134 }
135 }
136
137 void keyReleased(){
138 initialize();
139 //exit();
140 //saveFrame(STRNG+"-###.tga");
141 }
142
143 void initialize(){
144 if(tendrils!=null){
145 tendrils.clear();
146 }
147
148 if(physics!=null){
149 physics.clear();
150 }
151
152 RG.init(this);
153 f = new RFont(this.FONT,372,RFont.CENTER);
154 group = f.toGroup(STRNG);
155
156 RCommand.setSegmentator(RCommand.UNIFORMLENGTH);
157 RCommand.setSegmentLength(20);
158
159 group = group.toPolygonGroup();
160 group.centerIn(g, MARGIN, 1, 1);
161
162 physics = new ParticleSystem( 0.0f, 0.05f );
163
164 mouse = physics.makeParticle();
165 mouse.makeFixed();
166
167 tendrils = new Vector();
168
169 if(group!=null){
170 for(int k=0;k<group.countElements();k++){
171 RPolygon p = (RPolygon)(group.elements[k]);
172 if(p!=null){
173 for(int i=0;i<p.countContours();i++){
174 RPoint[] ps = p.contours[i].getPoints();
175 if(ps!=null){
176 tendrils.add( new T3ndril( physics, new Vector3D( ps[0].x, ps[0].y, 0 ), mouse ) );
177 for(int j=1;j<ps.length;j++){
178 ((T3ndril)tendrils.lastElement()).addPoint( new Vector3D( ps[j].x, ps[j].y, 0 ) );
179 }
180 ((T3ndril)tendrils.lastElement()).addPoint( new Vector3D( ps[0].x, ps[0].y, 0 ) );
181 }
182 }
183 }
184 }
185 }
186 }
187
188 class T3ndril
189 {
190 public Vector particles;
191 public Vector springs;
192 public Spring firstspring;
193 ParticleSystem physics;
194
195 public T3ndril( ParticleSystem p, Vector3D firstPoint, Particle followPoint )
196 {
197 particles = new Vector();
198 springs = new Vector();
199
200 physics = p;
201
202 Particle firstParticle = p.makeParticle( 1.0f, firstPoint.x(), firstPoint.y(), firstPoint.z() );
203 particles.add( firstParticle );
204 firstspring = physics.makeSpring( followPoint, firstParticle, 0.1f, 0.1f, 5 );
205 }
206
207 public void addPoint( Vector3D p )
208 {
209 Particle thisParticle = physics.makeParticle( 1.0f, p.x(), p.y(), p.z() );
210 springs.add( physics.makeSpring( ((Particle)particles.lastElement()),
211 thisParticle,
212 1.0f,
213 1.0f,
214 ((Particle)particles.lastElement()).position().distanceTo( thisParticle.position() ) ) );
215 particles.add( thisParticle );
216 }
217 }
218
219
220 void resize(int w, int h)
221 {
222 super.resize(w,h);
223 if (g != null)
224 smooth();
225 }
Something went wrong with that request. Please try again.