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); } }
public void dettachPlayer() { if (attachedPlayer!=null) { attachedPlayer = null; //sets to null if player is not null } platformJoint.Dispose(); platformJoint = null; //gets rid of the joint }
//the player that will be attatched public void attachPlayer(Player p) { if (platformJoint == null) { attachedPlayer = p; //creates a revolute joint between the platform and player body at the platforms position platformJoint = JointFactory.Instance.CreateRevoluteJoint(body, p.Body, body.Position); sim.Add(platformJoint); platformJoint.Broke += new EventHandler<EventArgs>(platformJoint_Broke);//fires platformJOint_broke event handler } }
float speed; //the speed of the movingplatform #endregion Fields #region Constructors public MovingPlatform() : base() { Coordinates = new Vector2[10]; //the array of coordinates. movingplatforms can have up to 10 coordinates int i = 0; foreach (Vector2 v in Coordinates) //default all coordiantes to 1,1 { Coordinates.SetValue(new Vector2(1, 1), i); i++; } speed = 4.5f; //this is the movingplatforms speed platformJoint = null; // platformJoint defaulted To null }
void platformJoint_Broke(object sender, EventArgs e) { //destroys the join and sets it to null sim.Remove(platformJoint); platformJoint.Dispose(); platformJoint = null; }
public RevoluteJoint CreateRevoluteJoint(Body body1, Body body2, Vector2 initialAnchorPosition) { RevoluteJoint revoluteJoint = new RevoluteJoint(body1, body2, initialAnchorPosition); return revoluteJoint; }
/// <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); }
protected void mouseHandle() { mouseStateCurrent = Mouse.GetState(); for (int i = 3; i > 0; i--) mPoint[i] = mPoint[i - 1]; mPoint[0] = new Vector2(mouseStateCurrent.X + camera.Position.X - 512, mouseStateCurrent.Y + camera.Position.Y - 381); mouseBody.Position = mPoint[0]; if (staticBetween(heros.hBody.Position, mPoint[0])) mouseColor = Color.Red; else mouseColor = Color.Blue; if (mouseStatePrevious.LeftButton == ButtonState.Released && mouseStateCurrent.LeftButton == ButtonState.Pressed && !staticBetween(heros.hBody.Position, mPoint[0])) { picked = physicsSimulator.Collide(mPoint[0]); if (picked != null && picked != heros.hGeom) { if (!picked.Body.IsStatic & picked.CollisionGroup == 1) { mouseJoint = JointFactory.Instance.CreateRevoluteJoint(physicsSimulator, mouseBody, picked.Body, mPoint[0]); picked.Body.RotationalDragCoefficient = 500f; } if (!picked.Body.IsStatic & picked.CollisionGroup != 1) mouseSpring = SpringFactory.Instance.CreateFixedLinearSpring(physicsSimulator, picked.Body, picked.Body.GetLocalPosition(mPoint[0]), mPoint[0], 200, 20); } } else if ((mouseStatePrevious.LeftButton == ButtonState.Pressed && mouseStateCurrent.LeftButton == ButtonState.Released) | staticBetween(heros.hBody.Position, mPoint[0])) { if (mouseJoint != null && mouseJoint.IsDisposed == false) { mouseJoint.Dispose(); mouseJoint = null; picked.Body.RotationalDragCoefficient = 0.001f; picked = null; } if (mouseSpring != null && mouseSpring.IsDisposed == false) { mouseSpring.Dispose(); mouseSpring = null; picked = null; } } if (mouseStateCurrent.LeftButton == ButtonState.Pressed && mouseSpring != null) mouseSpring.WorldAttachPoint = mPoint[0]; mouseStatePrevious = mouseStateCurrent; }
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); } }