本文整理汇总了C#中Box2D.Dynamics.b2World.CreateBody方法的典型用法代码示例。如果您正苦于以下问题:C# b2World.CreateBody方法的具体用法?C# b2World.CreateBody怎么用?C# b2World.CreateBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Box2D.Dynamics.b2World
的用法示例。
在下文中一共展示了b2World.CreateBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MouseTouch
public MouseTouch(b2World world, RubeBasicLayer layer)
{
parent = layer;
m_world = world;
b2BodyDef bodyDef = new b2BodyDef();
m_groundBody = m_world.CreateBody(bodyDef);
}
示例2: JumpPad
public JumpPad(b2Vec2 JumpImpuls, CCPoint Position, b2World gameWorld)
{
this.Texture = new CCTexture2D ("jumppad");
this.Scale = SpriteScale;
this.Position = Position;
this.IsAntialiased = false;
jumpImpuls = JumpImpuls;
totalJumps = 0;
//box2d
b2BodyDef jumpPadDef = new b2BodyDef ();
jumpPadDef.type = b2BodyType.b2_kinematicBody;
jumpPadDef.position = new b2Vec2 ((Position.X + this.ScaledContentSize.Width/2)/PhysicsHandler.pixelPerMeter, (Position.Y + this.ScaledContentSize.Height/4) / PhysicsHandler.pixelPerMeter);
JumpPadBody = gameWorld.CreateBody (jumpPadDef);
b2PolygonShape jumpPadShape = new b2PolygonShape ();
jumpPadShape.SetAsBox ((float)this.ScaledContentSize.Width / PhysicsHandler.pixelPerMeter / 2, (float)this.ScaledContentSize.Height / PhysicsHandler.pixelPerMeter / 4);// /4 weil die hitbox nur die hälfte der textur ist
b2FixtureDef jumpPadFixture = new b2FixtureDef ();
jumpPadFixture.shape = jumpPadShape;
jumpPadFixture.density = 0.0f; //Dichte
jumpPadFixture.restitution = 0f; //Rückprall
jumpPadFixture.friction = 0f;
jumpPadFixture.userData = WorldFixtureData.jumppad;
JumpPadBody.CreateFixture (jumpPadFixture);
//
}
示例3: InitPhysics
void InitPhysics()
{
var gravity = new b2Vec2(0.0f, -10.0f);
world = new b2World(gravity);
world.SetAllowSleeping(true);
world.SetContinuousPhysics(true);
var def = new b2BodyDef();
def.allowSleep = true;
def.position = b2Vec2.Zero;
def.type = b2BodyType.b2_staticBody;
b2Body groundBody = world.CreateBody(def);
groundBody.SetActive(true);
b2EdgeShape groundBox = new b2EdgeShape();
groundBox.Set(b2Vec2.Zero, new b2Vec2(900, 100));
b2FixtureDef fd = new b2FixtureDef();
fd.friction = 0.3f;
fd.restitution = 0.1f;
fd.shape = groundBox;
groundBody.CreateFixture(fd);
}
示例4: CreateBodyWithSpriteAndFixture
protected void CreateBodyWithSpriteAndFixture(b2World world, b2BodyDef bodyDef,
b2FixtureDef fixtureDef, string spriteName)
{
// this is the meat of our class, it creates (OR recreates) the body in the world with the body definition, fixture definition and sprite name
RemoveBody(); //if remove the body if it already exists
RemoveSprite(); //if remove the sprite if it already exists
sprite = new CCSprite(spriteName);
AddChild(sprite);
body = world.CreateBody(bodyDef);
body.UserData = this;
if (fixtureDef != null)
body.CreateFixture(fixtureDef);
}
示例5: SetJson
public void SetJson(string fullpath)
{
Console.WriteLine("Full path is: %s", fullpath);
Nb2dJson json = new Nb2dJson();
StringBuilder tmp = new StringBuilder();
m_world = json.ReadFromFile(fullpath, tmp);
if (m_world != null)
{
Console.WriteLine("Loaded JSON ok");
m_world.SetDebugDraw(m_debugDraw);
b2BodyDef bodyDef = new b2BodyDef();
m_groundBody = m_world.CreateBody(bodyDef);
}
else
Console.WriteLine(tmp); //if this warning bothers you, turn off "Typecheck calls to printf/scanf" in the project build settings
}
示例6: BoxProp
public BoxProp(
b2World b2world,
double[] size,
double[] position
)
{
/*
static rectangle shaped prop
pars:
size - array [width, height]
position - array [x, y], in world meters, of center
*/
this.size = size;
//initialize body
var bdef = new b2BodyDef();
bdef.position = new b2Vec2(position[0], position[1]);
bdef.angle = 0;
bdef.fixedRotation = true;
this.body = b2world.CreateBody(bdef);
//initialize shape
var fixdef = new b2FixtureDef();
var shape = new b2PolygonShape();
fixdef.shape = shape;
shape.SetAsBox(this.size[0] / 2, this.size[1] / 2);
fixdef.restitution = 0.4; //positively bouncy!
this.body.CreateFixture(fixdef);
}
示例7: ApplicationSprite
// http://blog.allanbishop.com/box2d-2-1a-tutorial-part-1/
public ApplicationSprite()
{
_world = new b2World(new b2Vec2(0, 10), true);
var groundBodyDef = new b2BodyDef();
groundBodyDef.position.Set(SWF_HALF_WIDTH / PIXELS_TO_METRE,
SWF_HEIGHT / PIXELS_TO_METRE - 20 / PIXELS_TO_METRE);
var groundBody = _world.CreateBody(groundBodyDef);
var groundBox = new b2PolygonShape();
groundBox.SetAsBox(SWF_HALF_WIDTH / PIXELS_TO_METRE,
20 / PIXELS_TO_METRE);
var groundFixtureDef = new b2FixtureDef();
groundFixtureDef.shape = groundBox;
groundFixtureDef.density = 1;
groundFixtureDef.friction = 1;
groundBody.CreateFixture(groundFixtureDef);
var bodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(SWF_HALF_WIDTH / PIXELS_TO_METRE, 4);
var body = _world.CreateBody(bodyDef);
var dynamicBox = new b2PolygonShape();
dynamicBox.SetAsBox(1, 1);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = dynamicBox;
fixtureDef.density = 1;
fixtureDef.friction = 0.3;
body.CreateFixture(fixtureDef);
var debugSprite = new Sprite();
addChild(debugSprite);
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(PIXELS_TO_METRE);
debugDraw.SetLineThickness(1.0);
debugDraw.SetAlpha(1);
debugDraw.SetFillAlpha(0.4);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
_world.SetDebugDraw(debugDraw);
// Add event for main loop
this.stage.enterFrame +=
delegate
{
var timeStep = 1 / 30.0;
var velocityIterations = 6;
var positionIterations = 2;
_world.Step(timeStep, velocityIterations, positionIterations);
_world.ClearForces();
_world.DrawDebugData();
};
}
示例8: Test
public Test()
{
m_destructionListener = new DestructionListener();
m_debugDraw = new CCBox2dDraw("fonts/arial-12", 1);
b2Vec2 gravity = new b2Vec2();
gravity.Set(0.0f, -10.0f);
m_world = new b2World(gravity);
m_bomb = null;
m_textLine = 30;
m_mouseJoint = null;
m_pointCount = 0;
m_destructionListener.test = this;
m_world.SetDestructionListener(m_destructionListener);
m_world.SetContactListener(this);
m_world.SetDebugDraw(m_debugDraw);
m_world.SetContinuousPhysics(true);
m_world.SetWarmStarting(true);
m_bombSpawning = false;
m_stepCount = 0;
b2BodyDef bodyDef = new b2BodyDef();
m_groundBody = m_world.CreateBody(bodyDef);
}
示例9: InitPhysics
void InitPhysics()
{
CCSize s = Layer.VisibleBoundsWorldspace.Size;
var gravity = new b2Vec2 (0.0f, -10.0f);
world = new b2World (gravity);
world.SetAllowSleeping (true);
world.SetContinuousPhysics (true);
var def = new b2BodyDef ();
def.allowSleep = true;
def.position = b2Vec2.Zero;
def.type = b2BodyType.b2_staticBody;
b2Body groundBody = world.CreateBody (def);
groundBody.SetActive (true);
b2EdgeShape groundBox = new b2EdgeShape ();
groundBox.Set (b2Vec2.Zero, new b2Vec2 (s.Width / PTM_RATIO, 0));
b2FixtureDef fd = new b2FixtureDef ();
fd.shape = groundBox;
groundBody.CreateFixture (fd);
}
示例10: initPhysics
private void initPhysics()
{
CCSize s = CCDirector.SharedDirector.WinSize;
var gravity = new b2Vec2(0.0f, -10.0f);
_world = new b2World(gravity);
float debugWidth = s.Width / PTM_RATIO * 2f;
float debugHeight = s.Height / PTM_RATIO * 2f;
CCDraw debugDraw = new CCDraw(new b2Vec2(debugWidth / 2f + 10, s.Height - debugHeight - 10), 2);
debugDraw.AppendFlags(b2DrawFlags.e_shapeBit);
_world.SetDebugDraw(debugDraw);
_world.SetAllowSleeping(true);
_world.SetContinuousPhysics(true);
//m_debugDraw = new GLESDebugDraw( PTM_RATIO );
//world->SetDebugDraw(m_debugDraw);
//uint32 flags = 0;
//flags += b2Draw::e_shapeBit;
// flags += b2Draw::e_jointBit;
// flags += b2Draw::e_aabbBit;
// flags += b2Draw::e_pairBit;
// flags += b2Draw::e_centerOfMassBit;
//m_debugDraw->SetFlags(flags);
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2BodyDef def = b2BodyDef.Create();
def.allowSleep = true;
def.position = b2Vec2.Zero;
def.type = b2BodyType.b2_staticBody;
b2Body groundBody = _world.CreateBody(def);
groundBody.SetActive(true);
// Define the ground box shape.
// bottom
b2EdgeShape groundBox = new b2EdgeShape();
groundBox.Set(b2Vec2.Zero, new b2Vec2(s.Width / PTM_RATIO, 0));
b2FixtureDef fd = b2FixtureDef.Create();
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// top
groundBox = new b2EdgeShape();
groundBox.Set(new b2Vec2(0, s.Height / PTM_RATIO), new b2Vec2(s.Width / PTM_RATIO, s.Height / PTM_RATIO));
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// left
groundBox = new b2EdgeShape();
groundBox.Set(new b2Vec2(0, s.Height / PTM_RATIO), b2Vec2.Zero);
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// right
groundBox = new b2EdgeShape();
groundBox.Set(new b2Vec2(s.Width / PTM_RATIO, s.Height / PTM_RATIO), new b2Vec2(s.Width / PTM_RATIO, 0));
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// _world.Dump();
}
示例11: createPhysicsBody
public b2Body createPhysicsBody(b2World bodyWorld)
{
//definiert den physikalischen körper des Männchens
if (characterBody == null) {
b2BodyDef characterDef = new b2BodyDef ();
characterDef.type = b2BodyType.b2_dynamicBody;
characterDef.position = new b2Vec2 (CharacterPosition.X / PhysicsHandler.pixelPerMeter, CharacterPosition.Y / PhysicsHandler.pixelPerMeter);
characterBody = bodyWorld.CreateBody (characterDef);
b2PolygonShape characterShape = new b2PolygonShape ();
characterShape.SetAsBox ((float)CharacterSize.Width / PhysicsHandler.pixelPerMeter / 2, (float)CharacterSize.Height / PhysicsHandler.pixelPerMeter / 2);
b2FixtureDef characterFixture = new b2FixtureDef ();
characterFixture.shape = characterShape;
characterFixture.density = 0.0f; //Dichte
characterFixture.friction = 0f;
characterFixture.restitution = 0f; //Rückprall
characterBody.CreateFixture (characterFixture);
b2PolygonShape characterGroundSensorShape = new b2PolygonShape ();
b2Vec2 GroundSensorPosition = new b2Vec2 (0f, -0.29f * CharacterSprite.ScaleY);
characterGroundSensorShape.SetAsBox (((float)CharacterSize.Width - 1f) / PhysicsHandler.pixelPerMeter / 2, 5f / PhysicsHandler.pixelPerMeter, GroundSensorPosition, 0f);
//untergrundsensor
b2FixtureDef groundSensor = new b2FixtureDef ();
groundSensor.isSensor = true;
groundSensor.userData = WorldFixtureData.playergroundsensor;
groundSensor.shape = characterGroundSensorShape;
groundSensor.density = 0.0f; //Dichte
groundSensor.friction = 0f;
groundSensor.restitution = 0f; //Rückprall
characterBody.CreateFixture (groundSensor);
b2PolygonShape characterLeftSensorShape = new b2PolygonShape ();
b2Vec2 LeftSensorPosition = new b2Vec2 (-0.15f * CharacterSprite.ScaleX, -0.5f);
characterLeftSensorShape.SetAsBox ((float)5f / PhysicsHandler.pixelPerMeter, CharacterSize.Height / PhysicsHandler.pixelPerMeter / 4, LeftSensorPosition, 0f);
//leftsensor
b2FixtureDef leftSensor = new b2FixtureDef ();
leftSensor.isSensor = true;
leftSensor.userData = WorldFixtureData.playerleftsensor;
leftSensor.shape = characterLeftSensorShape;
leftSensor.density = 0.0f; //Dichte
leftSensor.friction = 0f;
leftSensor.restitution = 0f; //Rückprall
characterBody.CreateFixture (leftSensor);
b2PolygonShape characterRightSensorShape = new b2PolygonShape ();
b2Vec2 RightSensorPosition = new b2Vec2 (0.15f * CharacterSprite.ScaleX, -0.5f);
characterRightSensorShape.SetAsBox ((float)5f / PhysicsHandler.pixelPerMeter, CharacterSize.Height / PhysicsHandler.pixelPerMeter / 4, RightSensorPosition, 0f);
//rightsensor
b2FixtureDef rightSensor = new b2FixtureDef ();
rightSensor.isSensor = true;
rightSensor.userData = WorldFixtureData.playerrightsensor;
rightSensor.shape = characterRightSensorShape;
rightSensor.density = 0.0f; //Dichte
rightSensor.friction = 0f;
rightSensor.restitution = 0f; //Rückprall
characterBody.CreateFixture (rightSensor);
}
return characterBody;
}
示例12: CreatePhysicsBody
/// <summary>
///
/// </summary>
/// <param name="world"></param>
public virtual void CreatePhysicsBody(b2World world, int ptm)
{
PtmRatio = ptm;
PhysicsBodyDef = new b2BodyDef();
PhysicsBodyDef.type = BodyType;
PhysicsBodyDef.position = new b2Vec2(GetPointToMeter(InitialPositionX), GetPointToMeter(InitialPositionY));
PhysicsBodyDef.fixedRotation = FixedRotation;
PhysicsBodyDef.gravityScale = GravityScale;
PhysicsBodyDef.linearDamping = LinearDamping;
PhysicsBodyDef.bullet = Bullet;
PhysicsBody = world.CreateBody(PhysicsBodyDef);
PhysicsBody.Mass = Mass;
PhysicsBody.ResetMassData();
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = CreatePhysicsShape();
fixtureDef.density = Density;
fixtureDef.friction = Friction;
PhysicsBodyFixture = PhysicsBody.CreateFixture(fixtureDef);
}
示例13: AddShape
private void AddShape(b2World world, CCPoint position)
{
b2Vec2 positionVec = new b2Vec2(position.X, position.Y);
var box = new CCPhysicsSprite("hd/images/cloud", IntroLayer.PTM_RATIO);
box.Position = position;
var def = new b2BodyDef();
def.position = new b2Vec2(positionVec.x / IntroLayer.PTM_RATIO, positionVec.y / IntroLayer.PTM_RATIO);
def.linearVelocity = new b2Vec2(0.0f, -1.0f);
def.type = b2BodyType.b2_dynamicBody;
b2Body body = world.CreateBody(def);
// Polygon Shape
//var shape = new b2PolygonShape();
//shape.SetAsBox(50f / IntroLayer.PTM_RATIO, 50f / IntroLayer.PTM_RATIO);
// Circle Shape
var shape = new b2CircleShape();
shape.Radius = 50f / IntroLayer.PTM_RATIO;
var fd = new b2FixtureDef();
fd.shape = shape;
fd.density = 1f;
fd.restitution = 0f;
fd.friction = 0.2f;
body.CreateFixture(fd);
box.PhysicsBody = body;
sprites.Add(box);
AddChild(box);
}
示例14: SetupWorld
static void SetupWorld(bool setupGround)
{
var gravity = new b2Vec2(0.0f, -10.0f);
_world = new b2World(gravity);
_world.SetAllowSleeping(true);
_world.SetContinuousPhysics(true);
_world.SetSubStepping(true);
_world.SetWarmStarting(true);
_world.SetDestructionListener(new Destructo());
_world.SetContactListener(new Contacto());
if (!setupGround)
{
return;
}
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2BodyDef def = b2BodyDef.Create();
def.allowSleep = true;
def.position = b2Vec2.Zero;
def.type = b2BodyType.b2_staticBody;
b2Body groundBody = _world.CreateBody(def);
groundBody.SetActive(true);
// bottom
b2EdgeShape groundBox = new b2EdgeShape();
groundBox.Set(b2Vec2.Zero, new b2Vec2(width, 0));
b2FixtureDef fd = b2FixtureDef.Create();
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// top
groundBox = new b2EdgeShape();
groundBox.Set(new b2Vec2(0, height), new b2Vec2(width, height));
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// left
groundBox = new b2EdgeShape();
groundBox.Set(new b2Vec2(0, height), b2Vec2.Zero);
fd.shape = groundBox;
groundBody.CreateFixture(fd);
// right
groundBox = new b2EdgeShape();
groundBox.Set(new b2Vec2(width, height), new b2Vec2(width, 0));
fd.shape = groundBox;
groundBody.CreateFixture(fd);
_world.Dump();
}
示例15: 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
//.........这里部分代码省略.........