Rebuild collision in realtime on water mesh for line trace,

Good evening, I have asked question:

Buoyancy problem with getting vertices from plane (C++) - C++ - Unreal Engine Forums,

but this was wrong, I have done it other way by raycast. I have spent some time on UE4 API and I saw that I have to rebuild collision to do it but I have problems with it.

Can someone help me figure out how to do it correctly?

Header file

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Water.generated.h"

UCLASS()
class PIRATEWARS419_API AWater : public AActor
{
	GENERATED_BODY()
	
public:	
	UPROPERTY(EditAnywhere)
		AActor * WaterActor;

	UFUNCTION(BlueprintCallable, Category = "Water")
		void UpdateCollision();

	UFUNCTION(BlueprintCallable, Category = "Water")
		void GetWaterMesh();
	
private:
	UStaticMesh * WaterMesh;

};


cpp file

#include "Water.h"
#include <Runtime/Engine/Classes/Engine/StaticMesh.h>
#include <Runtime/Engine/Classes/PhysicsEngine/BodySetup.h>


/* Use it only once it gets mesh from actor */
void AWater::GetWaterMesh() {
	TArray<UStaticMeshComponent*> Components;
	WaterActor->GetComponents<UStaticMeshComponent>(Components);
	UStaticMeshComponent* StaticMeshComponent = Components[0];
	WaterMesh = StaticMeshComponent->StaticMesh;

	StaticMeshComponent = NULL;
	Components[0] = NULL;
}

/* This function Update collision of water mesh, use it every frame */
void AWater::UpdateCollision() {
	FBodySetupUVInfo WaterUVInfo;

	// UV INFO FOR LINE TRACE!
	WaterUVInfo = WaterMesh->BodySetup->UVInfo;

	//WaterMesh->BodySetup->InvalidatePhysicsData();
	WaterMesh->BodySetup->CreatePhysicsMeshes();

	// Create UVInfo
	//WaterUVInfo.FillFromTriMesh(CollisionData);
}

Is this water mesh animated via the material using World Position Offset or what is the situation here?

Yes, there is water mesh (just a plane) animated by material using World Position Offset.

That part is happening in the vertex shader portion of the renderer so the way you’re going about it won’t work. You only have access to geometry before all that.

You basically have two options at this point but before I explain what you need to do, can you explain to me what you are doing the line traces for?

Are you using the location and normal of the surface for floating objects and boyancy? Or are there some other strange things you’re trying to do? Also are you just tracing down?

Thanks for answer!
I didn’t knew this thanks for info! :slight_smile:

Yes, I’m trying to make buoyancy of ships, objects etc.

  1. I would keep what you’ve made so far and create a function in C++ based on the math you’re using to drive your World Position Offset to calculate what ever points you want to sample from the surface to give you location and normal of the surface. (outside of the shader)

  2. You could use something called “Custom Mesh Component” to first add mesh triangles manually. Then use another function on the component to update the vertex positions each frame. This does totally work fine and updating it isn’t a huge performance cost. I’m not sure about the performance of physics but it may work perfectly fine.

I’d say the first option is easy enough to do. The second option might be necessary if you are trying to calculate bullet hit detection at an angle to the surface or something.

Could you possibly take a screenshot(s) of the nodes you feed into your World Position Offset in your material? I can post a full C++ example of what the hell i’m talking about later tonight. I kind of want to try it for myself for fun. I’ll see if the second solution even works and profile the performance cost.

Thanks for help I will try it :)!

Here is screenshot of my heightmap

Do you need whole material?

I’m working on what you’ve said so far.

Do you know if I can make something like panner in c++/blueprint (or even something similar)?

Can I create function in c++/blueprint and use it in material?

Thanks a lot!
Finaly I made sphere buoyant so I think I can do everything buoyant with this now.
I have just done Custom UV in shader and made exactly same in C++ Class and done some math.
I will need to edit it a bit but at least it works now!

You should make this comment answer.

Thanks a lot again!