void Start() { if (connectedBody == null) { return; } var jointDef = new DistanceJointDef(); jointDef.CollideConnected = collideConnected; if (fixedLength) { jointDef.Body1 = GetComponent <Box2DBody>(); jointDef.Body2 = connectedBody; jointDef.LocalAnchor1 = transform.InverseTransformPoint(anchor); jointDef.LocalAnchor2 = connectedBody.transform.InverseTransformPoint(connectedAnchor); jointDef.Length = length; } else { jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, anchor, connectedAnchor); } jointDef.FrequencyHz = frequencyHz; jointDef.DampingRatio = dampingRatio; var world = Box2DWorld.Instance(); joint = (DistanceJoint)world.CreateJoint(jointDef); }
// Use this for initialization protected override IntPtr Init() { DistanceJointDef jd = new DistanceJointDef(other.body, body.body); jd.Initialize(other.body, body.body, anchor1, anchor2); jd.frequencyHz = frequencyHz; jd.dampingRatio = dampingRatio; return(API.CreateDistanceJoint(B2DWorld.instance.world, jd)); }
/// <summary> /// Initializes a new instance of the <see cref="DistanceJoint" /> class /// </summary> /// <param name="def">The def</param> public DistanceJoint(DistanceJointDef def) : base(def) { localAnchorA = def.LocalAnchorA; localAnchorB = def.LocalAnchorB; length = MathUtils.Max(def.Length, Settings.LinearSlop); minLength = MathUtils.Max(def.MinLength, Settings.LinearSlop); maxLength = MathUtils.Max(def.MaxLength, minLength); stiffness = def.Stiffness; damping = def.Damping; }
public void CreateDistanceJoint(Body bodyA, Body bodyB) { var jd = new DistanceJointDef(); jd.bodyA = bodyA; jd.bodyB = bodyB; jd.collideConnected = true; jd.stiffness = 1000f; jd.localAnchorA = new Vector2(0, 0.01f); jd.length = 0.5f; b2DWorld.CreateJoint(jd); }
/// <summary> /// Creates a distance joint between two bodies /// </summary> /// <param name="b1"></param> /// <param name="b2"></param> /// <returns></returns> public DistanceJoint joinBodies_Distance(Body b1, Body b2) { Vector2 pos1 = b1.GetWorldCenter(); Vector2 pos2 = b2.GetWorldCenter(); DistanceJointDef djd = new DistanceJointDef(); djd.Initialize(b1, b2, pos1, pos2); djd.collideConnected = false; DistanceJoint dj = physicsWorld.CreateJoint(djd) as DistanceJoint; return(dj); }
// Связь основанная на дистанции жесткость которой можно регулировать public Joint AddDistanceJoint(Body b1, Body b2, float x1, float y1, float x2, float y2, bool collideConnected = true, float hz = 1f) { DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(b1, b2, new Vec2(x1, y1), new Vec2(x2, y2)); jd.CollideConnected = collideConnected; jd.FrequencyHz = hz; Joint joint = world.CreateJoint(jd); return(joint); }
public Joint AddDistanceJoint(Body b1, Body b2, float x1, float y1, float x2, float y2, float length, bool collideConnected = true, float hz = 1f) { DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(b1, b2, new Vec2(x1 / metric, y1 / metric), new Vec2(x2 / metric, y2 / metric)); jd.FrequencyHz = 0.3f; jd.CollideConnected = collideConnected; jd.FrequencyHz = hz; jd.Length = length; Joint joint = world.CreateJoint(jd); return(joint); }
public Joint JointDistance(RigidBody body, float x1, float y1, float x2, float y2, bool isCollide, float hz, float length, float dampingRatio = 0) { var p1 = GetBody().GetPosition() + new Vec2(x1 / PhysicsController.metric, y1 / PhysicsController.metric); var p2 = body.GetBody().GetPosition() + new Vec2(x2 / PhysicsController.metric, y2 / PhysicsController.metric); DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(GetBody(), body.GetBody(), p1, p2); jd.FrequencyHz = hz; jd.CollideConnected = isCollide; jd.Length = length; jd.DampingRatio = dampingRatio; Joint joint = Physics.GetWorld().CreateJoint(jd); return(joint); }
public void ApplyAttribtues(DistanceJointDef j) { j.collideConnected = collideConnected; if (length != 1.0f) { j.length = length; } if (frequencyHz != 0.0f) { j.frequencyHz = frequencyHz; } if (dampingRatio != 0.0f) { j.dampingRatio = dampingRatio; } }
private DistanceJointTest() { Body ground; { BodyDef bd = new BodyDef(); ground = BodyFactory.CreateFromDef(World, bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.AddFixture(shape); } { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.AngularDamping = 0.1f; bd.Position = new Vector2(0.0f, 5.0f); Body body = BodyFactory.CreateFromDef(World, bd); PolygonShape shape = new PolygonShape(5.0f); shape.SetAsBox(0.5f, 0.5f); body.AddFixture(shape); _hertz = 1.0f; _dampingRatio = 0.7f; DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(ground, body, new Vector2(0.0f, 15.0f), bd.Position); jd.CollideConnected = true; _length = jd.Length; _minLength = _length; _maxLength = _length; JointHelper.LinearStiffness(_hertz, _dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping); jd.Stiffness = stiffness; jd.Damping = damping; _joint = (DistanceJoint)JointFactory.CreateFromDef(World, jd); } }
private void LoadAsteroid(Vector2 positon) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; //bd.angularVelocity = -2.0f; bd.position = positon; Body asteroid = world.CreateBody(bd); CircleShape pShape = new CircleShape(); pShape._radius = GameContent.asteroid[0].Width / 2; FixtureDef fd = new FixtureDef(); fd.shape = pShape; fd.density = 0.05f; fd.friction = 0.3f; asteroid.CreateFixture(fd); DistanceJointDef jd = new DistanceJointDef(); jd.bodyA = ground; jd.bodyB = asteroid; jd.localAnchorA = asteroid.Position; jd.localAnchorB = Vector2.Zero; jd.frequencyHz = 0.18f; jd.dampingRatio = 0.95f; jd.length = 0; jd.collideConnected = false; world.CreateJoint(jd); asteroids.Add(new Asteroid(GameContent, random.Next(16), asteroid)); }
public DistanceJointTest() { Body ground = null; { BodyDef bd = new BodyDef(); ground = World.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { BodyDef bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.AngularDamping = 0.1f; bd.Position.Set(0.0f, 5.0f); Body body = World.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); body.CreateFixture(shape, 5.0f); m_hertz = 1.0f; m_dampingRatio = 0.7f; DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(ground, body, new Vector2(0.0f, 15.0f), bd.Position); jd.CollideConnected = true; m_length = jd.Length; m_minLength = m_length; m_maxLength = m_length; JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, m_hertz, m_dampingRatio, jd.BodyA, jd.BodyB); m_joint = (DistanceJoint)World.CreateJoint(jd); } }
public Cloth() { FixtureDef boxFix = new FixtureDef(new CircleShape(ClothBodySize), 0.2f); BodyDef boxBod = new BodyDef(BodyType.Dynamic, Vec2.Empty); boxFix.Filter.GroupIndex = -1; boxBod.Position = new Vec2(-ClothTotalWidth / 2, 30); Body bar; { bar = m_world.CreateBody(new BodyDef(BodyType.Static, new Vec2(-ClothBodySpacingWidth / 2, 30))); var fd = new FixtureDef(new PolygonShape((ClothTotalWidth / 2) + ClothBodySpacingWidth, 0.25f)); fd.Filter.GroupIndex = -1; bar.CreateFixture(fd); } for (int y = 0; y < ClothSegmentsHeight; ++y) { for (int x = 0; x < ClothSegmentsWidth; ++x) { Body body = m_world.CreateBody(boxBod); boxBod.Position += new Vec2(ClothBodySpacingWidth, 0); body.CreateFixture(boxFix); if (y == 0) { WeldJointDef wjd = new WeldJointDef(); wjd.Initialize(body, bar, body.WorldCenter); m_world.CreateJoint(wjd); } cloth[x, y] = body; } boxBod.Position = new Vec2(-ClothTotalWidth / 2, boxBod.Position.Y - ClothBodySpacingWidth); } for (int y = 0; y < ClothSegmentsHeight; ++y) { for (int x = 0; x < ClothSegmentsWidth; ++x) { Body leftBody, rightBody; DistanceJointDef djd = new DistanceJointDef(); djd.FrequencyHz = 15 + Rand.RandomFloat(0, 6); djd.DampingRatio = 0.11f + Rand.RandomFloat(0.01f, 0.15f); // connect to right if (x != ClothSegmentsWidth - 1) { leftBody = cloth[x, y]; rightBody = cloth[x + 1, y]; djd.Initialize(leftBody, rightBody, leftBody.WorldCenter, rightBody.WorldCenter); m_world.CreateJoint(djd); } // connect to up if (y != 0) { leftBody = cloth[x, y]; rightBody = cloth[x, y - 1]; djd.Initialize(leftBody, rightBody, leftBody.WorldCenter, rightBody.WorldCenter); m_world.CreateJoint(djd); } } } }
void CreateLeg(float s, Vector2 wheelAnchor) { Vector2 p1 = new Vector2(5.4f * s, -6.1f); Vector2 p2 = new Vector2(7.2f * s, -1.2f); Vector2 p3 = new Vector2(4.3f * s, -1.9f); Vector2 p4 = new Vector2(3.1f * s, 0.8f); Vector2 p5 = new Vector2(6.0f * s, 1.5f); Vector2 p6 = new Vector2(2.5f * s, 3.7f); FixtureDef fd1 = new FixtureDef(); FixtureDef fd2 = new FixtureDef(); fd1.filter.groupIndex = -1; fd2.filter.groupIndex = -1; fd1.density = 1.0f; fd2.density = 1.0f; PolygonShape poly1 = new PolygonShape(); PolygonShape poly2 = new PolygonShape(); Vector2[] vertices = new Vector2[3]; if (s > 0.0f) { vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices, 3); } else { vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices, 3); } fd1.shape = poly1; fd2.shape = poly2; BodyDef bd1 = new BodyDef(); BodyDef bd2 = new BodyDef(); bd1.type = BodyType.Dynamic; bd2.type = BodyType.Dynamic; bd1.position = _offset; bd2.position = p4 + _offset; bd1.angularDamping = 10.0f; bd2.angularDamping = 10.0f; Body body1 = _world.CreateBody(bd1); Body body2 = _world.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); DistanceJointDef djd = new DistanceJointDef(); // Using a soft distanceraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. djd.dampingRatio = 0.5f; djd.frequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + _offset, p5 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + _offset, p4 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, _chassis, p4 + _offset); _world.CreateJoint(rjd); }
Web() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-5.0f, 5.0f); _bodies[0] = _world.CreateBody(bd); _bodies[0].CreateFixture(shape, 5.0f); bd.position = new Vector2(5.0f, 5.0f); _bodies[1] = _world.CreateBody(bd); _bodies[1].CreateFixture(shape, 5.0f); bd.position = new Vector2(5.0f, 15.0f); _bodies[2] = _world.CreateBody(bd); _bodies[2].CreateFixture(shape, 5.0f); bd.position = new Vector2(-5.0f, 15.0f); _bodies[3] = _world.CreateBody(bd); _bodies[3].CreateFixture(shape, 5.0f); DistanceJointDef jd = new DistanceJointDef(); Vector2 p1, p2, d; jd.frequencyHz = 4.0f; jd.dampingRatio = 0.5f; jd.bodyA = ground; jd.bodyB = _bodies[0]; jd.localAnchorA = new Vector2(-10.0f, 0.0f); jd.localAnchorB = new Vector2(-0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[0] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[1]; jd.localAnchorA = new Vector2(10.0f, 0.0f); jd.localAnchorB = new Vector2(0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[1] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[2]; jd.localAnchorA = new Vector2(10.0f, 20.0f); jd.localAnchorB = new Vector2(0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[2] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[3]; jd.localAnchorA = new Vector2(-10.0f, 20.0f); jd.localAnchorB = new Vector2(-0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[3] = _world.CreateJoint(jd); jd.bodyA = _bodies[0]; jd.bodyB = _bodies[1]; jd.localAnchorA = new Vector2(0.5f, 0.0f); jd.localAnchorB = new Vector2(-0.5f, 0.0f);; p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[4] = _world.CreateJoint(jd); jd.bodyA = _bodies[1]; jd.bodyB = _bodies[2]; jd.localAnchorA = new Vector2(0.0f, 0.5f); jd.localAnchorB = new Vector2(0.0f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[5] = _world.CreateJoint(jd); jd.bodyA = _bodies[2]; jd.bodyB = _bodies[3]; jd.localAnchorA = new Vector2(-0.5f, 0.0f); jd.localAnchorB = new Vector2(0.5f, 0.0f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[6] = _world.CreateJoint(jd); jd.bodyA = _bodies[3]; jd.bodyB = _bodies[0]; jd.localAnchorA = new Vector2(0.0f, -0.5f); jd.localAnchorB = new Vector2(0.0f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[7] = _world.CreateJoint(jd); } }
public WebTest() { Body ground; { BodyDef bd = new BodyDef(); ground = World.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); BodyDef bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-5.0f, 5.0f); m_bodies[0] = World.CreateBody(bd); m_bodies[0].CreateFixture(shape, 5.0f); bd.Position.Set(5.0f, 5.0f); m_bodies[1] = World.CreateBody(bd); m_bodies[1].CreateFixture(shape, 5.0f); bd.Position.Set(5.0f, 15.0f); m_bodies[2] = World.CreateBody(bd); m_bodies[2].CreateFixture(shape, 5.0f); bd.Position.Set(-5.0f, 15.0f); m_bodies[3] = World.CreateBody(bd); m_bodies[3].CreateFixture(shape, 5.0f); DistanceJointDef jd = new DistanceJointDef(); Vector2 p1; Vector2 p2; Vector2 d; float frequencyHz = 2.0f; float dampingRatio = 0.0f; jd.BodyA = ground; jd.BodyB = m_bodies[0]; jd.LocalAnchorA.Set(-10.0f, 0.0f); jd.LocalAnchorB.Set(-0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[0] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = m_bodies[1]; jd.LocalAnchorA.Set(10.0f, 0.0f); jd.LocalAnchorB.Set(0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[1] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = m_bodies[2]; jd.LocalAnchorA.Set(10.0f, 20.0f); jd.LocalAnchorB.Set(0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[2] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = m_bodies[3]; jd.LocalAnchorA.Set(-10.0f, 20.0f); jd.LocalAnchorB.Set(-0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[3] = World.CreateJoint(jd); jd.BodyA = m_bodies[0]; jd.BodyB = m_bodies[1]; jd.LocalAnchorA.Set(0.5f, 0.0f); jd.LocalAnchorB.Set(-0.5f, 0.0f); ; p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[4] = World.CreateJoint(jd); jd.BodyA = m_bodies[1]; jd.BodyB = m_bodies[2]; jd.LocalAnchorA.Set(0.0f, 0.5f); jd.LocalAnchorB.Set(0.0f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[5] = World.CreateJoint(jd); jd.BodyA = m_bodies[2]; jd.BodyB = m_bodies[3]; jd.LocalAnchorA.Set(-0.5f, 0.0f); jd.LocalAnchorB.Set(0.5f, 0.0f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[6] = World.CreateJoint(jd); jd.BodyA = m_bodies[3]; jd.BodyB = m_bodies[0]; jd.LocalAnchorA.Set(0.0f, -0.5f); jd.LocalAnchorB.Set(0.0f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointUtils.LinearStiffness(out jd.Stiffness, out jd.Damping, frequencyHz, dampingRatio, jd.BodyA, jd.BodyB); m_joints[7] = World.CreateJoint(jd); } }
public Web() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); shape.Density = 5; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-5.0f, 5.0f); m_bodies[0] = m_world.CreateBody(bd); m_bodies[0].CreateFixture(shape); bd.Position.Set(5.0f, 5.0f); m_bodies[1] = m_world.CreateBody(bd); m_bodies[1].CreateFixture(shape); bd.Position.Set(5.0f, 15.0f); m_bodies[2] = m_world.CreateBody(bd); m_bodies[2].CreateFixture(shape); bd.Position.Set(-5.0f, 15.0f); m_bodies[3] = m_world.CreateBody(bd); m_bodies[3].CreateFixture(shape); DistanceJointDef jd = new DistanceJointDef(); Vec2 p1, p2, d; jd.frequencyHz = 2.0f; jd.dampingRatio = 0.0f; jd.bodyA = ground; jd.bodyB = m_bodies[0]; jd.localAnchorA.Set(-10.0f, 0.0f); jd.localAnchorB.Set(-0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[0] = m_world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = m_bodies[1]; jd.localAnchorA.Set(10.0f, 0.0f); jd.localAnchorB.Set(0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[1] = m_world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = m_bodies[2]; jd.localAnchorA.Set(10.0f, 20.0f); jd.localAnchorB.Set(0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[2] = m_world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = m_bodies[3]; jd.localAnchorA.Set(-10.0f, 20.0f); jd.localAnchorB.Set(-0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[3] = m_world.CreateJoint(jd); jd.bodyA = m_bodies[0]; jd.bodyB = m_bodies[1]; jd.localAnchorA.Set(0.5f, 0.0f); jd.localAnchorB.Set(-0.5f, 0.0f);; p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[4] = m_world.CreateJoint(jd); jd.bodyA = m_bodies[1]; jd.bodyB = m_bodies[2]; jd.localAnchorA.Set(0.0f, 0.5f); jd.localAnchorB.Set(0.0f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[5] = m_world.CreateJoint(jd); jd.bodyA = m_bodies[2]; jd.bodyB = m_bodies[3]; jd.localAnchorA.Set(-0.5f, 0.0f); jd.localAnchorB.Set(0.5f, 0.0f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[6] = m_world.CreateJoint(jd); jd.bodyA = m_bodies[3]; jd.bodyB = m_bodies[0]; jd.localAnchorA.Set(0.0f, -0.5f); jd.localAnchorB.Set(0.0f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[7] = m_world.CreateJoint(jd); } }
private void CreateLeg(float s, Vector2 wheelAnchor) { var p1 = new Vector2(5.4f * s, -6.1f); var p2 = new Vector2(7.2f * s, -1.2f); var p3 = new Vector2(4.3f * s, -1.9f); var p4 = new Vector2(3.1f * s, 0.8f); var p5 = new Vector2(6.0f * s, 1.5f); var p6 = new Vector2(2.5f * s, 3.7f); var fd1 = new FixtureDef { Filter = { GroupIndex = -1 }, Density = 1.0f }; var fd2 = new FixtureDef { Filter = { GroupIndex = -1 }, Density = 1.0f }; var poly1 = new PolygonShape(); var poly2 = new PolygonShape(); if (s > 0.0f) { var vertices = new Vector2[3]; vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices); } else { var vertices = new Vector2[3]; vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices); } fd1.Shape = poly1; fd2.Shape = poly2; var bd1 = new BodyDef(); var bd2 = new BodyDef(); bd1.BodyType = BodyType.DynamicBody; bd2.BodyType = BodyType.DynamicBody; bd1.Position = _offset; bd2.Position = p4 + _offset; bd1.AngularDamping = 10.0f; bd2.AngularDamping = 10.0f; var body1 = World.CreateBody(bd1); var body2 = World.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); var djd = new DistanceJointDef(); // Using a soft distance constraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. djd.DampingRatio = 0.5f; djd.FrequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + _offset, p5 + _offset); World.CreateJoint(djd); djd.Initialize(body1, body2, p3 + _offset, p4 + _offset); World.CreateJoint(djd); djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); World.CreateJoint(djd); djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); World.CreateJoint(djd); var rjd = new RevoluteJointDef(); rjd.Initialize(body2, _chassis, p4 + _offset); World.CreateJoint(rjd); }
public WorldData Deserialize(Stream stream) { XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream); if (root.Name.ToLower() != "world") throw new Exception(); WorldData data = new WorldData(); if (root.Attributes.Count == 0) throw new Exception("No version"); else if (int.Parse(root.Attributes[0].Value) != WorldXmlSerializer.XmlVersion) throw new Exception("Wrong version XML file"); data.Version = int.Parse(root.Attributes[0].Value); foreach (var main in root.Elements) { switch (main.Name.ToLower()) { case "gravity": { data.Gravity = ReadVector(main); } break; case "shapes": { foreach (var n in main.Elements) { if (n.Name.ToLower() != "shape") throw new Exception(); ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true); string name = ""; switch (type) { case ShapeType.Circle: { CircleShape shape = new CircleShape(); foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "radius": shape.Radius = float.Parse(sn.Value); break; case "position": shape.Position = ReadVector(sn); break; default: throw new Exception(); } } _shapes.Add(new ShapeSerialized(shape, name)); } break; case ShapeType.Polygon: { PolygonShape shape = new PolygonShape(); foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "vertices": { List<Vec2> verts = new List<Vec2>(); foreach (var vert in sn.Elements) verts.Add(ReadVector(vert)); shape.Vertices = verts.ToArray(); } break; case "centroid": shape.Centroid = ReadVector(sn); break; } } _shapes.Add(new ShapeSerialized(shape, name)); } break; } } } break; case "fixtures": { foreach (var n in main.Elements) { FixtureDef fixture = new FixtureDef(); if (n.Name.ToLower() != "fixture") throw new Exception(); string name = ""; int id = 0; foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "shape": id = int.Parse(sn.Value); break; case "density": fixture.Density = float.Parse(sn.Value); break; case "filterdata": fixture.Filter = (FilterData)ReadSimpleType(sn, typeof(FilterData), true); break; case "friction": fixture.Friction = float.Parse(sn.Value); break; case "issensor": fixture.IsSensor = bool.Parse(sn.Value); break; case "restitution": fixture.Restitution = float.Parse(sn.Value); break; case "userdata": fixture.UserData = ReadSimpleType(sn, null, false); break; } } fixture.Shape = _shapes[id].Shape; _fixtures.Add(new FixtureDefSerialized(fixture, id, name)); } } break; case "bodies": { foreach (var n in main.Elements) { BodyDef body = new BodyDef(); if (n.Name.ToLower() != "body") throw new Exception(); body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true); List<int> fixtures = new List<int>(); string name = ""; foreach (var sn in n.Elements) { switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "active": body.Active = bool.Parse(sn.Value); break; case "allowsleep": body.AllowSleep = bool.Parse(sn.Value); break; case "angle": body.Angle = float.Parse(sn.Value); break; case "angulardamping": body.AngularDamping = float.Parse(sn.Value); break; case "angularvelocity": body.AngularVelocity = float.Parse(sn.Value); break; case "awake": body.Awake = bool.Parse(sn.Value); break; case "bullet": body.Bullet = bool.Parse(sn.Value); break; case "fixedrotation": body.FixedRotation = bool.Parse(sn.Value); break; case "inertiascale": body.InertiaScale = float.Parse(sn.Value); break; case "lineardamping": body.LinearDamping = float.Parse(sn.Value); break; case "linearvelocity": body.LinearVelocity = ReadVector(sn); break; case "position": body.Position = ReadVector(sn); break; case "userdata": body.UserData = ReadSimpleType(sn, null, false); break; case "fixtures": { foreach (var v in sn.Elements) fixtures.Add(int.Parse(v.Value)); break; } } } _bodies.Add(new BodyDefSerialized(null, body, fixtures, name)); } } break; case "joints": { foreach (var n in main.Elements) { JointDef mainDef = null; if (n.Name.ToLower() != "joint") throw new Exception(); JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true); int bodyA = -1, bodyB = -1; bool collideConnected = false; object userData = null; string name = ""; switch (type) { case JointType.Distance: mainDef = new DistanceJointDef(); break; case JointType.Friction: mainDef = new FrictionJointDef(); break; case JointType.Line: mainDef = new LineJointDef(); break; case JointType.Prismatic: mainDef = new PrismaticJointDef(); break; case JointType.Pulley: mainDef = new PulleyJointDef(); break; case JointType.Revolute: mainDef = new RevoluteJointDef(); break; case JointType.Weld: mainDef = new WeldJointDef(); break; default: throw new Exception("Invalid or unsupported joint"); } foreach (var sn in n.Elements) { // check for specific nodes switch (type) { case JointType.Distance: { switch (sn.Name.ToLower()) { case "dampingratio": ((DistanceJointDef)mainDef).DampingRatio = float.Parse(sn.Value); break; case "frequencyhz": ((DistanceJointDef)mainDef).FrequencyHz = float.Parse(sn.Value); break; case "length": ((DistanceJointDef)mainDef).Length = float.Parse(sn.Value); break; case "localanchora": ((DistanceJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((DistanceJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Friction: { switch (sn.Name.ToLower()) { case "localanchora": ((FrictionJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((FrictionJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "maxforce": ((FrictionJointDef)mainDef).MaxForce = float.Parse(sn.Value); break; case "maxtorque": ((FrictionJointDef)mainDef).MaxTorque = float.Parse(sn.Value); break; } } break; case JointType.Line: { switch (sn.Name.ToLower()) { case "enablelimit": ((LineJointDef)mainDef).EnableLimit = bool.Parse(sn.Value); break; case "enablemotor": ((LineJointDef)mainDef).EnableMotor = bool.Parse(sn.Value); break; case "localanchora": ((LineJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((LineJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "localaxisa": ((LineJointDef)mainDef).LocalAxisA = ReadVector(sn); break; case "maxmotorforce": ((LineJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value); break; case "motorspeed": ((LineJointDef)mainDef).MotorSpeed = float.Parse(sn.Value); break; case "lowertranslation": ((LineJointDef)mainDef).LowerTranslation = float.Parse(sn.Value); break; case "uppertranslation": ((LineJointDef)mainDef).UpperTranslation = float.Parse(sn.Value); break; } } break; case JointType.Prismatic: { switch (sn.Name.ToLower()) { case "enablelimit": ((PrismaticJointDef)mainDef).EnableLimit = bool.Parse(sn.Value); break; case "enablemotor": ((PrismaticJointDef)mainDef).EnableMotor = bool.Parse(sn.Value); break; case "localanchora": ((PrismaticJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PrismaticJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "localaxisa": ((PrismaticJointDef)mainDef).LocalAxis = ReadVector(sn); break; case "maxmotorforce": ((PrismaticJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value); break; case "motorspeed": ((PrismaticJointDef)mainDef).MotorSpeed = float.Parse(sn.Value); break; case "lowertranslation": ((PrismaticJointDef)mainDef).LowerTranslation = float.Parse(sn.Value); break; case "uppertranslation": ((PrismaticJointDef)mainDef).UpperTranslation = float.Parse(sn.Value); break; case "referenceangle": ((PrismaticJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Pulley: { switch (sn.Name.ToLower()) { case "groundanchora": ((PulleyJointDef)mainDef).GroundAnchorA = ReadVector(sn); break; case "groundanchorb": ((PulleyJointDef)mainDef).GroundAnchorB = ReadVector(sn); break; case "lengtha": ((PulleyJointDef)mainDef).LengthA = float.Parse(sn.Value); break; case "lengthb": ((PulleyJointDef)mainDef).LengthB = float.Parse(sn.Value); break; case "localanchora": ((PulleyJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PulleyJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "maxlengtha": ((PulleyJointDef)mainDef).MaxLengthA = float.Parse(sn.Value); break; case "maxlengthb": ((PulleyJointDef)mainDef).MaxLengthB = float.Parse(sn.Value); break; case "ratio": ((PulleyJointDef)mainDef).Ratio = float.Parse(sn.Value); break; } } break; case JointType.Revolute: { switch (sn.Name.ToLower()) { case "enablelimit": ((RevoluteJointDef)mainDef).EnableLimit = bool.Parse(sn.Value); break; case "enablemotor": ((RevoluteJointDef)mainDef).EnableMotor = bool.Parse(sn.Value); break; case "localanchora": ((RevoluteJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((RevoluteJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; case "maxmotortorque": ((RevoluteJointDef)mainDef).MaxMotorTorque = float.Parse(sn.Value); break; case "motorspeed": ((RevoluteJointDef)mainDef).MotorSpeed = float.Parse(sn.Value); break; case "lowerangle": ((RevoluteJointDef)mainDef).LowerAngle = float.Parse(sn.Value); break; case "upperangle": ((RevoluteJointDef)mainDef).UpperAngle = float.Parse(sn.Value); break; case "referenceangle": ((RevoluteJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Weld: { switch (sn.Name.ToLower()) { case "localanchora": ((WeldJointDef)mainDef).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((WeldJointDef)mainDef).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Gear: throw new Exception("Gear joint is unsupported"); } switch (sn.Name.ToLower()) { case "name": name = sn.Value; break; case "bodya": bodyA = int.Parse(sn.Value); break; case "bodyb": bodyB = int.Parse(sn.Value); break; case "collideconnected": collideConnected = bool.Parse(sn.Value); break; case "userdata": userData = ReadSimpleType(sn, null, false); break; } } mainDef.CollideConnected = collideConnected; mainDef.UserData = userData; _joints.Add(new JointDefSerialized(mainDef, bodyA, bodyB, name)); } } break; } } return data; }
/// <summary> /// Resets the bike and the driver to their initial positions /// Resets all angular and linear speeds /// Recreates the joints if they were destroyed /// </summary> public void Reset() { foreach (var part in parts) { part.SetAngularVelocity(0); part.SetLinearVelocity(Vector2.Zero); } frontWheel.SetTransform(frontWheelInitPos / Level.FACTOR, 0); frontFork.SetTransform(frontForkInitPos / Level.FACTOR, 0); rearWheel.SetTransform(rearWheelInitPos / Level.FACTOR, 0); rearFork.SetTransform(rearForkInitPos / Level.FACTOR, rearForkInitRot * Level.DEG_TO_RAD); bikeBody.SetTransform(bikeBodyInitPos / Level.FACTOR, bikeBodyInitRot * Level.DEG_TO_RAD); head.SetTransform(headInitPos / Level.FACTOR, headInitRot * Level.DEG_TO_RAD); humanBody.SetTransform(humanBodyInitPos / Level.FACTOR, 0); hand.SetTransform(handInitPos / Level.FACTOR, handInitRot * Level.DEG_TO_RAD); arm.SetTransform(armInitPos / Level.FACTOR, armInitRot * Level.DEG_TO_RAD); if (OffTheBike) { RevoluteJointDef handToBikeDef = new RevoluteJointDef(); Vector2 anchor = hand.GetWorldPoint(new Vector2(17.06f / Level.FACTOR, 4.26f / Level.FACTOR)); handToBikeDef.Initialize(hand, bikeBody, anchor); handToBikeDef.maxMotorTorque = 300.0f; handToBike = world.CreateJoint(handToBikeDef); RevoluteJointDef humanToBikeDef = new RevoluteJointDef(); anchor = humanBody.GetWorldCenter(); anchor.Y += (30.0f / Level.FACTOR); humanToBikeDef.Initialize(humanBody, bikeBody, anchor); humanToBikeDef.maxMotorTorque = 300.0f; humanToBike = world.CreateJoint(humanToBikeDef); DistanceJointDef armToBikeDef = new DistanceJointDef(); armToBikeDef.Initialize(hand, bikeBody, hand.GetWorldPoint(new Vector2(-17.00f / Level.FACTOR, 4.26f / Level.FACTOR)), bikeBody.GetWorldCenter()); armToBikeDef.length = 40.0f / Level.FACTOR; armToBikeDef.frequencyHz = 10.0f; armToBikeDef.dampingRatio = 1.0f; armToBikeDef.collideConnected = true; armToBike = world.CreateJoint(armToBikeDef); OffTheBike = false; } if (!bikeBody.IsAwake()) { bikeBody.SetAwake(true); } if (RotationData.device) { motor._enableMotor = true; } }
void CreateLeg(float s, Vector2 wheelAnchor) { Vector2 p1 = new Vector2(5.4f * s, -6.1f); Vector2 p2 = new Vector2(7.2f * s, -1.2f); Vector2 p3 = new Vector2(4.3f * s, -1.9f); Vector2 p4 = new Vector2(3.1f * s, 0.8f); Vector2 p5 = new Vector2(6.0f * s, 1.5f); Vector2 p6 = new Vector2(2.5f * s, 3.7f); ShapeDef sp = new ShapeDef(1.0f); sp.groupIndex = -1; Vector2[] vertices1 = new Vector2[3]; Vector2[] vertices2 = new Vector2[3]; if (s > 0.0f) { vertices1[0] = p1; vertices1[1] = p2; vertices1[2] = p3; vertices2[0] = Vector2.zero; vertices2[1] = p5 - p4; vertices2[2] = p6 - p4; } else { vertices1[0] = p1; vertices1[1] = p3; vertices1[2] = p2; vertices2[0] = Vector2.zero; vertices2[1] = p6 - p4; vertices2[2] = p5 - p4; } BodyDef bd1 = new BodyDef(BodyType.DYNAMIC_BODY); BodyDef bd2 = new BodyDef(BodyType.DYNAMIC_BODY); bd1.position = m_offset; bd2.position = p4 + m_offset; bd1.angularDamping = 10.0f; bd2.angularDamping = 10.0f; var body1 = API.CreateBody(world, bd1); var body2 = API.CreateBody(world, bd2); API.AddPolygonShape(body1, vertices1, vertices1.Length, sp); API.AddPolygonShape(body2, vertices2, vertices2.Length, sp); DistanceJointDef djd = new DistanceJointDef(); djd.dampingRatio = 0.5f; djd.frequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + m_offset, p5 + m_offset); API.CreateDistanceJoint(world, djd); djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset); API.CreateDistanceJoint(world, djd); djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset); API.CreateDistanceJoint(world, djd); djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset); API.CreateDistanceJoint(world, djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, m_chassis, p4 + m_offset); API.CreateRevoluteJoint(world, rjd); }
public Dominos() { Body b1 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); BodyDef bd = new BodyDef(); b1 = _world.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.25f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-1.5f, 10.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.1f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-6.0f + 1.0f * i, 11.25f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f); BodyDef bd = new BodyDef(); bd.position = new Vector2(1.0f, 6.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } Body b2 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.5f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-7.0f, 4.0f); b2 = _world.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } Body b3 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.125f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-0.9f, 1.0f); bd.angle = -0.15f; b3 = _world.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } RevoluteJointDef jd = new RevoluteJointDef(); Vector2 anchor; anchor = new Vector2(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.collideConnected = true; _world.CreateJoint(jd); Body b4 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f, 15.0f); b4 = _world.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor = new Vector2(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); _world.CreateJoint(jd); Body b5 = null; { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(6.5f, 3.0f); b5 = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 10.0f; fd.friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor = new Vector2(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); _world.CreateJoint(jd); Body b6 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(6.5f, 4.1f); b6 = _world.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor = new Vector2(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); _world.CreateJoint(jd); Body b7 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(7.4f, 1.0f); b7 = _world.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } DistanceJointDef djd = new DistanceJointDef(); djd.bodyA = b3; djd.bodyB = b7; djd.localAnchorA = new Vector2(6.0f, 0.0f); djd.localAnchorB = new Vector2(0.0f, -1.0f); Vector2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA); djd.length = d.Length(); _world.CreateJoint(djd); { float radius = 0.2f; CircleShape shape = new CircleShape(); shape._radius = radius; for (int i = 0; i < 4; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(5.9f + 2.0f * radius * i, 2.4f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }
public void JointReactions() { var gravity = new Vector2(0, -10.0f); World world = new World(gravity); BodyDef bodyDef = new BodyDef(); Body ground = world.CreateBody(bodyDef); CircleShape circle = new CircleShape(); circle.Radius = 1.0f; FixtureDef fixtureDef = new FixtureDef(); // Disable collision fixtureDef.Filter.MaskBits = 0; fixtureDef.Density = 1.0f; fixtureDef.Shape = circle; bodyDef.BodyType = BodyType.DynamicBody; bodyDef.Position.Set(-2.0f, 3.0f); var bodyA = world.CreateBody(bodyDef); var bodyB = world.CreateBody(bodyDef); var bodyC = world.CreateBody(bodyDef); circle.ComputeMass(out var massData, fixtureDef.Density); var mg = massData.Mass * gravity.Y; bodyA.CreateFixture(fixtureDef); bodyB.CreateFixture(fixtureDef); bodyC.CreateFixture(fixtureDef); DistanceJointDef distanceJointDef = new DistanceJointDef(); distanceJointDef.Initialize(ground, bodyA, bodyDef.Position + new Vector2(0.0f, 4.0f), bodyDef.Position); distanceJointDef.MinLength = distanceJointDef.Length; distanceJointDef.MaxLength = distanceJointDef.Length; PrismaticJointDef prismaticJointDef = new PrismaticJointDef(); prismaticJointDef.Initialize(ground, bodyB, bodyDef.Position, new Vector2(1.0f, 0.0f)); RevoluteJointDef revoluteJointDef = new RevoluteJointDef(); revoluteJointDef.Initialize(ground, bodyC, bodyDef.Position); var distanceJoint = (DistanceJoint)world.CreateJoint(distanceJointDef); var prismaticJoint = (PrismaticJoint)world.CreateJoint(prismaticJointDef); var revoluteJoint = (RevoluteJoint)world.CreateJoint(revoluteJointDef); const float timeStep = 1 / 60f; const float invTimeStep = 60.0f; const int velocityIterations = 6; const int positionIterations = 2; world.Step(timeStep, velocityIterations, positionIterations); const float tol = 1e-5f; { var F = distanceJoint.GetReactionForce(invTimeStep); var T = distanceJoint.GetReactionTorque(invTimeStep); F.X.ShouldBe(0.0f); Math.Abs(F.Y + mg).ShouldBeLessThan(tol); T.ShouldBe(0.0f); } { var F = prismaticJoint.GetReactionForce(invTimeStep); var T = prismaticJoint.GetReactionTorque(invTimeStep); F.X.ShouldBe(0.0f); Math.Abs(F.Y + mg).ShouldBeLessThan(tol); T.ShouldBe(0.0f); } { var F = revoluteJoint.GetReactionForce(invTimeStep); var T = revoluteJoint.GetReactionTorque(invTimeStep); F.X.ShouldBe(0.0f); Math.Abs(F.Y + mg).ShouldBeLessThan(tol); T.ShouldBe(0.0f); } }
private DominosTest() { Body b1; { EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); BodyDef bd = new BodyDef(); b1 = BodyFactory.CreateFromDef(World, bd); b1.AddFixture(shape); } { PolygonShape shape = new PolygonShape(0.0f); shape.SetAsBox(6.0f, 0.25f); BodyDef bd = new BodyDef(); bd.Position = new Vector2(-1.5f, 10.0f); Body ground = BodyFactory.CreateFromDef(World, bd); ground.AddFixture(shape); } { PolygonShape shape = new PolygonShape(20.0f); shape.SetAsBox(0.1f, 1.0f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Friction = 0.1f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-6.0f + 1.0f * i, 11.25f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(fd); } } { PolygonShape shape = new PolygonShape(0.0f); shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f); BodyDef bd = new BodyDef(); bd.Position = new Vector2(1.0f, 6.0f); Body ground = BodyFactory.CreateFromDef(World, bd); ground.AddFixture(shape); } Body b2; { PolygonShape shape = new PolygonShape(0.02f); shape.SetAsBox(0.25f, 1.5f); BodyDef bd = new BodyDef(); bd.Position = new Vector2(-7.0f, 4.0f); b2 = BodyFactory.CreateFromDef(World, bd); b2.AddFixture(shape); } Body b3; { PolygonShape shape = new PolygonShape(10.0f); shape.SetAsBox(6.0f, 0.125f); BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-0.9f, 1.0f); bd.Angle = -0.15f; b3 = BodyFactory.CreateFromDef(World, bd); b3.AddFixture(shape); } RevoluteJointDef jd = new RevoluteJointDef(); Vector2 anchor; anchor = new Vector2(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.CollideConnected = true; JointFactory.CreateFromDef(World, jd); Body b4; { PolygonShape shape = new PolygonShape(10.0f); shape.SetAsBox(0.25f, 0.25f); BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-10.0f, 15.0f); b4 = BodyFactory.CreateFromDef(World, bd); b4.AddFixture(shape); } anchor = new Vector2(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); JointFactory.CreateFromDef(World, jd); Body b5; { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(6.5f, 3.0f); b5 = BodyFactory.CreateFromDef(World, bd); PolygonShape shape = new PolygonShape(10.0f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f); b5.AddFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f); b5.AddFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f); b5.AddFixture(fd); } anchor = new Vector2(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); JointFactory.CreateFromDef(World, jd); Body b6; { PolygonShape shape = new PolygonShape(30.0f); shape.SetAsBox(1.0f, 0.1f); BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(6.5f, 4.1f); b6 = BodyFactory.CreateFromDef(World, bd); b6.AddFixture(shape); } anchor = new Vector2(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); JointFactory.CreateFromDef(World, jd); Body b7; { PolygonShape shape = new PolygonShape(10.0f); shape.SetAsBox(0.1f, 1.0f); BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(7.4f, 1.0f); b7 = BodyFactory.CreateFromDef(World, bd); b7.AddFixture(shape); } DistanceJointDef djd = new DistanceJointDef(); djd.BodyA = b3; djd.BodyB = b7; djd.LocalAnchorA = new Vector2(6.0f, 0.0f); djd.LocalAnchorB = new Vector2(0.0f, -1.0f); Vector2 d = djd.BodyB.GetWorldPoint(djd.LocalAnchorB) - djd.BodyA.GetWorldPoint(djd.LocalAnchorA); djd.Length = d.Length(); JointHelper.LinearStiffness(1.0f, 1.0f, djd.BodyA, djd.BodyB, out float stiffness, out float damping); djd.Stiffness = stiffness; djd.Damping = damping; JointFactory.CreateFromDef(World, djd); { float radius = 0.2f; CircleShape shape = new CircleShape(10.0f); shape.Radius = radius; for (int i = 0; i < 4; ++i) { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(5.9f + 2.0f * radius * i, 2.4f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(shape); } } }
private WebTest() { Body ground; { BodyDef bd = new BodyDef(); ground = BodyFactory.CreateFromDef(World, bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.AddFixture(shape); } { PolygonShape shape = new PolygonShape(5.0f); shape.SetAsBox(0.5f, 0.5f); BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-5.0f, 5.0f); _bodies[0] = BodyFactory.CreateFromDef(World, bd); _bodies[0].AddFixture(shape); bd.Position = new Vector2(5.0f, 5.0f); _bodies[1] = BodyFactory.CreateFromDef(World, bd); _bodies[1].AddFixture(shape); bd.Position = new Vector2(5.0f, 15.0f); _bodies[2] = BodyFactory.CreateFromDef(World, bd); _bodies[2].AddFixture(shape); bd.Position = new Vector2(-5.0f, 15.0f); _bodies[3] = BodyFactory.CreateFromDef(World, bd); _bodies[3].AddFixture(shape); DistanceJointDef jd = new DistanceJointDef(); Vector2 p1, p2, d; float frequencyHz = 2.0f; float dampingRatio = 0.0f; jd.BodyA = ground; jd.BodyB = _bodies[0]; jd.LocalAnchorA = new Vector2(-10.0f, 0.0f); jd.LocalAnchorB = new Vector2(-0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[0] = JointFactory.CreateFromDef(World, jd); jd.BodyA = ground; jd.BodyB = _bodies[1]; jd.LocalAnchorA = new Vector2(10.0f, 0.0f); jd.LocalAnchorB = new Vector2(0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[1] = JointFactory.CreateFromDef(World, jd); jd.BodyA = ground; jd.BodyB = _bodies[2]; jd.LocalAnchorA = new Vector2(10.0f, 20.0f); jd.LocalAnchorB = new Vector2(0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[2] = JointFactory.CreateFromDef(World, jd); jd.BodyA = ground; jd.BodyB = _bodies[3]; jd.LocalAnchorA = new Vector2(-10.0f, 20.0f); jd.LocalAnchorB = new Vector2(-0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[3] = JointFactory.CreateFromDef(World, jd); jd.BodyA = _bodies[0]; jd.BodyB = _bodies[1]; jd.LocalAnchorA = new Vector2(0.5f, 0.0f); jd.LocalAnchorB = new Vector2(-0.5f, 0.0f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[4] = JointFactory.CreateFromDef(World, jd); jd.BodyA = _bodies[1]; jd.BodyB = _bodies[2]; jd.LocalAnchorA = new Vector2(0.0f, 0.5f); jd.LocalAnchorB = new Vector2(0.0f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[5] = JointFactory.CreateFromDef(World, jd); jd.BodyA = _bodies[2]; jd.BodyB = _bodies[3]; jd.LocalAnchorA = new Vector2(-0.5f, 0.0f); jd.LocalAnchorB = new Vector2(0.5f, 0.0f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[6] = JointFactory.CreateFromDef(World, jd); jd.BodyA = _bodies[3]; jd.BodyB = _bodies[0]; jd.LocalAnchorA = new Vector2(0.0f, -0.5f); jd.LocalAnchorB = new Vector2(0.0f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; _joints[7] = JointFactory.CreateFromDef(World, jd); } }
public Dominos() { Body b1; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); b1 = _world.CreateBody(bd); b1.CreateFixture(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(6.0f, 0.25f); BodyDef bd = new BodyDef(); bd.Position.Set(-1.5f, 10.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.1f, 1.0f); sd.Density = 20.0f; sd.Friction = 0.1f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(-6.0f + 1.0f * i, 11.25f); Body body = _world.CreateBody(bd); body.CreateFixture(sd); body.SetMassFromShapes(); } } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(7.0f, 0.25f, Vec2.Zero, 0.3f); BodyDef bd = new BodyDef(); bd.Position.Set(1.0f, 6.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(sd); } Body b2; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.25f, 1.5f); BodyDef bd = new BodyDef(); bd.Position.Set(-7.0f, 4.0f); b2 = _world.CreateBody(bd); b2.CreateFixture(sd); } Body b3; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(6.0f, 0.125f); sd.Density = 10.0f; BodyDef bd = new BodyDef(); bd.Position.Set(-0.9f, 1.0f); bd.Angle = -0.15f; b3 = _world.CreateBody(bd); b3.CreateFixture(sd); b3.SetMassFromShapes(); } RevoluteJointDef jd = new RevoluteJointDef(); Vec2 anchor = new Vec2(); anchor.Set(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.CollideConnected = true; _world.CreateJoint(jd); Body b4; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.25f, 0.25f); sd.Density = 10.0f; BodyDef bd = new BodyDef(); bd.Position.Set(-10.0f, 15.0f); b4 = _world.CreateBody(bd); b4.CreateFixture(sd); b4.SetMassFromShapes(); } anchor.Set(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); _world.CreateJoint(jd); Body b5; { BodyDef bd = new BodyDef(); bd.Position.Set(6.5f, 3.0f); b5 = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.Density = 10.0f; sd.Friction = 0.1f; sd.SetAsBox(1.0f, 0.1f, new Vec2(0.0f, -0.9f), 0.0f); b5.CreateFixture(sd); sd.SetAsBox(0.1f, 1.0f, new Vec2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(sd); sd.SetAsBox(0.1f, 1.0f, new Vec2(0.9f, 0.0f), 0.0f); b5.CreateFixture(sd); b5.SetMassFromShapes(); } anchor.Set(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); _world.CreateJoint(jd); Body b6; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(1.0f, 0.1f); sd.Density = 30.0f; sd.Friction = 0.2f; BodyDef bd = new BodyDef(); bd.Position.Set(6.5f, 4.1f); b6 = _world.CreateBody(bd); b6.CreateFixture(sd); b6.SetMassFromShapes(); } anchor.Set(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); _world.CreateJoint(jd); Body b7; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.1f, 1.0f); sd.Density = 10.0f; BodyDef bd = new BodyDef(); bd.Position.Set(7.4f, 1.0f); b7 = _world.CreateBody(bd); b7.CreateFixture(sd); b7.SetMassFromShapes(); } DistanceJointDef djd = new DistanceJointDef(); djd.Body1 = b3; djd.Body2 = b7; djd.LocalAnchor1.Set(6.0f, 0.0f); djd.LocalAnchor2.Set(0.0f, -1.0f); Vec2 d = djd.Body2.GetWorldPoint(djd.LocalAnchor2) - djd.Body1.GetWorldPoint(djd.LocalAnchor1); djd.Length = d.Length(); _world.CreateJoint(djd); { CircleDef sd = new CircleDef(); sd.Radius = 0.2f; sd.Density = 10.0f; for (int i = 0; i < 4; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(5.9f + 2.0f * sd.Radius * i, 2.4f); Body body = _world.CreateBody(bd); body.CreateFixture(sd); body.SetMassFromShapes(); } } }
public Web() { Body ground; { var bd = new BodyDef(); ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-5.0f, 5.0f); _bodies[0] = World.CreateBody(bd); _bodies[0].CreateFixture(shape, 5.0f); bd.Position.Set(5.0f, 5.0f); _bodies[1] = World.CreateBody(bd); _bodies[1].CreateFixture(shape, 5.0f); bd.Position.Set(5.0f, 15.0f); _bodies[2] = World.CreateBody(bd); _bodies[2].CreateFixture(shape, 5.0f); bd.Position.Set(-5.0f, 15.0f); _bodies[3] = World.CreateBody(bd); _bodies[3].CreateFixture(shape, 5.0f); var jd = new DistanceJointDef(); Vector2 p1; Vector2 p2; Vector2 d; jd.FrequencyHz = 2.0f; jd.DampingRatio = 0.0f; jd.BodyA = ground; jd.BodyB = _bodies[0]; jd.LocalAnchorA.Set(-10.0f, 0.0f); jd.LocalAnchorB.Set(-0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[0] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = _bodies[1]; jd.LocalAnchorA.Set(10.0f, 0.0f); jd.LocalAnchorB.Set(0.5f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[1] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = _bodies[2]; jd.LocalAnchorA.Set(10.0f, 20.0f); jd.LocalAnchorB.Set(0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[2] = World.CreateJoint(jd); jd.BodyA = ground; jd.BodyB = _bodies[3]; jd.LocalAnchorA.Set(-10.0f, 20.0f); jd.LocalAnchorB.Set(-0.5f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[3] = World.CreateJoint(jd); jd.BodyA = _bodies[0]; jd.BodyB = _bodies[1]; jd.LocalAnchorA.Set(0.5f, 0.0f); jd.LocalAnchorB.Set(-0.5f, 0.0f); ; p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[4] = World.CreateJoint(jd); jd.BodyA = _bodies[1]; jd.BodyB = _bodies[2]; jd.LocalAnchorA.Set(0.0f, 0.5f); jd.LocalAnchorB.Set(0.0f, -0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[5] = World.CreateJoint(jd); jd.BodyA = _bodies[2]; jd.BodyB = _bodies[3]; jd.LocalAnchorA.Set(-0.5f, 0.0f); jd.LocalAnchorB.Set(0.5f, 0.0f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[6] = World.CreateJoint(jd); jd.BodyA = _bodies[3]; jd.BodyB = _bodies[0]; jd.LocalAnchorA.Set(0.0f, -0.5f); jd.LocalAnchorB.Set(0.0f, 0.5f); p1 = jd.BodyA.GetWorldPoint(jd.LocalAnchorA); p2 = jd.BodyB.GetWorldPoint(jd.LocalAnchorB); d = p2 - p1; jd.Length = d.Length(); _joints[7] = World.CreateJoint(jd); } }
public static Joint AddJoint(this IJointable ithis, V2DJoint joint, float offsetX, float offsetY) { Joint result = null; JointDef jointDef = null; //Body targ0 = ithis.VScreen.bodyMap[joint.Body1]; //Body targ1 = ithis.VScreen.bodyMap[joint.Body2]; Body targ0 = GetBody(ithis, joint.Body1); Body targ1 = GetBody(ithis, joint.Body2); // gears need the first body static if (targ0 != null && targ1 != null && targ1.GetType() == BodyType.Static && targ0.GetType() != BodyType.Static) { Body temp = targ0; targ0 = targ1; targ1 = temp; } Vector2 pt0 = new Vector2(joint.X + offsetX, joint.Y + offsetY); string name = joint.Name; Vector2 anchor0 = new Vector2(pt0.X / V2DScreen.WorldScale, pt0.Y / V2DScreen.WorldScale); Vector2 anchor1 = new Vector2(); switch (joint.Type) { case V2DJointKind.Distance: Vector2 pt1 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY); anchor1 = new Vector2(pt1.X / V2DScreen.WorldScale, pt1.Y / V2DScreen.WorldScale); DistanceJointDef dj = new DistanceJointDef(); dj.Initialize(targ0, targ1, anchor0, anchor1); dj.collideConnected = joint.CollideConnected; dj.dampingRatio = joint.DampingRatio; dj.frequencyHz = joint.FrequencyHz; if (joint.Length != -1) { dj.length = joint.Length / V2DScreen.WorldScale; } jointDef = dj; break; case V2DJointKind.Revolute: float rot0 = joint.Min; //(typeof(joint["min"]) == "string") ? parseFloat(joint["min"]) / 180 * Math.PI : joint["min"]; float rot1 = joint.Max; //(typeof(joint["max"]) == "string") ? parseFloat(joint["max"]) / 180 * Math.PI : joint["max"]; RevoluteJointDef rj = new RevoluteJointDef(); rj.Initialize(targ0, targ1, anchor0); rj.lowerAngle = rot0; rj.upperAngle = rot1; rj.enableLimit = rot0 != 0 && rot1 != 0; rj.maxMotorTorque = joint.MaxMotorTorque; rj.motorSpeed = joint.MotorSpeed; rj.enableMotor = joint.EnableMotor; jointDef = rj; break; case V2DJointKind.Prismatic: float axisX = joint.AxisX; float axisY = joint.AxisY; float min = joint.Min; float max = joint.Max; PrismaticJointDef pj = new PrismaticJointDef(); Vector2 worldAxis = new Vector2(axisX, axisY); pj.Initialize(targ0, targ1, anchor0, worldAxis); pj.lowerTranslation = min / V2DScreen.WorldScale; pj.upperTranslation = max / V2DScreen.WorldScale; pj.enableLimit = joint.EnableLimit; pj.maxMotorForce = joint.MaxMotorTorque; pj.motorSpeed = joint.MotorSpeed; pj.enableMotor = joint.EnableMotor; jointDef = pj; break; case V2DJointKind.Pully: Vector2 pt2 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY); anchor1 = new Vector2(pt2.X / V2DScreen.WorldScale, pt2.Y / V2DScreen.WorldScale); Vector2 groundAnchor0 = new Vector2(joint.GroundAnchor1X / V2DScreen.WorldScale, joint.GroundAnchor1Y / V2DScreen.WorldScale); Vector2 groundAnchor1 = new Vector2(joint.GroundAnchor2X / V2DScreen.WorldScale, joint.GroundAnchor2Y / V2DScreen.WorldScale); float max0 = joint.MaxLength1; float max1 = joint.MaxLength2; float rat = joint.Ratio; PulleyJointDef puj = new PulleyJointDef(); puj.Initialize(targ0, targ1, groundAnchor0, groundAnchor1, anchor0, anchor1, rat); puj.maxLengthA = (max0 + max1) / V2DScreen.WorldScale; puj.maxLengthB = (max0 + max1) / V2DScreen.WorldScale; puj.collideConnected = joint.CollideConnected; jointDef = puj; break; case V2DJointKind.Gear: GearJointDef gj = new GearJointDef(); gj.bodyA = targ0; gj.bodyB = targ1; gj.joint1 = GetFirstGearableJoint(targ0.GetJointList()); gj.joint2 = GetFirstGearableJoint(targ1.GetJointList()); gj.ratio = joint.Ratio; jointDef = gj; break; } if (jointDef != null) { result = SetJointWithReflection(ithis, name, jointDef); if (result != null) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict["name"] = name; result.SetUserData(dict); } } return(result); }
public Web() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.5f); sd.Density = 5.0f; sd.Friction = 0.2f; BodyDef bd = new BodyDef(); bd.Position.Set(-5.0f, 5.0f); _bodies[0] = _world.CreateBody(bd); _bodies[0].CreateShape(sd); _bodies[0].SetMassFromShapes(); bd.Position.Set(5.0f, 5.0f); _bodies[1] = _world.CreateBody(bd); _bodies[1].CreateShape(sd); _bodies[1].SetMassFromShapes(); bd.Position.Set(5.0f, 15.0f); _bodies[2] = _world.CreateBody(bd); _bodies[2].CreateShape(sd); _bodies[2].SetMassFromShapes(); bd.Position.Set(-5.0f, 15.0f); _bodies[3] = _world.CreateBody(bd); _bodies[3].CreateShape(sd); _bodies[3].SetMassFromShapes(); DistanceJointDef jd = new DistanceJointDef(); Vec2 p1, p2, d; jd.FrequencyHz = 4.0f; jd.DampingRatio = 0.5f; jd.Body1 = ground; jd.Body2 = _bodies[0]; jd.LocalAnchor1.Set(-10.0f, 10.0f); jd.LocalAnchor2.Set(-0.5f, -0.5f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[0] = _world.CreateJoint(jd); jd.Body1 = ground; jd.Body2 = _bodies[1]; jd.LocalAnchor1.Set(10.0f, 10.0f); jd.LocalAnchor2.Set(0.5f, -0.5f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[1] = _world.CreateJoint(jd); jd.Body1 = ground; jd.Body2 = _bodies[2]; jd.LocalAnchor1.Set(10.0f, 30.0f); jd.LocalAnchor2.Set(0.5f, 0.5f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[2] = _world.CreateJoint(jd); jd.Body1 = ground; jd.Body2 = _bodies[3]; jd.LocalAnchor1.Set(-10.0f, 30.0f); jd.LocalAnchor2.Set(-0.5f, 0.5f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[3] = _world.CreateJoint(jd); jd.Body1 = _bodies[0]; jd.Body2 = _bodies[1]; jd.LocalAnchor1.Set(0.5f, 0.0f); jd.LocalAnchor2.Set(-0.5f, 0.0f);; p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[4] = _world.CreateJoint(jd); jd.Body1 = _bodies[1]; jd.Body2 = _bodies[2]; jd.LocalAnchor1.Set(0.0f, 0.5f); jd.LocalAnchor2.Set(0.0f, -0.5f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[5] = _world.CreateJoint(jd); jd.Body1 = _bodies[2]; jd.Body2 = _bodies[3]; jd.LocalAnchor1.Set(-0.5f, 0.0f); jd.LocalAnchor2.Set(0.5f, 0.0f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[6] = _world.CreateJoint(jd); jd.Body1 = _bodies[3]; jd.Body2 = _bodies[0]; jd.LocalAnchor1.Set(0.0f, -0.5f); jd.LocalAnchor2.Set(0.0f, 0.5f); p1 = jd.Body1.GetWorldPoint(jd.LocalAnchor1); p2 = jd.Body2.GetWorldPoint(jd.LocalAnchor2); d = p2 - p1; jd.Length = d.Length(); _joints[7] = _world.CreateJoint(jd); } }
public Dominos() { Body b1; { var shape = new EdgeShape(); shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); var bd = new BodyDef(); b1 = World.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.25f); var bd = new BodyDef(); bd.Position.Set(-1.5f, 10.0f); var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; fd.Friction = 0.1f; for (var i = 0; i < 10; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-6.0f + 1.0f * i, 11.25f); var body = World.CreateBody(bd); body.CreateFixture(fd); } } { var shape = new PolygonShape(); shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f); var bd = new BodyDef(); bd.Position.Set(1.0f, 6.0f); var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } Body b2; { var shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.5f); var bd = new BodyDef(); bd.Position.Set(-7.0f, 4.0f); b2 = World.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } Body b3; { var shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.125f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-0.9f, 1.0f); bd.Angle = -0.15f; b3 = World.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } var jd = new RevoluteJointDef(); var anchor = new Vector2(); anchor.Set(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.CollideConnected = true; World.CreateJoint(jd); Body b4; { var shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-10.0f, 15.0f); b4 = World.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor.Set(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); World.CreateJoint(jd); Body b5; { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(6.5f, 3.0f); b5 = World.CreateBody(bd); var shape = new PolygonShape(); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 10.0f; fd.Friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor.Set(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); World.CreateJoint(jd); Body b6; { var shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.1f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(6.5f, 4.1f); b6 = World.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor.Set(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); World.CreateJoint(jd); Body b7; { var shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(7.4f, 1.0f); b7 = World.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } var djd = new DistanceJointDef(); djd.BodyA = b3; djd.BodyB = b7; djd.LocalAnchorA.Set(6.0f, 0.0f); djd.LocalAnchorB.Set(0.0f, -1.0f); var d = djd.BodyB.GetWorldPoint(djd.LocalAnchorB) - djd.BodyA.GetWorldPoint(djd.LocalAnchorA); djd.Length = d.Length(); World.CreateJoint(djd); { var radius = 0.2f; var shape = new CircleShape(); shape.Radius = radius; for (var i = 0; i < 4; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f); var body = World.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }
void CreateLeg(double s, Vector2 wheelAnchor) { Vector2 p1 = new Vector2(5.4f * s, -6.1f); Vector2 p2 = new Vector2(7.2f * s, -1.2f); Vector2 p3 = new Vector2(4.3f * s, -1.9f); Vector2 p4 = new Vector2(3.1f * s, 0.8f); Vector2 p5 = new Vector2(6.0f * s, 1.5f); Vector2 p6 = new Vector2(2.5f * s, 3.7f); FixtureDef fd1 = new FixtureDef(); FixtureDef fd2 = new FixtureDef(); fd1.filter.groupIndex = -1; fd2.filter.groupIndex = -1; fd1.density = 1.0f; fd2.density = 1.0f; PolygonShape poly1 = new PolygonShape(); PolygonShape poly2 = new PolygonShape(); Vector2[] vertices = new Vector2[3]; if (s > 0.0f) { vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices, 3); } else { vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices, 3); } fd1.shape = poly1; fd2.shape = poly2; BodyDef bd1 = new BodyDef(); BodyDef bd2 = new BodyDef(); bd1.type = BodyType.Dynamic; bd2.type = BodyType.Dynamic; bd1.position = _offset; bd2.position = p4 + _offset; bd1.angularDamping = 10.0f; bd2.angularDamping = 10.0f; Body body1 = _world.CreateBody(bd1); Body body2 = _world.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); DistanceJointDef djd = new DistanceJointDef(); // Using a soft distanceraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. djd.dampingRatio = 0.5f; djd.frequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + _offset, p5 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + _offset, p4 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, _chassis, p4 + _offset); _world.CreateJoint(rjd); }
public void CreateLeg(float s, Vec2 wheelAnchor) { Vec2 p1 = new Vec2(5.4f * s, -6.1f); Vec2 p2 = new Vec2(7.2f * s, -1.2f); Vec2 p3 = new Vec2(4.3f * s, -1.9f); Vec2 p4 = new Vec2(3.1f * s, 0.8f); Vec2 p5 = new Vec2(6.0f * s, 1.5f); Vec2 p6 = new Vec2(2.5f * s, 3.7f); PolygonDef sd1 = new PolygonDef(), sd2 = new PolygonDef(); sd1.VertexCount = 3; sd2.VertexCount = 3; sd1.Filter.GroupIndex = -1; sd2.Filter.GroupIndex = -1; sd1.Density = 1.0f; sd2.Density = 1.0f; if (s > 0.0f) { sd1.Vertices[0] = p1; sd1.Vertices[1] = p2; sd1.Vertices[2] = p3; sd2.Vertices[0] = Vec2.Zero; sd2.Vertices[1] = p5 - p4; sd2.Vertices[2] = p6 - p4; } else { sd1.Vertices[0] = p1; sd1.Vertices[1] = p3; sd1.Vertices[2] = p2; sd2.Vertices[0] = Vec2.Zero; sd2.Vertices[1] = p6 - p4; sd2.Vertices[2] = p5 - p4; } BodyDef bd1 = new BodyDef(), bd2 = new BodyDef(); bd1.Position = _offset; bd2.Position = p4 + _offset; bd1.AngularDamping = 10.0f; bd2.AngularDamping = 10.0f; Body body1 = _world.CreateBody(bd1); Body body2 = _world.CreateBody(bd2); body1.CreateShape(sd1); body2.CreateShape(sd2); body1.SetMassFromShapes(); body2.SetMassFromShapes(); DistanceJointDef djd = new DistanceJointDef(); // Using a soft distance constraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. djd.DampingRatio = 0.5f; djd.FrequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + _offset, p5 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + _offset, p4 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, _chassis, p4 + _offset); _world.CreateJoint(rjd); }
/// <summary> /// Creates a new motorcycle and a driver into the given Box2D world. /// Creates all the parts of the motorcycle and driver and joints them together. /// </summary> /// <param name="pBikeSpeed">A pointer to the variable that describes the speed of the /// motorcycle</param> /// <param name="pRotationData">RotationData to provide the information of the rotation /// of the device</param> /// <param name="pWorld">The Box2D world where the bike is created into</param> /// <param name="pCamPos">A pointer to the variable that describes the position of the /// camera</param> /// <param name="pContent">The used ContentManager instance</param> /// <param name="pSpriteBatch">The used SpriteBatch instance</param> public Bike(float [] pBikeSpeed, RotationData pRotationData, World pWorld, float[] pCamPos, ContentManager pContent) { OffTheBike = false; camPos = pCamPos; world = pWorld; content = pContent; RotationData = pRotationData; bikeSpeed = pBikeSpeed; frontWheel = CreateCirclePart("wheel", frontWheelInitPos, 35.0f, 0, 0.1f, 0.9f, 0.2f); frontFork = CreateBoxPart("susp_lower_long", frontForkInitPos, 20.53f, 21.33f, 0, 0.8f, 1.0f, 0.2f); rearWheel = CreateCirclePart("rearWheel", rearWheelInitPos, 32.0f, 0, 0.4f, 1.0f, 0.2f); rearFork = CreateBoxPart("rearFork", rearForkInitPos, 64.0f, 17.0f, rearForkInitRot, 0.5f, 1.0f, 0.2f); bikeBody = CreateBikeBody(bikeBodyInitPos, bikeBodyInitRot, 0.5f, 1.0f, 0.2f); RevoluteJointDef motorDef = new RevoluteJointDef(); motorDef.Initialize(rearWheel, rearFork, rearWheel.GetWorldCenter()); motorDef.maxMotorTorque = 2.0f; motorDef.enableMotor = true; motor = (RevoluteJoint)world.CreateJoint(motorDef); RevoluteJointDef rearForkBodyDef = new RevoluteJointDef(); Vector2 anchor = rearFork.GetWorldCenter(); anchor.X += (32.0f / Level.FACTOR); anchor.Y += (13.5f / Level.FACTOR); rearForkBodyDef.Initialize(rearFork, bikeBody, anchor); rearForkBodyDef.bodyA = rearFork; rearForkBodyDef.bodyB = bikeBody; rearForkBodyDef.maxMotorTorque = 300.0f; world.CreateJoint(rearForkBodyDef); RevoluteJointDef frontWheelJointDef = new RevoluteJointDef(); frontWheelJointDef.Initialize(frontWheel, frontFork, frontWheel.GetWorldCenter()); frontWheelJointDef.maxMotorTorque = 300.0f; world.CreateJoint(frontWheelJointDef); DistanceJointDef frontSuspToBikeDef = new DistanceJointDef(); frontSuspToBikeDef.Initialize(bikeBody, frontFork, frontFork.GetWorldCenter() + new Vector2(0, 0.4f), frontFork.GetWorldCenter()); frontSuspToBikeDef.frequencyHz = 4.0f; frontSuspToBikeDef.dampingRatio = 0.1f; frontSuspToBikeDef.collideConnected = true; world.CreateJoint(frontSuspToBikeDef); DistanceJointDef rearForkDistanceDef = new DistanceJointDef(); rearForkDistanceDef.Initialize(bikeBody, rearFork, rearFork.GetWorldCenter() + new Vector2(0, 0.4f), rearFork.GetWorldCenter()); rearForkDistanceDef.frequencyHz = 7.0f; rearForkDistanceDef.dampingRatio = 0.1f; rearForkDistanceDef.collideConnected = true; world.CreateJoint(rearForkDistanceDef); PrismaticJointDef fSuspBikePrismaticDef = new PrismaticJointDef(); fSuspBikePrismaticDef.Initialize(bikeBody, frontFork, bikeBody.GetWorldCenter(), new Vector2(0, 1)); fSuspBikePrismaticDef.enableLimit = true; fSuspBikePrismaticDef.lowerTranslation = -0.2f; fSuspBikePrismaticDef.upperTranslation = 0.2f; fSuspBikePrismaticDef.collideConnected = true; world.CreateJoint(fSuspBikePrismaticDef); humanBody = CreateBoxPart("human", humanBodyInitPos, 17.0f, 64.0f, 0, 0.1f, 1.0f, 0.2f); head = CreateBoxPart("head", headInitPos, 38.4f, 29.9f, headInitRot, 0.1f, 1.0f, 0.2f); hand = CreateBoxPart("hand", handInitPos, 34.13f, 8.53f, handInitRot, 0.1f, 1.0f, 0.2f); arm = CreateBoxPart("arm", armInitPos, 42.67f, 8.53f, armInitRot, 0.1f, 1.0f, 0.2f); WeldJointDef headToHumanDef = new WeldJointDef(); headToHumanDef.Initialize(head, humanBody, head.GetWorldCenter()); world.CreateJoint(headToHumanDef); RevoluteJointDef humanToBikeDef = new RevoluteJointDef(); anchor = humanBody.GetWorldCenter(); anchor.Y += (30.0f / Level.FACTOR); humanToBikeDef.Initialize(humanBody, bikeBody, anchor); humanToBikeDef.maxMotorTorque = 300.0f; humanToBike = world.CreateJoint(humanToBikeDef); RevoluteJointDef humanToArmDef = new RevoluteJointDef(); anchor = arm.GetWorldPoint(new Vector2(-21.33f / Level.FACTOR, 4.26f / Level.FACTOR)); humanToArmDef.Initialize(humanBody, arm, anchor); humanToArmDef.maxMotorTorque = 300.0f; world.CreateJoint(humanToArmDef); RevoluteJointDef armToHandDef = new RevoluteJointDef(); anchor = arm.GetWorldPoint(new Vector2(21.33f / Level.FACTOR, 4.26f / Level.FACTOR)); armToHandDef.Initialize(arm, hand, anchor); armToHandDef.maxMotorTorque = 300.0f; world.CreateJoint(armToHandDef); RevoluteJointDef handToBikeDef = new RevoluteJointDef(); anchor = hand.GetWorldPoint(new Vector2(17.06f / Level.FACTOR, 4.26f / Level.FACTOR)); handToBikeDef.Initialize(hand, bikeBody, anchor); handToBikeDef.maxMotorTorque = 300.0f; handToBike = world.CreateJoint(handToBikeDef); DistanceJointDef armToBikeDef = new DistanceJointDef(); armToBikeDef.Initialize(hand, bikeBody, hand.GetWorldPoint(new Vector2(-17.00f / Level.FACTOR, 4.26f / Level.FACTOR)), bikeBody.GetWorldCenter()); armToBikeDef.length = 40.0f / Level.FACTOR; armToBikeDef.frequencyHz = 10.0f; armToBikeDef.dampingRatio = 1.0f; armToBikeDef.collideConnected = true; armToBike = world.CreateJoint(armToBikeDef); }