English 中文(简体)
我如何在3D太空 移动一个点到一个特定的方向?
原标题:How do I move a point in a specific direction in 3d Space?

我知道如何将点移向 2D 空间的方向,它是这样的:

pointX += sin(angle) * movementSpeed;
pointY += cos(angle) * movementSpeed;

但假设 angle 变量被 pitch yaw roll 变量所取代,我如何在 3d 空格中做同样的事情?

(btw I 正在制作射线行进算法)

问题回答

要使用 3D 空格移动一个点, 需要将这些角转换为方向矢量。 方向矢量描述此点移动的方向 。

  1. Convert Angles to Radians: Make sure your pitch, yaw, and roll angles are in radians. If they are in degrees, convert them to radians
  2. Compute the Direction Vector: Calculate the direction vector based on pitch and yaw.
  3. Move the Point: Use the direction vector to update the point s position.
#include <cmath>

// Convert degrees to radians if needed
constexpr float DEG_TO_RAD = M_PI / 180.0f;

// Function to move a point in 3D space
void movePointIn3DSpace(float& pointX, float& pointY, float& pointZ, float pitch, float yaw, float movementSpeed) {
    // Convert angles to radians if they are in degrees
    float pitchRad = pitch * DEG_TO_RAD;
    float yawRad = yaw * DEG_TO_RAD;

    // Calculate the direction vector
    float dirX = cos(pitchRad) * sin(yawRad);
    float dirY = sin(pitchRad);
    float dirZ = cos(pitchRad) * cos(yawRad);

    // Update the point s position
    pointX += dirX * movementSpeed;
    pointY += dirY * movementSpeed;
    pointZ += dirZ * movementSpeed;
}

// Example usage
int main() {
    float pointX = 0.0f, pointY = 0.0f, pointZ = 0.0f;
    float pitch = 30.0f;  // degrees
    float yaw = 45.0f;    // degrees
    float movementSpeed = 1.0f;

    movePointIn3DSpace(pointX, pointY, pointZ, pitch, yaw, movementSpeed);

    // Output the new position
    printf("New position: (%f, %f, %f)
", pointX, pointY, pointZ);

    return 0;
}

(在 cpp btw 中编码)





相关问题
Maths in LaTex table of contents

I am trying to add a table of contents for my LaTex document. The issue I am having is that this line: subsubsection{The expectation of (X^2)} Causes an error in the file that contains the ...

Math Overflow -- Handling Large Numbers

I am having a problem handling large numbers. I need to calculate the log of a very large number. The number is the product of a series of numbers. For example: log(2x3x66x435x444) though my actual ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Subsequent weighted algorithm for comparison

I have two rows of numbers ... 1) 2 2 1 0 0 1 2) 1.5 1 0 .5 1 2 Each column is compared to each other. Lower values are better. For example Column 1, row 2 s value (1.5) is more ...

热门标签