使用球面坐标三个js在球体上移动点

问题描述 投票:1回答:2

我试图在球体表面上随机移动一个点。目前我试图通过生成随机球面坐标然后使用函数.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)));

这种方式有效,但目前我的球体点并没有真正在球体上移动,而是跳得很远。

我认为这与phitheta提供的价值有关,但问题是我不知道phitheta的价值范围是什么。

如果有什么不明确的地方让我知道,我可以澄清一下!

javascript three.js 3d spherical-coordinate
2个回答
0
投票

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

enter image description here


1
投票

这是因为phitheta是弧度,而不是度数。

所以Math.random() * 2 -1对于弧度来说太大了。

根据current implementaion,这些参数似乎没有范围限制。

© www.soinside.com 2019 - 2024. All rights reserved.