/// <summary> /// Attaches the bodies with revolute joints. /// </summary> /// <param name="world">The world.</param> /// <param name="bodies">The bodies.</param> /// <param name="localAnchorA">The local anchor A.</param> /// <param name="localAnchorB">The local anchor B.</param> /// <param name="connectFirstAndLast">if set to <c>true</c> [connect first and last].</param> /// <param name="collideConnected">if set to <c>true</c> [collide connected].</param> public static List <FSRevoluteJoint> AttachBodiesWithRevoluteJoint(FSWorld world, List <FSBody> bodies, FVector2 localAnchorA, FVector2 localAnchorB, bool connectFirstAndLast, bool collideConnected) { List <FSRevoluteJoint> joints = new List <FSRevoluteJoint>(bodies.Count + 1); for (int i = 1; i < bodies.Count; i++) { FSRevoluteJoint joint = new FSRevoluteJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB); joint.CollideConnected = collideConnected; world.AddJoint(joint); joints.Add(joint); } if (connectFirstAndLast) { FSRevoluteJoint lastjoint = new FSRevoluteJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA, localAnchorB); lastjoint.CollideConnected = collideConnected; world.AddJoint(lastjoint); joints.Add(lastjoint); } return(joints); }
/// <summary> /// Creates a revolute joint and adds it to the world /// </summary> /// <param name="world"></param> /// <param name="bodyA"></param> /// <param name="bodyB"></param> /// <param name="anchor"></param> /// <returns></returns> public static FSRevoluteJoint CreateRevoluteJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchor) { FSRevoluteJoint joint = CreateRevoluteJoint(bodyA, bodyB, anchor); world.AddJoint(joint); return(joint); }
/// <summary> /// Creates a revolute joint. /// </summary> /// <param name="bodyA"></param> /// <param name="bodyB"></param> /// <param name="localAnchorB">The anchor of bodyB in local coordinates</param> /// <returns></returns> public static FSRevoluteJoint CreateRevoluteJoint(FSBody bodyA, FSBody bodyB, FVector2 localAnchorB) { FVector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(localAnchorB)); FSRevoluteJoint joint = new FSRevoluteJoint(bodyA, bodyB, localanchorA, localAnchorB); return(joint); }
public override void InitJoint() { base.InitJoint(); // Vector3 p0 = BodyB.transform.TransformPoint(new Vector3(LocalAnchorB.x, LocalAnchorB.y, -5f)); joint = JointFactory.CreateRevoluteJoint(FSWorldComponent.PhysicsWorld, BodyA.PhysicsBody, BodyB.PhysicsBody, BodyB.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p0))); joint.CollideConnected = CollideConnected; joint.LowerLimit = LowerLimit * Mathf.Deg2Rad; joint.UpperLimit = UpperLimit * Mathf.Deg2Rad; joint.LimitEnabled = LimitEnabled; joint.MaxMotorTorque = MaxMotorTorque; joint.MotorSpeed = MotorSpeed; joint.MotorEnabled = MotorEnabled; }
public override void Start() { base.Start(); FSBody body; tScale = physScale * 2f; // St position in world space m_offset = new FVector2(180f / physScale, -200f / physScale); m_motorSpeed = 2f; m_motorOn = true; FVector2 pivot = new FVector2(0f, 24f / tScale); for (int i = 0; i < 50; i++) { body = BodyFactory.CreateCircle(FSWorldComponent.PhysicsWorld, 3.75f / physScale, 1f, new FVector2((Random.value * 620f + 10f) / physScale, -340f / physScale)); body.BodyType = BodyType.Dynamic; } // chassis { m_chassis = BodyFactory.CreateBody(FSWorldComponent.PhysicsWorld, FVector2.Add(pivot, m_offset)); m_chassis.BodyType = BodyType.Dynamic; FSFixture m_chassis_f = FixtureFactory.AttachRectangle(150f / tScale, 60f / tScale, 1f, FVector2.Zero, m_chassis); //m_chassis_f.CollisionGroup = -1; m_chassis_f.CollisionCategories = Category.Cat10; m_chassis_f.CollidesWith = Category.Cat1; } // wheel { m_wheel = BodyFactory.CreateBody(FSWorldComponent.PhysicsWorld, FVector2.Add(pivot, m_offset)); m_wheel.BodyType = BodyType.Dynamic; FSFixture m_wheel_f = FixtureFactory.AttachCircle(48f / tScale, 1f, m_wheel); //m_wheel_f.CollisionGroup = -1; m_wheel_f.CollisionCategories = Category.Cat10; m_wheel_f.CollidesWith = Category.Cat1; } // glue chassis & wheel { m_motorJoint = JointFactory.CreateRevoluteJoint(FSWorldComponent.PhysicsWorld, m_wheel, m_chassis, FVector2.Zero); m_motorJoint.MotorSpeed = m_motorSpeed; m_motorJoint.MaxMotorTorque = 400f; m_motorJoint.CollideConnected = false; m_motorJoint.MotorEnabled = m_motorOn; } FVector2 wheelAnchor; wheelAnchor = new FVector2(0f, -24f / tScale) + pivot; CreateLeg(-1f, wheelAnchor); CreateLeg(1f, wheelAnchor); m_wheel.Rotation = 120f * Mathf.Deg2Rad; CreateLeg(-1f, wheelAnchor); CreateLeg(1f, wheelAnchor); m_wheel.Rotation = -120f * Mathf.Deg2Rad; CreateLeg(-1f, wheelAnchor); CreateLeg(1f, wheelAnchor); }