我创建了一个计算网格的演员,该网格在所述网格的每个正方形上生成项目。然而,我的网格正在从世界变换中进行计算,我希望它始终与我的地板网格表面的大小相关(以避免始终对坐标进行硬编码)。
头文件
//Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProceduralBlock.generated.h"
UCLASS()
class MYPROJECT_API AProceduralBlock : public AActor
{
GENERATED_BODY()
public:
Sets default values for this actor's properties
AProceduralBlock();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
//Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Room, meta = (AllowPrivateAccess = "true"));
UStaticMeshComponent* Block;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Block, meta = (AllowPrivateAccess = "true"));
TSubclassOf<AActor> BuildingClass;
void SpawnBuilding(UClass* ItemToSpawn);
void CreateGridDebug();
//position related
float Xcordinate;
float Ycordinate;
FVector Location;
float roomLength;
float roomWidth;
float radius;
//Frid related
int gridSquareWidth;
int gridSizeX;
int gridSizeY;
FVector topLeft;
float gridHeight;
FVector bottomRight;
FVector GetRandomPointInSquare(const FVector& upperLeft, const FVector& lowerRight);
void PlacePointsOnGrid();
};
CPP文件
//Fill out your copyright notice in the Description page of Project Settings.
#include "ProceduralBlock.h"
#include "DrawDebugHelpers.h"
//Sets default values
AProceduralBlock::AProceduralBlock()
{
//Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Block = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FloorComponent"));
SetRootComponent(Block);
gridSizeX = 5;
gridSizeY = 5;
gridSquareWidth = 200.f;
gridHeight = 1.f;
roomLength = 1000.f;
roomWidth = 1000.f;
topLeft = FVector(0.f);
bottomRight = FVector (1000.f, 1000.f,0.f);
radius = 25.f;
}
//Called when the game starts or when spawned
void AProceduralBlock::BeginPlay()
{
Super::BeginPlay();
CreateGridDebug();
PlacePointsOnGrid();
}
//Called every frame
void AProceduralBlock::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AProceduralBlock::SpawnBuilding(UClass* ItemToSpawn)
{
Xcordinate = FMath::FRandRange(-50.f,50.);
Ycordinate = FMath::FRandRange(-50.f,50.);
Location = FVector(Xcordinate,Ycordinate,0.f);
GetWorld()->SpawnActor<AActor>(ItemToSpawn, FVector(Location), FRotator(0.f));
}
void AProceduralBlock::CreateGridDebug()
{
for(int iterator = 0; iterator < gridSizeX + 1; iterator++)
{
FVector Start = topLeft + FVector(iterator * gridSquareWidth, 0.f, gridHeight);
FVector End = Start + FVector(0.f, roomLength, gridHeight);
DrawDebugLine(GetWorld(),Start,End,FColor::Red, true);
}
for(int iterator = 0; iterator < gridSizeX + 1; iterator++)
{
FVector Start = topLeft + FVector(0.f,iterator * gridSquareWidth, gridHeight);
FVector End = Start + FVector(roomWidth, 0.f, gridHeight);
DrawDebugLine(GetWorld(),Start,End,FColor::Red, true);
}
}
FVector AProceduralBlock::GetRandomPointInSquare(const FVector& upperLeft, const FVector& lowerRight)
{
float randomX = FMath::FRandRange(upperLeft.X, lowerRight.X);
float randomY = FMath::FRandRange(upperLeft.Y, lowerRight.Y);
return FVector(randomX,randomY, gridHeight);
}
void AProceduralBlock::PlacePointsOnGrid()
{
for(int i = 0; i < gridSizeX; i++)
{
for(int j = 0; j < gridSizeY; j++)
{
FVector UpperLeft(i* gridSquareWidth + radius, j*gridSquareWidth + radius, gridHeight);
FVector LowerRight(i * gridSquareWidth + gridSquareWidth - radius, j* gridSquareWidth + gridSquareWidth - radius, gridHeight);
FVector RandomPointInSquare = GetRandomPointInSquare(UpperLeft, LowerRight);
DrawDebugPoint(GetWorld(),RandomPointInSquare, 5.f, FColor::White,true);
GetWorld()->SpawnActor<AActor>(BuildingClass, RandomPointInSquare, FRotator(0.f));
}
}
}
我试着弄清楚如何计算立方体上的特定边,因为这是我的网格所基于的形状,但无济于事。