所以我有一个草图,其中包含数百个不同大小的矩形,保存在数组列表中,当我将鼠标放在它们上时,它们会被推开,但是当我离开并让它们回到位置时它们会回去在他们的位置口吃,他们永远不会再回到静止状态。这让我非常沮丧!!
帮助?!
编辑:
嗯,当很多方块一起移动时会发生这个问题,我会尽可能少地编写代码。
当使用图像初始化此类时,它会根据图像的颜色放置正方形。
class Picture extends PApplet {
int w, h;
PApplet parent;
PImage image;
PImage originalImage;
boolean started = false;
Frame frame;
int resolution = 18;
ArrayList<Square> sqrs = new ArrayList<Square>();
ArrayList<GravityPoint> points = new ArrayList<GravityPoint>();
boolean original = false;
Picture(PApplet _parent, PImage _img) {
super();
parent = _parent;
setImage(_img);
runSketch(new String[]{"Picture"}, this);
started = true;
frame = ((SmoothCanvas)getSurface().getNative()).getFrame();
}
void settings() {
size(w, h);
}
void setup() {
background(0);
sqrs.clear();
points.clear();
for (int i = 0; i < image.width; i += resolution)
for (int j = 0; j < image.height; j += resolution) {
Square sq = new Square(new PVector(i, j), (int) random(3, 15), getGraphics());
sq.setColor(image.get(i, j));
sqrs.add(sq);
points.add(new GravityPoint(sq.pos.copy()));
sqrs.get(sqrs.size() - 1).point = points.get(points.size() - 1);
}
}
void draw() {
background(0);
background(0);
if (original) image(originalImage, 0, 0);
else {
for (Square sq : sqrs) {
sq.update();
sq.show(null);
}
}
}
void mouseMoved() {
int radius = 100;
for (Square sq : sqrs) {
PVector mousePos = new PVector(mouseX, mouseY);
float distance = sq.pos.dist(mousePos);
if (distance <= radius) {
PVector force = mousePos.copy().sub(sq.pos.copy()).mult(-1 * map(distance, 0, radius, 5, 1));
sq.applyForce(force);
}
}
}
void mousePressed() {
if (mouseButton == RIGHT) frame.dispose();
if (mouseButton == LEFT) original = !original;
}
void keyPressed() {
int temp = resolution;
if (keyCode == DOWN) resolution++;
else if (keyCode == UP) resolution--;
else return;
resolution = constrain(resolution, 1, 18);
if (temp == resolution) return;
image = originalImage;
originalImage = originalImage.copy();
setup();
}
void setImage(PImage image_) {
image = image_;
int dw = parent.displayWidth;
int dh = parent.displayHeight;
if (image.width > dw) image.resize(dw, 0);
if (image.height > dh) image.resize(0, dh);
w = image.width;
h = image.height;
if (started) surface.setSize(w, h);
originalImage = image.copy();
}
}
这是方形类
class Square {
PVector pos = new PVector(0, 0);
PVector vel = new PVector(0, 0);
PVector acc = new PVector(0, 0);
PVector grv = new PVector(0, 0);
int size = 5;
GravityPoint point;
color col;
color coll;
PGraphics graphics;
Square(PVector p, int s, PGraphics gra) {
graphics = gra;
pos = p.copy();
size = s;
}
void show(PGraphics gr) {
PGraphics graphics;
if (gr != null)
graphics = gr;
else
graphics = this.graphics;
graphics.beginDraw();
graphics.pushStyle();
smooth();
graphics.rectMode(CENTER);
if (col == coll) graphics.colorMode(HSB, 255, 255, 255);
else graphics.colorMode(RGB, 255, 255, 255);
color c = color(map(pos.x, 30, width-30, 100, 255), 255, 255);
graphics.stroke(col == coll ? c : col);
graphics.strokeWeight(0.5);
graphics.noFill();
graphics.rect(pos.x, pos.y, size, size);
//graphics.shape(shape);
graphics.popStyle();
graphics.endDraw();
}
Square setColor(color _col) {
col = _col;
return this;
}
void update() {
if (point != null) applyForce(point.getVector(this).mult(4));
vel.add(acc);
vel.add(grv);
vel.limit(5);
pos.add(vel);
if (point == null) vel.mult(0);
acc.mult(0);
if (point != null && pos.dist(point.pos) < size/2) {
vel.mult(0);
acc.mult(0);
}
}
void applyForce(PVector force) {
acc.add(force);
}
}
此课程负责将矩形拉回原位
class GravityPoint{
PVector pos;
GravityPoint(PVector p){
pos = p.copy();
}
PVector getVector(Square sq){
return pos.copy().sub(sq.pos);
}
Square closest(ArrayList<Square> sqrs){
if(sqrs.size() <= 0) return null;
float distance = Float.MAX_VALUE;
Square closest = null;
for(Square sq : sqrs)
if(sq.point == null && sq.pos.dist(pos) < distance){
distance = sq.pos.dist(pos);
closest = sq;
}
return closest;
}
}
编辑2:
我认为这段代码复制了我主程序中发生的同样问题,而不是百分百肯定,如果你尝试帮助我使用我的主草图,我将非常感激! &LT; 3
(将鼠标放在正方形上,然后将其移开)
PVector pos;
PVector vel;
PVector acc;
PVector point;
void setup() {
size(500, 500);
pos = new PVector(width/2, height/2);
point = new PVector(width/2, height/2);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}
void draw() {
background(255);
PVector mouse = new PVector(mouseX, mouseY);
if (mouse.dist(pos) <= 50)
acc.add(mouse.copy().sub(pos).mult(-1));
acc.add(point.copy().sub(pos).mult(3));
vel.add(acc);
vel.limit(4);
pos.add(vel);
acc.mult(0);
rectMode(CENTER);
rect(pos.x, pos.y, 30, 30);
}
答案 0 :(得分:0)
好吧,我修好了。
问题在于方块永远不会失去速度,它们会在重力点附近跳跃并且永远不会失去速度。
我纠正了我的代码,因此它们越接近重力点就会失去速度。