处理 2,2,1 java。如果在 3d 形状内则重置位置
Processing 2,2,1 java. Reset position if inside 3d shape
我已经制作了这段代码,使用 3d 模型,模型在 100 之后开始变形 frames.I 有一个用鼠标移动的盒子形状,我想每当顶点在盒子里时,重置他们的位置回到原来的位置,并重建模型,而其他人变形。当它们在盒子里时总是重置位置,当它们在 not.Can 时移动任何人帮助 me.Until 现在发生的事情是,当顶点在盒子内时它们停止移动。
提前致谢
import peasy.*;
import saito.objloader.*;
OBJModel model ;
OBJModel tmpmodel ;
PeasyCam cam;
float easing = 0.005;
float r;
float k =0.00001;
int VertCount;
PVector[] Verts;
PVector Mouse;
void setup()
{
size(800, 800, P3D);
frameRate(30);
noStroke();
model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
model.enableDebug();
model.scale(100);
model.translateToCenter();
tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
tmpmodel.enableDebug();
tmpmodel.scale(100);
tmpmodel.translateToCenter();
cam = new PeasyCam(this, width/2, height/2, 0, 994);
}
void draw()
{
background(129);
lights();
int VertCount = model.getVertexCount ();
PVector[] Verts = new PVector[VertCount];
PVector[] locas = new PVector[VertCount];
float r =80;
PVector Mouse = new PVector(mouseX-width/2, mouseY-height/2, 0);
cam.setMouseControlled(true);
//println(frameCount);
pushMatrix();
translate(width/2, height/2, 0);
for (int i = 0; i < VertCount; i++) {
//PVector orgv = model.getVertex(i);
Verts[i]= model.getVertex(i);
arrayCopy(Verts, locas);
//PVector tmpv = new PVector();
if (frameCount> 100) {
float randX = random(-5, 5);
float randY = random(-5, 5);
float randZ = random(-5, 5);
PVector Ran = new PVector(randX, randY, randZ);
//float norX = abs(cos(k)) * randX;
//float norY = abs(cos(k)) * randY;
//float norZ = abs(cos(k)) * randZ;
if (Verts[i].x > Mouse.x - r/2 && Verts[i].x < Mouse.x + r/2) {
if (Verts[i].x > Mouse.y - r/2 && Verts[i].x < Mouse.y + r/2) {
if (Verts[i].x > Mouse.z - r/2 && Verts[i].x < Mouse.z + r/2) {
arrayCopy(locas, Verts);
}
}
} else {
Verts[i].x+=Ran.x;
Verts[i].y+=Ran.y;
Verts[i].z+=Ran.z;
if (Verts[i].x > width/2 || Verts[i].x < -width/2) {
Verts[i].x+=-Ran.x;
}
if (Verts[i].y > height/2 || Verts[i].y < -height/2) {
Verts[i].y+=-Ran.y;
}
if (Verts[i].z < -800/2 || Verts[i].z > 800/2) {
Verts[i].z+=-Ran.z;
}
}
tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
}
k+=0.0001;
}
pushMatrix();
translate(Mouse.x, Mouse.y, Mouse.z);
noFill();
stroke(255);
box(r);
popMatrix();
noStroke();
tmpmodel.draw();
popMatrix();
pushMatrix();
translate(width/2, height/2, 0);
noFill();
stroke(255);
box(width, height, 600);
popMatrix();
}
如果你想 运行 它使用来自 saito obj 示例或 PshapeObj 示例的模型。
关于您的代码,有些地方没有多大意义(一些评论可能对此有所帮助)。你为什么要这样复制数组?
无论如何,如果我是你,我会从简单的开始。我会采用面向对象的方法,您可以在其中创建一个 class 来封装点的原始位置及其当前位置。
这是一个在二维中执行此操作的示例,但此方法可以推广到三个维度:
ArrayList<MovingPoint> points = new ArrayList<MovingPoint>();
float circleDiameter = 200;
void setup(){
size(500, 500);
for(int i = 0; i < 100; i++){
points.add(new MovingPoint());
}
}
void draw(){
background(0);
noFill();
stroke(255, 0, 0);
ellipse(mouseX, mouseY, circleDiameter, circleDiameter);
fill(255);
stroke(255);
MovingPoint previousPoint = null;
for(MovingPoint mp : points){
mp.draw();
if(previousPoint != null){
line(previousPoint.current.x, previousPoint.current.y, mp.current.x, mp.current.y);
}
previousPoint = mp;
}
}
class MovingPoint{
PVector original;
PVector current;
public MovingPoint(){
original = new PVector(random(width), random(height));
current = original.copy();
}
void draw(){
if(dist(current.x, current.y, mouseX, mouseY) < circleDiameter/2){
//inside circle, reset position
current = original.copy();
}
else{
//outside circle, move randomly
current.x += random(-5, 5);
current.y += random(-5, 5);
}
ellipse(current.x, current.y, 10, 10);
}
}
您不必经历复制数组的繁琐过程。只需使用一个 class 记住每个点的原始位置和当前位置,然后根据鼠标位置在它们之间切换。
如果您仍然无法使其正常工作,请 post 另一个问题,该问题适用于此示例而不是您的整个草图。如果我们无法访问您正在使用的库,我们将很难提供帮助,因此您最好摆脱它们并将问题缩小到尽可能少的行。祝你好运。
我已经制作了这段代码,使用 3d 模型,模型在 100 之后开始变形 frames.I 有一个用鼠标移动的盒子形状,我想每当顶点在盒子里时,重置他们的位置回到原来的位置,并重建模型,而其他人变形。当它们在盒子里时总是重置位置,当它们在 not.Can 时移动任何人帮助 me.Until 现在发生的事情是,当顶点在盒子内时它们停止移动。
提前致谢
import peasy.*;
import saito.objloader.*;
OBJModel model ;
OBJModel tmpmodel ;
PeasyCam cam;
float easing = 0.005;
float r;
float k =0.00001;
int VertCount;
PVector[] Verts;
PVector Mouse;
void setup()
{
size(800, 800, P3D);
frameRate(30);
noStroke();
model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
model.enableDebug();
model.scale(100);
model.translateToCenter();
tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
tmpmodel.enableDebug();
tmpmodel.scale(100);
tmpmodel.translateToCenter();
cam = new PeasyCam(this, width/2, height/2, 0, 994);
}
void draw()
{
background(129);
lights();
int VertCount = model.getVertexCount ();
PVector[] Verts = new PVector[VertCount];
PVector[] locas = new PVector[VertCount];
float r =80;
PVector Mouse = new PVector(mouseX-width/2, mouseY-height/2, 0);
cam.setMouseControlled(true);
//println(frameCount);
pushMatrix();
translate(width/2, height/2, 0);
for (int i = 0; i < VertCount; i++) {
//PVector orgv = model.getVertex(i);
Verts[i]= model.getVertex(i);
arrayCopy(Verts, locas);
//PVector tmpv = new PVector();
if (frameCount> 100) {
float randX = random(-5, 5);
float randY = random(-5, 5);
float randZ = random(-5, 5);
PVector Ran = new PVector(randX, randY, randZ);
//float norX = abs(cos(k)) * randX;
//float norY = abs(cos(k)) * randY;
//float norZ = abs(cos(k)) * randZ;
if (Verts[i].x > Mouse.x - r/2 && Verts[i].x < Mouse.x + r/2) {
if (Verts[i].x > Mouse.y - r/2 && Verts[i].x < Mouse.y + r/2) {
if (Verts[i].x > Mouse.z - r/2 && Verts[i].x < Mouse.z + r/2) {
arrayCopy(locas, Verts);
}
}
} else {
Verts[i].x+=Ran.x;
Verts[i].y+=Ran.y;
Verts[i].z+=Ran.z;
if (Verts[i].x > width/2 || Verts[i].x < -width/2) {
Verts[i].x+=-Ran.x;
}
if (Verts[i].y > height/2 || Verts[i].y < -height/2) {
Verts[i].y+=-Ran.y;
}
if (Verts[i].z < -800/2 || Verts[i].z > 800/2) {
Verts[i].z+=-Ran.z;
}
}
tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
}
k+=0.0001;
}
pushMatrix();
translate(Mouse.x, Mouse.y, Mouse.z);
noFill();
stroke(255);
box(r);
popMatrix();
noStroke();
tmpmodel.draw();
popMatrix();
pushMatrix();
translate(width/2, height/2, 0);
noFill();
stroke(255);
box(width, height, 600);
popMatrix();
}
如果你想 运行 它使用来自 saito obj 示例或 PshapeObj 示例的模型。
关于您的代码,有些地方没有多大意义(一些评论可能对此有所帮助)。你为什么要这样复制数组?
无论如何,如果我是你,我会从简单的开始。我会采用面向对象的方法,您可以在其中创建一个 class 来封装点的原始位置及其当前位置。
这是一个在二维中执行此操作的示例,但此方法可以推广到三个维度:
ArrayList<MovingPoint> points = new ArrayList<MovingPoint>();
float circleDiameter = 200;
void setup(){
size(500, 500);
for(int i = 0; i < 100; i++){
points.add(new MovingPoint());
}
}
void draw(){
background(0);
noFill();
stroke(255, 0, 0);
ellipse(mouseX, mouseY, circleDiameter, circleDiameter);
fill(255);
stroke(255);
MovingPoint previousPoint = null;
for(MovingPoint mp : points){
mp.draw();
if(previousPoint != null){
line(previousPoint.current.x, previousPoint.current.y, mp.current.x, mp.current.y);
}
previousPoint = mp;
}
}
class MovingPoint{
PVector original;
PVector current;
public MovingPoint(){
original = new PVector(random(width), random(height));
current = original.copy();
}
void draw(){
if(dist(current.x, current.y, mouseX, mouseY) < circleDiameter/2){
//inside circle, reset position
current = original.copy();
}
else{
//outside circle, move randomly
current.x += random(-5, 5);
current.y += random(-5, 5);
}
ellipse(current.x, current.y, 10, 10);
}
}
您不必经历复制数组的繁琐过程。只需使用一个 class 记住每个点的原始位置和当前位置,然后根据鼠标位置在它们之间切换。
如果您仍然无法使其正常工作,请 post 另一个问题,该问题适用于此示例而不是您的整个草图。如果我们无法访问您正在使用的库,我们将很难提供帮助,因此您最好摆脱它们并将问题缩小到尽可能少的行。祝你好运。