x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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 :)!

Product Version: UE 4.19
Tags:
more ▼

asked Apr 15 '18 at 07:31 PM in C++ Programming

avatar image

Przemek2222
431 4 10 14

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Apr 16 '18 at 01:49 PM

avatar image

j.mueller RFG
304 3 10 10

avatar image Przemek2222 Apr 16 '18 at 05:19 PM

Thanks for answer j.mueller RFG!

"Assume x and y to be both 0" I have if for that if (Index

"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 :)!

avatar image j.mueller RFG Apr 16 '18 at 05:59 PM

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.

avatar image Przemek2222 Apr 17 '18 at 12:48 PM

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

avatar image Przemek2222 Apr 21 '18 at 06:04 PM

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?

https://answers.unrealengine.com/storage/attachments/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.

impact-point.png (46.0 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question