Javascript 三维空间中的轨道倾角

Javascript 三维空间中的轨道倾角,javascript,math,3d,Javascript,Math,3d,因此,我正在从事一个项目,我需要从服务器检索粒子数据(可能会获取数千个对象)(以便同步客户端),我正在努力找出如何计算多个粒子的轨道倾角 到目前为止,我有轨道总半径,宽度(粒子的起点和终点) 我还通过设置粒子需要的位置来渲染轨道: position_x: Math.sin(radians) * asteroidsOrbit, position_y: Common.random(-10, 10), position_z: Math.cos(radians) * ast

因此,我正在从事一个项目,我需要从服务器检索粒子数据(可能会获取数千个对象)(以便同步客户端),我正在努力找出如何计算多个粒子的轨道倾角

到目前为止,我有轨道总半径,宽度(粒子的起点和终点)

我还通过设置粒子需要的位置来渲染轨道:

     position_x: Math.sin(radians) * asteroidsOrbit,
     position_y: Common.random(-10, 10),
     position_z: Math.cos(radians) * asteroidsOrbit
我注意到,默认情况下,轨道垂直于X平面,其行为与它应该的一样。
但是,既然它在x平面上可能会旋转20度,我应该如何将旋转应用于它呢?

我用Python编写了一些代码,将旋转应用于3D空间中粒子的速度,可能会有所帮助:

我的报告摘录[编辑]:

由于旋转是围绕粒子的速度(
vp
)进行的,因此由
make\u rotation\u marix
产生的矩阵
R
不能直接应用于
vp
-我们必须首先改变基,使速度与(0,0,1)向量共线,应用旋转,然后将基准更改回原始基准。由于已知fi rst基准变化的结果为(0,0,sp),sp是粒子的速度(速度的大小),因此跳过fi rst基准变化 并且
R
应用于(0,0,sp)

为了改变基础,我们使用矩阵将(0,0,1)改变回初始粒子速度,由函数
Rotation\u a2b
提供。矩阵
Ra2b
将向量
a
转化为
b
,因此它被称为
a
=(0,0,1)和
b
等于归一化粒子的初始速度。我们规范化速度的事实意味着基的变化保留了向量的大小——或者可以使用
a
=(0,0,sp)。

我用Python编写了一些代码,将旋转应用于3D空间中粒子的速度,可能会有帮助:

我的报告摘录[编辑]:

由于旋转是围绕粒子的速度(
vp
)进行的,因此由
make\u rotation\u marix
产生的矩阵
R
不能直接应用于
vp
-我们必须首先改变基,使速度与(0,0,1)向量共线,应用旋转,然后将基准更改回原始基准。由于已知fi rst基准变化的结果为(0,0,sp),sp是粒子的速度(速度的大小),因此跳过fi rst基准变化 并且
R
应用于(0,0,sp)

为了改变基础,我们使用矩阵将(0,0,1)改变回初始粒子速度,由函数
Rotation\u a2b
提供。矩阵
Ra2b
将向量
a
转化为
b
,因此它被称为
a
=(0,0,1)和
b
等于归一化粒子的初始速度。我们规范化速度的事实意味着基的变化保留了向量的大小——或者可以使用
a
=(0,0,sp)。

