diff --git a/Content/Maps/ShipTest.umap b/Content/Maps/ShipTest.umap index d31f447..51e7e33 100644 --- a/Content/Maps/ShipTest.umap +++ b/Content/Maps/ShipTest.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:70d24d41060d4aaebe649f1f5e4ae25000b41052d7c44e6748389f6da2e267fd -size 632371 +oid sha256:85d6792a05ba0143f47208deda648c88d6674e2d778a7ca16c1e2d52ee8b211a +size 634533 diff --git a/Content/Maps/ShipTest_BuiltData.uasset b/Content/Maps/ShipTest_BuiltData.uasset index 1e0f422..3c89043 100644 --- a/Content/Maps/ShipTest_BuiltData.uasset +++ b/Content/Maps/ShipTest_BuiltData.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4bd7969f79741ee62a1e0db1b8dea4e972b520cc7b04de6984bfcefba9e98a37 +oid sha256:a69cf4d0f9fe5232228a1797b5b762e3ecc507289569096ff04468b17067cd46 size 1328744 diff --git a/Content/Ships/BP_Ship.uasset b/Content/Ships/BP_Ship.uasset deleted file mode 100644 index 1bf32cc..0000000 --- a/Content/Ships/BP_Ship.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bd67a875ba5fd86bb7d8ecef03ff163764cc892f65de7385e82d271c0a020311 -size 2128 diff --git a/Content/Ships/BP_TestCube.uasset b/Content/Ships/BP_TestCube.uasset new file mode 100644 index 0000000..bf2b0cf --- /dev/null +++ b/Content/Ships/BP_TestCube.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60f97c2b57e374f356a34d953f5e90a14ddc224af764c952fccacf92093027d9 +size 115733 diff --git a/Content/Ships/BP_TestShip.uasset b/Content/Ships/BP_TestShip.uasset index 59236a3..63b6e1e 100644 --- a/Content/Ships/BP_TestShip.uasset +++ b/Content/Ships/BP_TestShip.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1cecd53636894d9e62cf397d89bd0bae39c87d0c40e8dee8959e70d01a53b1ca -size 93925 +oid sha256:c4f5ac5b2d3aee2dd6b7a63e9bb8be702faaf7d151f495fe0982cc477f35f77c +size 108203 diff --git a/Content/Ships/Ship_Dark/_defaultMat.uasset b/Content/Ships/Ship_Dark/_defaultMat.uasset deleted file mode 100644 index 7cb06c6..0000000 --- a/Content/Ships/Ship_Dark/_defaultMat.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:f6124e235b71c165dd26a6883a23a16d38f20b343a3870dfa3c457bf423863dc -size 78766 diff --git a/Content/Ships/Ship_Dark/iron.uasset b/Content/Ships/Ship_Dark/iron.uasset deleted file mode 100644 index 9cf5548..0000000 --- a/Content/Ships/Ship_Dark/iron.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:69f8ea155cbed312cecebee26791dc796cb7ed6593ad87fb70f0057bb13c2445 -size 83821 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_front.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_front.uasset deleted file mode 100644 index a391233..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_front.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:447ba6d0c2587000898538d72a8a3aee4bfaacad2fbba6b750c4682e64af511d -size 92228 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_front1.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_front1.uasset deleted file mode 100644 index c0dfc00..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_front1.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:fd72447d6322f61b82900cbc53c881834fd6d5ec2e402ededea2a83ad0e4f48d -size 114893 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_left.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_left.uasset deleted file mode 100644 index fe506cc..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_left.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5013e42d3ea264cb4c41cfccad33f4b227246b814f26e0d57a5b20719f970a0b -size 91960 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_left2.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_left2.uasset deleted file mode 100644 index 4715b19..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_left2.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:73490c868d812126e2239fa26e4d0b074ffd6f83c9e19b582abdb79ba346f1c2 -size 117266 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_left3.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_left3.uasset deleted file mode 100644 index 7ef92c6..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_left3.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:1c55f501da6b8f978021bc6a718e60bea1b8ea40953237209eadd12dbacf2280 -size 93238 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_left4.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_left4.uasset deleted file mode 100644 index d15e5b9..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_left4.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:11f42419e204c1c365c9bb6f1c6055c5f415c488ae36ae3047400b7924789769 -size 87418 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_left5.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_left5.uasset deleted file mode 100644 index 8241191..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_left5.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:8afeeba8381074080e9e9b20d6871480091498493941f06a4b1331fb23fd4b4e -size 93244 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_left6.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_left6.uasset deleted file mode 100644 index a6e78f6..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_left6.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:2643c8cf61beb5a000c231d269cfa136c87ff4f03df1689050117534dee39dbe -size 87417 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_right.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_right.uasset deleted file mode 100644 index 594a2dc..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_right.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3c4f4ac4eb89c26fed80f52863f5b58425008b9ec5b7f1ef10e997a49856ecb5 -size 90753 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_right10.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_right10.uasset deleted file mode 100644 index f8a549e..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_right10.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:6bbee0ffbadef418141e8676a5296684157b565707f9b128d33b7e3eec0da540 -size 87406 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_right11.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_right11.uasset deleted file mode 100644 index b5b61ac..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_right11.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5e0d769f6dcc72380e54ad350b308da50c690180a2b41a0d7e749519d039e80c -size 93185 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_right7.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_right7.uasset deleted file mode 100644 index 3496ef9..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_right7.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:4f5b3d97447cc991219bf05d8e4b0a29c2026f976c68a1303214ea1df3184502 -size 114524 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_right8.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_right8.uasset deleted file mode 100644 index 6cf4a42..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_right8.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bed6e04a819ff6b5071281bbdf4b5277c890f706358e41c1134bb2169eb7d855 -size 87387 diff --git a/Content/Ships/Ship_Dark/ship_dark_cannon_right9.uasset b/Content/Ships/Ship_Dark/ship_dark_cannon_right9.uasset deleted file mode 100644 index 5a2f821..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_cannon_right9.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:59ddf4bceefd065f545eab311b300b46f15277bd5b3da301e6bbddd00497ffe0 -size 93283 diff --git a/Content/Ships/Ship_Dark/ship_dark_sail_back.uasset b/Content/Ships/Ship_Dark/ship_dark_sail_back.uasset deleted file mode 100644 index 71f90d9..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_sail_back.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:93680f3550b74b4d7790e8809ff6dc60a1c10ed7d31573db29e32b89a45786ac -size 102342 diff --git a/Content/Ships/Ship_Dark/ship_dark_sail_front.uasset b/Content/Ships/Ship_Dark/ship_dark_sail_front.uasset deleted file mode 100644 index e775b98..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_sail_front.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7fcd9d04efc0d08d721429b8b01f7722be2cb39ff155e765aca71fc9ee4278b1 -size 100907 diff --git a/Content/Ships/Ship_Dark/ship_dark_sail_middle.uasset b/Content/Ships/Ship_Dark/ship_dark_sail_middle.uasset deleted file mode 100644 index d512cb6..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_sail_middle.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:b68d9977f5b15ee0cdd32fcaff7ef59fe7b5839fa7c051ca2046369c2604f127 -size 103366 diff --git a/Content/Ships/Ship_Dark/ship_dark_ship_dark_8angles.uasset b/Content/Ships/Ship_Dark/ship_dark_ship_dark_8angles.uasset deleted file mode 100644 index 9fb155c..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_ship_dark_8angles.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:d4a08cd49ea8f93fb83bf47be09edbabc1f3e05c9823f1bcee8c5297ecce5e98 -size 197065 diff --git a/Content/Ships/Ship_Dark/ship_dark_steering.uasset b/Content/Ships/Ship_Dark/ship_dark_steering.uasset deleted file mode 100644 index 4202647..0000000 --- a/Content/Ships/Ship_Dark/ship_dark_steering.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:14008bde20012cf7848cd1240a69d78b5948c0e2bc1aaf1f44ba064009f45ea8 -size 140126 diff --git a/Content/Ships/Ship_Dark/window.uasset b/Content/Ships/Ship_Dark/window.uasset deleted file mode 100644 index 6da9aa3..0000000 --- a/Content/Ships/Ship_Dark/window.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bcf901f30064314a1d88518fc3d3db91a18c3964121b53323111556f8bc61c28 -size 80140 diff --git a/Content/Ships/Ship_Dark/wood.uasset b/Content/Ships/Ship_Dark/wood.uasset deleted file mode 100644 index b97c582..0000000 --- a/Content/Ships/Ship_Dark/wood.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cc3d2ad4a5415fd88ac86baab58d5f9722e0d41c73c49568d32e5e2224844e4b -size 83386 diff --git a/Content/Ships/Ship_Dark/woodDark.uasset b/Content/Ships/Ship_Dark/woodDark.uasset deleted file mode 100644 index 615baa4..0000000 --- a/Content/Ships/Ship_Dark/woodDark.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:50c9bdfa6d1ce8cc1ceffe315d362e83e729f1e03958796f9951e6cb96485ded -size 85518 diff --git a/Content/Test/TestBoat.uasset b/Content/Test/TestBoat.uasset new file mode 100644 index 0000000..b8c7c8d --- /dev/null +++ b/Content/Test/TestBoat.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c07b7cc7cfdc6cf68dc95116af5fa3d211907b2925751c84b5dd3b880dd75535 +size 102511 diff --git a/Source/SeaOfCrooks/Private/Components/BuoyancyComponent.cpp b/Source/SeaOfCrooks/Private/Components/BuoyancyComponent.cpp index 89cac4e..956fd44 100644 --- a/Source/SeaOfCrooks/Private/Components/BuoyancyComponent.cpp +++ b/Source/SeaOfCrooks/Private/Components/BuoyancyComponent.cpp @@ -22,16 +22,16 @@ UBuoyancyComponent::UBuoyancyComponent() // ... } - // Called when the game starts void UBuoyancyComponent::BeginPlay() { Super::BeginPlay(); - // ... UStaticMeshComponent* MeshComp = Cast(GetOwner()->GetRootComponent()); VolumeMesh = MeshComp->GetStaticMesh(); - + + VolumeAsPrimComp = Cast(MeshComp); + SubmergedTriangles = TArray(); SubmergedTriangles.SetNum(GetNumTriangles(), false); } @@ -46,7 +46,7 @@ void UBuoyancyComponent::TickComponent(float DeltaTime, ELevelTick TickType, FAc // determine height of vertices above or below surface of water ASOCGameState* State = Cast(GetWorld()->GetGameState()); - AOcean* Ocean = nullptr; + Ocean = nullptr; if(State) { Ocean = State->GetOcean(); @@ -70,11 +70,10 @@ void UBuoyancyComponent::TickComponent(float DeltaTime, ELevelTick TickType, FAc if(V1.Depth < 0.0f || V2.Depth < 0.0f || V3.Depth < 0.0f) { - FTriangleData temp(V1, V2, V3); + FTriangleData temp(V1, V2, V3, Ocean->GetOceanHeight()); Triangles.Add(temp); - FVector Center = (V1.Position + V2.Position + V3.Position) / 3.0f; if(DebugBuoyancy) - DrawDebugSphere(GetWorld(), Center, 5.0f, 12, FColor::Yellow, false, 0.0f, 0, 1.0f); + DrawDebugSphere(GetWorld(), temp.Center, 5.0f, 12, FColor::Yellow, false, 0.0f, 0, 1.0f); } } @@ -84,18 +83,33 @@ void UBuoyancyComponent::TickComponent(float DeltaTime, ELevelTick TickType, FAc { FString DebugMsg = FString::Printf(TEXT("Num Submerged Triangles: %d\nNum Generated Triangles: %d"), Triangles.Num(), SubmergedTriangles.Num()); - GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Cyan, DebugMsg); + GEngine->AddOnScreenDebugMessage(-1, -1.0f, FColor::Cyan, DebugMsg); for (auto Triangle : SubmergedTriangles) { - DrawDebugSphere(GetWorld(), Triangle.Highest.Position, 2.0f, 12, FColor::Red, + DrawDebugSphere(GetWorld(), Triangle.V3.GlobalPosition, 2.0f, 12, FColor::Red, false, 0.0f, 0, 1.0f); - DrawDebugSphere(GetWorld(), Triangle.Middle.Position, 2.0f, 12, FColor::Red, + DrawDebugSphere(GetWorld(), Triangle.V2.GlobalPosition, 2.0f, 12, FColor::Red, false, 0.0f, 0, 1.0f); - DrawDebugSphere(GetWorld(), Triangle.Lowest.Position, 2.0f, 12, FColor::Red, + DrawDebugSphere(GetWorld(), Triangle.V1.GlobalPosition, 2.0f, 12, FColor::Red, false, 0.0f, 0, 1.0f); } } + + if(VolumeAsPrimComp) + { + for (auto Triangle : SubmergedTriangles) + { + float rhoInKgCm3 = 0.001025f; + // F = -p*g*Hcenter*normal + FVector Force = -rhoInKgCm3 * GetWorld()->GetGravityZ() * Triangle.DistanceToSurface * Triangle.Area * Triangle.Normal; + FVector VerticalForce(0.0f, 0.0f, Force.Z); + VolumeAsPrimComp->AddForceAtLocation(VerticalForce, Triangle.Center); + } + } + + FString DebugMsg = FString::Printf(TEXT("Velocity.Z: %s"), *FString::SanitizeFloat(VolumeAsPrimComp->GetPhysicsLinearVelocity().Z)); + GEngine->AddOnScreenDebugMessage(-1, -1.0f, FColor::Magenta, DebugMsg); } } @@ -133,7 +147,7 @@ TArray UBuoyancyComponent::GetVertexPositions() // convert asset space vertex position to world space FVector WorldSpaceVertexPos = GetOwner()->GetActorLocation() + GetOwner()->GetTransform().TransformVector(VertexBuffer->VertexPosition(idx)); FVertexData temp; - temp.Position = WorldSpaceVertexPos; + temp.GlobalPosition = WorldSpaceVertexPos; VertexPositions.Add(temp); } } @@ -175,12 +189,21 @@ void UBuoyancyComponent::ProcessTriangles(TArray& Triangles) } else if (NumSubmergedVertices == 2) { - FVector H = Triangle.Highest.Position; - FVector M = Triangle.Middle.Position; - FVector L = Triangle.Lowest.Position; - float hH = Triangle.Highest.Depth; - float hM = Triangle.Middle.Depth; - float hL = Triangle.Lowest.Depth; + FVertexData V1 = Triangle.V1; + FVertexData V2 = Triangle.V2; + FVertexData V3 = Triangle.V3; + TArray Vertices = {V1, V2, V3}; + Vertices.Sort(); + V1 = Vertices[0]; + V2 = Vertices[1]; + V3 = Vertices[2]; + + FVector H = V3.GlobalPosition; + FVector M = V2.GlobalPosition; + FVector L = V1.GlobalPosition; + float hH = V3.Depth; + float hM = V2.Depth; + float hL = V1.Depth; FVector MH = H - M; float tM = -hL/(hH - hL); @@ -192,17 +215,26 @@ void UBuoyancyComponent::ProcessTriangles(TArray& Triangles) FVector LIL = tL * LH; FVector IL = LIL + L; - SubmergedTriangles.Add(FTriangleData(IM, IL, L)); - SubmergedTriangles.Add(FTriangleData(M, IM, L)); + SubmergedTriangles.Add(FTriangleData(L, IL, IM, Ocean->GetOceanHeight())); + SubmergedTriangles.Add(FTriangleData(M, L, IM, Ocean->GetOceanHeight())); } else if (NumSubmergedVertices == 1) { - FVector H = Triangle.Highest.Position; - FVector M = Triangle.Middle.Position; - FVector L = Triangle.Lowest.Position; - float hH = Triangle.Highest.Depth; - float hM = Triangle.Middle.Depth; - float hL = Triangle.Lowest.Depth; + FVertexData V1 = Triangle.V1; + FVertexData V2 = Triangle.V2; + FVertexData V3 = Triangle.V3; + TArray Vertices = {V1, V2, V3}; + Vertices.Sort(); + V1 = Vertices[0]; + V2 = Vertices[1]; + V3 = Vertices[2]; + + FVector H = V3.GlobalPosition; + FVector M = V2.GlobalPosition; + FVector L = V1.GlobalPosition; + float hH = V3.Depth; + float hM = V2.Depth; + float hL = V1.Depth; FVector LM = M - L; float tM = -hL/(hM - hL); @@ -214,7 +246,7 @@ void UBuoyancyComponent::ProcessTriangles(TArray& Triangles) FVector LJH = tH * LH; FVector JH = LJH + L; - SubmergedTriangles.Add(FTriangleData(JH, JM, L)); + SubmergedTriangles.Add(FTriangleData(L, JH, JM, Ocean->GetOceanHeight())); } } } diff --git a/Source/SeaOfCrooks/Private/ShipBase.cpp b/Source/SeaOfCrooks/Private/ShipBase.cpp index 5b512d8..97bf345 100644 --- a/Source/SeaOfCrooks/Private/ShipBase.cpp +++ b/Source/SeaOfCrooks/Private/ShipBase.cpp @@ -11,6 +11,13 @@ AShipBase::AShipBase() ShipMesh = CreateDefaultSubobject(TEXT("ShipMesh")); ShipMesh->SetupAttachment(RootComponent); + UPrimitiveComponent* ShipMeshAsPrim = Cast(ShipMesh); + if(ShipMeshAsPrim) + { + ShipMeshAsPrim->SetSimulatePhysics(true); + ShipMeshAsPrim->SetMassOverrideInKg(NAME_None, 800.0f, true); + ShipMeshAsPrim->SetLinearDamping(0.05f); + } BuoyancyComp = CreateDefaultSubobject(TEXT("BuoyancyComponent")); } diff --git a/Source/SeaOfCrooks/Public/Components/BuoyancyComponent.h b/Source/SeaOfCrooks/Public/Components/BuoyancyComponent.h index a79512e..3c6c780 100644 --- a/Source/SeaOfCrooks/Public/Components/BuoyancyComponent.h +++ b/Source/SeaOfCrooks/Public/Components/BuoyancyComponent.h @@ -4,14 +4,17 @@ #include "CoreMinimal.h" #include "Components/ActorComponent.h" +#include "Game/SOCGameState.h" + #include "BuoyancyComponent.generated.h" -USTRUCT() +USTRUCT(BlueprintType) struct FVertexData { GENERATED_BODY() - FVector Position; + FVector GlobalPosition; + // depth of the vertex above the surface of the water // negative value means the vertex is submerged float Depth; @@ -22,19 +25,19 @@ struct FVertexData FVertexData(FVector Position) { - this->Position = Position; + this->GlobalPosition = Position; Depth = 0.0f; } FVertexData(FVector Position, float Depth) { - this->Position = Position; + this->GlobalPosition = Position; this->Depth = Depth; } void CalculateDepth(float SurfaceHeight) { - Depth = Position.Z - SurfaceHeight; + Depth = GlobalPosition.Z - SurfaceHeight; } bool operator< (const FVertexData& rhs) const @@ -43,38 +46,61 @@ struct FVertexData } }; -USTRUCT() +USTRUCT(BlueprintType) struct FTriangleData { GENERATED_BODY() + + FVertexData V1; // lowest + FVertexData V2; // middle + FVertexData V3; // highest - FVertexData Highest; - FVertexData Middle; - FVertexData Lowest; + FVector Center; + + float DistanceToSurface; + FVector Normal; + + float Area; + FTriangleData() {} - FTriangleData(FVertexData V1, FVertexData V2, FVertexData V3) + FTriangleData(const FVertexData V1, const FVertexData V2, const FVertexData V3, float OceanHeight) { - TArray Vertices = {V1, V2, V3}; - Vertices.Sort(); - Lowest = Vertices[0]; - Middle = Vertices[1]; - Highest = Vertices[2]; - } + this->V1 = V1; + this->V2 = V2; + this->V3 = V3; - int32 GetNumSubmergedVertices() + Center = (V1.GlobalPosition + V2.GlobalPosition + V3.GlobalPosition) / 3.0f; + + DistanceToSurface = FMath::Abs(OceanHeight - Center.Z); + + Normal = FVector::CrossProduct(V2.GlobalPosition - V1.GlobalPosition, V3.GlobalPosition - V1.GlobalPosition); + Normal.Normalize(); + + // Heron's formula + float V12 = FVector::Distance(V1.GlobalPosition, V2.GlobalPosition); + float V23 = FVector::Distance(V2.GlobalPosition, V3.GlobalPosition); + float V13 = FVector::Distance(V1.GlobalPosition, V3.GlobalPosition); + + // Half Triangle Perimeter + float S = (V12 + V23 + V13) / 2.0f; + + Area = FMath::Sqrt(S * (S - V12) * (S - V23) * (S - V13)); + } + + int32 GetNumSubmergedVertices() const { int32 result = 0; - if(Highest.Depth < 0.0f) + if(V3.Depth < 0.0f) { result++; } - if(Middle.Depth < 0.0f) + if(V2.Depth < 0.0f) { result++; } - if(Lowest.Depth < 0.0f) + if(V1.Depth < 0.0f) { result++; } @@ -90,13 +116,15 @@ class SEAOFCROOKS_API UBuoyancyComponent : public UActorComponent public: // Sets default values for this component's properties UBuoyancyComponent(); - + protected: UPROPERTY(BlueprintReadOnly) UStaticMesh* VolumeMesh; - UPROPERTY() + UPROPERTY(BlueprintReadOnly) TArray SubmergedTriangles; + + AOcean* Ocean; // Called when the game starts virtual void BeginPlay() override; @@ -106,6 +134,8 @@ public: virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; private: + UPrimitiveComponent* VolumeAsPrimComp; + int32 GetNumTriangles(); TArray GetVertexPositions(); diff --git a/Source/SeaOfCrooks/SeaOfCrooks.Build.cs b/Source/SeaOfCrooks/SeaOfCrooks.Build.cs index d38dcf4..58574b4 100644 --- a/Source/SeaOfCrooks/SeaOfCrooks.Build.cs +++ b/Source/SeaOfCrooks/SeaOfCrooks.Build.cs @@ -8,7 +8,7 @@ public class SeaOfCrooks : ModuleRules { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; - PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "RenderCore", "RHI" }); + PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ProceduralMeshComponent", "RenderCore", "RHI" }); PrivateDependencyModuleNames.AddRange(new string[] { });