浏览代码

-Added latest physics tweaks and modified physics scene to debug performance

/dots-input
Mike Geig 5 年前
当前提交
2aa4f312
共有 5 个文件被更改,包括 17 次插入15 次删除
  1. 16
      Assets/Unity Physics Items/ApplyBuoyancyForceSystem.cs
  2. 10
      Assets/Unity Physics Items/BuoyantObject2.cs
  3. 4
      Assets/Unity Physics Items/DriveSystem.cs
  4. 2
      Assets/Unity Physics Items/PhysicsConversionSystem.cs

16
Assets/Unity Physics Items/ApplyBuoyancyForceSystem.cs


using Unity.Physics.Systems;
using Unity.Physics;
[UpdateAfter(typeof(GertsnerSystem)), UpdateAfter(typeof(ExportPhysicsWorld))]
[UpdateAfter(typeof(GertsnerSystem)), UpdateBefore(typeof(BuildPhysicsWorld))]
public class ApplyBuoyancyForceSystem : JobComponentSystem
{

DynamicBuffer<VoxelOffset> offsets = offsetBuffer[entity];
DynamicBuffer<VoxelHeight> heights = heightBuffer[entity];
var vel2 = vel;
Debug.Log("new pass: " + entity.ToString());
//Debug.Log("new pass: " + entity.ToString());
var entityTransform = new RigidTransform(rot.Value, pos.Value);
var wp = pos.Value + offsets[i].Value;
var wp = math.transform(entityTransform, offsets[i].Value);
float waterLevel = heights[i].Value.y;
if (wp.y - data.voxelResolution < waterLevel)

submergedAmount += subFactor;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;
submergedAmount += subFactor / offsets.Length;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;
//var force2 = data.localArchimedesForce * subFactor;

var localDampingForce = .005f * math.rcp(mass.InverseMass) * -velocity;
var force = localDampingForce + math.sqrt(subFactor) * data.localArchimedesForce;//\
ComponentExtensions.ApplyImpulse(ref vel, mass, pos, rot, force * dt, wp);
Debug.DrawLine(wp, force * dt + wp);
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, math.rcp(mass.InverseMass), dt));
//entity.ApplyImpulse(force, wp);//RB.AddForceAtPosition(force, wp);
//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));
}
}

10
Assets/Unity Physics Items/BuoyantObject2.cs


namespace WaterSystem
{
public class BuoyantObject2 : MonoBehaviour, IConvertGameObjectToEntity
public class BuoyantObject2 : MonoBehaviour//, IConvertGameObjectToEntity
{
public BuoyancyType _buoyancyType; // type of buoyancy to calculate
public float density; // density of the object, this is calculated off it's volume and mass

dstManager.AddBuffer<VoxelOffset>(entity);
dstManager.AddBuffer<VoxelHeight>(entity);
//var mass = dstManager.GetComponentData<Unity.Physics.PhysicsMass>(entity);
//mass.Transform.pos = centerOfMass;
//dstManager.SetComponentData(entity, mass);
var mass = dstManager.GetComponentData<Unity.Physics.PhysicsMass>(entity);
mass.CenterOfMass = centerOfMass;
dstManager.SetComponentData(entity, mass);
DynamicBuffer<VoxelOffset> offsets = dstManager.GetBuffer<VoxelOffset>(entity);
DynamicBuffer<VoxelHeight> heights = dstManager.GetBuffer<VoxelHeight>(entity);

offsets.Add(new VoxelOffset { Value = transform.TransformPoint(voxels[i]) - transform.position });
offsets.Add(new VoxelOffset { Value = voxels[i] - centerOfMass });// transform.TransformPoint(voxels[i]) - transform.position }); // << Is this right?
heights.Add(new VoxelHeight { Value = float3.zero });
}
}

4
Assets/Unity Physics Items/DriveSystem.cs


using System.Collections;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;

return job.Schedule(this, inputDeps);
}
[BurstCompile]
[RequireComponentTag(typeof(MoveWithInputTag))]
public struct DriveWithInputJob : IJobForEachWithEntity<Translation, Rotation, PhysicsVelocity, PhysicsMass, DrivingData>
{

steering = Mathf.Clamp(steering, -1f, 1f); // clamp for reasonable values
var sTorque = new float3(0f, data.torque, -data.torque * .5f) * steering / mass.InverseInertia;
ComponentExtensions.ApplyAngularImpulse(ref vel, mass, sTorque * dt);
Debug.Log(string.Format("Force: {0}, Torque: {1} Throttle: {2}", force, sTorque, throttle));
//Debug.Log(string.Format("Force: {0}, Torque: {1} Throttle: {2}", force, sTorque, throttle));
//RB.AddRelativeTorque(new Vector3(0f, torque, -torque * 0.5f) * modifier, ForceMode.Acceleration); // add torque based on input and torque amount
//}
}

2
Assets/Unity Physics Items/PhysicsConversionSystem.cs


// Update is called once per frame
protected override void OnUpdate()
{
//Entities.ForEach((BuoyantObject2 behaviour) => { behaviour.Convert(GetPrimaryEntity(behaviour), DstEntityManager, this); });
Entities.ForEach((BuoyantObject2 behaviour) => { behaviour.Convert(GetPrimaryEntity(behaviour), DstEntityManager, this); });
}
}
}

部分文件因为文件数量过多而无法显示

正在加载...
取消
保存