浏览代码

* Moved ApplyBuoyancyForceSystem to UpdateAfter ExportPhysicsWorld to better match waves (minic older frame delay)

* added engine position back for drive
* tidied use of ComponentExtensions
/dots-physics
Steve Ewart 5 年前
当前提交
a403f7e4
共有 3 个文件被更改,包括 53 次插入93 次删除
  1. 54
      Assets/Unity Physics Items/BuoyantObject_DOTS.cs
  2. 48
      Assets/Unity Physics Items/Systems/ApplyBuoyancyForceSystem.cs
  3. 44
      Assets/Unity Physics Items/Systems/DriveSystem.cs

54
Assets/Unity Physics Items/BuoyantObject_DOTS.cs


private void Start()
{
//Application.targetFrameRate = (int)(1f / Time.fixedDeltaTime);
Application.targetFrameRate = (int)(1f / Time.fixedDeltaTime);
_guid = gameObject.GetInstanceID();
Init();

{
float k = Mathf.Clamp01(waterLevel - (wp.y - voxelResolution)) / (voxelResolution * 2f);
submergedAmount += k / voxels.Length;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;
submergedAmount += k / voxels.Length;
var velocity = RB.GetPointVelocity(wp);
velocity.y *= 2f;

//Will be called by the PhysicsConversionSystem
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
//Calculat all initial values
// Calculate all initial values
//Add data needed for buoyancy
BuoyantData data = new BuoyantData();
//Add data needed for buoyancy
BuoyantData data = new BuoyantData();
data.type = _buoyancyType;
data.voxelResolution = voxelResolution;
data.localArchimedesForce = localArchimedesForce;

mass.CenterOfMass = centerOfMass;
dstManager.SetComponentData(entity, mass);
//Initialize the voxel and height buffers
DynamicBuffer<VoxelOffset> offsets = dstManager.GetBuffer<VoxelOffset>(entity);
//Initialize the voxel and height buffers
DynamicBuffer<VoxelOffset> offsets = dstManager.GetBuffer<VoxelOffset>(entity);
//Add engine position as first point
//offsets.Add(new VoxelOffset { Value = engine.enginePosition - centerOfMass });
//heights.Add(new VoxelHeight { Value = float3.zero });
//Add engine position as first point
var engine = GetComponent<Engine>();
if (engine)
{
offsets.Add(new VoxelOffset { Value = new float3(engine.enginePosition) - mass.CenterOfMass });
heights.Add(new VoxelHeight { Value = float3.zero });
}
//Add the rest of the positions
for (int i = 0; i < voxels.Length; i++)
//Add the rest of the positions
for (int i = 0; i < voxels.Length; i++)
offsets.Add(new VoxelOffset { Value = voxels[i] - centerOfMass });// transform.TransformPoint(voxels[i]) - transform.position }); // << Is this right?
offsets.Add(new VoxelOffset { Value = voxels[i] });
//Call other Convert methods
var body = GetComponentInChildren<BoatBodyComponent>();
//if (body)
body.Convert(conversionSystem.GetPrimaryEntity(body), dstManager, conversionSystem);
// Call other Convert methods
if (engine)
engine.Convert(conversionSystem.GetPrimaryEntity(engine), dstManager, conversionSystem);
var body = GetComponentInChildren<BoatBodyComponent>();
if (body)
body.Convert(conversionSystem.GetPrimaryEntity(body), dstManager, conversionSystem);
var engine = GetComponent<Engine>();
if (engine)
engine.Convert(conversionSystem.GetPrimaryEntity(engine), dstManager, conversionSystem);
}
}
struct DebugDrawing
struct DebugDrawing
{
public Vector3 force;
public Vector3 position;

48
Assets/Unity Physics Items/Systems/ApplyBuoyancyForceSystem.cs


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);
}
}
}

44
Assets/Unity Physics Items/Systems/DriveSystem.cs


public void Execute(Entity entity, int index, [ReadOnly] ref Translation pos, [ReadOnly] ref Rotation rot, ref PhysicsVelocity vel, [ReadOnly] ref PhysicsMass mass, ref DrivingData data)
{
var wp = pos.Value + data.engineOffset;
//if (wp.y <= -0.1f) // if the engine is deeper than 0.1
//{
//accel
data.throttle = Mathf.Clamp(data.throttle, 0f, 1f); // clamp for reasonable values
float3 forward = math.forward(rot.Value);
forward.y = 0f;
forward = math.normalize(forward);
var force = (forward * data.throttle * data.horsePower) / mass.InverseMass; //divide by iMass to counteract mass in impulse method
var torque = (data.throttle * new float3(-1, 0, 0)) / mass.InverseInertia;
var entityTransform = new RigidTransform(rot.Value, pos.Value);
var wp = math.transform(entityTransform, data.engineOffset+mass.CenterOfMass);
if (wp.y <= -0.1f) // if the engine is deeper than 0.1
{
data.throttle = Mathf.Clamp(data.throttle, 0f, 1f); // clamp for reasonable values
float3 forward = math.forward(rot.Value);
forward.y = 0f;
forward = math.normalize(forward);
//accel
var force = (forward * data.throttle * data.horsePower) / mass.InverseMass; //divide by iMass to counteract mass in impulse method
ComponentExtensions.ApplyLinearImpulse(ref vel, mass, force * dt);
//ComponentExtensions.ApplyLinearImpulse(ref vel, mass, up * 20000f * dt);
//ComponentExtensions.ApplyAngularImpulse(ref vel, mass, torque * dt);
//RB.AddForce(forward * modifier * horsePower, ForceMode.Acceleration); // add force forward based on input and horsepower
//RB.AddRelativeTorque(-Vector3.right * modifier, ForceMode.Acceleration);
vel.ApplyLinearImpulse(mass, force * dt);
//Turning
data.steering = Mathf.Clamp(data.steering, -1f, 1f); // clamp for reasonable values
var sTorque = new float3(0f, data.torque, -data.torque * .5f) * data.steering / mass.InverseInertia;
ComponentExtensions.ApplyAngularImpulse(ref vel, mass, sTorque * dt);
//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
//}
//Turning
var torque = (data.throttle * new float3(-1, 0, 0)) / mass.InverseInertia;
data.steering = Mathf.Clamp(data.steering, -1f, 1f); // clamp for reasonable values
var sTorque = new float3(0f, data.torque, -data.torque * .5f) * data.steering / mass.InverseInertia;
vel.ApplyAngularImpulse(mass, sTorque * dt);
}
}
}
}
正在加载...
取消
保存