//Create Particle Link public Particle2DLink CreateLink(GameObject object1, GameObject object2, float maxLength) { ParticleRod particleRod = object1.AddComponent <ParticleRod>(); particleRod.Initialize(object1, object2, maxLength); return(object1.GetComponent <ParticleRod>()); }
// Start is called before the first frame update void Start() { GameObject obj1 = GameObject.Find("Rod1"); GameObject obj2 = GameObject.Find("Rod2"); GameObject particleLinkObj = new GameObject("Link" + obj1.name + " " + obj2.name); ParticleRod newParticleRod = particleLinkObj.AddComponent <ParticleRod>(); newParticleRod.Initialize(obj1, obj2, 2); mParticleLinks.Add(newParticleRod); }
public void TestConstructor() { var pA = new Particle { Mass = 1 }; var pB = new Particle { Mass = 1, Position = new Vector2D(5, 0) }; var rod = new ParticleRod(pA, pB); Assert.AreEqual(pA, rod.ParticleA); Assert.AreEqual(pB, rod.ParticleB); }
// Start is called before the first frame update void Start() { GameObject particleLinkObject = new GameObject("LINK " + GameObject.Find("RodAnchor").name + " " + GameObject.Find("RodPlatform").name); ParticleRod newParticleRod = particleLinkObject.AddComponent <ParticleRod>(); newParticleRod.Initialize(GameObject.Find("RodPlatform"), GameObject.Find("RodAnchor"), 10); ContactResolver.Instance.mParticleLinks.Add(newParticleRod); //Swing Platform GameObject particleLinkObject2 = new GameObject("LINK " + GameObject.Find("SwingAnchor").name + " " + GameObject.Find("SwingPlatform").name); ParticleRod particleRod = particleLinkObject.AddComponent <ParticleRod>(); particleRod.Initialize(GameObject.Find("SwingPlatform"), GameObject.Find("SwingAnchor"), 10); ContactResolver.Instance.mParticleLinks.Add(particleRod); }
public void TestGetEnumerator() { var pA = new Particle { Mass = 1 }; var pB = new Particle { Mass = 1, Position = new Vector2D(5, 0) }; var rod = new ParticleRod(pA, pB); var contacts = new List <ParticleContact>(); contacts.AddRange(rod); Assert.AreEqual(0, contacts.Count, "长度未变化时不产生任何碰撞"); pB.Position = new Vector2D(6, 0); foreach (var contact in rod) { Assert.AreEqual(1, contact.Penetration); Assert.AreEqual(new Vector2D(1, 0), contact.ContactNormal); } pB.Position = new Vector2D(4, 0); foreach (var contact in rod) { Assert.AreEqual(1, contact.Penetration); Assert.AreEqual(new Vector2D(-1, 0), contact.ContactNormal); } pB.Position = new Vector2D(4, 0); IEnumerable iEnum = rod; foreach (ParticleContact contact in iEnum) { Assert.AreEqual(1, contact.Penetration); Assert.AreEqual(new Vector2D(-1, 0), contact.ContactNormal); } }
public void CreateProjectile(GameObject projectile, Transform spawnPoint, Transform playerPos) { float speed = 0.0f; Vector2 angle = new Vector2(spawnPoint.position.x - playerPos.position.x, spawnPoint.position.y - playerPos.position.y); Vector2 gravity; PhysicsObject2D proj; GameObject part; switch (mCurrentWeaponType) { case WeaponType.SPRING: { speed = 20.0f; float speed2 = 15.0f; gravity = new Vector2(0.0f, -6.0f); float mass1, mass2; mass1 = 1.0f; mass2 = 2.0f; part = Instantiate(projectile, spawnPoint.position, spawnPoint.rotation); mParticleManager.AddParticle(part.GetComponent <Particle2D>()); proj = part.GetComponent <PhysicsObject2D>(); proj.SetVel(angle * speed); proj.SetAcc(gravity); proj.SetInverseMass(mass1); proj.SetDamping(0.99f); mPhysicsObjects.Add(proj); PhysicsObject2D proj2; part = Instantiate(projectile, spawnPoint.position - new Vector3(1.0f, 0.0f, 0.0f), spawnPoint.rotation); proj2 = part.GetComponent <PhysicsObject2D>(); mParticleManager.AddParticle(part.GetComponent <Particle2D>()); proj2.SetVel(angle * speed2); proj2.SetAcc(gravity); proj2.SetInverseMass(mass2); proj2.SetDamping(0.99f); mPhysicsObjects.Add(proj2); proj2.gameObject.AddComponent <DestroyOnExit>(); SpringForceGenerator springForceGenerator = new SpringForceGenerator(proj, proj2, 1.0f, 50.0f, true); proj2.gameObject.GetComponent <DestroyOnExit>().mForceGenerators.Add(springForceGenerator); mForceManager.AddForceGenerator(springForceGenerator); BouyancyForceGenerator bGenerator = new BouyancyForceGenerator(true, proj, -6, 100, 0); mForceManager.AddForceGenerator(bGenerator); proj2.gameObject.GetComponent <DestroyOnExit>().mForceGenerators.Add(bGenerator); bGenerator = new BouyancyForceGenerator(true, proj2, -6, 100, 0); mForceManager.AddForceGenerator(bGenerator); proj2.gameObject.GetComponent <DestroyOnExit>().mForceGenerators.Add(bGenerator); } break; case WeaponType.ROD: { speed = 30.0f; gravity = new Vector2(0.0f, -6.0f); proj = Instantiate(projectile, spawnPoint.position, spawnPoint.rotation).GetComponent <PhysicsObject2D>(); proj.SetVel(angle * speed); proj.SetAcc(gravity); proj.SetInverseMass(1.0f); proj.SetDamping(0.99f); mPhysicsObjects.Add(proj); PhysicsObject2D proj2; proj2 = Instantiate(projectile, spawnPoint.position + new Vector3(2.5f, 0.0f), spawnPoint.rotation).GetComponent <PhysicsObject2D>(); proj2.SetVel(angle * speed); proj2.SetAcc(gravity); proj2.SetInverseMass(1.0f); proj2.SetDamping(0.99f); mPhysicsObjects.Add(proj2); proj2.gameObject.AddComponent <DestroyOnExit>(); Particle2DLink link = new ParticleRod(proj, proj2, 5.0f); AddParticleLink(link); BouyancyForceGenerator bGenerator = new BouyancyForceGenerator(true, proj, -6, 100, 0); mForceManager.AddForceGenerator(bGenerator); proj2.gameObject.GetComponent <DestroyOnExit>().mForceGenerators.Add(bGenerator); bGenerator = new BouyancyForceGenerator(true, proj2, -6, 100, 0); mForceManager.AddForceGenerator(bGenerator); proj2.gameObject.GetComponent <DestroyOnExit>().mForceGenerators.Add(bGenerator); } break; } }
/// <summary> /// 在物理世界创建一根质体硬棒 /// 该硬棒由两个质体(质点)组成,两个质体的距离不会发生变化 /// </summary> /// <param name="world"></param> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static ParticleRod CreateRod(this World world, Particle a, Particle b) { var rod = new ParticleRod(a, b); return(world.CreateContact(rod)); }
public void initialize() { Random rnd = new Random(); int numParticles = particleArray.Length; //lines float startX = 400.0f; float startY = 300.0f; float distanceX = 20.0f; float distanceY = 20.0f; float radius = 8.0f; int stride = 4; int cableCounter = 0; for (int i = 0; i < (numParticles - freeParticles); ++i) { int row = i / stride; int column = i - (stride * row); float x = startX + distanceX * ((float)(column)); float y = startY + distanceY * ((float)(row)); particleArray[i].setPosition(x, y, 0.0f); particleArray[i].Mass = 1.0f; particleArray[i].Radius = radius; //particleArray[i].setVelocity(20.0f, 90.0f + (float)(i * 10 * rnd.Next() % 20), 0.0f); particleArray[i].setVelocity(0.0f, 0.0f, 0.0f); particleArray[i].setAcceleration(0.0f, 0.0f, 0.0f); particleArray[i].Damping = 0.99f; particleArray[i].clearAccumulator(); if (row != 0 && column != 0) { // -- particleRods[cableCounter] = new ParticleRod(); particleRods[cableCounter].length = distanceX; particleRods[cableCounter].particles[0] = particleArray[i]; particleRods[cableCounter].particles[1] = particleArray[i - 1]; world1.getContactGenerators().Add(particleRods[cableCounter]); ++cableCounter; // \ particleRods[cableCounter] = new ParticleRod(); particleRods[cableCounter].length = (float)Math.Sqrt(distanceX * distanceX + distanceY * distanceY); particleRods[cableCounter].particles[0] = particleArray[i]; particleRods[cableCounter].particles[1] = particleArray[i - stride - 1]; world1.getContactGenerators().Add(particleRods[cableCounter]); ++cableCounter; // / particleRods[cableCounter] = new ParticleRod(); particleRods[cableCounter].length = (float)Math.Sqrt(distanceX * distanceX + distanceY * distanceY); particleRods[cableCounter].particles[0] = particleArray[i - 1]; particleRods[cableCounter].particles[1] = particleArray[i - stride]; world1.getContactGenerators().Add(particleRods[cableCounter]); ++cableCounter; } else { if (row != 0 && column == 0) { // -- particleRods[cableCounter] = new ParticleRod(); particleRods[cableCounter].length = distanceX; particleRods[cableCounter].particles[0] = particleArray[i]; particleRods[cableCounter].particles[1] = particleArray[i + 1]; world1.getContactGenerators().Add(particleRods[cableCounter]); ++cableCounter; } } if (i >= stride) { if (row != 0) { // | particleRods[cableCounter] = new ParticleRod(); particleRods[cableCounter].length = distanceY; particleRods[cableCounter].particles[0] = particleArray[i - stride]; particleRods[cableCounter].particles[1] = particleArray[i]; world1.getContactGenerators().Add(particleRods[cableCounter]); ++cableCounter; } } particleGravityArray[i] = new ParticleGravity(new Vec3(0.0f, 9.8f * 10.0f, 0.0f)); world1.getForceRegistry().add(particleArray[i], particleGravityArray[i]); } for (int i = particleCount; i < (particleCount + freeParticles); ++i) { int randX = Math.Abs(rnd.Next() % 36); float x = 15.0f + (float)((i - freeParticles) * 40); float y = 0.0f + (i - freeParticles); particleArray[i].setPosition(x, y, 0.0f); particleArray[i].Mass = 10.0f; particleArray[i].Radius = 20.0f + 3.0f * (float)(rnd.Next() % 3); particleArray[i].setVelocity(20.0f * (i % 2 == 0 ? -1.0f : 1.0f), 90.0f + (float)(i * 10 * rnd.Next() % 20), 0.0f); particleArray[i].setAcceleration(0.0f, 0.0f, 0.0f); particleArray[i].Damping = 0.99f; particleArray[i].clearAccumulator(); particleGravityArray[i] = new ParticleGravity(new Vec3(0.0f, 9.8f * 40.0f, 0.0f)); world1.getForceRegistry().add(particleArray[i], particleGravityArray[i]); } loadTextures(); loadModels(); buildGeometry(); buildCamera(); }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.Alpha1)) { transform.Rotate(Vector3.forward * 1.0f); } if (Input.GetKey(KeyCode.Alpha2)) { transform.Rotate(Vector3.back * 1.0f); } if (Input.GetKeyDown(KeyCode.W)) { mCurrentProjectile += 1; if (mCurrentProjectile == ProjectileType.NumberOfTypes) { mCurrentProjectile = 0; } } if (Input.GetKeyDown(KeyCode.Return)) { if (mCurrentProjectile == 0) { //rod weapon GameObject Projectile1 = Instantiate(bulletPrefab); Projectile1.transform.position = transform.position; Projectile1.transform.rotation = transform.rotation; Projectile1.tag = "Projectile"; Particle2D Projectile1Data = Projectile1.AddComponent <Particle2D>(); Projectile1Data.Mass = rodEndMass1; Projectile1Data.Velocity = transform.up * rodEndVelocity1; Projectile1Data.Acceleration = rodEndAcceleration1; Projectile1Data.DampingConstant = rodEndDampingConstant1; Projectile1Data.shouldIgnoreForces = rodEndshouldIgnoreForces1; ParticleManager.Instance.mParticles.Add(Projectile1Data); GameObject Projectile2 = Instantiate(bulletPrefab); Projectile2.transform.position = transform.position + new Vector3(0, 1, 0);; Projectile2.transform.rotation = transform.rotation; Projectile2.tag = "Projectile"; Particle2D Projectile2Data = Projectile2.AddComponent <Particle2D>(); Projectile2Data.Mass = rodEndMass2; Projectile2Data.Velocity = transform.up * rodEndVelocity2; Projectile2Data.Acceleration = rodEndAcceleration2; Projectile2Data.DampingConstant = rodEndDampingConstant2; Projectile2Data.shouldIgnoreForces = rodEndshouldIgnoreForces2; ParticleManager.Instance.mParticles.Add(Projectile2Data); GameObject particleLinkObject = new GameObject("LINK " + Projectile1.name + " " + Projectile2.name); ParticleRod newParticleRod = particleLinkObject.AddComponent <ParticleRod>(); newParticleRod.Initialize(Projectile1, Projectile2, 2); ContactResolver.Instance.mParticleLinks.Add(newParticleRod); } else { //spring weapon GameObject Projectile1 = Instantiate(bulletPrefab); Projectile1.transform.position = transform.position; Projectile1.transform.rotation = transform.rotation; Projectile1.tag = "Projectile"; Particle2D Projectile1Data = Projectile1.AddComponent <Particle2D>(); Projectile1Data.Mass = springEndMass1; Projectile1Data.Velocity = transform.up * springEndVelocity1; Projectile1Data.Acceleration = springEndAcceleration1; Projectile1Data.DampingConstant = springEndDampingConstant1; Projectile1Data.shouldIgnoreForces = springEndshouldIgnoreForces1; ParticleManager.Instance.mParticles.Add(Projectile1Data); GameObject Projectile2 = Instantiate(bulletPrefab); Projectile2.transform.position = transform.position + new Vector3(0, 1, 0); Projectile2.transform.rotation = transform.rotation; Projectile2.tag = "Projectile"; Particle2D Projectile2Data = Projectile2.AddComponent <Particle2D>(); Projectile2Data.Mass = springEndMass2; Projectile2Data.Velocity = transform.up * springEndVelocity2; Projectile2Data.Acceleration = springEndAcceleration2; Projectile2Data.DampingConstant = springEndDampingConstant2; Projectile2Data.shouldIgnoreForces = springEndshouldIgnoreForces2; ParticleManager.Instance.mParticles.Add(Projectile2Data); ForceManager.Instance.CreateSpringForceGenerator(Projectile1, Projectile2, springConstant, restLength); } } }
void CreateProjectile(Projectiles projectileType, GameObject projectile) { switch (projectileType) { case Projectiles.PISTOL: projectile.transform.position = transform.position; projectile.GetComponent <Particle2D>().CreateParticle2D(2.0f, 0.99f, 10.0f, transform.right, projGravity, true); projectile.GetComponent <Particle2D>().setShouldIgnoreForces(false); BuoyancyForceGenerator buoyancy = new BuoyancyForceGenerator(projectile, projectile.transform.localScale.y / 2.0f, 1.0f, WaterObject.waterTopPos.y, 80.0f); ForceManager.instance.AddForceGen(buoyancy); projectile.GetComponent <Particle2D>().setParticleInstantiated(true); Debug.Log(currentProjectileType); break; case Projectiles.SPRINGSHOT: projectile.transform.position = transform.position; GameObject secondShot = Instantiate(projectile); Vector3 newPos = new Vector3(0.0f, 2.0f, 0.0f); secondShot.transform.position = newPos; projectile.GetComponent <Particle2D>().CreateParticle2D(2.0f, 0.99f, 10.0f, transform.right, projGravity, true); secondShot.GetComponent <Particle2D>().CreateParticle2D(2.0f, 0.99f, 25.0f, transform.right, projGravity, true); projectile.GetComponent <Particle2D>().setShouldIgnoreForces(false); secondShot.GetComponent <Particle2D>().setShouldIgnoreForces(false); SpringForceGenerator springGen = new SpringForceGenerator(projectile, secondShot, 2.0f, 0.5f); ForceManager.instance.AddForceGen(springGen); BuoyancyForceGenerator springBuoy1 = new BuoyancyForceGenerator(projectile, projectile.transform.localScale.y / 2.0f, 1.0f, WaterObject.waterTopPos.y, 80.0f); ForceManager.instance.AddForceGen(springBuoy1); BuoyancyForceGenerator springBuoy2 = new BuoyancyForceGenerator(projectile, projectile.transform.localScale.y / 2.0f, 1.0f, WaterObject.waterTopPos.y, 80.0f); ForceManager.instance.AddForceGen(springBuoy2); projectile.GetComponent <Particle2D>().setParticleInstantiated(true); secondShot.GetComponent <Particle2D>().setParticleInstantiated(true); Debug.Log(currentProjectileType); break; case Projectiles.RODSHOT: projectile.transform.position = transform.position; GameObject connectedShot = Instantiate(projectile); Vector3 changePos = new Vector3(0.0f, 2.0f, 0.0f); connectedShot.transform.position = changePos; projectile.GetComponent <Particle2D>().CreateParticle2D(2.0f, 0.99f, 10.0f, transform.right, projGravity, true); connectedShot.GetComponent <Particle2D>().CreateParticle2D(2.0f, 0.99f, 25.0f, transform.right, projGravity, true); projectile.GetComponent <Particle2D>().setShouldIgnoreForces(false); connectedShot.GetComponent <Particle2D>().setShouldIgnoreForces(false); ParticleRod newRod = new ParticleRod(); newRod.InstantiateVariables(projectile.GetComponent <Particle2D>(), connectedShot.GetComponent <Particle2D>(), 10.0f); particleLinks.Add(newRod); BuoyancyForceGenerator rodBuoy1 = new BuoyancyForceGenerator(projectile, projectile.transform.localScale.y / 2.0f, 1.0f, WaterObject.waterTopPos.y, 80.0f); ForceManager.instance.AddForceGen(rodBuoy1); BuoyancyForceGenerator rodBuoy2 = new BuoyancyForceGenerator(projectile, projectile.transform.localScale.y / 2.0f, 1.0f, WaterObject.waterTopPos.y, 80.0f); ForceManager.instance.AddForceGen(rodBuoy2); projectile.GetComponent <Particle2D>().setParticleInstantiated(true); connectedShot.GetComponent <Particle2D>().setParticleInstantiated(true); Debug.Log(currentProjectileType); break; } }