private void AddSpringBody(ClosedShape shape) { DraggableSpringBody body = new DraggableSpringBody(); body.Setup(JellyWorldManager.World, shape, 1f, SpringK, Damping, SpringK, Damping, transform.position, transform.rotation.eulerAngles.z, Vector2.one); body.Gravity = GravityModifier; int i = springs.Count-1, l = -1; for(;i>l;i-=detail) { Vector2 v = springs[i]; try{ //TODO: check if triangle is already added, if so, flip triangle. body.addInternalSpring((int)v.x, (int)v.y,SpringK,Damping); } catch{ // Debug.Log("couldnt add spring at " +i); } } _body = body; }
private void AddSpringBody(ClosedShape shape) { DraggableSpringBody body = new DraggableSpringBody(); body.Setup(JellyWorldManager.World, shape, 1f, SpringK, Damping, SpringK, Damping, transform.position, transform.rotation.eulerAngles.z, Vector2.one); body.Gravity = GravityModifier; int i = springs.Count - 1, l = -1; for (; i > l; i -= detail) { Vector2 v = springs[i]; try{ //TODO: check if triangle is already added, if so, flip triangle. body.addInternalSpring((int)v.x, (int)v.y, SpringK, Damping); } catch { // Debug.Log("couldnt add spring at " +i); } } _body = body; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> void Update() { // UPDATE the physics! for (int i = 0; i < 3; i++) mWorld.update(1 / 120f); // cursor movement. cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // if the user presses the A button, create a new body at the cursor position. if (Input.GetKeyDown(KeyCode.A)) { GameObject s = new GameObject (); JelloPhysics.ClosedShape shape = new JelloPhysics.ClosedShape (); shape.begin(); shape.addVertex(new Vector2(-1.0f, 0f)); shape.addVertex(new Vector2(0f, 1.0f)); shape.addVertex(new Vector2(1.0f, 0f)); shape.addVertex(new Vector2(0f, -1.0f)); shape.finish(); DraggableSpringBody body = new DraggableSpringBody(); body.Setup(mWorld, shape, 1f, 150, 5,300,15, new Vector2(cursorPos.x, cursorPos.y),((float)UnityEngine.Random.Range(0,360)), Vector2.one); body.addInternalSpring(0, 2, 400f, 12f); body.addInternalSpring(1, 3, 400f, 12f); } if (Input.GetKeyDown(KeyCode.B)) { GameObject s = new GameObject (); JelloPhysics.ClosedShape shape = new JelloPhysics.ClosedShape (); shape.begin(); for (int i = 0; i < 360; i += 20) { shape.addVertex(new Vector2((float)Mathf.Cos(((float)-i) * Mathf.Deg2Rad), (float)Mathf.Sin(((float)-i) * Mathf.Deg2Rad))); } shape.finish(); DraggablePressureBody pb = new DraggablePressureBody(); pb.Setup(mWorld, shape, 1.0f, 40.0f, 10.0f, 1.0f, 300.0f, 20.0f, cursorPos, 0, Vector2.one); pb.addTriangle(0, 10, 9); pb.addTriangle(0, 9, 1); pb.addTriangle(1, 9, 8); pb.addTriangle(1, 8, 2); pb.addTriangle(2, 8, 7); pb.addTriangle(2, 7, 3); pb.addTriangle(3, 7, 6); pb.addTriangle(3, 6, 4); pb.addTriangle(4, 6, 5); pb.addTriangle(17, 10, 0); pb.addTriangle(17, 11, 10); pb.addTriangle(16, 11, 17); pb.addTriangle(16, 12, 11); pb.addTriangle(15, 12, 16); pb.addTriangle(15, 13, 12); pb.addTriangle(14, 12, 15); pb.addTriangle(14, 13, 12); pb.finalizeTriangles(Color.white); pb.addInternalSpring(0, 2, 400f, 12f); pb.addInternalSpring(1, 3, 400f, 12f); } // dragging! if (Input.GetMouseButton(0)) { if (dragBody != null) { PointMass pm = dragBody.getPointMass(dragPoint); if (dragBody.GetType().Name == "DraggableSpringBody") ((DraggableSpringBody)dragBody).setDragForce(JelloPhysics.VectorTools.calculateSpringForce(pm.Position, pm.Velocity, cursorPos, Vector2.zero, 0.0f, 100.0f, 10.0f), dragPoint); else if (dragBody.GetType().Name == "DraggablePressureBody") ((DraggablePressureBody)dragBody).setDragForce(JelloPhysics.VectorTools.calculateSpringForce(pm.Position, pm.Velocity, cursorPos, Vector2.zero, 0.0f, 100.0f, 10.0f), dragPoint); } } else { dragBody = null; dragPoint = -1; } if (Input.GetMouseButtonDown(0)) { if (dragBody == null) { int body; mWorld.getClosestPointMass(cursorPos, out body, out dragPoint); dragBody = mWorld.getBody(body); } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> void Update() { // UPDATE the physics! for (int i = 0; i < 3; i++) { mWorld.update(1 / 120f); } // cursor movement. cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // if the user presses the A button, create a new body at the cursor position. if (Input.GetKeyDown(KeyCode.A)) { GameObject s = new GameObject(); JelloPhysics.ClosedShape shape = new JelloPhysics.ClosedShape(); shape.begin(); shape.addVertex(new Vector2(-1.0f, 0f)); shape.addVertex(new Vector2(0f, 1.0f)); shape.addVertex(new Vector2(1.0f, 0f)); shape.addVertex(new Vector2(0f, -1.0f)); shape.finish(); DraggableSpringBody body = new DraggableSpringBody(); body.Setup(mWorld, shape, 1f, 150, 5, 300, 15, new Vector2(cursorPos.x, cursorPos.y), ((float)UnityEngine.Random.Range(0, 360)), Vector2.one); body.addInternalSpring(0, 2, 400f, 12f); body.addInternalSpring(1, 3, 400f, 12f); } if (Input.GetKeyDown(KeyCode.B)) { GameObject s = new GameObject(); JelloPhysics.ClosedShape shape = new JelloPhysics.ClosedShape(); shape.begin(); for (int i = 0; i < 360; i += 20) { shape.addVertex(new Vector2((float)Mathf.Cos(((float)-i) * Mathf.Deg2Rad), (float)Mathf.Sin(((float)-i) * Mathf.Deg2Rad))); } shape.finish(); DraggablePressureBody pb = new DraggablePressureBody(); pb.Setup(mWorld, shape, 1.0f, 40.0f, 10.0f, 1.0f, 300.0f, 20.0f, cursorPos, 0, Vector2.one); pb.addTriangle(0, 10, 9); pb.addTriangle(0, 9, 1); pb.addTriangle(1, 9, 8); pb.addTriangle(1, 8, 2); pb.addTriangle(2, 8, 7); pb.addTriangle(2, 7, 3); pb.addTriangle(3, 7, 6); pb.addTriangle(3, 6, 4); pb.addTriangle(4, 6, 5); pb.addTriangle(17, 10, 0); pb.addTriangle(17, 11, 10); pb.addTriangle(16, 11, 17); pb.addTriangle(16, 12, 11); pb.addTriangle(15, 12, 16); pb.addTriangle(15, 13, 12); pb.addTriangle(14, 12, 15); pb.addTriangle(14, 13, 12); pb.finalizeTriangles(Color.white); pb.addInternalSpring(0, 2, 400f, 12f); pb.addInternalSpring(1, 3, 400f, 12f); } // dragging! if (Input.GetMouseButton(0)) { if (dragBody != null) { PointMass pm = dragBody.getPointMass(dragPoint); if (dragBody.GetType().Name == "DraggableSpringBody") { ((DraggableSpringBody)dragBody).setDragForce(JelloPhysics.VectorTools.calculateSpringForce(pm.Position, pm.Velocity, cursorPos, Vector2.zero, 0.0f, 100.0f, 10.0f), dragPoint); } else if (dragBody.GetType().Name == "DraggablePressureBody") { ((DraggablePressureBody)dragBody).setDragForce(JelloPhysics.VectorTools.calculateSpringForce(pm.Position, pm.Velocity, cursorPos, Vector2.zero, 0.0f, 100.0f, 10.0f), dragPoint); } } } else { dragBody = null; dragPoint = -1; } if (Input.GetMouseButtonDown(0)) { if (dragBody == null) { int body; mWorld.getClosestPointMass(cursorPos, out body, out dragPoint); dragBody = mWorld.getBody(body); } } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { #if XBOX360 graphics.PreferredBackBufferWidth = this.Window.ClientBounds.Width; graphics.PreferredBackBufferHeight = this.Window.ClientBounds.Height; graphics.PreferMultiSampling = true; #else graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600; #endif graphics.ApplyChanges(); screenWidth = graphics.GraphicsDevice.Viewport.Width; screenHeight = graphics.GraphicsDevice.Viewport.Height; // setup camera cameraPos = new Vector3(-2f, -15f, 25f); cameraLookVector = Vector3.Forward; // basic effect used for drawing all objects, based on vertexcolor. lineEffect = new BasicEffect(graphics.GraphicsDevice, null); lineEffect.LightingEnabled = false; lineEffect.VertexColorEnabled = true; lineEffect.Alpha = 1.0f; // initialize physics world mWorld = new World(); // lists to keep track of bodies. mSpringBodies = new List<DraggableSpringBody>(); mPressureBodies = new List<DraggablePressureBody>(); mStaticBodies = new List<Body>(); // STATIC COLLISION SHAPE // all Closed Shape objects are assumed to be a list of lines going from point to point, with the last point // connecting back to the first point to close the shape. This is a simple rectangle to represent the ground for this demo. // ClosedShape objects are automatically "centered" when you call finish(), and that center becomes the center when // setting the position of the object. ClosedShape stat = new ClosedShape(); stat.begin(); stat.addVertex(new Vector2(-20f,-1f)); stat.addVertex(new Vector2(-20f, 1f)); stat.addVertex(new Vector2(20f, 1f)); stat.addVertex(new Vector2(20f, -1f)); stat.finish(); // creating the body. Since this is a static body, we can use the base class Body. // setting the mass per point to PositiveInfinity makes it immobile / static. Body b = new Body(mWorld, stat, float.PositiveInfinity, new Vector2(0f, -19f), 0, Vector2.One, false); mStaticBodies.Add(b); // this is a more complex body, in the shape of a capital "I", and connected with many internal springs. ClosedShape shape = new ClosedShape(); shape.begin(); shape.addVertex(new Vector2(-1.5f, 2.0f)); shape.addVertex(new Vector2(-0.5f, 2.0f)); shape.addVertex(new Vector2(0.5f, 2.0f)); shape.addVertex(new Vector2(1.5f, 2.0f)); shape.addVertex(new Vector2(1.5f, 1.0f)); shape.addVertex(new Vector2(0.5f, 1.0f)); shape.addVertex(new Vector2(0.5f, -1.0f)); shape.addVertex(new Vector2(1.5f, -1.0f)); shape.addVertex(new Vector2(1.5f, -2.0f)); shape.addVertex(new Vector2(0.5f, -2.0f)); shape.addVertex(new Vector2(-0.5f, -2.0f)); shape.addVertex(new Vector2(-1.5f, -2.0f)); shape.addVertex(new Vector2(-1.5f, -1.0f)); shape.addVertex(new Vector2(-0.5f, -1.0f)); shape.addVertex(new Vector2(-0.5f, 1.0f)); shape.addVertex(new Vector2(-1.5f, 1.0f)); shape.finish(); // draggablespringbody is an inherited version of SpringBody that includes polygons for visualization, and the // ability to drag the body around the screen with the cursor. for (int x = -8; x <= 8; x += 4) { DraggableSpringBody body = new DraggableSpringBody(mWorld, shape, 1f, 150.0f, 5.0f, 300.0f, 15.0f, new Vector2(x, 0), 0.0f, Vector2.One); body.addInternalSpring(0, 14, 300.0f, 10.0f); body.addInternalSpring(1, 14, 300.0f, 10.0f); body.addInternalSpring(1, 15, 300.0f, 10.0f); body.addInternalSpring(1, 5, 300.0f, 10.0f); body.addInternalSpring(2, 14, 300.0f, 10.0f); body.addInternalSpring(2, 5, 300.0f, 10.0f); body.addInternalSpring(1, 5, 300.0f, 10.0f); body.addInternalSpring(14, 5, 300.0f, 10.0f); body.addInternalSpring(2, 4, 300.0f, 10.0f); body.addInternalSpring(3, 5, 300.0f, 10.0f); body.addInternalSpring(14, 6, 300.0f, 10.0f); body.addInternalSpring(5, 13, 300.0f, 10.0f); body.addInternalSpring(13, 6, 300.0f, 10.0f); body.addInternalSpring(12, 10, 300.0f, 10.0f); body.addInternalSpring(13, 11, 300.0f, 10.0f); body.addInternalSpring(13, 10, 300.0f, 10.0f); body.addInternalSpring(13, 9, 300.0f, 10.0f); body.addInternalSpring(6, 10, 300.0f, 10.0f); body.addInternalSpring(6, 9, 300.0f, 10.0f); body.addInternalSpring(6, 8, 300.0f, 10.0f); body.addInternalSpring(7, 9, 300.0f, 10.0f); // polygons! body.addTriangle(0, 15, 1); body.addTriangle(1, 15, 14); body.addTriangle(1, 14, 5); body.addTriangle(1, 5, 2); body.addTriangle(2, 5, 4); body.addTriangle(2, 4, 3); body.addTriangle(14, 13, 6); body.addTriangle(14, 6, 5); body.addTriangle(12, 11, 10); body.addTriangle(12, 10, 13); body.addTriangle(13, 10, 9); body.addTriangle(13, 9, 6); body.addTriangle(6, 9, 8); body.addTriangle(6, 8, 7); body.finalizeTriangles(Color.SpringGreen, Color.Navy); mSpringBodies.Add(body); } // pressure body. similar to a SpringBody, but with internal pressurized gas to help maintain shape. JelloPhysics.ClosedShape ball = new ClosedShape(); ball.begin(); for (int i = 0; i < 360; i += 20) { ball.addVertex(new Vector2((float)Math.Cos(MathHelper.ToRadians((float)-i)), (float)Math.Sin(MathHelper.ToRadians((float)-i)))); } ball.finish(); // make many of these. for (int x = -10; x <= 10; x+=5) { DraggablePressureBody pb = new DraggablePressureBody(mWorld, ball, 1.0f, 40.0f, 10.0f, 1.0f, 300.0f, 20.0f, new Vector2(x, -12), 0, Vector2.One); pb.addTriangle(0, 10, 9); pb.addTriangle(0, 9, 1); pb.addTriangle(1, 9, 8); pb.addTriangle(1, 8, 2); pb.addTriangle(2, 8, 7); pb.addTriangle(2, 7, 3); pb.addTriangle(3, 7, 6); pb.addTriangle(3, 6, 4); pb.addTriangle(4, 6, 5); pb.addTriangle(17, 10, 0); pb.addTriangle(17, 11, 10); pb.addTriangle(16, 11, 17); pb.addTriangle(16, 12, 11); pb.addTriangle(15, 12, 16); pb.addTriangle(15, 13, 12); pb.addTriangle(14, 12, 15); pb.addTriangle(14, 13, 12); pb.finalizeTriangles((x==-10) ? Color.Teal : Color.Maroon); mPressureBodies.Add(pb); } // default cursor position, etc. mCursorPos = new Vector2(cameraPos.X, cameraPos.Y); base.Initialize(); }