public static Float2 Rotate(this Float2 v, float radians) { var ca = MathFast.Cos(radians); var sa = MathFast.Sin(radians); return(new Float2(ca * v.X - sa * v.Y, sa * v.X + ca * v.Y)); }
void LeoECS() { _world = new EcsWorld(); JobMoveSystem.MinSpeed = param.minSpeed; JobMoveSystem.MaxSpeed = param.maxSpeed; JobMoveSystem.BoidScale = boidScale; JobMoveSystem.WallScale = param.wallScale * 0.5f; JobMoveSystem.WallDistance = param.wallDistance; JobMoveSystem.WallWeight = param.wallWeight; JobMoveSystem.NeighborFov = MathFast.Cos(param.neighborFov * MathFast.Deg2Rad); JobMoveSystem.NeighborDistance = param.neighborDistance; JobMoveSystem.SeparationWeight = param.separationWeight; JobMoveSystem.AlignmentWeight = param.alignmentWeight; JobMoveSystem.CohesionWeight = param.cohesionWeight; JobMoveSystem.EntitiesCount = boidCount; JobMoveSystem.NumBoidsPerJob = boidCount / System.Environment.ProcessorCount; if (BoidsVisualisationSystem._nativeMatrices.IsCreated) { BoidsVisualisationSystem._nativeMatrices.Dispose(); } BoidsVisualisationSystem._nativeMatrices = new NativeArray <Matrix4x4> (boidCount, Allocator.Persistent); BoidsVisualisationSystem._matrices = new Matrix4x4[boidCount]; var timeSystem = new TimeSystem(); _systems = new EcsSystems(_world); _systems .Add(timeSystem) .Add(new BoidsSimulationSystem()) .Add(new JobMoveSystem()) .Add(new BoidsVisualisationSystem() { mesh = mesh, material = material, scale = boidScale }) .Inject(timeSystem) .Initialize(); var random = new RngXorShift(853); BoidEntityData.Create(boidCount); for (int i = 0; i < boidCount; ++i) { var initSpeed = param.initSpeed; //init them with these default values var boid = _world.CreateEntityWith <BoidEntityData> (); boid.id = i; boid.Position = new Float3(random.GetFloat(), random.GetFloat(), random.GetFloat()); var vel = new Float3(random.GetFloat(), random.GetFloat(), random.GetFloat()); vel.Normalize(); vel.Scale(initSpeed); boid.Velocity = vel; boid.Acceleration = Float3.Zero; } _world.ProcessDelayedUpdates(); }
void LeoECS() { _world = new EcsWorld(); ThreadMoveSystem.MinSpeed = param.minSpeed; ThreadMoveSystem.MaxSpeed = param.maxSpeed; ThreadMoveSystem.BoidScale = boidScale; ThreadMoveSystem.WallScale = param.wallScale * 0.5f; ThreadMoveSystem.WallDistance = param.wallDistance; ThreadMoveSystem.WallWeight = param.wallWeight; ThreadMoveSystem.NeighborFov = MathFast.Cos(param.neighborFov * MathFast.Deg2Rad); ThreadMoveSystem.NeighborDistance = param.neighborDistance; ThreadMoveSystem.SeparationWeight = param.separationWeight; ThreadMoveSystem.AlignmentWeight = param.alignmentWeight; ThreadMoveSystem.CohesionWeight = param.cohesionWeight; ThreadMoveSystem.Neighbours = new BoidEntityData[boidCount][]; for (int i = 0; i < boidCount; i++) { ThreadMoveSystem.Neighbours[i] = new BoidEntityData[boidCount]; } BoidsVisualisationSystem2._matrices = new Matrix4x4[boidCount]; var timeSystem = new TimeSystem(); _systems = new EcsSystems(_world); _systems .Add(timeSystem) .Add(new BoidsSimulationSystem()) .Add(new ThreadMoveSystem()) .Add(new BoidsVisualisationSystem2() { mesh = mesh, material = material, scale = boidScale }) // .Add (new BoidsVisualisationSystem () { mesh = mesh, material = material, scale = boidScale }) .Inject(timeSystem) .Initialize(); var random = new RngXorShift(853); for (int i = 0; i < boidCount; ++i) { var initSpeed = param.initSpeed; //init them with these default values var boid = _world.CreateEntityWith <BoidEntityData> (); boid.position = new Float3(random.GetFloat(), random.GetFloat(), random.GetFloat()); boid.velocity = new Float3(random.GetFloat(), random.GetFloat(), random.GetFloat()); boid.velocity.Normalize(); boid.velocity.Scale(initSpeed); boid.acceleration = Float3.Zero; } _world.ProcessDelayedUpdates(); }
void CosTest() { Debug.Log(">>>>> cos tests >>>>>"); const int T = 10000; var sw = new System.Diagnostics.Stopwatch(); float f; float s = 1.345f; sw.Reset(); sw.Start(); for (int i = 0; i < T; i++) { f = Mathf.Cos(s); } sw.Stop(); Debug.LogFormat("mathf.cos time on {0} iterations: {1}", T, sw.ElapsedTicks); sw.Reset(); sw.Start(); for (int i = 0; i < T; i++) { f = (float)System.Math.Cos(s); } sw.Stop(); Debug.LogFormat("system.math.cos time on {0} iterations: {1}", T, sw.ElapsedTicks); // Warmup cache. f = MathFast.Cos(s); sw.Reset(); sw.Start(); for (int i = 0; i < T; i++) { f = MathFast.Cos(s); } sw.Stop(); Debug.LogFormat("mathfast.cos time on {0} iterations: {1}", T, sw.ElapsedTicks); var rng = Service <Rng> .Get(); for (int i = 0; i < 10; i++) { f = rng.GetFloat() * MathFast.PI_2; Debug.LogFormat("cos({0}) error checking => {1} / {2}", f, Mathf.Cos(f), MathFast.Cos(f)); } }