public DEMParticles simulate(DEMParticles particles, float t) { Vector2[] forces = particles.forces; List<Vector2> positions = particles.positions; List<Vector2> velocities = particles.velocities; List<float> massesInv = particles.massesInv; for (int iTarget = 0; iTarget < particles.length; iTarget++) { AABB boundingBox = particles.buildAABB(iTarget); int[] neighborIndices = spacePartitioner.search(boundingBox); forces[iTarget] = estimateForce(particles, iTarget, neighborIndices, t); } for (int iTarget = 0; iTarget < particles.length; iTarget++) { forces[iTarget] += boundaryForce.calcForce(particles, iTarget, t); } for (int iTarget = 0; iTarget < particles.length; iTarget++) { Vector2 accel = forces[iTarget] * massesInv[iTarget]; Vector2 velocity = velocities[iTarget]; positions[iTarget] += t * velocity; velocities[iTarget] += t * accel; } return particles; }
// Use this for initialization void Start() { this.particles = new DEMParticles(); this.spacePartitioner = new GridSP<int>( boundBL, boundTR, 20, 20, new IntEqualityComparer()); this.gravitationalForce = new GravitationalForce(new Vector2(0, -9.8f)); this.boundaryForce = new BoundaryForce( kn, cn, boundBL.x, boundBL.y, boundTR.x, boundTR.y); this.dem = new DEM(spacePartitioner, gravitationalForce, boundaryForce); spheres = new GameObject[nParticles]; for (int i = 0; i < nParticles; i++) { GameObject s = GameObject.CreatePrimitive(PrimitiveType.Sphere); s.transform.localScale = new Vector3(diameter, diameter, diameter); s.transform.position = new Vector3( Random.Range(boundBL.x, boundTR.x), Random.Range(boundBL.y, boundTR.y), 0); Destroy(s.collider); spheres[i] = s; } int length = spheres.Length; float[] radii = new float[length]; float[] masses = new float[length]; Vector2[] positions = new Vector2[length]; Vector2[] velocities = new Vector2[length]; for (int i = 0; i < length; i++) { GameObject sphere = spheres[i]; float radius = 0.5f * sphere.transform.localScale.x; radii[i] = radius; masses[i] = mass; //(rho * Mathf.PI * radius); positions[i] = new Vector2( sphere.transform.position.x, sphere.transform.position.y); velocities[i] = Vector2.zero; } particles.addParticle(masses, radii, positions, velocities); for (int i = 0; i < length; i++) { spacePartitioner.add(particles.buildAABB(i), i); } }