1

我正在尝试编写一个程序来模拟地月系统,并且它工作了一段时间,但出乎意料的是,处理开始停滞不前。每次我点击“运行”时,都会出现大约 10 秒的异常延迟,然后我的 Windows 11 任务栏通知我草图窗口已打开,但我无法访问它,并且没有任何显示。其他程序运行良好,包括使用 P3D 的程序。

这是我的代码:

PShape earth,moon;
void setup()
{
  size(6000,6000,P3D);
  PImage img = loadImage("earth.jpg");
  PImage img2= loadImage("moon.jpg");
  noStroke();
  earth=createShape(SPHERE,212.6); //radius of earth is 6378, sized down to by a factor of 30
  earth.setTexture(img);
  moon=createShape(SPHERE,57.9); //radius of moon is 1737, sized down by a factor of 30
  moon.setTexture(img2);
}
float z = -300; //transversal along z-axis
float angle; // angle of rotation wrt center
float rotAngMoon=6.68; //angle of moon's rotation
float rotAngEarth=23.5; //angle of earth's rotation
float inclination=5.14; //angle between moon's orbital plane and earth's orbital plane
float orbitDistEarth=155.6666; //distance from earth's center to barycenter (4670, sized down by a factor of 30)
float orbitDistMoon=1298.9; //distance from moon's center to barycenter (389670, sized down by a factor of 300)
void draw()
{
  background(0);
  translate(width/2,height/2,2*z);
  
  pushMatrix(); // Earth
  rotateY(27.321661*angle); //Earth spins faster
  translate(orbitDistEarth,0,0); //go to the point where earth would be
  rotateZ(rotAngEarth*PI/180); //rotate the earth
  shape(earth);
  popMatrix();
  
  pushMatrix(); //moon
  rotateY(angle);
  translate(-orbitDistMoon,(tan(inclination*PI/180))*orbitDistMoon*sin(angle*PI/180),0); //go to the point where the moon would be, additionally, the moon lies on a plane of inclination

//the line above might be incorrect with respect to the simulation, but I was debugging it when the stalls started occurring. There doesn't seem to be any error with it as far as I can see, so it should run fine

  rotateZ(rotAngMoon*PI/180); //rotate the moon 
  shape(moon);
  popMatrix();
  
  
  angle+=0.01;
  
}

关于可能出现问题的任何想法?

4

1 回答 1

1

由于 6000 宽和 6000 高的巨大窗口大小,您的系统已停止运行。这远远超过你在今天的屏幕上看到的(2021 年)。减小窗口大小:

size(6000, 6000, P3D);

size(1920, 1280, P3D);
于 2021-09-18T05:41:33.730 回答