public void Execute(ArchetypeChunk batchInChunk, int batchIndex) { var boidChunk = batchInChunk.GetNativeArray(boidTypeHandle); var translationsChunk = batchInChunk.GetNativeArray(translationTypeHandle); var ltwChunk = batchInChunk.GetNativeArray(ltwTypeHandle); var obstacleAvoidanceChunk = batchInChunk.GetNativeArray(obstacleAvoidanceTypeHandle); var rotationChunk = batchInChunk.GetNativeArray(rotationTypeHandle); for (int i = 0; i < batchInChunk.Count; i++) { float3 force = Vector3.zero; Translation p = translationsChunk[i]; ObstacleAvoidance oa = obstacleAvoidanceChunk[i]; LocalToWorld ltw = ltwChunk[i]; Boid b = boidChunk[i]; Rotation r = rotationChunk[i]; oa.normal = Vector3.zero; oa.point = Vector3.zero; float3 forward = math.mul(r.Value, Vector3.forward); forward = math.normalize(forward); var input = new RaycastInput() { Start = p.Value, End = p.Value + (forward * oa.forwardFeelerDepth), //Filter = CollisionFilter.Default Filter = new CollisionFilter { BelongsTo = ~0u, CollidesWith = ~0u, GroupIndex = 0 } }; oa.start = input.Start; oa.end = input.End; Unity.Physics.RaycastHit hit; if (collisionWorld.CastRay(input, out hit)) { Debug.Log("Collides"); float dist = math.distance(hit.Position, p.Value); force += hit.SurfaceNormal * (oa.forwardFeelerDepth / dist); oa.normal = hit.SurfaceNormal; oa.point = hit.Position; } oa.force = force; obstacleAvoidanceChunk[i] = oa; } }
public void Execute(ArchetypeChunk batchInChunk, int batchIndex) { var wanderChunk = batchInChunk.GetNativeArray(wanderTypeHandle); var boidChunk = batchInChunk.GetNativeArray(boidTypeHandle); var translationsChunk = batchInChunk.GetNativeArray(translationTypeHandle); var rotationsChunk = batchInChunk.GetNativeArray(rotationTypeHandle); var seperationChunk = batchInChunk.GetNativeArray(seperationTypeHandle); var cohesionChunk = batchInChunk.GetNativeArray(cohesionTypeHandle); var alignmentChunk = batchInChunk.GetNativeArray(alignmentTypeHandle); var constrainChunk = batchInChunk.GetNativeArray(constrainTypeHandle); var oaChunk = batchInChunk.GetNativeArray(obstacleTypeHandle); for (int i = 0; i < batchInChunk.Count; i++) { Wander w = wanderChunk[i]; Translation p = translationsChunk[i]; Rotation r = rotationsChunk[i]; Boid b = boidChunk[i]; Seperation s = seperationChunk[i]; Cohesion c = cohesionChunk[i]; Alignment a = alignmentChunk[i]; Constrain con = constrainChunk[i]; ObstacleAvoidance oa = oaChunk[i]; b.force = AccululateForces(ref b, ref oa, ref s, ref a, ref c, ref w, ref con) * b.weight; b.force = Vector3.ClampMagnitude(b.force, b.maxForce); Vector3 newAcceleration = (b.force * b.weight) * (1.0f / b.mass); newAcceleration.y *= limitUpAndDown; b.acceleration = Vector3.Lerp(b.acceleration, newAcceleration, dT); b.velocity += b.acceleration * dT; b.velocity = Vector3.ClampMagnitude(b.velocity, b.maxSpeed); float speed = b.velocity.magnitude; speeds[b.boidId] = speed; if (speed > 0) { Vector3 tempUp = Vector3.Lerp(b.up, (Vector3.up) + (b.acceleration * banking), dT * 3.0f); rotations[b.boidId] = Quaternion.LookRotation(b.velocity, tempUp); b.up = rotations[b.boidId] * Vector3.up; positions[b.boidId] += b.velocity * dT; b.velocity *= (1.0f - (damping * dT)); } b.force = Vector3.zero; boidChunk[i] = b; } }
private Vector3 AccululateForces(ref Boid b, ref ObstacleAvoidance oa, ref Seperation s, ref Alignment a, ref Cohesion c, ref Wander w, ref Constrain con) { Vector3 force = Vector3.zero; force += oa.force; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += b.fleeForce; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += s.force; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += a.force; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += c.force; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += w.force; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += con.force; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } force += b.seekForce; if (force.magnitude >= b.maxForce) { force = Vector3.ClampMagnitude(force, b.maxForce); return(force); } return(force); }