private void CreateParticle(Vector3?pos = null) { if (activeParticles.Count < MaximumParticles) { MyParticle p = null; float l = lifespan + Random.Range(-lifespanDeviation / 2, lifespanDeviation / 2); float si = size + Random.Range(-sizeDeviation / 2, sizeDeviation / 2); float sp = speed + Random.Range(-speedDeviation / 2, speedDeviation / 2); Vector3 d = direction + new Vector3(Random.Range(-directionDeviation.x / 2, directionDeviation.x / 2), Random.Range(-directionDeviation.y / 2, directionDeviation.y / 2), Random.Range(-directionDeviation.z / 2, directionDeviation.z / 2)); if (inactiveParticles.Count > 0) { p = inactiveParticles.Pop(); p.Reset(l, si, sp, d); p.gameObject.transform.position = (isSubEmitter && pos != null) ? (pos.Value) : (transform.position); p.gameObject.SetActive(true); } else { GameObject g = GameObject.Instantiate(particlePrefab, (isSubEmitter && pos != null) ? (pos.Value) : (transform.position), Quaternion.identity, transform); p = new MyParticle(g, l, si, sp, d); } onBirth.Invoke(p.gameObject.transform.position); activeParticles.Add(p); } }
void Swirl(MyParticle flake) { int count = flake.GetCount(); if (count < spline.getListLength()) { Vector3 positionAtI = spline.getPositionAtI(count); Vector3[] coordinateFrame = frenet.frenetAt(count); Vector3 T = coordinateFrame[0]; Vector3 N = coordinateFrame[1]; Vector3 B = coordinateFrame[2]; float omega = GetOmega(flake, T); Debug.DrawLine(positionAtI, positionAtI + T); Debug.DrawLine(positionAtI, positionAtI + N); Debug.DrawLine(positionAtI, positionAtI + B); //we need the position it should be at, plus rotation flake.transform.position = positionAtI; flake.transform.rotation = Quaternion.LookRotation(T, N); Vector3 oldPosition = flake.transform.position; flake.transform.RotateAround(positionAtI, T, Time.time); flake.IncCount(); } }
public override void InitVelocity(MyParticle p) { p.Velocity = Vector3.forward; //p.Velocity.x = baseVelocity * Mathf.Cos(theta) * Mathf.Sin(phi); //p.Velocity.y = baseVelocity * Mathf.Cos(phi); //p.Velocity.z = baseVelocity * Mathf.Sin(theta) * Mathf.Sin(phi); }
void CurvedSwirl(MyParticle flake) { int count = flake.GetCount(); if (count < spline.getListLength()) { //transform of flake = this point Vector3 positionAtI = spline.getPositionAtI(count); Vector3[] coordinateFrame = frenet.frenetAt(count); Vector3 T = coordinateFrame[0]; Vector3 N = coordinateFrame[1]; Vector3 B = coordinateFrame[2]; float omega = GetCurvedOmega(flake, T, N, B, positionAtI); //we need the position it should be at, plus rotation flake.transform.position = positionAtI; flake.transform.rotation = Quaternion.LookRotation(T, N); Vector3 flakeOriginalPosition = flake.getInitUnitPosition(); flake.transform.position = positionAtI + flakeOriginalPosition; float factor = 1f; Vector3 oldPosition = flake.transform.position; flake.transform.RotateAround(positionAtI, T, omega * Time.time); flake.IncCount(); } }
float GetCurvedOmega(MyParticle flake, Vector3 T, Vector3 N, Vector3 B, Vector3 splinePosition) { float omega; float distance = (float)flake.GetCount() / (float)spline.getListLength(); Vector3 center = splinePosition - distance * T; // axis is the direction Vector3 xvi = vortex.BaseToParticle(flake.transform.position, center); float li = vortex.ProjectionLengthAtParticle(xvi); Vector3 ri = vortex.OrthoVFromAxisToParticle(xvi, li); float riMag = vortex.RadiusDistFromParticleToAxis(ri); //if (vortex.InVortexCylinder(li, riMag)) //{ float fi = vortex.Frequency(riMag); omega = vortex.GetOmega(fi); // }else{ // omega = 0f; // } return(omega); }
public override void InitPosition(MyParticle p, Vector3 pos) { // Debug.Log("pos no init: " + pos); p.Position.x = p.PreviousPosition.x = pos.x + ((float)rg.NextDouble() * 100.0f); p.Position.y = p.PreviousPosition.y = pos.y + ((float)rg.NextDouble() * 10.0f); p.Position.z = p.PreviousPosition.z = pos.z + ((float)rg.NextDouble() * 15.0f); }
public void updateForce(MyParticle particle, float duration) { // Calculate spring vector MyVector3 force; force = particle.getPosition(); force.SubtractVectorFromThisVector(other.getPosition()); //Check is the bungie is compressed // if bungie is compressed break out of the method without adding a force float magnitude = force.magnitude(); if (magnitude < restLength) { return; } else if (magnitude > restLength) { //Everything after this comment will happen if the if condition isn't true //Calculate the magnitude of the force magnitude = springConstant * (restLength - magnitude); //magnitude = springConstant * (magnitude-restLength); //Calculate the final force and apply it force.normalize(); force.multiplyVectorByScalar(magnitude); particle.addForce(force); } else { return; } }
private void ResetComputeSim() { InitTerrainTex(); NumRepulsors = 4; RepulsorPos = new Vector4[NumRepulsors]; RepulsorVel = new Vector4[NumRepulsors]; for (int i = 0; i < NumRepulsors; i++) { RepulsorVel[i] = new Vector4(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)); RepulsorPos[i].x = Random.Range(TexResolutionX * 0.45f, TexResolutionX * 0.55f); RepulsorPos[i].y = TexResolutionY * (i + 1) / 5.0f; RepulsorPos[i].z = -0.5f; } MyParticle[] pArray = new MyParticle[NumParticles]; //particleBuffer.GetData(pArray); for (int i = 0; i < NumParticles; i++) { MyParticle p = new MyParticle(); p.pos = new Vector2(Random.Range(0.0f, TexResolutionX), Random.Range(0, TexResolutionY)); p.vel = new Vector2(0.0f, -10.0f); p.acc = new Vector2(0.0f, 0.0f); p.noise = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)); pArray[i] = p; } particleBuffer.SetData(pArray); ComputeStepFrame(); }
public MySpring(MyParticleSystem parnetParticleSystem, MyParticle targetOne, MyParticle targetTwo, float restLength, float strength, float damping) { if (parnetParticleSystem == null) { throw new System.ArgumentNullException("particleSystem", "Cannot supply null as ParticleSystem to CustomSpring"); } if (targetOne == null) { throw new System.ArgumentNullException("particle1", "Cannot supply null as Particle1 to CustomSpring"); } if (targetTwo == null) { throw new System.ArgumentNullException("particle2", "Cannot supply null as Particle2 to CustomSpring"); } //add the string to the particle system this.parnetParticleSystem = parnetParticleSystem; this.parnetParticleSystem.springs.Add(this); this.targetOne = targetOne; this.targetTwo = targetTwo; this.rest = restLength; this.strength = strength; this.damping = damping; lineRender = targetOne.gameObject.AddComponent <LineRenderer>(); lineRender.material = new Material(Shader.Find("Particles/Additive")); lineRender.SetWidth(0.2F, 0.2F); }
public void updateForce(MyParticle particle, float duration) { //checking if particle has infinite mass if (!particle.hasFiniteMass()) { return; } //calculates position of the particle relative to the anchor MyVector3 position; position = particle.getPosition(); position.SubtractVectorFromThisVector(this.anchor); //Calculate constants and check whether they are in bounds float gamma = 0.5f * Mathf.Sqrt(4 * springConstant - damping * damping); if (gamma == 0.0f) { return; } MyVector3 c = position.multiplyVectorByScalarVR(damping / (2.0f * gamma)).returnAddVectorToThisVector(particle.getVelocity().multiplyVectorByScalarVR(1.0f / gamma)); //Calculate the target position MyVector3 target = position.multiplyVectorByScalarVR(Mathf.Cos(gamma * duration)).returnAddVectorToThisVector(c.multiplyVectorByScalarVR(Mathf.Sin(gamma * duration))); target.multiplyVectorByScalar(Mathf.Exp(-0.5f * duration * damping)); //Calculate resulting acceration and force MyVector3 accel = (target.returnSubtractVectorFromThisVector(position)).multiplyVectorByScalarVR(1.0f / (duration * duration)).returnSubtractVectorFromThisVector(particle.getVelocity().multiplyVectorByScalarVR(duration)); particle.addForce(accel.multiplyVectorByScalarVR(particle.getMass())); }
/*Updates force*/ /* * void ParticleForceGenerator.updateForce (MyParticle particle, float duration){ * * //check we don't have infinite mass * if (!particle.hasFiniteMass()) * return; * //app mass-scaled force to the particle * particle.addForce(gravity.multiplyVectorByScalarVR(particle.getMass ())); * } */ public void updateForce(MyParticle particle, float duration) { //ParticleForceGenerator.updateForce (particle, duration); //check we don't have infinite mass if (!particle.hasFiniteMass()) { return; } //app mass-scaled force to the particle particle.addForce(gravity.multiplyVectorByScalarVR(particle.getMass())); }
public void InitParticle(MyParticle p, Vector3 pos) { InitFormulas(); //p.LifeTime = 1.0f; InitLifeTime(p); p.Mat = ParticleMaterial; InitPosition(p, pos); p.PreviousVelocity.x = p.PreviousVelocity.y = p.PreviousVelocity.z = 0; InitVelocity(p); InitAcceleration(p); p.Size = 0.1f; }
private void setPhaseSpace(List <PhaseSpace> phaseSpace) { if (particles.Count > 0) { for (int i = 0; i < particles.Count; i++) { MyParticle particle = this.particles[i]; // TODO: Should it plus ? particle.position = new Vector3(phaseSpace[i].x, phaseSpace[i].y, phaseSpace[i].z); particle.velocity = new Vector3(phaseSpace[i].x_v, phaseSpace[i].y_v, phaseSpace[i].z_v); } } }
public void updateForce(MyParticle particle, float duration) { MyVector3 force; force = particle.getVelocity(); //Calculate Total Drag coefficient float dragCoeff = force.magnitude(); dragCoeff = k1 * dragCoeff + k2 * dragCoeff * dragCoeff; //Calculate and apply the final force force.normalize(); force.multiplyVectorByScalar(-dragCoeff); particle.addForce(force); }
void InitParticleList() { Vector3 position; for (int i = 0; i < numFlakes; i++) { position = new Vector3(heightRandList[i % maxRandLength], randList[i % maxRandLength].y, randList[(i) % maxRandLength].x); flakeList[i] = Instantiate(snowPrefab, position, Quaternion.identity); MyParticle flake = flakeList[i].GetComponent <MyParticle>(); flake.SetInitUnitPosition(position); flake.SetCount(Random.Range(0, 100)); float scale = heightRandList[(i + 4) % maxRandLength] - sky; } Clear(flakeList); }
private void ResetComputeSim() { MyParticle[] pArray = new MyParticle[NumParticles]; //particleBuffer.GetData(pArray); for (int i = 0; i < NumParticles; i++) { MyParticle p = new MyParticle(); p.pos = new Vector2(Random.Range(10, TexResolution - 10), Random.Range(10, TexResolution - 10)); p.dir = new Vector2(Random.Range(-50, +50), Random.Range(-50, +50)); Color c = Random.ColorHSV(0, 1.0f, 0.5f, 1.0f, 0.5f, 1.0f); p.col = new Vector4(c.r, c.g, c.b, 0.0f); pArray[i] = p; } particleBuffer.SetData(pArray); ComputeStepFrame(); }
public void updateForce(MyParticle particle, float duration) { // Calculate spring vector MyVector3 force; force = particle.getPosition(); force.SubtractVectorFromThisVector(anchor); //Calculate the magnitude of the force float magnitude = force.magnitude(); magnitude = absValue(magnitude - this.restLength); magnitude *= springConstant; //Calculate the final force and apply it force.normalize(); force.multiplyVectorByScalar(-1 * magnitude); particle.addForce(force); }
float GetOmega(MyParticle flake, Vector3 axis) { float omega; Vector3 center = axis; // axis is the direction Vector3 xvi = vortex.BaseToParticle(flake.transform.position, center); float li = vortex.ProjectionLengthAtParticle(xvi); Vector3 ri = vortex.OrthoVFromAxisToParticle(xvi, li); float riMag = vortex.RadiusDistFromParticleToAxis(ri); if (vortex.InVortexCylinder(li, riMag)) { float fi = vortex.Frequency(riMag); omega = vortex.GetOmega(fi); } else { omega = 0f; } return(omega); }
//I might come back and add some get and set methods to modify volume and waterHeight public void updateForce(MyParticle particle, float duration) { float depth = particle.getPosition().getComponentY(); //check is object is out of water // if (depth >= waterHeight + maxDepth) { return; } MyVector3 force = new MyVector3(0f, 0f, 0f); //check if we're at max depth if (depth <= waterHeight - maxDepth) { force.y = liquidDensity * volume; particle.addForce(force); return; } //if the other 2 aren't true we're partially submerged force.y = liquidDensity * volume * (depth - maxDepth - waterHeight); particle.addForce(force); }
public void addParticle(MyParticle particle1) { particle [1] = particle1; }
public ParticleContact(MyParticle particle1) { particle [0] = particle1; }
/* * public Move(MyVector3 force) * { * this.force = force; * } */ public void updateForce(MyParticle particle, float duration) { particle.addForce(this.force.multiplyVectorByScalarVR(particle.getMass())); }
private void findNeighbors(MyParticle particle) { particle.neighborCount = 0; Dictionary<int, List<int>> gridX; List<int> gridY; for (int nx = -1; nx < 2; nx++) { for (int ny = -1; ny < 2; ny++) { int x = particle.ci + nx; int y = particle.cj + ny; if (_grid.TryGetValue(x, out gridX) && gridX.TryGetValue(y, out gridY)) { for (int a = 0; a < gridY.Count; a++) { if (gridY[a] != particle.index) { particle.neighbors[particle.neighborCount] = gridY[a]; particle.neighborCount++; if (particle.neighborCount >= MAX_NEIGHBORS) return; } } } } } }
/*Removes given pair from Registry. If not in registry nothing happens*/ public void remove(MyParticle particle, ParticleForceGenerator fg) { registration.setValues(particle, fg); Registry.Remove(registration); listLength--; }
protected void UpdatePosition(MyParticle p) { p.Position.x = p.PreviousPosition.x + Time.deltaTime * p.PreviousVelocity.x; p.Position.y = p.PreviousPosition.y + Time.deltaTime * p.PreviousVelocity.y; p.Position.z = p.PreviousPosition.z + Time.deltaTime * p.PreviousVelocity.z; }
protected void UpdateMaterial(MyParticle p) { ParticleColor.a = p.LifeTime; p.Mat.SetColor("_TintColor", ParticleColor); }
protected abstract void InitLifeTime(MyParticle p);
public abstract void UpdateAcceleration(MyParticle p);
public abstract void InitVelocity(MyParticle p);
public override void InitAcceleration(MyParticle p) { p.Acceleration.x = (p.Velocity.x - p.PreviousVelocity.x) / Time.deltaTime; p.Acceleration.y = (p.Velocity.y - p.PreviousVelocity.y) / Time.deltaTime; p.Acceleration.z = (p.Velocity.z - p.PreviousVelocity.z) / Time.deltaTime; }
public override void InitPosition(MyParticle p, Vector3 pos) { p.Position.x = p.PreviousPosition.x = pos.x; p.Position.y = p.PreviousPosition.y = pos.y; p.Position.z = p.PreviousPosition.z = pos.z; }
protected override void InitLifeTime(MyParticle p) { p.LifeTime = 1.0f; }
protected void UpdateSize(MyParticle p) { p.Size += p.LifeTimeDelta; }
public void Init(GameObject sprite) { _sprite = sprite; _activeParticles = new List<int>(MAX_PARTICLES); _liquid = new MyParticle[MAX_PARTICLES]; for (int i = 0; i < MAX_PARTICLES; i++) { _liquid[i] = new MyParticle(Vector2.zero, Vector2.zero, false); _liquid[i].index = i; } _delta = new Vector2[MAX_PARTICLES]; _scaledPositions = new Vector2[MAX_PARTICLES]; _scaledVelocities = new Vector2[MAX_PARTICLES]; _grid = new Dictionary<int, Dictionary<int, List<int>>>(); }
protected void UpdateVelocity(MyParticle p) { p.Velocity.x = p.PreviousVelocity.x + Time.deltaTime * p.Acceleration.x; p.Velocity.y = p.PreviousVelocity.y + Time.deltaTime * p.Acceleration.y; p.Velocity.z = p.PreviousVelocity.z + Time.deltaTime * p.Acceleration.z; }
public int contactGenRegLen = 0;//you use this but don't do anything with it public void addToParitcleReg(MyParticle particle) { particleReg.Add(particle); partRegLen++; //MonoBehaviour.print("ParticleWorld: Particle added to registry"); }
public abstract void InitAcceleration(MyParticle p);
protected override void InitLifeTime(MyParticle p) { p.LifeTime = (float)rg.NextDouble(); }
private MySpring addNewSpring(MyParticleSystem _particleSystem, MyParticle targetOne, MyParticle targetTwo, float springRest, float springStr, float springDamp) { return(new MySpring(_particleSystem, targetOne, targetTwo, springRest, springStr, springDamp)); }
public ParticleBungie(MyParticle other, float springConstant, float restLength) { this.other = other; this.springConstant = springConstant; this.restLength = restLength; }
public abstract void InitPosition(MyParticle p, Vector3 pos);
public ParticleContact(MyParticle particle1, MyParticle particle2) { particle [0] = particle1; particle [1] = particle2; }
// Update is called once per frame void Update() { if (this.currentState == ParticleOptions.None) { if (Input.GetMouseButtonDown(0)) { var hitParticle = GetParticleAtPos(); if (hitParticle != null) { this.currentState = ParticleOptions.Moving; particle = hitParticle; particle.tempPinned = true; } } if (Input.GetMouseButtonDown(2)) { var hitParticle = GetParticleAtPos(); if (hitParticle != null) { this.currentState = ParticleOptions.Lifteing; particle = hitParticle; _yLocked = true; particle.tempPinned = true; } } } // --- what we wonna do then left mouse bnt is hold down --- else if (this.currentState == ParticleOptions.Moving) { var pos = GetMousePos("Ground"); if (pos.HasValue) { particle.transform.position = pos.Value; particle.velocity = pos.Value; } if (Input.GetMouseButtonUp(0)) { //Debug.Log("setteing State back To none"); this.currentState = ParticleOptions.None; particle.tempPinned = false; } } // --- what we wonna do in middel mouse in hold down --- else if (this.currentState == ParticleOptions.Lifteing) { var height = GetMousePos("Height"); if (height.HasValue && _yLocked == true) { var pos = particle.transform.position; var heightY = (height.Value.y - pos.y) * Time.fixedDeltaTime + _middleMouseClickAdjust; Vector3 tempPos = new Vector3(pos.x, heightY, pos.z); //Debug.Log("tempPos =" + tempPos); particle.transform.position = tempPos; particle.velocity = tempPos; } if (Input.GetMouseButtonUp(2)) { //Debug.Log("setteing State back To none"); this.currentState = ParticleOptions.None; _yLocked = false; particle.tempPinned = false; } } //--- toggleing the right mouse bnt --- if (Input.GetMouseButtonUp(1) && this.currentState != ParticleOptions.DisplayConnections) { this.currentState = ParticleOptions.DisplayConnections; this.optionsCanvesPrefab.SetActive(true); particle.tempPinned = true; if (particle != null) { nameFieldPrefab.GetComponent <Text>().text = particle.name.ToString(); Debug.Log("partciles name : " + particle.name); } if (particle == null) { Debug.Log("_partcile was null"); } } else if (Input.GetMouseButtonUp(1) && this.currentState == ParticleOptions.DisplayConnections) { this.currentState = ParticleOptions.None; this.optionsCanvesPrefab.SetActive(false); particle.tempPinned = false; } }
public void addParticle(MyParticle particle1, MyParticle particle2) { particle [0] = particle1; particle [1] = particle2; }
public void setValues(MyParticle p, ParticleForceGenerator f) { this.particle = p; this.fg = f; }
public override void InitPosition(MyParticle p, Vector3 pos) { p.Position.x = p.PreviousPosition.x = pos.x + (float)(rg.NextDouble()); p.Position.y = p.PreviousPosition.y = pos.y - (float)(rg.NextDouble() * 10.0f); p.Position.z = p.PreviousPosition.z = pos.z + (float)(rg.NextDouble()); }
/*Registers given force generator to apply to the particle*/ public void add(MyParticle particle, ParticleForceGenerator fg) { registration.setValues(particle, fg); Registry.Add(registration); listLength++; }
// Auxiliary updates protected void UpdatePreviousValues(MyParticle p) { p.PreviousPosition.x = p.Position.x; p.PreviousPosition.y = p.Position.y; p.PreviousPosition.z = p.Position.z; p.PreviousVelocity.x = p.Velocity.x; p.PreviousVelocity.y = p.Velocity.y; p.PreviousVelocity.z = p.Velocity.z; }
public override void InitVelocity(MyParticle p) { p.Velocity.x = baseVelocity * Mathf.Cos(theta) * Mathf.Sin(phi); p.Velocity.y = baseVelocity * Mathf.Cos(phi); p.Velocity.z = baseVelocity * Mathf.Sin(theta) * Mathf.Sin(phi); }