gpt4 book ai didi

c++ - 从鼠标位置数学设置 Actor 世界位置

转载 作者:行者123 更新时间:2023-11-30 04:42:04 31 4
gpt4 key购买 nike

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

首先 - 代码运行良好,问题更多是关于 vector 代数而不是虚幻引擎。我迷路了,我什至不知道如何正确命名变量 lol

因此,这是我从我发现的实现我需要的功能的蓝图示例之一转换而来的代码:

    FVector WorldLocation;
    FVector WorldDirection;
    PlayerController->DeprojectMousePositionToWorld(WorldLocation, WorldDirection);

    // Have no idea how it works but it works! I really need to learn more about vector Algebra :(((
    float DistanceAboveGround = 200.0f;
    float NotSureWhyThisValue = -1.0f;
    float WhyThisOperation = WorldLocation.Z / WorldDirection.Z + DistanceAboveGround;

    FVector IsThisNewWorldDirection = WorldDirection * WhyThisOperation * NotSureWhyThisValue;
    FVector ActorWorldLocation = WorldLocation + IsThisNewWorldDirection;            
    // end of "Have no idea how it works" code
    Capsule->SetWorldLocation(ActorWorldLocation);

如果有人可以解释我为什么需要执行这些操作,由//不知道.. 注释行?如果我什至能理解如何正确命名“NotSureWhyThisValue”、“WhatDoesItMean”、“WhyThisOperation”和“IsThisNewWorldDirection”?这对我来说将是一个巨大的进步,完美的案例是解释每一行......

提前致谢!

最佳答案

这只是一个重新实现 FMath::LinePlaneIntersection因为飞机的法线是 (0,0,1),所以它只是走了一些捷径:

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);

See this answer by Bas Smit here如果您想更好地理解数学(复制如下):

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;
}

基本上,WhyThisOperation 结果是 -t,然后乘以 -1 使其成为 t,然后乘以通过WorldDirection (rayD) 加上WorldPosition (rayP) 得到交点。

关于c++ - 从鼠标位置数学设置 Actor 世界位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925092/

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