/
BarrelPhysics.cs
102 lines (77 loc) · 3.18 KB
/
BarrelPhysics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrelPhysics : MonoBehaviour {
//Drags
public GameObject underWaterObj;
//Script that's doing everything needed with the boat mesh, such as finding out which part is above the water
private ModifyBarrelMesh modifyBoatMesh;
//Mesh for debugging
private Mesh underWaterMesh;
//The boats rigidbody
private Rigidbody boatRB;
//The density of the water the boat is traveling in
private float rhoWater = 1027f;
void Start()
{
//Get the boat's rigidbody
boatRB = gameObject.GetComponent<Rigidbody>();
//Init the script that will modify the boat mesh
modifyBoatMesh = new ModifyBarrelMesh(gameObject);
//Meshes that are below and above the water
underWaterMesh = underWaterObj.GetComponent<MeshFilter>().mesh;
}
void Update()
{
//Generate the under water mesh
modifyBoatMesh.GenerateUnderwaterMesh();
//Display the under water mesh
modifyBoatMesh.DisplayMesh(underWaterMesh, "UnderWater Mesh", modifyBoatMesh.underWaterTriangleData);
}
void FixedUpdate()
{
//Add forces to the part of the boat that's below the water
if (modifyBoatMesh.underWaterTriangleData.Count > 0)
{
AddUnderWaterForces();
}
}
//Add all forces that act on the squares below the water
void AddUnderWaterForces()
{
//Get all triangles
List<TriangleData> underWaterTriangleData = modifyBoatMesh.underWaterTriangleData;
for (int i = 0; i < underWaterTriangleData.Count; i++)
{
//This triangle
TriangleData triangleData = underWaterTriangleData[i];
//Calculate the buoyancy force
Vector3 buoyancyForce = BuoyancyForce(rhoWater, triangleData);
//Add the force to the boat
boatRB.AddForceAtPosition(buoyancyForce, triangleData.center);
//Debug
//Normal
Debug.DrawRay(triangleData.center, triangleData.normal * 3f, Color.white);
//Buoyancy
Debug.DrawRay(triangleData.center, buoyancyForce.normalized * -3f, Color.blue);
}
}
//The buoyancy force so the boat can float
private Vector3 BuoyancyForce(float rho, TriangleData triangleData)
{
//Buoyancy is a hydrostatic force - it's there even if the water isn't flowing or if the boat stays still
// F_buoyancy = rho * g * V
// rho - density of the mediaum you are in
// g - gravity
// V - volume of fluid directly above the curved surface
// V = z * S * n
// z - distance to surface
// S - surface area
// n - normal to the surface
Vector3 buoyancyForce = rho * Physics.gravity.y * triangleData.distanceToSurface * triangleData.area * triangleData.normal;
//The vertical component of the hydrostatic forces don't cancel out but the horizontal do
buoyancyForce.x = 0f;
buoyancyForce.z = 0f;
return buoyancyForce;
}
}