融入动画技术的交互应用

背景
落花使人惆怅,硕果让人欢乐,人生本就是如此,悲欣交集,希望我们都能够在惨淡的人生中活出自己的一片红花硕果。

技术
运用了向量、力、粒子系统的动画技术,实现了一个交互应用。

截图
融入动画技术的交互应用
融入动画技术的交互应用

点击鼠标触发单个粒子系统的运行,设置了两种粒子,樱花和苹果,粒子受到恒定物体的斥力,最后会自然消亡。

程序
粒子
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
float mass = 1; // Let’s do something better here!

Particle(PVector l) {
acceleration = new PVector(0,0);
velocity = new PVector(random(-1,1),random(-2,0));
position = l.get();
lifespan = 255;
}

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

void applyForce(PVector force) {
acceleration.add(PVector.div(force,mass));
}

void update() {
velocity.add(acceleration);
position.add(velocity);
acceleration.mult(0);
lifespan -= 1.0;
}

void display() {
stroke(255,0,0,lifespan);
fill(255,0,0,lifespan);
ellipse(position.x, position.y, 15, 15);
pushMatrix();
translate(position.x,position.y);
rotate(frameCount / -100);
stroke(0, lifespan);
line(0, 0, 0, -15);
popMatrix();

}

boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}

}

粒子系统
class ParticleSystem{
ArrayList particles;
PVector origin;
float aliveTime;
ParticleSystem(PVector location_){
origin = location_.get();
particles = new ArrayList();
aliveTime = 255;
}
void update(){
origin = new PVector(mouseX, mouseY);
}
void addParticle(){
float Rate = random(1);
if(Rate < 0.5)
particles.add(new Particle(origin));
else
particles.add(new Confetti(origin));
}
void run(){
Iterator it = particles.iterator();
while(it.hasNext()){
Particle p = it.next();
p.run();
if(p.isDead()){
it.remove();
}
}
aliveTime -= 1;
}
boolean isDead(){
if(aliveTime <= 0){
return true;
}else{
return false;
}
}
void applyForce(PVector force){
for(Particle p: particles){
p.applyForce(force);
}
}
void applyRepeller(Repeller re){
for(Particle p: particles){
PVector force = re.repel§;
p.applyForce(force);
}
}
}

排斥的物体
class Repeller{
PVector position;
float r = 10;
float G = 100;
Repeller(float x, float y){
position = new PVector(x, y);
}
void display(){
stroke(0);
fill(0,0);
ellipse(position.x, position.y, r2, r2);
}
PVector repel(Particle p){
PVector dir = PVector.sub(position, p.position);
float d = dir.mag();
d = constrain(d, 5, 100);
dir.normalize();
float force = -1G/(dd);

dir.mult(force);
return dir;

}
}
参考
https://blog.****.net/Ant_look/article/details/50866807
https://blog.****.net/weixin_34128501/article/details/88160171
代码本色教程