本文整理汇总了C#中Box2D.Dynamics.b2World.CreateJoint方法的典型用法代码示例。如果您正苦于以下问题:C# b2World.CreateJoint方法的具体用法?C# b2World.CreateJoint怎么用?C# b2World.CreateJoint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Box2D.Dynamics.b2World
的用法示例。
在下文中一共展示了b2World.CreateJoint方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Wheel
public Wheel(
b2World b2world,
double x,
double y,
double width,
double length,
bool revolving,
bool powered
)
{
this.revolving = revolving;
this.powered = powered;
this.Initialize = car =>
{
/*
wheel object
pars:
car - car this wheel belongs to
x - horizontal position in meters relative to car's center
y - vertical position in meters relative to car's center
width - width in meters
length - length in meters
revolving - does this wheel revolve when steering?
powered - is this wheel powered?
*/
var position = new double[] { x, y };
//this.car=pars.car;
//this.revolving=pars.revolving;
//this.powered=pars.powered;
//initialize body
var def = new b2BodyDef();
def.type = b2Body.b2_dynamicBody;
def.position = car.body.GetWorldPoint(new b2Vec2(position[0], position[1]));
def.angle = car.body.GetAngle();
this.body = b2world.CreateBody(def);
//initialize shape
var fixdef = new b2FixtureDef();
fixdef.density = 1;
fixdef.isSensor = true; //wheel does not participate in collision calculations: resulting complications are unnecessary
var fixdef_shape = new b2PolygonShape();
fixdef.shape = fixdef_shape;
fixdef_shape.SetAsBox(width / 2, length / 2);
body.CreateFixture(fixdef);
var jointdef = new b2RevoluteJointDef();
//create joint to connect wheel to body
if (revolving)
{
jointdef.Initialize(car.body, body, body.GetWorldCenter());
jointdef.enableMotor = false; //we'll be controlling the wheel's angle manually
}
else
{
jointdef.Initialize(car.body, body, body.GetWorldCenter()
//, new b2Vec2(1, 0)
);
jointdef.enableLimit = true;
//jointdef.lowerTranslation = 0;
//jointdef.upperTranslation = 0;
}
b2world.CreateJoint(jointdef);
#region setAngle
this.setAngle =
(angle) =>
{
/*
angle - wheel angle relative to car, in degrees
*/
body.SetAngle(car.body.GetAngle() + angle.DegreesToRadians());
};
#endregion
#region getLocalVelocity
Func<double[]> getLocalVelocity = delegate
{
/*returns get velocity vector relative to car
*/
var res = car.body.GetLocalVector(car.body.GetLinearVelocityFromLocalPoint(new b2Vec2(position[0], position[1])));
return new double[] { res.x, res.y };
};
#endregion
#region getDirectionVector
//.........这里部分代码省略.........
示例2: j2b2Joint
//.........这里部分代码省略.........
//joint = gearDef = JointFactory.CreateGearJoint(world, joint1, joint2, ratio);
}
// Wheel joints are apparently not implemented in JBox2D yet, but
// when they are, commenting out the following section should work.
else if (type == "wheel")
{
jointDef = revoluteDef = new b2RevoluteJointDef();
revoluteDef.localAnchorA = jsonToVec("anchorA", jointValue);
revoluteDef.localAnchorB = jsonToVec("anchorB", jointValue);
revoluteDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];
revoluteDef.motorSpeed = jsonToFloat("motorSpeed", jointValue);
revoluteDef.maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);
//jointDef = wheelDef = new b2WheelJointDef(); //JointFactory.CreateWheelJoint(world, bodyA, bodyB, localAnchorB, localAxisA);
//var localAnchorA = jsonToVec("anchorA", jointValue);
//var localAnchorB = (jsonToVec("anchorB", jointValue));
//var localAxisA = (jsonToVec("localAxisA", jointValue));
//var enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];
//var motorSpeed = jsonToFloat("motorSpeed", jointValue);
//var maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);
//var frequencyHz = jsonToFloat("springFrequency", jointValue);
//var dampingRatio = jsonToFloat("springDampingRatio", jointValue);
//wheelDef.LocalAnchorA = localAnchorA;
//wheelDef.LocalAnchorB = localAnchorB;
//wheelDef.MotorEnabled = enableMotor;
//wheelDef.MotorSpeed = motorSpeed;
//wheelDef.SpringFrequencyHz = frequencyHz;
//wheelDef.MaxMotorTorque = maxMotorTorque;
//wheelDef.SpringDampingRatio = dampingRatio;
}
else if (type == "weld")
{
jointDef = weldDef = new b2WeldJointDef();
weldDef.localAnchorA = jsonToVec("anchorA", jointValue);
weldDef.localAnchorB = jsonToVec("anchorB", jointValue);
weldDef.referenceAngle = 0;
}
else if (type == "friction")
{
jointDef = frictionDef = new b2FrictionJointDef();
frictionDef.localAnchorA = jsonToVec("anchorA", jointValue);
frictionDef.localAnchorB = jsonToVec("anchorB", jointValue);
frictionDef.maxForce = jsonToFloat("maxForce", jointValue);
frictionDef.maxTorque = jsonToFloat("maxTorque", jointValue);
}
else if (type == "rope")
{
jointDef = ropeDef = new b2RopeJointDef();
ropeDef.localAnchorA = jsonToVec("anchorA", jointValue);
ropeDef.localAnchorB = jsonToVec("anchorB", jointValue);
ropeDef.maxLength = jsonToFloat("maxLength", jointValue);
}
//else if (type == "motor")
//{
// var maxForce = jsonToFloat("maxForce", jointValue);
// var maxMotorTorque = jsonToFloat("maxTorque", jointValue);
// var angularOffset = jsonToFloat("refAngle", jointValue);
// joint = motorDef = new MotorJoint(bodyA, bodyB);
// world.AddJoint(joint);
// motorDef.LinearOffset = jsonToVec("anchorA", jointValue);
// motorDef.MaxForce = maxForce;
// motorDef.MaxTorque = maxMotorTorque;
// motorDef.AngularOffset = angularOffset;
//}
if (null != jointDef)
{
// set features common to all joints
jointDef.BodyA = m_bodies[bodyIndexA];
jointDef.BodyB = m_bodies[bodyIndexB];
jointDef.CollideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"];
joint = world.CreateJoint(jointDef);
if (type.Equals("mouse"))
((b2MouseJoint)joint).SetTarget(mouseJointTarget);
String jointName = jointValue["name"] == null ? "" : (string)jointValue["name"];
if (!jointName.Equals(""))
{
SetJointName(joint, jointName);
}
}
return joint;
}