A motor joint is used to control the relative motion between two bodies. A typical usage is to control the movement of a dynamic body with respect to the ground.
Наследование: Joint
Пример #1
0
        public Turret(Vector2 farseerLoc, World w, RagdollManager r, Fixture f)
        {
            

            DebugMaterial gray = new DebugMaterial(MaterialType.Blank)
            {
                Color = Color.DarkGray
            };

            body = new Body(w);
            pivot = FixtureFactory.AttachCircle(.9f, 1, body, gray);
            FixtureFactory.AttachRectangle(barrelLength, .5f, 1, new Vector2(barrelLength / 2, 0), body, gray);
            body.Position = farseerLoc;
            body.BodyType = BodyType.Dynamic;
            //b.CollidesWith = Category.None;

            if (f == null)
            {

                motor = JointFactory.CreateFixedRevoluteJoint(w, body, Vector2.Zero, farseerLoc);
            }
            else
            {
                motor = new RevoluteJoint(body, f.Body, Vector2.Zero, f.Body.GetLocalPoint(farseerLoc));
                w.AddJoint(motor);
            }

            motor.MotorEnabled = true;
            motor.MaxMotorTorque = 5000;

            Init(w, r);
        }
Пример #2
0
        public static MotorJoint CreateMotorJoint(World world, Body bodyA, Body bodyB, bool useWorldCoordinates = false)
        {
            MotorJoint joint = new MotorJoint(bodyA, bodyB, useWorldCoordinates);

            world.Add(joint);
            return(joint);
        }
Пример #3
0
		MotorJointTest()
		{
			Body ground = BodyFactory.CreateEdge(World, new Vector2(-20, 0), new Vector2(20, 0));
			
			// Define motorized body
			Body body = BodyFactory.CreateRectangle(World, 4, 1, 2, new Vector2(0, 8));
			body.BodyType = BodyType.Dynamic;
			body.Friction = 0.6f;
			
			_joint = new MotorJoint(ground, body);
			_joint.MaxForce = 1000.0f;
			_joint.MaxTorque = 1000.0f;
			
			World.AddJoint(_joint);
		}
