浏览代码

-Achieved math parity, but the physics results are still way off. Buoyancy is also wrong on both objects

/dots-input
Mike Geig 5 年前
当前提交
00bed225
共有 6 个文件被更改,包括 1017 次插入25 次删除
  1. 34
      Assets/Unity Physics Items/ApplyBuoyancyForceSystem.cs
  2. 2
      Assets/Unity Physics Items/BuoyantObject2.cs
  3. 950
      Assets/Unity Physics Items/Physics Scene.unity
  4. 6
      Packages/com.verasl.water-system/Scripts/BuoyantObject.cs
  5. 42
      Assets/Unity Physics Items/Slippery.asset
  6. 8
      Assets/Unity Physics Items/Slippery.asset.meta

34
Assets/Unity Physics Items/ApplyBuoyancyForceSystem.cs


return job.Schedule(this, inputDeps);
}
public struct ForceJob : IJobForEachWithEntity<Translation, Rotation, PhysicsVelocity, PhysicsMass, BuoyantData>
//[BurstCompile]
public struct ForceJob : IJobForEachWithEntity<Translation, Rotation, PhysicsVelocity, PhysicsMass, PhysicsDamping, BuoyantData>
{
public float dt;

[ReadOnly]
public BufferFromEntity<VoxelHeight> heightBuffer;
public void Execute(Entity entity, int index, ref Translation pos, ref Rotation rot, ref PhysicsVelocity vel, ref PhysicsMass mass, ref BuoyantData data)
public void Execute(Entity entity, int index, ref Translation pos, ref Rotation rot, ref PhysicsVelocity vel, ref PhysicsMass mass, ref PhysicsDamping damping, ref BuoyantData data)
{
DynamicBuffer<VoxelOffset> offsets = offsetBuffer[entity];
DynamicBuffer<VoxelHeight> heights = heightBuffer[entity];

if (wp.y - data.voxelResolution < waterLevel)
{
float k = Mathf.Clamp01(waterLevel - (wp.y - data.voxelResolution)) / (data.voxelResolution * 2f);
//float depth = waterLevel - wp.y + (data.voxelResolution * 2f);
float subFactor = Mathf.Clamp01(waterLevel - (wp.y - data.voxelResolution)) / (data.voxelResolution * 2f);//depth / data.voxelResolution);
submergedAmount += k / data.voxelResolution;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;
submergedAmount += subFactor;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;
var force2 = data.localArchimedesForce * subFactor;
var force = localDampingForce + math.sqrt(k) * data.localArchimedesForce;//\
ComponentExtensions.ApplyImpulse(ref vel, mass, pos, rot, force * dt * dt, wp);
var force = localDampingForce + math.sqrt(subFactor) * data.localArchimedesForce;//\
ComponentExtensions.ApplyImpulse(ref vel, mass, pos, rot, force * dt, wp);
//Debug.Log(string.Format("B Position: {0} -- Force: {1}", wp.ToString(), force.ToString()));
//data.percentSubmerged = Mathf.Lerp(data.percentSubmerged, submergedAmount, 0.25f);
//RB.drag = data.baseDrag + (data.baseDrag * (data.percentSubmerged * 10f));
//RB.angularDrag = data.baseAngularDrag + (data.percentSubmerged * 0.5f);
//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);
}
}

2
Assets/Unity Physics Items/BuoyantObject2.cs


samplePoints = new float3[1];
}
float archimedesForceMagnitude = WATER_DENSITY * Mathf.Abs(Physics.gravity.y) * volume;
float archimedesForceMagnitude = WATER_DENSITY * -Physics.gravity.y * volume;
localArchimedesForce = new Vector3(0, archimedesForceMagnitude, 0) / samplePoints.Length;
}

950
Assets/Unity Physics Items/Physics Scene.unity
文件差异内容过多而无法显示
查看文件

6
Packages/com.verasl.water-system/Scripts/BuoyantObject.cs


for(var i = 0; i < voxels.Length; i++) BuoyancyForce(voxels[i], heights[i].y, ref submergedAmount, ref debugInfo[i]);
Physics.SyncTransforms();
Physics.autoSyncTransforms = true;
//UpdateDrag(submergedAmount);
UpdateDrag(submergedAmount);
}
else if(_buoyancyType == BuoyancyType.Physical)
{

private void BuoyancyForce(Vector3 position, float waterHeight, ref float submergedAmount, ref DebugDrawing _debug)
{
var wp = transform.TransformPoint(position);
float waterLevel = waterHeight;
float waterLevel = waterHeight;
_debug.position = wp;
_debug.waterHeight = waterLevel;

RB.AddForceAtPosition(force, wp);
_debug.force = force; // For drawing force gizmos
Debug.Log(string.Format("Position: {0:f1} -- Force: {1:f2} -- Height: {2:f2}\nVelocty: {3:f2} -- Damp: {4:f2} -- Mass: {5:f1} -- K: {6:f2}", wp, force, waterHeight, velocity, localDampingForce, RB.mass, localArchimedesForce));
Debug.Log(string.Format("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, RB.mass, localArchimedesForce));
}
}

42
Assets/Unity Physics Items/Slippery.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 79f877434772c463192af154345455c9, type: 3}
m_Name: Slippery
m_EditorClassIdentifier:
m_Value:
m_SupportsTemplate: 0
m_Template: {fileID: 0}
m_IsTrigger:
m_Override: 1
m_Value: 0
m_Friction:
m_Override: 1
m_Value:
Value: 0
CombineMode: 0
m_Restitution:
m_Override: 1
m_Value:
Value: 0
CombineMode: 0
m_BelongsTo:
m_Override: 1
m_Value: 0101010101010101010101010101010101010101010101010101010101010101
m_CollidesWith:
m_Override: 1
m_Value: 0101010101010101010101010101010101010101010101010101010101010101
m_RaisesCollisionEvents:
m_Override: 1
m_Value: 0
m_CustomFlags:
m_Override: 1
m_Value: 0000000000000000

8
Assets/Unity Physics Items/Slippery.asset.meta


fileFormatVersion: 2
guid: 15aa0e33a6fa41749b19f36537af1280
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存