Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从鼠标位置数学设置演员世界位置_C++_Linear Algebra_Unreal Engine4_Game Development_Vectormath - Fatal编程技术网

C++ 从鼠标位置数学设置演员世界位置

C++ 从鼠标位置数学设置演员世界位置,c++,linear-algebra,unreal-engine4,game-development,vectormath,C++,Linear Algebra,Unreal Engine4,Game Development,Vectormath,不幸的是,我的数学知识相当令人沮丧,但我真的很想理解我必须通过鼠标拖动来控制演员的代码。这段代码还可以作为在屏幕上单击鼠标将对象放置在世界中的方法 首先,代码很好用,问题更多的是向量代数,而不是虚幻的引擎。我太迷茫了,甚至不知道如何正确命名变量lol 下面是我从一个Blueprint示例中转换的代码,该示例实现了我需要的功能: FVector世界定位; FVector世界方向; PlayerController->DeProjectMousePositionWorld(世界位置,世界方向); /

不幸的是,我的数学知识相当令人沮丧,但我真的很想理解我必须通过鼠标拖动来控制演员的代码。这段代码还可以作为在屏幕上单击鼠标将对象放置在世界中的方法

首先,代码很好用,问题更多的是向量代数,而不是虚幻的引擎。我太迷茫了,甚至不知道如何正确命名变量lol

下面是我从一个Blueprint示例中转换的代码,该示例实现了我需要的功能:

FVector世界定位;
FVector世界方向;
PlayerController->DeProjectMousePositionWorld(世界位置,世界方向);
//我不知道它是怎么工作的,但它是工作的!我真的需要学习更多关于向量代数的知识:(((
浮球离地距离=200.0f;
float notsure为什么该值=-1.0f;
float WhyThisOperation=世界位置.Z/世界方向.Z+地上距离;
FVector是该新世界方向=世界方向*为什么操作*不确定该值为何;
FVector ActorWorldLocation=世界位置+IsThisNewWorldDirection
//结束“不知道它是如何工作的”代码
胶囊->设置世界位置(ActorWorldLocation);
如果有人能解释我为什么需要做这些操作,从//不知道..注释行开始?如果我能理解如何正确命名“不确定它的价值”、“什么要求”、“为什么操作”和“这是新世界的方向?”这对我来说将是一个巨大的进步,完美的例子将是解释每一行虽然


提前感谢!

这只是 因为平面的法线为(0,0,1),所以它只采用某些快捷方式:

如果您想更好地理解数学(复制如下):


基本上,
whythysoperation
原来是
-t
,然后它乘以-1得到
t
,然后它乘以
WorldDirection
rayD
)并添加到
WorldPosition
rayP
)获取交点。

这只是用来计算演员高度值的吗?代码很有效,我可以在滴答声中使用它,并可以通过改变鼠标位置来拖动我的演员。有一件事可能有助于理解虚幻的场景-这是一种RTS类游戏,所以想象一下星际争霸3,我通过鼠标点击在地图上生成了一个演员。这看起来干净多了谢谢!一个问题为什么我们需要在这里“WorldLocation+WorldDirection”?@paxer我编辑了我的问题,以便更好地解释您给出的代码。在我的代码中,您需要在该函数的行上有第二个点,并添加原点和方向,从而在行上为您提供另一个点:)
FVector WorldLocation;
FVector WorldDirection;
float DistanceAboveGround = 200.0f;

PlayerController->DeprojectMousePositionToWorld(WorldLocation, WorldDirection); 

FVector PlaneOrigin(0.0f, 0.0f, DistanceAboveGround);

FVector ActorWorldLocation = FMath::LinePlaneIntersection(
            WorldLocation,
            WorldLocation + WorldDirection,
            PlaneOrigin,
            FVector::UpVector);

Capsule->SetWorldLocation(ActorWorldLocation);
Vector3 Intersect(Vector3 planeP, Vector3 planeN, Vector3 rayP, Vector3 rayD)
{
    var d = Vector3.Dot(planeP, -planeN);
    var t = -( d 
            + rayP.z * planeN.z 
            + rayP.y * planeN.y 
            + rayP.x * planeN.x) 
          / (rayD.z * planeN.z 
            + rayD.y * planeN.y 
            + rayD.x * planeN.x);
    return rayP + t * rayD;
}