gpt4 book ai didi

java - 如何使粒子对象跟随鼠标移动

转载 作者:行者123 更新时间:2023-11-30 01:44:51 27 4
gpt4 key购买 nike

我希望粒子对象在移动时简单地跟随 mouseX 和 mouseY 位置。我可以用 mouseX 和 Y 替换“origin”值吗?

  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;
}
}
}

最佳答案

这个例子更简单,更容易理解 - 希望它有所帮助。

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);
}

关于java - 如何使粒子对象跟随鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495610/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com