目录
序章 随机游走
第一章 向量
第二章 力
第三章 震荡
第四章 粒子系统
序章 随机游走
1.借以回顾本书的中心编程思想——面向对象编程。我们要用面向对象方法来模拟物体在 Processing窗口的运动,随机游走模型就是这个例子的模板。
2.随机游走模型引入了贯穿本书的两个关键问题:如何定义对象的行为规则,以及如何用 Processing模拟这些行为规则。
3.在本书中,我们需要对随机性、概率和Perlin噪声有基本的了解,随机游走模型展示了 其中的关键点,这在我们以后的学习中会很有用。
Perlin噪声
Perlin噪声算法表现出了一定的自然性,因为它能生成符合自然排序(“平滑”)的伪随机 数序列。图I-5展示了Perlin噪声的效果,x轴代表时间;请注意曲线的平滑性。
调整由Perlin噪声功能产生的细节的特征和水平。 类似于物理学中的谐波,噪声是在几个八度计算的。 较低的八度对输出信号贡献更大,因此定义噪声的过强度,而较高的八度将噪声序列中的细节细化。
默认情况下,噪声计算超过4个八度音阶,每个八度音阶的贡献与其前身的比例相差一倍,从第一个八度音阶的50%强度开始。 可以通过添加附加的功能参数来改变该衰减量。
通过改变这些参数,由noise()函数创建的信号可以适应非常特定的需求和特性。
```java
float noiseVal;
float noiseScale=0.02;
void draw() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width/2; x++) {
noiseDetail(3,0.5);
noiseVal = noise((mouseX+x) * noiseScale, (mouseY+y) * noiseScale);
stroke(noiseVal*255);
point(x,y);
noiseDetail(8,0.65);
noiseVal = noise((mouseX + x + width/2) * noiseScale,
(mouseY + y) * noiseScale);
stroke(noiseVal * 255);
point(x + width/2, y);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200104181842684.gif)
**第一章向量**
小球向鼠标方向移动
我们可以将其看成两个不同方向单位矢量的合成
)
```java
class Mover {
// The Mover tracks position, velocity, and acceleration
PVector position;
PVector velocity;
PVector acceleration; // The Mover's maximum speed
float topspeed;
Mover() {
// Start in the center
position = new PVector(width/2,height/2);
velocity = new PVector(0,0);
topspeed = 5;
}
void update() {
// Compute a vector that points from position to mouse
PVector mouse = new PVector(mouseX,mouseY);
PVector acceleration = PVector.sub(mouse,position); // Set magnitude of acceleration
acceleration.setMag(0.2); // Velocity changes according to acceleration
velocity.add(acceleration); // Limit the velocity by topspeed
velocity.limit(topspeed); // position changes by velocity
position.add(velocity);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(position.x,position.y,48,48);
}
}
Mover mover;
void setup() {
size(640,360);
mover = new Mover();
}
void draw() {
background(255); // Update the position
mover.update(); // Display the Mover
mover.display();
}
第二章 力
嗯“s”重启
1.小球
生成一组小球,自由落体后进入水面,在出水面,最后模仿进出过程更好的体现在液体中的阻力,主要运用了牛顿定律中的自由落体的公式`
class Mover {
// position, velocity, and acceleration
PVector position;
PVector velocity;
PVector acceleration;
// Mass is tied to size
float mass;
Mover(float m, float x, float y) {
mass = m;
position = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
void applyForce(PVector force) {
// Divide by mass
PVector f = PVector.div(force, mass);
// Accumulate all forces in acceleration
acceleration.add(f);
}
void update() {
// Velocity changes according to acceleration
velocity.add(acceleration);
// position changes by velocity
position.add(velocity);
// We must clear acceleration each frame
acceleration.mult(0);
}
// Draw Mover
void display() {
stroke(0);
strokeWeight(2);
fill(127, 200);
ellipse(position.x, position.y, mass*16, mass*16);
}
// Bounce off bottom of window
void checkEdges() {
if (position.y > height) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
position.y = height;
}
}
}
2.液体
根据阻力公式,计算小球在液体中所受的阻力。
class Liquid {
// Liquid is a rectangle