//--------------- Internals Below ------------------- static public b2Joint Create(b2JointDef def, object allocator) { b2Joint joint = null; if (def.type == e_distanceJoint) { //void* mem = allocator->Allocate(sizeof(b2DistanceJoint)); joint = new b2DistanceJoint(def as b2DistanceJointDef); } else if (def.type == e_mouseJoint) { //void* mem = allocator->Allocate(sizeof(b2MouseJoint)); joint = new b2MouseJoint(def as b2MouseJointDef); } else if (def.type == e_prismaticJoint) { //void* mem = allocator->Allocate(sizeof(b2PrismaticJoint)); joint = new b2PrismaticJoint(def as b2PrismaticJointDef); } else if (def.type == e_revoluteJoint) { //void* mem = allocator->Allocate(sizeof(b2RevoluteJoint)); joint = new b2RevoluteJoint(def as b2RevoluteJointDef); } else if (def.type == e_pulleyJoint) { //void* mem = allocator->Allocate(sizeof(b2PulleyJoint)); joint = new b2PulleyJoint(def as b2PulleyJointDef); } else if (def.type == e_gearJoint) { //void* mem = allocator->Allocate(sizeof(b2GearJoint)); joint = new b2GearJoint(def as b2GearJointDef); } else if (def.type == e_lineJoint) { //void* mem = allocator->Allocate(sizeof(b2LineJoint)); joint = new b2LineJoint(def as b2LineJointDef); } else if (def.type == e_weldJoint) { //void* mem = allocator->Allocate(sizeof(b2WeldJoint)); joint = new b2WeldJoint(def as b2WeldJointDef); } else if (def.type == e_frictionJoint) { //void* mem = allocator->Allocate(sizeof(b2FrictionJoint)); joint = new b2FrictionJoint(def as b2FrictionJointDef); } else if (def.type == e_ropeJoint) { joint = new b2RopeJoint(def as b2RopeJointDef); } else { //b2Settings.b2Assert(false); } return(joint); }
public b2GearJoint(b2GearJointDef def) : base(def) { m_joint1 = def.joint1; m_joint2 = def.joint2; m_typeA = m_joint1.GetJointType(); m_typeB = m_joint2.GetJointType(); Debug.Assert(m_typeA == b2JointType.e_revoluteJoint || m_typeA == b2JointType.e_prismaticJoint); Debug.Assert(m_typeB == b2JointType.e_revoluteJoint || m_typeB == b2JointType.e_prismaticJoint); float coordinateA, coordinateB; // TODO_ERIN there might be some problem with the joint edges in b2Joint. m_bodyC = m_joint1.GetBodyA(); m_bodyA = m_joint1.GetBodyB(); // Get geometry of joint1 b2Transform xfA = m_bodyA.XF; float aA = m_bodyA.Sweep.a; b2Transform xfC = m_bodyC.XF; float aC = m_bodyC.Sweep.a; if (m_typeA == b2JointType.e_revoluteJoint) { b2RevoluteJoint revolute = (b2RevoluteJoint)def.joint1; m_localAnchorC = revolute.GetLocalAnchorA(); m_localAnchorA = revolute.GetLocalAnchorB(); m_referenceAngleA = revolute.GetReferenceAngle(); m_localAxisC.SetZero(); coordinateA = aA - aC - m_referenceAngleA; } else { b2PrismaticJoint prismatic = (b2PrismaticJoint)def.joint1; m_localAnchorC = prismatic.GetLocalAnchorA(); m_localAnchorA = prismatic.GetLocalAnchorB(); m_referenceAngleA = prismatic.GetReferenceAngle(); m_localAxisC = prismatic.GetLocalXAxisA(); b2Vec2 pC = m_localAnchorC; b2Vec2 pA = b2Math.b2MulT(xfC.q, b2Math.b2Mul(xfA.q, m_localAnchorA) + (xfA.p - xfC.p)); coordinateA = b2Math.b2Dot(pA - pC, m_localAxisC); } m_bodyD = m_joint2.GetBodyA(); m_bodyB = m_joint2.GetBodyB(); // Get geometry of joint2 b2Transform xfB = m_bodyB.XF; float aB = m_bodyB.Sweep.a; b2Transform xfD = m_bodyD.XF; float aD = m_bodyD.Sweep.a; if (m_typeB == b2JointType.e_revoluteJoint) { b2RevoluteJoint revolute = (b2RevoluteJoint)def.joint2; m_localAnchorD = revolute.GetLocalAnchorA(); m_localAnchorB = revolute.GetLocalAnchorB(); m_referenceAngleB = revolute.GetReferenceAngle(); m_localAxisD.SetZero(); coordinateB = aB - aD - m_referenceAngleB; } else { b2PrismaticJoint prismatic = (b2PrismaticJoint)def.joint2; m_localAnchorD = prismatic.GetLocalAnchorA(); m_localAnchorB = prismatic.GetLocalAnchorB(); m_referenceAngleB = prismatic.GetReferenceAngle(); m_localAxisD = prismatic.GetLocalXAxisA(); b2Vec2 pD = m_localAnchorD; b2Vec2 pB = b2Math.b2MulT(xfD.q, b2Math.b2Mul(xfB.q, m_localAnchorB) + (xfB.p - xfD.p)); coordinateB = b2Math.b2Dot(pB - pD, m_localAxisD); } m_ratio = def.ratio; m_constant = coordinateA + m_ratio * coordinateB; m_impulse = 0.0f; }
public static b2Joint Create(b2JointDef def) { b2Joint joint = null; switch (def.JointType) { case b2JointType.e_distanceJoint: { joint = new b2DistanceJoint((b2DistanceJointDef)def); } break; case b2JointType.e_mouseJoint: { joint = new b2MouseJoint((b2MouseJointDef)def); } break; case b2JointType.e_prismaticJoint: { joint = new b2PrismaticJoint((b2PrismaticJointDef)def); } break; case b2JointType.e_revoluteJoint: { joint = new b2RevoluteJoint((b2RevoluteJointDef)def); } break; case b2JointType.e_pulleyJoint: { joint = new b2PulleyJoint((b2PulleyJointDef)def); } break; case b2JointType.e_gearJoint: { joint = new b2GearJoint((b2GearJointDef)def); } break; case b2JointType.e_wheelJoint: { joint = new b2WheelJoint((b2WheelJointDef)def); } break; case b2JointType.e_weldJoint: { joint = new b2WeldJoint((b2WeldJointDef)def); } break; case b2JointType.e_frictionJoint: { joint = new b2FrictionJoint((b2FrictionJointDef)def); } break; case b2JointType.e_ropeJoint: { joint = new b2RopeJoint((b2RopeJointDef)def); } break; default: Debug.Assert(false); break; } return joint; }
public static b2Joint Create(b2JointDef def) { b2Joint joint = null; switch (def.JointType) { case b2JointType.e_distanceJoint: { joint = new b2DistanceJoint((b2DistanceJointDef)def); } break; case b2JointType.e_mouseJoint: { joint = new b2MouseJoint((b2MouseJointDef)def); } break; case b2JointType.e_prismaticJoint: { joint = new b2PrismaticJoint((b2PrismaticJointDef)def); } break; case b2JointType.e_revoluteJoint: { joint = new b2RevoluteJoint((b2RevoluteJointDef)def); } break; case b2JointType.e_pulleyJoint: { joint = new b2PulleyJoint((b2PulleyJointDef)def); } break; case b2JointType.e_gearJoint: { joint = new b2GearJoint((b2GearJointDef)def); } break; case b2JointType.e_wheelJoint: { joint = new b2WheelJoint((b2WheelJointDef)def); } break; case b2JointType.e_weldJoint: { joint = new b2WeldJoint((b2WeldJointDef)def); } break; case b2JointType.e_frictionJoint: { joint = new b2FrictionJoint((b2FrictionJointDef)def); } break; case b2JointType.e_ropeJoint: { joint = new b2RopeJoint((b2RopeJointDef)def); } break; default: Debug.Assert(false); break; } return(joint); }
public CircleStress() { b2Body leftWall = null; b2Body rightWall = null; { // Ground b2PolygonShape sd = new b2PolygonShape(); sd.SetAsBox(50.0f, 10.0f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_staticBody; bd.position = new b2Vec2(0.0f, -10.0f); b2Body b = m_world.CreateBody(bd); b2FixtureDef fd = new b2FixtureDef(); fd.shape = sd; fd.friction = 1.0f; b.CreateFixture(fd); // Walls sd.SetAsBox(3.0f, 50.0f); bd = new b2BodyDef(); bd.position = new b2Vec2(45.0f, 25.0f); rightWall = m_world.CreateBody(bd); rightWall.CreateFixture(sd, 0); bd.position = new b2Vec2(-45.0f, 25.0f); leftWall = m_world.CreateBody(bd); leftWall.CreateFixture(sd, 0); // Corners bd = new b2BodyDef(); sd.SetAsBox(20.0f, 3.0f); bd.angle = (float)(-Math.PI / 4.0); bd.position = new b2Vec2(-35f, 8.0f); b2Body myBod = m_world.CreateBody(bd); myBod.CreateFixture(sd, 0); bd.angle = (float)(Math.PI / 4.0); bd.position = new b2Vec2(35f, 8.0f); myBod = m_world.CreateBody(bd); myBod.CreateFixture(sd, 0); // top sd.SetAsBox(50.0f, 10.0f); bd.type = b2BodyType.b2_staticBody; bd.angle = 0; bd.position = new b2Vec2(0.0f, 75.0f); b = m_world.CreateBody(bd); fd.shape = sd; fd.friction = 1.0f; b.CreateFixture(fd); } { b2CircleShape cd; b2FixtureDef fd = new b2FixtureDef(); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; int numPieces = 5; float radius = 6f; bd.position = new b2Vec2(0.0f, 10.0f); b2Body body = m_world.CreateBody(bd); for (int i = 0; i < numPieces; i++) { cd = new b2CircleShape(); cd.Radius = 1.2f; fd.shape = cd; fd.density = 25; fd.friction = .1f; fd.restitution = .9f; float xPos = radius * (float) Math.Cos(2f * Math.PI * (i / (float) (numPieces))); float yPos = radius * (float) Math.Sin(2f * Math.PI * (i / (float) (numPieces))); cd.Position.Set(xPos, yPos); body.CreateFixture(fd); } body.SetBullet(false); b2RevoluteJointDef rjd = new b2RevoluteJointDef(); rjd.Initialize(body, m_groundBody, body.Position); rjd.motorSpeed = (float) Math.PI; rjd.maxMotorTorque = 1000000.0f; rjd.enableMotor = true; joint = (b2RevoluteJoint) m_world.CreateJoint(rjd); } { int loadSize = 41; for (int j = 0; j < 15; j++) { for (int i = 0; i < loadSize; i++) { b2CircleShape circ = new b2CircleShape(); b2BodyDef bod = new b2BodyDef(); bod.type = b2BodyType.b2_dynamicBody; circ.Radius = 1.0f + (i % 2 == 0 ? 1.0f : -1.0f) * .5f * Rand.RandomFloat(.5f, 1f); b2FixtureDef fd2 = new b2FixtureDef(); fd2.shape = circ; fd2.density = circ.Radius * 1.5f; fd2.friction = 0.5f; fd2.restitution = 0.7f; float xPos = -39f + 2 * i; float yPos = 50f + j; bod.position = new b2Vec2(xPos, yPos); b2Body myBody = m_world.CreateBody(bod); myBody.CreateFixture(fd2); } } } m_world.Gravity = new b2Vec2(0, -50); }