public void Climb(ref Path p, ref RevoluteJoint j) { if (p.Bodies.IndexOf(j.Body2) != 0) { Vector2 nextPart = p.Bodies[p.Bodies.IndexOf(j.Body2) - 1].Position; j.Dispose(); j = null; hBody.Position = new Vector2(hBody.Position.X - (hBody.Position.X - nextPart.X) / 3, hBody.Position.Y - (hBody.Position.Y - nextPart.Y) / 3); } }
/// <summary> /// Creates a track. /// </summary> /// <param name="points">The points.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="mass">The mass.</param> /// <param name="endless">if set to <c>true</c> [endless].</param> /// <param name="collisionGroup">Collision group for the chain.</param> /// <param name="type">The joint/spring type.</param> /// <returns></returns> public Path CreateTrack(Vertices points, float width, float height, float mass, bool endless, int collisionGroup, LinkType type) { Path path = new Path(width, height, mass, endless); // create the path foreach (Vector2 v in points) path.Add(v); // add all the points to the path path.Update(); // update the path Geom geom; for (int i = 0; i < path.Bodies.Count; i++) { geom = GeomFactory.Instance.CreateRectangleGeom(path.Bodies[i], width, height); geom.CollisionGroup = collisionGroup; path.Add(geom); // add a geom to the chain } path.LinkBodies(type, Min, Max, SpringConstant, DampingConstant); // link bodies together return path; }
/// <summary> /// Creates a rope. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="mass">The mass.</param> /// <param name="pinStart">if set to <c>true</c> [pin start].</param> /// <param name="pinEnd">if set to <c>true</c> [pin end].</param> /// <param name="type">The joint/spring type.</param> /// <returns></returns> public Path CreateRope(Vector2 start, Vector2 end, float width, float height, float mass, bool pinStart, bool pinEnd, LinkType type) { Path path = new Path(width, height, mass, false); // create the path path.Add(start); // add starting point path.Add(Path.FindMidpoint(start, end)); // add midpoint of line (must have this because my code needs at least 3 control points) path.Add(end); // add end point path.Update(); // call update to create all the bodies path.LinkBodies(type, Min, Max, SpringConstant, DampingConstant, SpringRestLengthFactor); // link bodies together if (pinStart) path.Add(JointFactory.Instance.CreateFixedRevoluteJoint(path.Bodies[0], start)); if (pinEnd) path.Add(JointFactory.Instance.CreateFixedRevoluteJoint(path.Bodies[path.Bodies.Count - 1], path.ControlPoints[2])); foreach (Joint j in path.Joints) // ropes need a little give ;) { j.BiasFactor = 0.01f; j.Softness = 0.05f; } return (path); }
public override void LoadContent() { _chainTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 20, 10, Color.White, Color.Black); _obstaclesTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 40, 40, Color.Brown, Color.Black); _wheelTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 45, 2, Color.White, Color.Black); _chainOrigin = new Vector2(_chainTexture.Width / 2f, _chainTexture.Height / 2f); _wheelOrigin = new Vector2(_wheelTexture.Width / 2f, _wheelTexture.Height / 2f); _obstaclesOrigin = new Vector2(_obstaclesTexture.Width / 2f, _obstaclesTexture.Height / 2f); Vector2 center = new Vector2(400, 500); _controlPoints = new Vertices(); _controlPoints.Add(new Vector2(-15, -50) + center); _controlPoints.Add(new Vector2(-50, -50) + center); _controlPoints.Add(new Vector2(-100, -25) + center); _controlPoints.Add(new Vector2(-100, 25) + center); _controlPoints.Add(new Vector2(-50, 50) + center); _controlPoints.Add(new Vector2(50, 50) + center); _controlPoints.Add(new Vector2(100, 25) + center); _controlPoints.Add(new Vector2(100, -25) + center); _controlPoints.Add(new Vector2(50, -50) + center); _controlPoints.Add(new Vector2(-10, -50) + center); _track = ComplexFactory.Instance.CreateTrack(PhysicsSimulator, _controlPoints, 20.0f, 10.0f, 3.0f, true, 2, LinkType.RevoluteJoint); foreach (Geom g in _track.Geoms) g.FrictionCoefficient = 1.0f; _wheel1 = BodyFactory.Instance.CreateCircleBody(PhysicsSimulator, 45, 30); _wheel1.Position = new Vector2(-50, 0) + center; _wheel2 = BodyFactory.Instance.CreateCircleBody(PhysicsSimulator, 45, 30); _wheel2.Position = new Vector2(50, 0) + center; _wheelg = GeomFactory.Instance.CreateCircleGeom(PhysicsSimulator, _wheel1, 45, 36); _wheelg.FrictionCoefficient = 1.0f; _wheelg = GeomFactory.Instance.CreateCircleGeom(PhysicsSimulator, _wheel2, 45, 36); _wheelg.FrictionCoefficient = 1.0f; _spring = SpringFactory.Instance.CreateLinearSpring(PhysicsSimulator, _wheel1, new Vector2(), _wheel2, new Vector2(), 5200, 3050); _spring.RestLength += 20; _obstacles = new GenericList<Body>(8); base.LoadContent(); }
public void Finish(PhysicsSimulator pS) { chain = ComplexFactory.Instance.CreateChain(pS, startV, endV, bodies, 1f, 1, LinkType.RevoluteJoint); JointFactory.Instance.CreateFixedRevoluteJoint(pS, chain.Bodies[0], startV); finished = true; }
/// <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> protected override void Update(GameTime gameTime) { kbCurrent = Keyboard.GetState(); if (kbCurrent.IsKeyDown(Keys.Escape)) this.Exit(); if (kbCurrent.IsKeyDown(Keys.Right) | kbCurrent.IsKeyDown(Keys.D)) { //heros.hBody.Position = new Vector2(heros.hBody.Position.X + 10, heros.hBody.Position.Y); heros.hBody.ApplyImpulse(new Vector2(40, 0)); bgX += 2; if (bgX1 > 1024) bgX = bgX - 1024; bgX1 += 4; if (bgX1 > 1024) bgX1 = bgX1 - 1024; } if (kbCurrent.IsKeyDown(Keys.Left) | kbCurrent.IsKeyDown(Keys.A)) { //heros.hBody.Position = new Vector2(heros.hBody.Position.X - 10, heros.hBody.Position.Y); heros.hBody.ApplyImpulse(new Vector2(-40, 0)); bgX -= 2; if (bgX < 0) bgX = bgX + 1024; bgX1 -= 4; if (bgX1 < 0) bgX1 = bgX1 + 1024; } if (((kbCurrent.IsKeyDown(Keys.Up) & kbPrevious.IsKeyUp(Keys.Up))) | kbCurrent.IsKeyDown(Keys.W) & kbPrevious.IsKeyUp(Keys.W)) { //heros.hBody.Position = new Vector2(heros.hBody.Position.X, heros.hBody.Position.Y-10); //if (heros.hBody.GetVelocityAtLocalPoint(new Vector2(16,16)).Y>=-30) // heros.hBody.ApplyImpulse(new Vector2(0, -800)); if (hand==null) heros.Jump(-3200); } if (kbCurrent.IsKeyDown(Keys.Down)) { //heros.hBody.Position = new Vector2(heros.hBody.Position.X, heros.hBody.Position.Y + 10); } foreach (Path p in pltfrm.rBodies.swings) foreach (Body b in p.Bodies) if (heros.hGeom.Collide(b.Position) & hand == null) { hand = JointFactory.Instance.CreateRevoluteJoint(physicsSimulator, heros.hBody, b, heros.hBody.Position); heldRope = p; } if ((kbCurrent.IsKeyDown(Keys.Up)|kbCurrent.IsKeyDown(Keys.W)) & hand != null) heros.Climb(ref heldRope, ref hand); if ((kbCurrent.IsKeyDown(Keys.Down) | kbCurrent.IsKeyDown(Keys.S)) & hand != null) heros.SlideDown(ref heldRope, ref hand); camera.Update(kbCurrent, kbPrevious); mouseHandle(); physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f); kbPrevious = kbCurrent; base.Update(gameTime); }
/// <summary> /// Creates a rope. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="mass">The mass.</param> /// <param name="pinStart">if set to <c>true</c> [pin start].</param> /// <param name="pinEnd">if set to <c>true</c> [pin end].</param> /// <param name="collisionGroup">Collision group for the chain.</param> /// <param name="type">The joint/spring type.</param> /// <returns></returns> public Path CreateRope(Vector2 start, Vector2 end, float width, float height, float mass, bool pinStart, bool pinEnd, int collisionGroup, LinkType type) { Path path = new Path(width, height, mass, false); // create the path path.Add(start); // add starting point path.Add(Path.FindMidpoint(start, end)); // add midpoint of line (must have this because my code needs at least 3 control points) path.Add(end); // add end point path.Update(); // call update to create all the bodies Geom geom; for (int i = 0; i < path.Bodies.Count; i++) { geom = GeomFactory.Instance.CreateRectangleGeom(path.Bodies[i], width, height); geom.collisionGroup = collisionGroup; path.Add(geom); // add a geom to the chain } path.LinkBodies(type, Min, Max, SpringConstant, DampingConstant); // link bodies together if (pinStart) path.Add(JointFactory.Instance.CreateFixedRevoluteJoint(path.Bodies[0], start)); if (pinEnd) path.Add(JointFactory.Instance.CreateFixedRevoluteJoint(path.Bodies[path.Bodies.Count - 1], path.ControlPoints[2])); foreach (Joint j in path.Joints) // ropes need a little give ;) { j.BiasFactor = 0.01f; j.Softness = 0.05f; } return (path); }
public void SlideDown(ref Path p, ref RevoluteJoint j) { if (p.Bodies.IndexOf(j.Body2) != p.Bodies.Count - 1) { Vector2 nextPart = p.Bodies[p.Bodies.IndexOf(j.Body2) + 1].Position; j.Dispose(); j = null; hBody.Position = new Vector2(hBody.Position.X - (hBody.Position.X - nextPart.X) / 3, hBody.Position.Y - (hBody.Position.Y - nextPart.Y) / 3); } else { j.Dispose(); j = null; hBody.Position = new Vector2(hBody.Position.X, hBody.Position.Y + 10); } }
public override void LoadContent() { _chainTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 20, 20, Color.White, Color.Black); _chainOrigin = new Vector2(_chainTexture.Width / 2f, _chainTexture.Height / 2f); _chain = ComplexFactory.Instance.CreateChain(PhysicsSimulator, new Vector2(150, 100), new Vector2(200, 300), 20.0f, 10.0f, 1, true, false, LinkType.RevoluteJoint); _chain.CreateGeoms(PhysicsSimulator, 1); _chainPin = ComplexFactory.Instance.CreateChain(PhysicsSimulator, new Vector2(250, 100), new Vector2(300, 300), 20.0f, 10.0f, 1, true, false, LinkType.PinJoint); _chainPin.CreateGeoms(PhysicsSimulator, 2); ComplexFactory.Instance.SpringConstant = 150; // values inside let us setup additional parameters ComplexFactory.Instance.DampingConstant = 10; ComplexFactory.Instance.SpringRestLengthFactor = 1f; _chainSpring = ComplexFactory.Instance.CreateChain(PhysicsSimulator, new Vector2(350, 100), new Vector2(400, 300), 20.0f, 10.0f, 1, true, false, LinkType.LinearSpring); _chainSpring.CreateGeoms(PhysicsSimulator, 3); ComplexFactory.Instance.Min = 0; ComplexFactory.Instance.Max = 15; _chainSlide = ComplexFactory.Instance.CreateChain(PhysicsSimulator, new Vector2(450, 100), new Vector2(500, 300), 20.0f, 10.0f, 1, true, false, LinkType.SliderJoint); _chainSlide.CreateGeoms(PhysicsSimulator, 4); ComplexFactory.Instance.SpringConstant = 300; // values inside let us setup additional parameters ComplexFactory.Instance.DampingConstant = 10; ComplexFactory.Instance.SpringRestLengthFactor = 0.1f; _chainSpring2 = ComplexFactory.Instance.CreateChain(PhysicsSimulator, new Vector2(650, 100), new Vector2(600, 600), 20.0f, 10.0f, 40.0f, 1, true, false, LinkType.LinearSpring); _chainSpring2.CreateGeoms(PhysicsSimulator, 5); base.LoadContent(); }