Пример #4
0
		Joint j2b2Joint(World world, JObject jointValue)
		{
			Joint joint = null;
			
			int bodyIndexA = (int)jointValue["bodyA"];
			int bodyIndexB = (int)jointValue["bodyB"];
			if (bodyIndexA >= m_bodies.Count || bodyIndexB >= m_bodies.Count)
				return null;
			
			// set features common to all joints
			var bodyA = m_bodies[bodyIndexA];
			var bodyB = m_bodies[bodyIndexB];
			var collideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"];
			
			// keep these in scope after the if/else below
			RevoluteJoint revoluteDef;
			PrismaticJoint prismaticDef;
			DistanceJoint distanceDef;
			PulleyJoint pulleyDef;
			FixedMouseJoint mouseDef;
			GearJoint gearDef;
			WheelJoint wheelDef;
			WeldJoint weldDef;
			FrictionJoint frictionDef;
			RopeJoint ropeDef;
			MotorJoint motorDef;
			
			
			
			Vector2 mouseJointTarget = new Vector2(0, 0);
			string type = jointValue["type"].ToString() == null ? "" : jointValue["type"].ToString();
			if (type == "revolute")
			{
				joint = revoluteDef = JointFactory.CreateRevoluteJoint(world, bodyA, bodyB, jsonToVec("anchorB", jointValue));
				revoluteDef.LocalAnchorA = jsonToVec("anchorA", jointValue);
				revoluteDef.LocalAnchorB = jsonToVec("anchorB", jointValue);
				revoluteDef.ReferenceAngle = jsonToFloat("refAngle", jointValue);
				revoluteDef.LimitEnabled = jointValue["enableLimit"] == null ? false : (bool)jointValue["enableLimit"];
				revoluteDef.LowerLimit = jsonToFloat("lowerLimit", jointValue);
				revoluteDef.UpperLimit = jsonToFloat("upperLimit", jointValue);
				revoluteDef.MotorEnabled = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];
				revoluteDef.MotorSpeed = jsonToFloat("motorSpeed", jointValue);
				revoluteDef.MaxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);
			}
			else if (type == "prismatic")
			{
				
				var localAxis = new Vector2();
				
				var localAnchorA = jsonToVec("anchorA", jointValue);
				var localAnchorB = jsonToVec("anchorB", jointValue);
				
				if (jointValue["localAxisA"] != null)
					localAxis = jsonToVec("localAxisA", jointValue);
				else
					localAxis = jsonToVec("localAxis1", jointValue);
				
				
				
				joint = prismaticDef = JointFactory.CreatePrismaticJoint(world, bodyA, bodyB, localAnchorB, localAxis);
				prismaticDef.LocalAnchorA = localAnchorA;
				prismaticDef.ReferenceAngle = jsonToFloat("refAngle", jointValue);
				prismaticDef.LimitEnabled = (bool)jointValue["enableLimit"];
				prismaticDef.LowerLimit = jsonToFloat("lowerLimit", jointValue);
				prismaticDef.UpperLimit = jsonToFloat("upperLimit", jointValue);
				prismaticDef.MotorEnabled = (bool)jointValue["enableMotor"];
				prismaticDef.MotorSpeed = jsonToFloat("motorSpeed", jointValue);
				prismaticDef.MaxMotorForce = jsonToFloat("maxMotorForce", jointValue);
			}
			else if (type == "distance")
			{
				joint = distanceDef = JointFactory.CreateDistanceJoint(world, bodyA, bodyB, jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue));
				distanceDef.LocalAnchorA = (jsonToVec("anchorA", jointValue));
				distanceDef.LocalAnchorB = (jsonToVec("anchorB", jointValue));
				distanceDef.Length = jsonToFloat("length", jointValue);
				distanceDef.Frequency = jsonToFloat("frequency", jointValue);
				distanceDef.DampingRatio = jsonToFloat("dampingRatio", jointValue);
			}
			else if (type == "pulley")
			{
				joint = pulleyDef = JointFactory.CreatePulleyJoint(world, bodyA, bodyB,
				                                                   jsonToVec("groundAnchorA", jointValue), jsonToVec("groundAnchorB", jointValue),
				                                                   jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue),
				                                                   jsonToFloat("ratio", jointValue));
				
				pulleyDef.LengthA = jsonToFloat("lengthA", jointValue);
				pulleyDef.LengthB = jsonToFloat("lengthB", jointValue);
				//pulleyDef.Ratio = jsonToFloat("ratio", jointValue);
			}
			else if (type == "mouse")
			{
				joint = mouseDef = JointFactory.CreateFixedMouseJoint(world, bodyA, jsonToVec("target", jointValue));
				//mouseJointTarget = jsonToVec("target", jointValue);
				//mouseDef.target.set(jsonToVec("anchorB", jointValue));// alter after creating joint
				mouseDef.MaxForce = jsonToFloat("maxForce", jointValue);
				mouseDef.Frequency = jsonToFloat("frequency", jointValue);
				mouseDef.DampingRatio = jsonToFloat("dampingRatio", jointValue);
			}
			// Gear joints are apparently not implemented in JBox2D yet, but
			// when they are, commenting out the following section should work.
			
			else if (type == "gear")
			{
				int jointIndex1 = (int)jointValue["joint1"];
				int jointIndex2 = (int)jointValue["joint2"];
				var joint1 = m_joints[jointIndex1];
				var joint2 = m_joints[jointIndex2];
				var ratio = jsonToFloat("ratio", jointValue);
				
				joint = gearDef = JointFactory.CreateGearJoint(world, joint1, joint2, ratio);
				
			}
			
			// Wheel joints are apparently not implemented in JBox2D yet, but
			// when they are, commenting out the following section should work.
			
			else if (type == "wheel")
			{
				var localAnchorA = jsonToVec("anchorA", jointValue);
				var localAnchorB = (jsonToVec("anchorB", jointValue));
				var localAxisA = (jsonToVec("localAxisA", jointValue));
				var enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];
				var motorSpeed = jsonToFloat("motorSpeed", jointValue);
				var maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);
				var frequencyHz = jsonToFloat("springFrequency", jointValue);
				var dampingRatio = jsonToFloat("springDampingRatio", jointValue);
				
				joint = wheelDef = JointFactory.CreateWheelJoint(world, bodyA, bodyB, localAnchorB, localAxisA);
				
				wheelDef.LocalAnchorA = localAnchorA;
				wheelDef.LocalAnchorB = localAnchorB;
				wheelDef.MotorEnabled = enableMotor;
				wheelDef.MotorSpeed = motorSpeed;
				wheelDef.SpringFrequencyHz = frequencyHz;
				wheelDef.MaxMotorTorque = maxMotorTorque;
				wheelDef.SpringDampingRatio = dampingRatio;
			}
			else if (type == "weld")
			{
				joint = weldDef = JointFactory.CreateWeldJoint(world, bodyA, bodyB, jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue));
                weldDef.LocalAnchorA = jsonToVec("anchorA", jointValue);
                weldDef.LocalAnchorB = jsonToVec("anchorB", jointValue);
                weldDef.FrequencyHz = jsonToFloat("frequency", jointValue);
                weldDef.DampingRatio = jsonToFloat("dampingRatio", jointValue);

			}
			else if (type == "friction")
			{
				joint = frictionDef = JointFactory.CreateFrictionJoint(world, bodyA, bodyB, jsonToVec("anchorA", jointValue));
                frictionDef.LocalAnchorB = jsonToVec("anchorB", jointValue);
				frictionDef.MaxForce = jsonToFloat("maxForce", jointValue);
				frictionDef.MaxTorque = jsonToFloat("maxTorque", jointValue);
			}
			else if (type == "rope")
			{
				joint = ropeDef = new RopeJoint(bodyA, bodyB, jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue));
				world.AddJoint(joint);
				ropeDef.MaxLength = jsonToFloat("maxLength", jointValue);
			}
			
			else if (type == "motor")
			{
				var maxForce = jsonToFloat("maxForce", jointValue);
				var maxMotorTorque = jsonToFloat("maxTorque", jointValue);
				var angularOffset = jsonToFloat("refAngle", jointValue);
				
				joint = motorDef = new MotorJoint(bodyA, bodyB);
                world.AddJoint(joint);
                motorDef.LinearOffset = jsonToVec("anchorA", jointValue);
                motorDef.MaxForce = maxForce;
				motorDef.MaxTorque = maxMotorTorque;
				motorDef.AngularOffset = angularOffset;
			}
			
			
			
			if (null != joint)
			{
				// set features common to all joints
				/*joint.BodyA = bodyA;
				joint.BodyB = bodyB;*/
				joint.CollideConnected = collideConnected;
				
				string jointName = jointValue["name"] == null ? "" : jointValue["name"].ToString();
				if (jointName != "")
				{
					SetJointName(joint, jointName);
				}
			}
			
			
			
			
			return joint;
		}
Пример #5
0
		public override Joint createJoint()
		{
			var joint = new MotorJoint( bodyA, bodyB );
			joint.collideConnected = collideConnected;
			joint.linearOffset = linearOffset * FSConvert.displayToSim;
			joint.maxForce = maxForce;
			joint.maxTorque = maxTorque;
			joint.angularOffset = angularOffset;
			return joint;
		}
Пример #6
0
 public static MotorJoint CreateMotorJoint(World world, Body bodyA, Body bodyB, bool useWorldCoordinates = false)
 {
     MotorJoint joint = new MotorJoint(bodyA, bodyB, useWorldCoordinates);
     world.AddJoint(joint);
     return joint;
 }
Пример #7
0
 public static MotorJoint CreateMotorJoint(World world, Body bodyA, Body bodyB)
 {
     MotorJoint joint = new MotorJoint(bodyA, bodyB);
     world.AddJoint(joint);
     return joint;
 }