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; } }
public void Execute(ArchetypeChunk batchInChunk, int batchIndex) { var boidChunk = batchInChunk.GetNativeArray(boidTypeHandle); var constrainChunk = batchInChunk.GetNativeArray(constrainTypeHandle); for (int i = 0; i < batchInChunk.Count; i++) { Constrain con = constrainChunk[i]; Boid b = boidChunk[i]; Vector3 force = Vector3.zero; Vector3 toTarget = positions[b.boidId] - centre; if (toTarget.magnitude > radius) { force = Vector3.Normalize(toTarget) * (radius - toTarget.magnitude); } /* * float xDist = positions[b.boidId].x - centre.x; * if (Mathf.Abs(xDist) > radius) * { * force.x += radius - xDist; * } * float yDist = positions[b.boidId].y - centre.y; * if (Mathf.Abs(yDist) > radius) * { * force.y += radius - yDist; * } * float zDist = positions[b.boidId].z - centre.z; * if (Mathf.Abs(zDist) > radius) * { * force.z += radius - zDist; * } */ con.force = force * weight; constrainChunk[i] = con; } }
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); }