forked from hapticdata/toxiclibsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoftBodySquare_pjs.html
120 lines (109 loc) · 3.87 KB
/
SoftBodySquare_pjs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!doctype html>
<html lang="en">
<head>
<title>Soft Body Square - toxiclibs.js examples</title>
<meta name="author" content="Kyle Phillips">
<!-- Date: 2011-01-09 -->
<script type="text/javascript" src="../build/toxiclibs.js"></script>
<script type="text/javascript" src="js/processing.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen">
</head>
<body class="container_12">
<header class="grid_12">
<hgroup>
<h1>Soft Body Sqaure</h1><h2>w/ <a href="http://processingjs.org">Processing.js</a></h2>
</hgroup>
</header>
<script type="text/processing" data-processing-target="myCanvas">
/*
* Copyright (c) 2008-2009 Karsten Schmidt
*
* This demo & library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
int DIM=10;
int REST_LENGTH=20;
float STRENGTH=0.125;
float INNER_STRENGTH = 0.13;
VerletPhysics2D physics;
VerletParticle2D head,tail;
void setup() {
size(960, 582);
smooth();
physics=new toxi.physics2d.VerletPhysics2D();
physics.addBehavior(new toxi.physics2d.GravityBehavior(new toxi.Vec2D(0,0.1)));
physics.setWorldBounds(new toxi.Rect(0,0,width,height));
for(int y=0,idx=0; y<DIM; y++) {
for(int x=0; x<DIM; x++) {
VerletParticle2D p = new toxi.physics2d.VerletParticle2D(x*REST_LENGTH,y*REST_LENGTH);
physics.addParticle(p);
if (x>0) {
VerletSpring2D s= new toxi.physics2d.VerletSpring2D(p,physics.particles[idx-1],REST_LENGTH,STRENGTH);
physics.addSpring(s);
}
if (y>0) {
VerletSpring2D s = new toxi.physics2d.VerletSpring2D(p,physics.particles[idx-DIM],REST_LENGTH,STRENGTH);
physics.addSpring(s);
}
idx++;
}
}
VerletParticle2D p=physics.particles[0];
VerletParticle2D q=physics.particles[physics.particles.length-1];
float len=sqrt(sq(REST_LENGTH*(DIM-1))*2);
VerletSpring2D s=new toxi.physics2d.VerletSpring2D(p,q,len,INNER_STRENGTH);
physics.addSpring(s);
p=physics.particles[DIM-1];
q=physics.particles[physics.particles.length-DIM];
s=new toxi.physics2d.VerletSpring2D(p,q,len,INNER_STRENGTH);
physics.addSpring(s);
var headIdx = (DIM-1)/2;
console.log(headIdx)
head=physics.particles[Math.floor(headIdx)];
head.lock();
}
void draw() {
background(0);
stroke(255);
head.set(mouseX,mouseY);
physics.update();
for(int i =0; i< physics.springs.length; i++) {
VerletSpring2D s = physics.springs[i];
line(s.a.x,s.a.y,s.b.x,s.b.y);
}
}
</script>
<canvas id="myCanvas" width="200" height="200">
your browser doesnt support canvas
</canvas>
<section class="grid_12" id="details">
<aside id="authors" class="grid_6">
<p>original example by <a href="http://postspectacular.com">Karsten Schmidt</a><br/>
Toxiclibs ported to js by <a href="http://workofkylephillips.com">Kyle Phillips</a></p>
</aside>
<aside id="usage" class="grid_6">
<p><strong>Usage:</strong><br><ul>
<li>move mouse to drag/deform the square</li>
</ul></p><br>
</aside>
<article id="description" class="grid_12">
<p>Softbody square demo is showing how to create a 2D square mesh out of
verlet particles and make it stable enough to avoid total structural
deformation by including an inner skeleton.</p>
</article>
</section>
</body>
</html>