Java 如何使粒子对象跟随鼠标移动
我希望粒子对象在移动时简单地跟随mouseX和mouseY位置。我可以用mouseX和Y替换原点值吗Java 如何使粒子对象跟随鼠标移动,java,processing,Java,Processing,我希望粒子对象在移动时简单地跟随mouseX和mouseY位置。我可以用mouseX和Y替换原点值吗 void run() { update(); display(); } // Method to update position void update() { velocity.add(acceleration); location.add(velocity); lifespan -= 2.0; } // Method t
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(location.x, location.y, 12, 12);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
看看这个例子:
PVector pos;
PVector vel;
PVector acc;
void setup()
{
size(400, 400);
pos = new PVector(width / 2, height / 2);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}
void draw()
{
background(255);
followMouse();
update();
ellipse(pos.x, pos.y, 10, 10);
}
void followMouse()
{
PVector mouse = new PVector(mouseX, mouseY);
//calculating what acceleration would be needed to instantly reach the mouse
acc = mouse.sub(pos);
//reducing the acceleration to not reach the mouse instantly
acc.mult(0.1);
}
void update()
{
vel.add(acc);
//dampening the velocity, so the ball gets slower when close to the mouse
vel.mult(0.8);
pos.add(vel);
}
看看这个例子:
PVector pos;
PVector vel;
PVector acc;
void setup()
{
size(400, 400);
pos = new PVector(width / 2, height / 2);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}
void draw()
{
background(255);
followMouse();
update();
ellipse(pos.x, pos.y, 10, 10);
}
void followMouse()
{
PVector mouse = new PVector(mouseX, mouseY);
//calculating what acceleration would be needed to instantly reach the mouse
acc = mouse.sub(pos);
//reducing the acceleration to not reach the mouse instantly
acc.mult(0.1);
}
void update()
{
vel.add(acc);
//dampening the velocity, so the ball gets slower when close to the mouse
vel.mult(0.8);
pos.add(vel);
}
这个例子更简单,更容易理解,希望能有所帮助
float x;
float y;
float easing = 0.05;
void setup() {
size(640, 360);
noStroke();
}
void draw() {
background(51);
float targetX = mouseX;
float dx = targetX - x;
x += dx * easing;
float targetY = mouseY;
float dy = targetY - y;
y += dy * easing;
ellipse(x, y, 66, 66);
}
这个例子更简单,更容易理解,希望能有所帮助
float x;
float y;
float easing = 0.05;
void setup() {
size(640, 360);
noStroke();
}
void draw() {
background(51);
float targetX = mouseX;
float dx = targetX - x;
x += dx * easing;
float targetY = mouseY;
float dy = targetY - y;
y += dy * easing;
ellipse(x, y, 66, 66);
}
Processing的lerp函数适用于这种情况,因为您正在寻找要lerp到鼠标光标的粒子
posX = lerp(posX, mouseX, 0.5);
posY = lerp(posY, mouseY, 0.5);
这里的最后一个参数0.5是两个值之间的插值量,其中0.1非常接近当前粒子位置;0.5介于粒子位置和光标位置之间,等等
Lerp是线性插值的缩写。您可能会发现其他形式的插值或缓和更适合于模拟粒子的运动,例如来自penner easing集合的插值或缓和:
Processing的lerp函数适用于这种情况,因为您正在寻找要lerp到鼠标光标的粒子
posX = lerp(posX, mouseX, 0.5);
posY = lerp(posY, mouseY, 0.5);
这里的最后一个参数0.5是两个值之间的插值量,其中0.1非常接近当前粒子位置;0.5介于粒子位置和光标位置之间,等等
Lerp是线性插值的缩写。您可能会发现其他形式的插值或缓和更适合于模拟粒子的运动,例如来自penner easing集合的插值或缓和:
你会想先读一读。这个社区很有帮助,但你必须遵守它的规则!另外,发布代码中与您的问题相关的部分。这是一个大得多的块中较小的相关部分-我已经对位置项的位置进行了注释。您需要首先阅读。这个社区很有帮助,但你必须遵守它的规则!另外,发布与您的问题相关的代码部分。这是一个大得多的块中较小的相关部分-我已经对位置项的位置进行了注释。