gpt4 book ai didi

java - 从原点出发一定距离的方向

转载 作者:行者123 更新时间:2023-12-02 06:11:30 25 4
gpt4 key购买 nike

我想将生物推离我右键单击生物的方向一定距离,但是当我使用此代码时,生物被推开的距离会根据我与生物的距离有多近或多远而变化。

    target.motionX = (target.posX - player.posX) * 0.5;
target.motionZ = (target.posZ - player.posZ) * 0.5;

我知道我需要获取玩家所面对的方向,然后将生物移离该方向一定距离。我只是不知道该怎么做。

最佳答案

首先你必须找到距离:

float xDis = target.posX - player.posX;
float zDis = target.posZ - player.posZ;

如果xDis非零,则计算角度:

float tangent = zDis / xDis;
float angle = Math.arctan(tangent);

然后,确定抛出 vector 的长度:

float length = 0.5;

然后是两个轴上的投影:

float xProj = length*Math.cos(angle)*Math.signum(xDis);
float zProj = length*Math.sin(angle)*Math.signum(xDis);

这些是您的 motionXmotionY 部分。

如果 xDis 为零,那么我认为您需要这种特殊情况。

float xProj = 0;
float zProj = length*Math.signum(zDis);

关于java - 从原点出发一定距离的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com