1.向量旋转
问题: 将一个向量旋转90
方法:旋转矩阵
FVector FrontDir = EndMousePoint - Point;
FrontDir.Normalize();
FVector Left = FVector(-FrontDir.Y, FrontDir.X, 0);
Verties.Add(Point + Left * (WallWedith / 2));
Verties.Add(FVector(Verties[0].X, Verties[0].Y, Verties[0].Z + WallHeight));
Verties.Add(Point - Left * (WallWedith / 2));
Verties.Add(FVector(Verties[2].X, Verties[2].Y, Verties[2].Z + WallHeight));
Verties.Add(EndMousePoint - Left * (WallWedith / 2));
Verties.Add(FVector(Verties[4].X, Verties[4].Y, Verties[4].Z + WallHeight));
Verties.Add(EndMousePoint + Left * (WallWedith / 2));
Verties.Add(FVector(Verties[6].X, Verties[6].Y, Verties[6].Z + WallHeight));
Point:起点坐标, EndMousePoint:终点坐标。
依次加入点的坐标,WallWedith:立方体的宽,WallHeight:立方体的高。
2.ProceduralMeshActor
问题:利用ProceduralMeshActor创建立方体
//侧面
for (int32 i = 0; i < Verties.Num() - 1; i = i + 2) {Triangles.Add(i);Triangles.Add((i+2)% Verties.Num());Triangles.Add(i+1);Triangles.Add(i+1);Triangles.Add((i+2)% Verties.Num());Triangles.Add((i+3) % Verties.Num());
}//上下面
for (int32 i = 0; i < 2; i++) {for (int32 j = 0; j < Verties.Num(); j++) {if (j % 2 == i) {Triangles.Add(j);}}Triangles.Add(i);Triangles.Add(i+4);}
先构建一个侧面,通过循环构建所有侧面,上下面单独处理
3.鼠标屏幕坐标转换成空间坐标(有偏差,有待改进)
FVector2D MousePosition;UWorld* world = GetWorld();
if (world)
{APlayerController* pc = world->GetFirstPlayerController();if (pc){ULocalPlayer* LocalPlayer = pc->GetLocalPlayer();if (LocalPlayer && LocalPlayer->ViewportClient){LocalPlayer->ViewportClient->GetMousePosition(MousePosition);}}
}float Scale = UWidgetLayoutLibrary::GetViewportScale(this);FVector WorldLocation;
FVector WorldDirection;
APlayerController::DeprojectScreenPositionToWorld(MousePosition.X, MousePosition.Y, WorldLocation,WorldDirection);MouseLocal = FVector(WorldDirection.X * 1000/ Scale, WorldDirection.Y * 1000/ Scale, 0);
记得在Build.cs文件里添加"UMG"