|
|
|
|
|
|
using Unity.Physics.Systems; |
|
|
|
using Unity.Physics; |
|
|
|
|
|
|
|
[UpdateAfter(typeof(GertsnerSystem)), UpdateBefore(typeof(BuildPhysicsWorld))] |
|
|
|
[UpdateAfter(typeof(GertsnerSystem)), UpdateAfter(typeof(ExportPhysicsWorld))] |
|
|
|
public class ApplyBuoyancyForceSystem : JobComponentSystem |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
Debug.Log(string.Format("DeltaTime: {0}, Time.time {1}, Calc Delta: {2}", Time.deltaTime, Time.time, Time.time - lastTime)); |
|
|
|
lastTime = Time.time; |
|
|
|
var job = new ForceJob() |
|
|
|
{ |
|
|
|
dt = Time.fixedDeltaTime, |
|
|
|
|
|
|
DynamicBuffer<VoxelOffset> offsets = offsetBuffer[entity]; |
|
|
|
DynamicBuffer<VoxelHeight> heights = heightBuffer[entity]; |
|
|
|
|
|
|
|
float submergedAmount = 0f; |
|
|
|
//Debug.Log("new pass: " + entity.ToString());
|
|
|
|
float3 avPos = float3.zero; |
|
|
|
float3 avForce = float3.zero; |
|
|
|
float avgHeight = 0; |
|
|
|
int total = 0; |
|
|
|
//Apply buoyant force
|
|
|
|
for (var i = 0; i < offsets.Length; i++) |
|
|
|
|
|
|
|
//Apply buoyant force
|
|
|
|
float submergedAmount = 0f; |
|
|
|
for (var i = 0; i < offsets.Length; i++) |
|
|
|
{ |
|
|
|
var wp = math.transform(entityTransform, offsets[i].Value); |
|
|
|
float waterLevel = heights[i].Value.y; |
|
|
|
|
|
|
//float depth = waterLevel - wp.y + (data.voxelResolution * 2f);
|
|
|
|
float subFactor = Mathf.Clamp01((waterLevel - (wp.y - data.voxelResolution)) / (data.voxelResolution * 2f));//depth / data.voxelResolution);
|
|
|
|
float subFactor = Mathf.Clamp01((waterLevel - (wp.y - data.voxelResolution)) / (data.voxelResolution * 2f)); |
|
|
|
submergedAmount += subFactor / offsets.Length;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;
|
|
|
|
submergedAmount += subFactor / offsets.Length; |
|
|
|
//var force2 = data.localArchimedesForce * subFactor;
|
|
|
|
|
|
|
|
|
|
|
|
var velocity = ComponentExtensions.GetLinearVelocity(vel, mass, pos, rot, wp); |
|
|
|
var velocity = vel.GetLinearVelocity(mass, pos, rot, wp); |
|
|
|
var force = localDampingForce + math.sqrt(subFactor) * data.localArchimedesForce;//\
|
|
|
|
ComponentExtensions.ApplyImpulse(ref vel, mass, pos, rot, force * dt, wp); |
|
|
|
//entity.ApplyImpulse(force, wp);//RB.AddForceAtPosition(force, wp);
|
|
|
|
avgHeight += force.y; |
|
|
|
total++; |
|
|
|
avPos += offsets[i].Value; |
|
|
|
avForce += math.rotate(math.inverse(rot.Value), force * dt); |
|
|
|
//Debug.Log(string.Format("ECS: Position: {0:f1} -- Force: {1:f2} -- Height: {2:f2}\nVelocty: {3:f2} -- Damp: {4:f2} -- Mass: {5:f1} -- K: {6:f2}", wp, force, waterLevel, velocity, localDampingForce, math.rcp(mass.InverseMass), dt));
|
|
|
|
var force = localDampingForce + math.sqrt(subFactor) * data.localArchimedesForce; |
|
|
|
vel.ApplyImpulse(mass, pos, rot, force * dt, wp); |
|
|
|
|
|
|
|
//Update drag
|
|
|
|
//Debug.Log("Avg force: " + avForce / total + " Avg pos: " + avPos / total);
|
|
|
|
//Debug.Log($"Avg force: {avForce / total} Avg pos: {avPos / total}");
|
|
|
|
//submergedAmount /= offsets.Length;
|
|
|
|
//damping.Linear = Mathf.Lerp(data.baseDrag, 1f, submergedAmount);
|
|
|
|
//damping.Angular = Mathf.Lerp(data.baseAngularDrag, 1f, submergedAmount);
|
|
|
|
|
|
|
|
//data.percentSubmerged = Mathf.Lerp(data.percentSubmerged, submergedAmount, 0.25f);
|
|
|
|
//damping.Linear = data.baseDrag + (data.baseDrag * (data.percentSubmerged * 10f));
|
|
|
|
//damping.Angular = data.baseAngularDrag + (data.percentSubmerged * 0.5f);
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |