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);
}
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?
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)
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 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!