VoxelPlugin/VoxelPluginFreeLegacy

Static Trees do not fit the surface of the land

coolsnake opened this issue · 1 comments

I create the land by loading a heightmap png with the method mentioned here #74
and want to plant some static tree on the land.
The location of the trees is (x,y), and z is from the heightmap. and call UVoxelBlueprintLibrary::AddInstances to create the trees. The result is that some trees are planted too deeply into the land and some trees are too high from the land.

the sample code to calculate the z is shown as below.

    int VoxelSize = 100;
    int minxx = X * VoxelSize - (HeightmapData.GetWidth() * VoxelSize / 2) - 0.5 * VoxelSize;
    int maxxx = X * VoxelSize - (HeightmapData.GetWidth() * VoxelSize / 2) + 0.5 * VoxelSize;
    int minyy = Y * VoxelSize - (HeightmapData.GetHeight() * VoxelSize / 2) - 0.5 * VoxelSize;
    int maxyy = Y * VoxelSize - (HeightmapData.GetHeight() * VoxelSize / 2) + 0.5 * VoxelSize;

    int xx = minxx + rand() % ((maxxx + 1) - minxx);
    int yy = minyy + rand() % ((maxyy + 1) - minyy);

    int MinPosDev = IntanceObjectMsg[i].MinPosDev;
    int MaxPosDev = IntanceObjectMsg[i].MaxPosDev;
    double objectx = xx + (MinPosDev + rand() % ((MaxPosDev + 1) - MinPosDev));
    double objecty = yy + (MinPosDev + rand() % ((MaxPosDev + 1) - MinPosDev));
    double objectz = (ThisVoxelHeight - (HeightmapData.GetMaxHeight()-HeightmapData.GetMinHeight())/2.0);


    FVector fv;
    fv.X = objectx;
    fv.Y = objecty;
    fv.Z = objectz;
    FTransform ft;
    ft.SetLocation(fv);

    FVector RotateFv = FVector(0, 0, rand() % (0 - 360));
    FQuat FQ;
    FQ = FQuat::MakeFromEuler(RotateFv);
    ft.SetRotation(FQ);
    float scaleNum = 0.5f + rand() / double(RAND_MAX);
    FVector ScaleFv = FVector(scaleNum, scaleNum, scaleNum);
    ft.SetScale3D(ScaleFv);

I don't know why. I hope I can get the help from experts. Thanks.

fixed.