private float3 GetTangent(int index, float fractionAlongPath) { float3 p0, p1, p2, p3; GetSplineSectionInternal(index, out p0, out p1, out p2, out p3); return(CatmullRom.GetTangent(p0, p1, p2, p3, fractionAlongPath)); }
private void CatmullRomTest(MyVector3 posA, MyVector3 posB, MyVector3 handleA, MyVector3 handleB) { CatmullRom catmullRomCurve = new CatmullRom(posA, posB, handleA, handleB); //Store the interpolated values so we later can display them List <Vector3> positions = new List <Vector3>(); List <Vector3> tangents = new List <Vector3>(); List <float> tValues = new List <float>(); //Loop between 0 and 1 in steps, where 1 step is minimum //So if steps is 5 then the line will be cut in 5 sections int steps = 5; float stepSize = 1f / (float)steps; float t = 0f; //+1 becuase wa also have to include the first point for (int i = 0; i < steps + 1; i++) { //Debug.Log(t); MyVector3 interpolatedPos = CatmullRom.GetPosition(posA, posB, handleA, handleB, t); positions.Add(interpolatedPos.ToVector3()); MyVector3 interpolatedTangent = CatmullRom.GetTangent(posA, posB, handleA, handleB, t); tangents.Add(interpolatedTangent.ToVector3()); tValues.Add(t); t += stepSize; } List <InterpolationTransform> transforms = InterpolationTransform.GetTransforms_RotationMinimisingFrame(catmullRomCurve, tValues, MyVector3.Up); //Display //DisplayInterpolation.DisplayCurve(positions, useRandomColor: true); DisplayInterpolation.DisplayCurve(positions, Color.black); //The actual curve for comparison DisplayInterpolation.DisplayCurve(catmullRomCurve, Color.gray); //The control points //The start and end values and the handle points DisplayInterpolation.DisplayHandle(handleA.ToVector3(), posA.ToVector3()); DisplayInterpolation.DisplayHandle(handleB.ToVector3(), posB.ToVector3()); //Other stuff //DisplayInterpolation.DisplayDirections(positions, tangents, 1f, Color.blue); DisplayInterpolation.DisplayOrientations(transforms, 1f); }
public void Execute(Entity entity, int index, ref Spawner thisSpawner) { if (thisSpawner.delaySpawn > 0) { thisSpawner.delaySpawn--; } else { RoadSection rs = RoadSections[thisSpawner.RoadIndex]; Interlocked.Increment(ref vehicleUID); float backOfVehiclePos = thisSpawner.Time - rs.vehicleHalfLen; float frontOfVehiclePos = thisSpawner.Time + rs.vehicleHalfLen; int occupationIndexStart = math.max(0, (int)(math.floor(backOfVehiclePos * rs.occupationLimit))); int occupationIndexEnd = math.min(rs.occupationLimit - 1, (int)(math.floor(frontOfVehiclePos * rs.occupationLimit))); if (!Occupied(occupationIndexStart, occupationIndexEnd, thisSpawner.RoadIndex, thisSpawner.LaneIndex)) { int vehiclePoolIndex = GetSpawnVehicleIndex(ref thisSpawner.random, thisSpawner.poolSpawn); float speedMult = VehiclePool[vehiclePoolIndex].VehicleSpeed; float speedRangeSelected = thisSpawner.random.NextFloat(0.0f, 1.0f); float initialSpeed = 0.0f; var vehicleEntity = EntityCommandBuffer.Instantiate(index, VehiclePool[vehiclePoolIndex].VehiclePrefab); EntityCommandBuffer.SetComponent(index, vehicleEntity, new VehiclePathing { vehicleId = vehicleUID, RoadIndex = thisSpawner.RoadIndex, LaneIndex = (byte)thisSpawner.LaneIndex, WantedLaneIndex = (byte)thisSpawner.LaneIndex, speed = initialSpeed, speedRangeSelected = speedRangeSelected, speedMult = speedMult, targetSpeed = initialSpeed, curvePos = thisSpawner.Time, random = new Unity.Mathematics.Random(thisSpawner.random.NextUInt(1, uint.MaxValue)) }); var heading = CatmullRom.GetTangent(rs.p0, rs.p1, rs.p2, rs.p3, 0.0f); EntityCommandBuffer.SetComponent(index, vehicleEntity, new VehicleTargetPosition { IdealPosition = thisSpawner.Position }); EntityCommandBuffer.SetComponent(index, vehicleEntity, new VehiclePhysicsState { Position = thisSpawner.Position, Heading = heading, SpeedMult = speedMult }); } var speedInverse = 1.0f / thisSpawner.minSpeed; thisSpawner.delaySpawn = (int)Constants.VehicleLength + thisSpawner.random.NextInt((int)(speedInverse * 10.0f), (int)(speedInverse * 120.0f)); } }
public void Execute(ref VehiclePathing p, ref VehicleTargetPosition pos, [ReadOnly] ref VehiclePhysicsState physicsState) { var rs = RoadSections[p.RoadIndex]; float3 c0 = CatmullRom.GetPosition(rs.p0, rs.p1, rs.p2, rs.p3, p.curvePos); float3 c1 = CatmullRom.GetTangent(rs.p0, rs.p1, rs.p2, rs.p3, p.curvePos); float3 c2 = CatmullRom.GetConcavity(rs.p0, rs.p1, rs.p2, rs.p3, p.curvePos); float curveSpeed = length(c1); pos.IdealPosition = c0; pos.IdealSpeed = p.speed; if (lengthsq(physicsState.Position - c0) < kMaxTetherSquared) { p.curvePos += Constants.VehicleSpeedFudge / rs.arcLength * p.speed / curveSpeed * DeltaTimeSeconds; } }