一、射線檢測
1)定義
射線檢測(Ray Casting) 是一種計算機圖形和計算機圖形學中的基本技術,用于檢測光線或射線是否與三維場景中的物體相交,以確定相交點的位置和其他相關信息。射線檢測通常用于實現(xiàn)各種交互功能、渲染效果和物理模擬,包括但不限于鼠標拾取、光線追蹤、碰撞檢測和物體拾取等。
1)射線與3D場景中的物體交互的流程
步驟 | 描述 |
---|---|
1 |
定義射線: 定義射線的起點和方向向量。 |
2 |
檢測相交: 沿著射線的方向,從起點開始沿射線前進,檢測射線是否與場景中的任何物體相交。 通常,這涉及到進行碰撞檢測,以確定是否有物體與射線相交。 |
3 |
確定交點: 如果射線與物體相交,計算交點的位置。 交點通常以3D坐標的形式給出,表示射線與物體相交的點。 |
4 |
處理交互: 根據(jù)應用的需求,您可以在交互點上執(zhí)行特定的操作,如選擇物體、執(zhí)行動作或渲染效果。 |
5 |
遍歷所有可能的相交點: 射線檢測通??梢苑祷囟鄠€相交點,因此可以考慮遍歷所有可能的交點以處理多重相交。 |
2)射線檢測藍圖函數(shù)
藍圖函數(shù) | 描述 |
---|---|
LineTraceByChannel |
執(zhí)行一條射線檢測,檢測與指定碰撞通道相交的物體。返回一個 Hit Result 結構。 |
SphereTraceByChannel |
以球體的形狀執(zhí)行射線檢測,檢測球體與物體的碰撞。返回一個 Hit Result 結構。 |
LineTraceMultiByChannel |
執(zhí)行射線檢測,檢測與指定碰撞通道相交的所有物體。返回一個 Hit Results 數(shù)組。 |
SphereTraceMultiByChannel |
以球體的形狀執(zhí)行射線檢測,檢測球體與多個物體的碰撞。返回一個 Hit Results 數(shù)組。 |
BoxTraceByChannel |
執(zhí)行射線檢測,檢測與指定碰撞通道相交的物體,使用盒子形狀。返回一個 Hit Result 結構。 |
MultiSphereTraceByChannel |
執(zhí)行多個球體形狀的射線檢測,檢測多個球體與物體的碰撞。返回一個 Hit Results 數(shù)組。 |
LineTraceForObjects |
執(zhí)行射線檢測,檢測與指定物體類型相交的物體。返回一個 Hit Result 結構。 |
SphereTraceForObjects |
以球體的形狀執(zhí)行射線檢測,檢測與指定物體類型相交的物體。返回一個 Hit Result 結構。 |
BoxTraceForObjects |
執(zhí)行射線檢測,檢測與指定物體類型相交的物體,使用盒子形狀。返回一個 Hit Result 結構。 |
MultiSphereTraceForObjects |
執(zhí)行多個球體形狀的射線檢測,檢測與指定物體類型相交的物體。返回一個 Hit Results 數(shù)組。 |
CapsuleTraceByChannel |
以膠囊體的形狀執(zhí)行射線檢測,檢測膠囊體與物體的碰撞。返回一個 Hit Result 結構。 |
CapsuleTraceForObjects |
以膠囊體的形狀執(zhí)行射線檢測,檢測與指定物體類型相交的物體。返回一個 Hit Result 結構。 |
3)藍圖實現(xiàn)根據(jù)鼠標點擊位置獲取場景中的坐標值
4)根據(jù)相機中心點獲取場景中的坐標值
需要獲取到pawn里的相機。
5)射線檢測相關C++函數(shù)
(僅列舉linetrace系列其他大同小異)文章來源:http://www.zghlxwxcb.cn/news/detail-734139.html
-
LineTraceSingleByChannel
:- 用于檢測一條射線與第一個相交物體的碰撞。
- 返回一個
FHitResult
結構,其中包含有關碰撞的信息,如碰撞點、碰撞法線和碰撞物體的引用。
bool UWorld::LineTraceSingleByChannel(FHitResult& OutHit, const FVector Start, const FVector End, ECollisionChannel TraceChannel, const FCollisionQueryParams& Params)
-
LineTraceMultiByChannel
:- 用于檢測一條射線與多個相交物體的碰撞。
- 返回一個
TArray<FHitResult>
,其中包含所有相交物體的碰撞信息。
int32 UWorld::LineTraceMultiByChannel(TArray<FHitResult>& OutHits, const FVector Start, const FVector End, ECollisionChannel TraceChannel, const FCollisionQueryParams& Params)
-
LineTraceSingleByObjectType
:- 類似于
LineTraceSingleByChannel
,但是使用物體類型(EObjectTypeQuery
)而不是碰撞通道進行檢測。
- 類似于
bool UWorld::LineTraceSingleByObjectType(FHitResult& OutHit, const FVector Start, const FVector End, FObjectQueryParams ObjectQueryParams, const FCollisionQueryParams& Params)
-
LineTraceMultiByObjectType
:- 類似于
LineTraceMultiByChannel
,但是使用物體類型(EObjectTypeQuery
)而不是碰撞通道進行檢測。
- 類似于
int32 UWorld::LineTraceMultiByObjectType(TArray<FHitResult>& OutHits, const FVector Start, const FVector End, FObjectQueryParams ObjectQueryParams, const FCollisionQueryParams& Params)
6)C++實現(xiàn)手動創(chuàng)建射線檢測
FVector StartLocation; // 射線的起點坐標
FVector ForwardVector; // 射線的方向向量
FHitResult HitResult; // 用于存儲碰撞信息的變量
// 設置射線的起點坐標
StartLocation = PlayerCameraComponent->GetComponentLocation(); // PlayerCameraComponent是攝像機組件
// 設置射線的方向向量
ForwardVector = PlayerCameraComponent->GetForwardVector(); // 獲取攝像機的前向向量
// 建立射線
FVector EndLocation = ((ForwardVector * RayLength) + StartLocation); // 計算射線的終點坐標
// 進行射線檢測
if (GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility))
{
// 射線與物體相交,可以在HitResult中獲取碰撞信息
AActor* HitActor = HitResult.GetActor();
FVector ImpactPoint = HitResult.ImpactPoint;
// 進一步處理交互邏輯
}
PlayerCameraComponent:攝像機組件
LineTraceSingleByChannel:射線檢測函數(shù)
HitResult:碰撞的物體和碰撞點
RayLength:射線的長度;
ECC_Visibility:射線檢測所使用的碰撞通道文章來源地址http://www.zghlxwxcb.cn/news/detail-734139.html
7)C++實現(xiàn)點擊獲取場景中的坐標值
void AYourPlayerController::GetSceneLocationFromMouse()
{
// 獲取玩家控制器
APlayerController* PlayerController = this;
if (PlayerController)
{
// 獲取鼠標點擊位置
FVector MouseLocation, MouseDirection;
PlayerController->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);
// 創(chuàng)建射線,用于射線檢測
FHitResult HitResult;
FCollisionQueryParams CollisionParams;
// 執(zhí)行射線檢測
if (GetWorld()->LineTraceSingleByChannel(HitResult, MouseLocation, MouseLocation + MouseDirection * YourRayLength, ECC_Visibility, CollisionParams))
{
// 獲取射線與場景相交的位置
FVector SceneLocation = HitResult.Location;
// 打印結果
UE_LOG(LogTemp, Warning, TEXT("Scene Location: %s"), *SceneLocation.ToString());
}
}
}
二、非射線檢測的情況
1)根據(jù)相機當前位置獲取中心點的世界坐標
void AYourPlayerController::GetCameraCenterLocation()
{
// 獲取玩家控制器的視圖控制器
APlayerController* PlayerController = this;
if (PlayerController)
{
// 獲取相機組件
UCameraComponent* CameraComponent = PlayerController->PlayerCameraManager->GetCameraComponent();
if (CameraComponent)
{
// 獲取相機位置
FVector CameraLocation = CameraComponent->GetComponentLocation();
// 獲取相機旋轉
FRotator CameraRotation = CameraComponent->GetComponentRotation();
// 計算相機中心點的位置(通常位于相機位置的前方,視角方向)
FVector CameraForwardVector = CameraRotation.Vector();
FVector CameraCenterLocation = CameraLocation + CameraForwardVector * YourDistance; // 替換 YourDistance 為相機中心點到相機位置的距離
// 將相機中心點的位置轉換為場景中的坐標
FVector WorldLocation = CameraCenterLocation;
// 打印結果
UE_LOG(LogTemp, Warning, TEXT("Camera Center Location: %s"), *WorldLocation.ToString());
}
}
}
到了這里,關于【虛幻引擎UE】UE4/UE5 基于2D屏幕坐標獲取場景3D坐標 射線檢測(藍圖/C++)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!