互动编程习作——表现随机行为及牛顿运动学

这篇博客探讨了使用Processing实现随机游走、向量应用、力的影响以及角动量和指向运动。通过实例展示了如何创建随鼠标移动的球体、模拟带磁性球体的吸引效应、交互式杠杆旋转以及粒子系统的角动量和指向运动。
摘要由CSDN通过智能技术生成

第一章 随机游走与概率分布

以上所有素材都取自这里!
为了实现随机游走,我采用了:
rect(0,0,width,height);
position.add(velocity);
ellipse(mouseX,mouseY,16,16);
生成一个随鼠标移动的球体,并在背景板留下轨迹。
生成一个在不间断运动的球体:
if ((position.x > width) || (position.x < 0)) {
velocity.x = velocity.x * -1;
}
if ((position.y > height) || (position.y < 0)) {
velocity.y = velocity.y * -1;
}
运动规律是按照速度前进,遇到边界则改变速度方向。
为了使运动的物体和鼠标交互,我采用:
if ((mouseX > position.x) && velocity.x<=0) {
velocity.x = velocity.x * -1;
}
if ((mouseX < position.x) && velocity.x>=0) {
velocity.x = velocity.x * -1;
}
让物体根据鼠标的位置移动,向鼠标靠拢。
效果如图:在这里插入图片描述

第二章 Processing中的向量。

我模拟的中心是一个带磁性的球体,无数的小铁球靠近磁体:
引用了两个类:
attractor类:
class Attractor {
float mass; // Mass, tied to size
PVector position; // position
float g;

Attractor() {
position = new PVector(0,0);
mass = 20;
g = 0.4;
}

PVector attract(Mover m) {
PVector force = PVector.sub(position,m.position); // Calculate direction of force
float distance = force.mag(); // Distance between objects
distance = constrain(distance,5.0,25.0); // Limiting the distance to eliminate “extreme” results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn’t matter here, we just want this vector for direction)
float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}

// Method to display
void display() {
stroke(0);
noFill();
pushMatrix();
translate(position.x,position.y,position.z);
sphere(mass*2);
popMatrix();
}
}
mover类:
class Mover {

PVector position;
PVector velocity;
PVector acceleration;
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) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}

void update() {
velocity.add(acceleration);
position.add(velocity);
acceleration.mult(0);
}

void display() {
stroke(0);
strokeWeight(2);
fill(0);
ellipse(position.x,position.y,mass16,mass16);
}

void checkEdges() {

if (position.x > width) {
  position.x = width;
  velocity.x *= -1;
} else if (position.x < 0) {
  velocity.x *= -1;
  position.x = 0;
}
else if(position.x < mouseX && velocity.x<=0)
{
  velocity.x *= -1;
}

else if(position.x > mouseX && velocity.x>=0)
{
velocity.x *= -1;
}
if (position.y > height) {
velocity.y *= -1;
position.y = height;
}
}
}
随机生成20个小球:
float angle = 0;
Attractor a;
Mover[] movers = new Mover[20];
void setup() {
size(640,360,P3D);
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1,4),0,0);
}
a = new Attractor();
}
小球的移动和磁体的旋转:
background(255);
lights();
for (int i = 0; i < movers.length; i++) {

PVector wind = new PVector(0.01,0);
PVector gravity = new PVector(0,0.1);

movers[i].applyForce(wind);
movers[i].applyForce(gravity);

movers[i].update();
movers[i].display();
movers[i].checkEdges();

}
sphereDetail(12);
translate(mouseX,mouseY);
rotateY(angle);
angle += 0.003;
a.display();
}
效果如图:
在这里插入图片描述

第三章 Processing与力。

一个一直移动的杠杆(跟着鼠标移动):
stroke(0);
rectMode(CENTER);
translate(mouseX, mouseY);
rotate(angle);
line(-50, 0, 50, 0);
stroke(0);
strokeWeight(2);
fill(127);
ellipse(50, 0, 16, 16);
ellipse(-50, 0, 16, 16);
angle += 0.05;
交互:当顺时针旋转且按下RIGHT键时,杠杆旋转方向改变,改为逆时针旋转,反之,当逆时针旋转且按下LEFT键时,杠杆旋转方向改变,改为顺时针旋转:
keyPressed();
if(i== 1)
{
angle-=0.2;}
if(j==1)
{
angle+=0.2;}
}
void update()
{
keyPressed();
}
void keyPressed() {
if (angle>=0 && keyCode= = RIGHT) {
i=1;
}
else if (angle<=0 && keyCode == LEFT) {
j=1;
}
}
效果如图:在这里插入图片描述

第四章:角运动 指向运动

引用四个class:
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;

float r = 6;

Particle(float x, float y) {
acceleration = new PVector();
velocity = PVector.random2D();
position = new PVector(x, y);
lifespan = 255.0;
}

void run() {
update();
display();
}

void intersects(ArrayList particles) {
for (Particle other : particles) {
if (other != this) {
PVector dir = PVector.sub(position, other.position);
if (dir.mag() < r*2) {
dir.setMag(0.5);
applyForce(dir);
}
}
}
}

void applyForce(PVector f) {
acceleration.add(f);
}

// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
acceleration.mult(0);
lifespan -= 0.5;
}

// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(position.x, position.y, r2, r2);
}

// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
}
class Particle1 {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;

float r;

Particle1(float x, float y, float r_) {
acceleration = new PVector(0,0.01);
velocity = PVector.random2D();
velocity.mult(0.5);
position = new PVector(x,y);
lifespan = 255.0;
r = r_;
}

void run() {
update();
display();
}

// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 2.0;
}

// Method to display
void display() {
stroke(255);
fill(100);
rectMode(CENTER);
rect(position.x,position.y,r,r);
}

// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
class ParticleSystem {
ArrayList particles;

ParticleSystem(PVector position) {
particles = new ArrayList();
}

void addParticle(float x, float y) {
particles.add(new Particle(x, y));
}

void display() {
for (Particle p : particles) {
p.display();
}
}

void applyForce(PVector f) {
for (Particle p : particles) {
p.applyForce(f);
}
}

void intersection() {
for (Particle p : particles) {
p.intersects(particles);
}
}

void update() {
for (int i = particles.size()-1; i >= 0; i–) {
Particle p = particles.get(i);
p.update();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
class ParticleSystem1 {
ArrayList particles;

int rows = 50;
int cols = 50;

boolean intact = true;

ParticleSystem1(float x, float y, float r) {
particles = new ArrayList();

for (int i = 0; i < rows*cols; i++) {
  addParticle(x + (i%cols)*r, y + (i/rows)*r, r);
}

}

void addParticle(float x, float y, float r) {
particles.add(new Particle1(x, y, r));
}

void display() {
for (Particle1 p : particles) {
p.display();
}
}

void shatter() {
intact = false;
}

void update() {
if (!intact) {
for (Particle1 p : particles) {
p.update();
}
}
}
}
主函数:
ParticleSystem ps;
ParticleSystem1 ps1;
void setup() {
size(640,360);
ps = new ParticleSystem(new PVector(width/2,50));
ps1 = new ParticleSystem1(0,0,5);
}

void draw() {
background(255);
ps.addParticle(random(200),random(200));

//PVector gravity = new PVector(0,0.1);
//ps.applyForce(gravity);
ps.update();
ps.intersection();
ps.display();
ps1.display();
ps1.update();
}
void mousePressed() {
ps1.shatter();
}
效果如图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值