以下是一个简单的示例代码,其中包括了计算移动距离的函数:

from panda3d.core import Vec3

# 计算向任意方向移动距离的函数
def get_distance_along_direction(start_pos, end_pos, direction):
    # 计算起点到终点的向量
    move_vec = end_pos - start_pos
    # 将方向向量标准化
    direction.normalize()
    # 计算向量点积,得到向量在该方向上的投影长度
    distance = move_vec.dot(direction)
    return distance

# 测试代码
# 定义起点、终点和方向
p1 = Vec3(0, 0, 0)
p2 = Vec3(10, 0, 0)
direction = Vec3(0, 0, 1) # 向上的方向

# 计算移动距离
distance = get_distance_along_direction(p1, p2, direction)

# 输出结果
print('移动距离:', distance)

输出结果为:

移动距离: 0.0

这是因为在给定的方向(向上)上,从起点到终点并没有移动。如果将方向改为向右(X轴正方向),则可以得到正确的结果:

direction = Vec3(1, 0, 0) # 向右的方向

# 计算移动距离
distance = get_distance_along_direction(p1, p2, direction)

# 输出结果
print('移动距离:', distance)

输出结果为:

移动距离: 10.0
Panda3D 计算移动轨迹中特定方向的距离

原文地址: https://www.cveoy.top/t/topic/oSaM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录