你可以尝试使用,但是如果你遇到边缘情况(0°,90°或类似)的问题,你可以考虑。如果使用旋转矩阵,请记住,旋转的向量是正确的(通常旋转矩阵应用于向量[0,0,1],因此需要找到从[0,0,1]到要旋转的任何向量的基变化。您可以尝试使用,但如果在边缘情况(0°、90°或类似)方面遇到问题如果你使用旋转矩阵记住,那么你旋转的向量是正确的(通常旋转矩阵适用于向量[0, 0, 1),所以你需要找到从[0,01]的基础变化。在纯JS中,当我发现有一个NodeJS库(math3d)可以完全满足我的需要时,我一直在努力,在纯JS中我也做了类似的事情,当我发现有一个NodeJS库(math3d)可以满足我的需要时
     position_x: Math.sin(radians) * asteroidsOrbit,
     position_y: Common.random(-10, 10),
     position_z: Math.cos(radians) * asteroidsOrbit
def make_rotation_matrix(theta=0.,phi=0.,cos_theta=False,cos_phi=False):
    """
    Returns a rotation matrix for given angles.
    Makes the rotation matrix for a given theta angle around the y axis (polar
    angle) followed by another rotation around the z axis by a given phi angle
    (azimuthal angle).

    Parameters
    ----------
    theta : float
        Polar angle, between 0 and pi (or its cosine between -1 and 1).
    phi : float
        Azimuthal angle, between 0 and 2pi.
    cos_theta : boolean
        Wether to consider theta as an angle (False) or its cosine (True).
    cos_phi : boolean
        Wether to consider phi as an angle (False) or its cosine (True).

    Returns
    ----------
    out : np.array
        Rotation matrix (shape == (3,3)).

    Examples
    --------
    >>> make_rotation_matrix(0,10)
    array([[-0.83907153,  0.54402111, -0.        ],
           [-0.54402111, -0.83907153, -0.        ],
           [-0.        ,  0.        ,  1.        ]])
    >>> make_rotation_matrix(10,0)
    array([[-0.83907153, -0.        , -0.54402111],
           [-0.        ,  1.        , -0.        ],
           [ 0.54402111,  0.        , -0.83907153]])
    >>> make_rotation_matrix(0.1,0,True)
    array([[ 0.1       , -0.        ,  0.99498744],
           [ 0.        ,  1.        ,  0.        ],
           [-0.99498744,  0.        ,  0.1       ]])
    """
    if cos_theta:
        cos_theta = theta
        sin_theta = np.sqrt(1. - cos_theta**2)
    else:
        cos_theta = np.cos(theta)
        sin_theta = np.sin(theta)
    if cos_phi:
        cos_phi = phi
        sin_phi = np.sqrt(1. - cos_phi**2)
    else:
        cos_phi = np.cos(phi)
        sin_phi = np.sin(phi)
    return np.array(
    [[cos_theta*cos_phi ,  -sin_phi  ,  sin_theta*cos_phi],
     [cos_theta*sin_phi ,   cos_phi  ,  sin_theta*sin_phi],
     [     -sin_theta   ,      0     ,  cos_theta        ]])


def skew_symmetric_cross_product(vector):
    """
    Returns the skew symmetric cross product of given vector.

    Parameters
    ----------
    vector : iterable
        Iterable object, must have at least 3 elements, corresponding to
        the vectors x, y and z components.

    Returns
    ----------
    out : np.array
        Skew symmetric cross product of the given vector (shape == (3,3)).

    Examples
    --------
    >>> skew_symmetric_cross_product([1,0,0])
    array([[ 0.,  0.,  0.],
           [ 0.,  0., -1.],
           [ 0.,  1.,  0.]])
    >>> skew_symmetric_cross_product([0,1,0])
    array([[ 0.,  0.,  1.],
           [ 0.,  0.,  0.],
           [-1.,  0.,  0.]])
    >>> skew_symmetric_cross_product([0,0,1])
    array([[ 0., -1.,  0.],
           [ 1.,  0.,  0.],
           [ 0.,  0.,  0.]])
    """
    return np.array([[ 0.         , -vector[2] , vector[1]  ],
                     [ vector[2]  , 0.         , -vector[0] ],
                     [ -vector[1] , vector[0]  , 0.         ]])


def rotation_a2b(a,b):
    """
    Returns the rotation matrix that transforms vector a in b.
    From
    http://math.stackexchange.com/questions/180418/

    Parameters
    ----------
    a : iterable
        Iterable object, must have at least 3 elements, corresponding to
        the vectors x, y and z components.

    b : iterable
        Similar to a.

    Returns
    ----------
    out : np.array
        Rotation matrix that transforms a in b (shape == (3,3)).

    Examples
    --------
    >>> rotation_a2b([1,0,0],[1,0,0])
    array([[ 1.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  1.]])
    >>> rotation_a2b([1,0,0],[0,1,0])
    array([[ 0., -1.,  0.],
           [ 1.,  0.,  0.],
           [ 0.,  0.,  1.]])
    >>> rotation_a2b([1,0,0],[0,0,1])
    array([[ 0.,  0., -1.],
           [ 0.,  1.,  0.],
           [ 1.,  0.,  0.]])
    """
    if np.all(a==b):
        return np.identity(3)
    v = np.cross(a,b)
    v_sscp = skew_symmetric_cross_product(v)
    s = np.sqrt(np.dot(v,v))
    c = np.dot(a,b)
    return np.identity(3) + v_sscp +\
           (((1-c)/(s**2))*np.dot(v_sscp,v_sscp))