Buoyancy problem with getting vertices from plane (C++)

Good evening,
I have problem with getting vertices from plane.
I’m trying to make buoyancy and make ship float but when I try to get vertex height then I have 0 always.
Can someone help me do it? Or even point me how to do it correctly.

Here is code I have so far

// Fill out your copyright notice in the Description page of Project Settings.

#include "HeightMapReader.h"
#include <Runtime/Engine/Classes/Engine/Engine.h>
#include <Runtime/Engine/Classes/Engine/StaticMeshActor.h>
#include <Runtime/Engine/Classes/Components/StaticMeshComponent.h>
#include <Runtime/Engine/Classes/Engine/StaticMesh.h>

/* Update ALL verticles (that will be slow, depending on verts ammount...) Returns true on succes or null on fail */
bool AHeightMapReader::UpdateBuffer(AStaticMeshActor * AStaticMesh) {
	// Clear First
	StaticMeshActor = nullptr;
	StaticMeshComponent = nullptr;

	VertexBuffer = NULL;
	VertexCount = NULL;
	SideVertexCount = NULL;

	// Save & Get Component
	StaticMeshActor = AStaticMesh;
	StaticMeshComponent = AStaticMesh->GetStaticMeshComponent();

	// Check
	if (!IsValidLowLevel()) return NULL;
	if (!StaticMeshComponent) return NULL;
	if (!StaticMeshComponent->StaticMesh) return NULL;
	if (!StaticMeshComponent->StaticMesh->RenderData) return NULL;

	if (StaticMeshComponent->StaticMesh->RenderData->LODResources.Num() > 0)
	{
		VertexBuffer = &StaticMeshComponent->StaticMesh->RenderData->LODResources[0].VertexBuffers.PositionVertexBuffer;
		if (VertexBuffer)
		{
			VertexCount = VertexBuffer->GetNumVertices(); // Whole static mesh vertices count
			SideVertexCount = sqrt(VertexCount); // Side of the static mesh rectangle vertex count
		}
		else
			UE_LOG(LogTemp, Error, TEXT("VertexBuffer is NULL or something!"));
	}
	else
		UE_LOG(LogTemp, Error, TEXT("No vertices found (or failed access)!?"));

	// Return NULL on Error
	return NULL;
}

/* Get's postion from point depending on Array. Requires UpdateBuffer working...!  */ 
FVector AHeightMapReader::GetPoinFromVertexesArray(float x, float y) {

	int X = x;
	int Y = y;

	if (X > SideVertexCount) return { NULL, NULL, NULL };
	if (Y > SideVertexCount) return { NULL, NULL, NULL };

	VertexLocation = { 0, 0, 0 };

	int Index = Y * SideVertexCount - SideVertexCount + X; // May be incorrect 

														   // Check if Index is not bigger then max vertives ammount so it don't crash but just return NULL as error
	if (Index > VertexCount)
		return { NULL, NULL, NULL };

	if (Index <= 0)
		return { NULL, NULL, NULL };

	UE_LOG(LogTemp, Warning, TEXT("Vertices Count: %d"), VertexCount);
	UE_LOG(LogTemp, Warning, TEXT("Number of Current vertex: %d"), Index);

	UE_LOG(LogTemp, Warning, TEXT("X: %d"), VertexLocation.X);
	UE_LOG(LogTemp, Warning, TEXT("Y: %d"), VertexLocation.Y);
	UE_LOG(LogTemp, Warning, TEXT("Z: %d"), VertexLocation.Z);

	return { NULL, NULL, NULL };
}

Thanks for any info :)!

GetPoinFromVertexesArray() is actually nowhere accessing the vertex buffer. VertexLocation is initialized with zeros but then does not get overwritten with vertex data from the calculated index. Also the index calculation seems to be wrong. Assume x and y to be both 0.f, then the calculated index will be negative. Probably the line should read

unsigned const Index = Y * SideVertexCount + X;

But even this will work only if the plane is guaranteed to be always quadratic.

Because it seems rather unlikely that x and y will exactly hit a vertex in the general case you also could consider to do raycast instead of grabbing the vertex data directly from the mesh.

Also I would recommend to watch out for how (not) to mix up types.

Thanks for answer j.mueller RFG!

“Assume x and y to be both 0”
I have if for that
if (Index <= 0)
return { NULL, NULL, NULL };
Otherwise it will show exception.

“But even this will work only if the plane is guaranteed to be always quadratic.”
Yes mine water plane is square.

This idea with raycast seems to be good, I will try it instead of static x and y.
Thanks for help :)!

Guess I should have been a little more verbose on the index calculation. I’m absolutely aware of the intention of the checks, but what I was trying to show was that there is a general issue with the calculation when it generates an invalid index for valid input (presuming that x and y are always in valid local space coordinates of the plane). In fact the original calculation introduces an offset of one row, the subsequent exception then is thrown because the code tries to access memory in front of your buffer.

Thanks a lot for answer again! I think I will use raycast instead.
I think you’ve solved my problem, thanks again :)!

Well, I have tried raycast, and it gives me point but It don’t change later, always stay with same location.
Do you know what can I do with this?

238355-impact-point.png

// EDIT

I know what is causing that raycast use collison which is not regenerated when waves are moving, I will have to regenerate it every frame or do something to fix that.