我试图在球体表面上随机移动一个点。目前我试图通过生成随机球面坐标然后使用函数.setFromSphericalCoords()
将这些坐标转换为3d位置来实现此目的
这就是代码看起来像每帧生成一个新的随机球面坐标:
element.kralenSpherical.phi += Math.random() * 2 -1;
if(element.kralenSpherical.phi <= 0 ) element.kralenSpherical.phi = 0;
else if(element.kralenSpherical.phi >= 180 ) element.kralenSpherical.phi = 180;
element.kralenSpherical.theta += Math.random() * 2 -1;
if(element.kralenSpherical.theta >= 360 ) element.kralenSpherical.theta = 0;
else if(element.kralenSpherical.theta <= 0) element.kralenSpherical.theta = 360;
element.kraal.position.copy(element.BaseLocation.clone().add(sphericalVector.setFromSphericalCoords(element.kralenSpherical.radius, element.kralenSpherical.phi, element.kralenSpherical.theta)));
这种方式有效,但目前我的球体点并没有真正在球体上移动,而是跳得很远。
我认为这与phi
和theta
提供的价值有关,但问题是我不知道phi
和theta
的价值范围是什么。
如果有什么不明确的地方让我知道,我可以澄清一下!
不是three.js,但这应该很容易翻译。众所周知,这是处理:
void setup() {
size(300, 300, P3D);
frameRate(300);
background(0);
}
void draw() {
lights();
translate(width/2, height/2);
stroke(255,255,0);
noFill();
//sphere(75);
PVector v = noise_spherical_point(frameCount * 0.009, 75);
translate(v.x, v.y, v.z);
fill(255,0,0);
noStroke();
sphere(1);
}
PVector noise_spherical_point(float t, float rad) {
float x = noise(t) * 2 -1;
float y = noise(0, t) * 2 -1;
float z = noise(0, 0, t) * 2 -1;
PVector v = new PVector(x, y, z);
v = v.normalize();
v.mult(rad);
return v;
}