从鼠标位置数学设置 Actor 世界位置
Set Actor world location from mouse position math
不幸的是,我的数学知识相当令人沮丧,但我真的很想了解我必须通过鼠标拖动来控制 Actor 的代码。此代码还可以用作通过在屏幕上单击鼠标在世界中放置对象的方式。
首先 - 代码运行良好,问题更多是关于向量代数而不是 Unreal Engine。我迷路了,我什至不知道如何正确命名变量 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
) 得到交点.
不幸的是,我的数学知识相当令人沮丧,但我真的很想了解我必须通过鼠标拖动来控制 Actor 的代码。此代码还可以用作通过在屏幕上单击鼠标在世界中放置对象的方式。
首先 - 代码运行良好,问题更多是关于向量代数而不是 Unreal Engine。我迷路了,我什至不知道如何正确命名变量 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
) 得到交点.