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> /// 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); }
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); } }
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 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); } }
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); }
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); }
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); }
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); FixtureDef fd1 = new FixtureDef(), fd2 = new FixtureDef(); fd1.Filter.GroupIndex = fd2.Filter.GroupIndex = -1; fd1.Density = 1.0f; fd2.Density = 1.0f; PolygonShape poly1 = new PolygonShape(), poly2 = new PolygonShape(); if (s > 0.0f) { Vec2[] vertices = new Vec2[3]; vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Vertices = vertices; vertices[0] = Vec2.Empty; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Vertices = vertices; } else { Vec2[] vertices = new Vec2[3]; vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Vertices = vertices; vertices[0] = Vec2.Empty; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Vertices = vertices; } fd1.Shape = poly1; fd2.Shape = poly2; BodyDef bd1 = new BodyDef(), bd2 = new BodyDef(); bd1.BodyType = BodyType.Dynamic; bd2.BodyType = BodyType.Dynamic; bd1.Position = m_offset; bd2.Position = p4 + m_offset; bd1.AngularDamping = 10.0f; bd2.AngularDamping = 10.0f; Body body1 = m_world.CreateBody(bd1); Body body2 = m_world.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); 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 + m_offset, p5 + m_offset); m_world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset); m_world.CreateJoint(djd); djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset); m_world.CreateJoint(djd); djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset); m_world.CreateJoint(djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, m_chassis, p4 + m_offset); m_world.CreateJoint(rjd); }
private 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(), fd2 = new FixtureDef(); fd1.Filter.Group = -1; fd2.Filter.Group = -1; PolygonShape poly1 = new PolygonShape(1.0f), poly2 = new PolygonShape(1.0f); if (s > 0.0f) { Vertices vertices = new Vertices(3); vertices.Add(p1); vertices.Add(p2); vertices.Add(p3); poly1.Vertices = vertices; vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Vertices = vertices; } else { Vertices vertices = new Vertices(3); vertices.Add(p1); vertices.Add(p3); vertices.Add(p2); poly1.Vertices = vertices; vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Vertices = vertices; } fd1.Shape = poly1; fd2.Shape = poly2; BodyDef bd1 = new 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 = BodyFactory.CreateFromDef(World, bd1); Body body2 = BodyFactory.CreateFromDef(World, bd2); body1.AddFixture(fd1); body2.AddFixture(fd2); { DistanceJointDef jd = 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. float dampingRatio = 0.5f; float frequencyHz = 10.0f; jd.Initialize(body1, body2, p2 + _offset, p5 + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); jd.Initialize(body1, body2, p3 + _offset, p4 + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); jd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); jd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); } { RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(body2, _chassis, p4 + _offset); JointFactory.CreateFromDef(World, jd); } }
/// <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 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); }
// Main... public ElasticBody() { // Bottom static body { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 2.0f); sd.Friction = 0.1f; sd.Restitution = 0.1f; BodyDef bd = new BodyDef(); bd.Position.Set(-1.0f, -7.5f); _ground = _world.CreateBody(bd); _ground.CreateFixture(sd); } // Upper static body { PolygonDef sd = new PolygonDef(); sd.SetAsBox(20.0f, 0.50f, new Vec2(0.0f, 0.0f), 0.047f * Box2DNet.Common.Settings.Pi); sd.Friction = 0.01f; sd.Restitution = 0.001f; BodyDef bd = new BodyDef(); bd.Position.Set(-20.0f, 93.0f); Body g = _world.CreateBody(bd); g.CreateFixture(sd); sd.SetAsBox(15.0f, 0.50f, new Vec2(-15.0f, 12.5f), 0.0f); g.CreateFixture(sd); sd.SetAsBox(20.0f, 0.5f, new Vec2(0.0f, -25.0f), -0.5f); g.CreateFixture(sd); } // Left channel left wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.7f, 55.0f); sd.Friction = 0.1f; sd.Restitution = 0.1f; BodyDef bd = new BodyDef(); bd.Position.Set(-49.3f, 50.0f); Body g = _world.CreateBody(bd); g.CreateFixture(sd); } // Right wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.7f, 55.0f); sd.Friction = 0.1f; sd.Restitution = 0.1f; BodyDef bd = new BodyDef(); bd.Position.Set(45.0f, 50.0f); Body g = _world.CreateBody(bd); g.CreateFixture(sd); } // Left channel right upper wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 20.0f); sd.Friction = 0.05f; sd.Restitution = 0.01f; BodyDef bd = new BodyDef(); bd.Position.Set(-42.0f, 70.0f); bd.Angle = -0.03f * Box2DNet.Common.Settings.Pi; Body g = _world.CreateBody(bd); g.CreateFixture(sd); } // Left channel right lower wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.50f, 23.0f); sd.Friction = 0.05f; sd.Restitution = 0.01f; BodyDef bd = new BodyDef(); bd.Position.Set(-44.0f, 27.0f); Body g = _world.CreateBody(bd); g.CreateFixture(sd); // Bottom motors CircleDef cd = new CircleDef(); cd.Radius = 3.0f; cd.Density = 15.0f; cd.Friction = 1.0f; cd.Restitution = 0.2f; // 1. bd.Position.Set(-40.0f, 2.5f); Body body = _world.CreateBody(bd); body.CreateFixture(cd); body.SetMassFromShapes(); RevoluteJointDef jr = new RevoluteJointDef(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); jr.MaxMotorTorque = 30000.0f; jr.EnableMotor = true; jr.MotorSpeed = 20.0f; _world.CreateJoint(jr); // 1. left down bd.Position.Set(-46.0f, -2.5f); cd.Radius = 1.5f; jr.MotorSpeed = -20.0f; body = _world.CreateBody(bd); body.CreateFixture(cd); sd.SetAsBox(2.0f, 0.50f); body.CreateFixture(sd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter()); _world.CreateJoint(jr); // 2. cd.Radius = 3.0f; jr.MotorSpeed = 20.0f; bd.Position.Set(-32.0f, 2.5f); body = _world.CreateBody(bd); body.CreateFixture(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 3. jr.MotorSpeed = 20.0f; bd.Position.Set(-24.0f, 1.5f); body = _world.CreateBody(bd); body.CreateFixture(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 4. bd.Position.Set(-16.0f, 0.8f); body = _world.CreateBody(bd); body.CreateFixture(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 5. bd.Position.Set(-8.0f, 0.5f); body = _world.CreateBody(bd); body.CreateFixture(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 6. bd.Position.Set(0.0f, 0.1f); body = _world.CreateBody(bd); body.CreateFixture(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 7. bd.Position.Set(8.0f, -0.5f); body = _world.CreateBody(bd); body.CreateFixture(cd); sd.SetAsBox(3.7f, 0.5f); body.CreateFixture(sd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 8. right rotator sd.SetAsBox(5.0f, 0.5f); sd.Density = 2.0f; bd.Position.Set(18.0f, 1.0f); Body rightmotor = _world.CreateBody(bd); rightmotor.CreateFixture(sd); sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DNet.Common.Settings.Pi / 3.0f); rightmotor.CreateFixture(sd); sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DNet.Common.Settings.Pi * 2.0f / 3.0f); rightmotor.CreateFixture(sd); cd.Radius = 4.2f; rightmotor.CreateFixture(cd); rightmotor.SetMassFromShapes(); jr.Initialize(g, rightmotor, rightmotor.GetWorldCenter()); jr.MaxMotorTorque = 70000.0f; jr.MotorSpeed = -4.0f; _world.CreateJoint(jr); // 9. left rotator sd.SetAsBox(8.5f, 0.5f); sd.Density = 2.0f; bd.Position.Set(-34.0f, 17.0f); body = _world.CreateBody(bd); body.CreateFixture(sd); sd.SetAsBox(8.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DNet.Common.Settings.Pi * .5f); body.CreateFixture(sd); cd.Radius = 7.0f; cd.Friction = 0.9f; body.CreateFixture(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter()); jr.MaxMotorTorque = 100000.0f; jr.MotorSpeed = -5.0f; _world.CreateJoint(jr); // big compressor sd.SetAsBox(3.0f, 4.0f); sd.Density = 10.0f; bd.Position.Set(-16.0f, 17.0f); Body hammerleft = _world.CreateBody(bd); hammerleft.CreateFixture(sd); hammerleft.SetMassFromShapes(); DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(body, hammerleft, body.GetWorldCenter() + new Vec2(0.0f, 6.0f), hammerleft.GetWorldCenter()); _world.CreateJoint(jd); bd.Position.Set(4.0f, 17.0f); Body hammerright = _world.CreateBody(bd); hammerright.CreateFixture(sd); hammerright.SetMassFromShapes(); jd.Initialize(body, hammerright, body.GetWorldCenter() - new Vec2(0.0f, 6.0f), hammerright.GetWorldCenter()); _world.CreateJoint(jd); // pusher sd.SetAsBox(6.0f, 0.75f); bd.Position.Set(-21.0f, 9.0f); Body pusher = _world.CreateBody(bd); pusher.CreateFixture(sd); sd.SetAsBox(2.0f, 1.5f, new Vec2(-5.0f, 0.0f), 0.0f); pusher.SetMassFromShapes(); pusher.CreateFixture(sd); jd.Initialize(rightmotor, pusher, rightmotor.GetWorldCenter() + new Vec2(-8.0f, 0.0f), pusher.GetWorldCenter() + new Vec2(5.0f, 0.0f)); _world.CreateJoint(jd); } // Static bodies above motors { PolygonDef sd = new PolygonDef(); CircleDef cd = new CircleDef(); sd.SetAsBox(9.0f, 0.5f); sd.Friction = 0.05f; sd.Restitution = 0.01f; BodyDef bd = new BodyDef(); bd.Position.Set(-15.5f, 12.0f); bd.Angle = 0.0f; Body g = _world.CreateBody(bd); g.CreateFixture(sd); sd.SetAsBox(8.0f, 0.5f, new Vec2(23.0f, 0.0f), 0.0f); g.CreateFixture(sd); // compressor statics sd.SetAsBox(7.0f, 0.5f, new Vec2(-2.0f, 9.0f), 0.0f); g.CreateFixture(sd); sd.SetAsBox(9.0f, 0.5f, new Vec2(22.0f, 9.0f), 0.0f); g.CreateFixture(sd); sd.SetAsBox(19.0f, 0.5f, new Vec2(-9.0f, 15.0f), -0.05f); g.CreateFixture(sd); sd.SetAsBox(4.7f, 0.5f, new Vec2(15.0f, 11.5f), -0.5f); g.CreateFixture(sd); // below compressor sd.SetAsBox(26.0f, 0.3f, new Vec2(17.0f, -4.4f), -0.02f); g.CreateFixture(sd); cd.Radius = 1.0f; cd.Friction = 1.0f; cd.LocalPosition = new Vec2(29.0f, -6.0f); g.CreateFixture(cd); cd.Radius = 0.7f; cd.LocalPosition = new Vec2(-2.0f, -4.5f); g.CreateFixture(cd); } // Elevator { BodyDef bd = new BodyDef(); CircleDef cd = new CircleDef(); PolygonDef sd = new PolygonDef(); bd.Position.Set(40.0f, 4.0f); _elev = _world.CreateBody(bd); sd.SetAsBox(0.5f, 2.5f, new Vec2(3.0f, -3.0f), 0.0f); sd.Density = 1.0f; sd.Friction = 0.01f; _elev.CreateFixture(sd); sd.SetAsBox(7.0f, 0.5f, new Vec2(-3.5f, -5.5f), 0.0f); _elev.CreateFixture(sd); sd.SetAsBox(0.5f, 2.5f, new Vec2(-11.0f, -3.5f), 0.0f); _elev.CreateFixture(sd); _elev.SetMassFromShapes(); PrismaticJointDef jp = new PrismaticJointDef(); jp.Initialize(_ground, _elev, bd.Position, new Vec2(0.0f, 1.0f)); jp.LowerTranslation = 0.0f; jp.UpperTranslation = 100.0f; jp.EnableLimit = true; jp.EnableMotor = true; jp.MaxMotorForce = 10000.0f; jp.MotorSpeed = 0.0f; _joint_elev = (PrismaticJoint)_world.CreateJoint(jp); // Korb sd.SetAsBox(2.3f, 0.5f, new Vec2(1.0f, 0.0f), 0.0f); sd.Density = 0.5f; bd.Position.Set(29.0f, 6.5f); Body body = _world.CreateBody(bd); body.CreateFixture(sd); sd.SetAsBox(2.5f, 0.5f, new Vec2(3.0f, -2.0f), Box2DNet.Common.Settings.Pi / 2.0f); body.CreateFixture(sd); sd.SetAsBox(4.6f, 0.5f, new Vec2(7.8f, -4.0f), 0.0f); body.CreateFixture(sd); sd.SetAsBox(0.5f, 4.5f, new Vec2(12.0f, 0.0f), 0.0f); body.CreateFixture(sd); sd.SetAsBox(0.5f, 0.5f, new Vec2(13.0f, 4.0f), 0.0f); body.CreateFixture(sd); cd.Radius = 0.7f; cd.Density = 1.0f; cd.Friction = 0.01f; cd.LocalPosition = new Vec2(0.0f, 0.0f); body.CreateFixture(cd); body.SetMassFromShapes(); RevoluteJointDef jr = new RevoluteJointDef(); jr.Initialize(_elev, body, bd.Position); jr.EnableLimit = true; jr.LowerAngle = -0.2f; jr.UpperAngle = Box2DNet.Common.Settings.Pi * 1.1f; jr.CollideConnected = true; _world.CreateJoint(jr); // upper body exit sd.SetAsBox(14.0f, 0.5f, new Vec2(-3.5f, -10.0f), 0.0f); bd.Position.Set(17.5f, 96.0f); body = _world.CreateBody(bd); body.CreateFixture(sd); } // "Elastic body" 64 bodies - something like a lin. elastic compound // connected via dynamic forces (springs) { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.55f, 0.55f); sd.Density = 1.5f; sd.Friction = 0.01f; sd.Filter.GroupIndex = -1; Vec2 startpoint = new Vec2(30.0f, 20.0f); BodyDef bd = new BodyDef(); bd.IsBullet = false; bd.AllowSleep = false; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { bd.Position.Set(j * 1.02f, 2.51f + 1.02f * i); bd.Position += startpoint; Body body = _world.CreateBody(bd); bodies[8 * i + j] = body; body.CreateFixture(sd); body.SetMassFromShapes(); } } } }
/// <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); }
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); }