/// <summary> /// /// </summary> /// <returns></returns> public override b2Shape CreatePhysicsShape() { var shape = new b2PolygonShape(); shape.SetAsBox(PhysicWidth / 2f, PhysicHeight / 2f); GroundSprite.DrawRect(new CCRect(0, 0, Width, Height), CCColor4B.Black); return shape; }
/** * Set this as a single edge. */ static public b2PolygonShape AsEdge(b2Vec2 v1, b2Vec2 v2) { b2PolygonShape polygonShape = new b2PolygonShape(); polygonShape.SetAsEdge(v1, v2); return(polygonShape); }
public override b2Shape Copy() { b2PolygonShape s = new b2PolygonShape(); s.Set(this); return(s); }
public static b2PolygonShape AsBox(float hx, float hy) { b2PolygonShape polygonShape = new b2PolygonShape(); polygonShape.SetAsBox(hx, hy); return(polygonShape); }
public static b2PolygonShape AsOrientedBox(float hx, float hy, b2Vec2 center = null, float angle = 0.0f) { b2PolygonShape polygonShape = new b2PolygonShape(); polygonShape.SetAsOrientedBox(hx, hy, center, angle); return(polygonShape); }
public static b2PolygonShape AsVector(List <b2Vec2> vertices, int vertexCount) { b2PolygonShape polygonShape = new b2PolygonShape(); polygonShape.SetAsVector(vertices, vertexCount); return(polygonShape); }
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); // }
public static b2PolygonShape AsArray(b2Vec2[] vertices, int vertexCount) { b2PolygonShape polygonShape = new b2PolygonShape(); polygonShape.SetAsArray(vertices, vertexCount); return(polygonShape); }
public b2PolygonShape(b2PolygonShape copy) : base((b2Shape)copy) { Centroid = copy.Centroid; Array.Copy(copy.Vertices, Vertices, copy.Vertices.Length); Array.Copy(copy.Normals, Normals, copy.Normals.Length); m_vertexCount = copy.m_vertexCount; }
public ContinuousTest() { { b2BodyDef bd = new b2BodyDef(); bd.position.Set(0.0f, 0.0f); b2Body body = m_world.CreateBody(bd); b2EdgeShape edge = new b2EdgeShape(); edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f)); body.CreateFixture(edge, 0.0f); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f); body.CreateFixture(shape, 0.0f); } #if true { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 20.0f); //bd.angle = 0.1f; b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(2.0f, 0.1f); m_body = m_world.CreateBody(bd); m_body.CreateFixture(shape, 1.0f); m_angularVelocity = Rand.RandomFloat(-50.0f, 50.0f); //m_angularVelocity = 46.661274f; m_body.LinearVelocity = new b2Vec2(0.0f, -100.0f); m_body.AngularVelocity = m_angularVelocity; } #else { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 2.0f); b2Body body = m_world.CreateBody(bd); b2CircleShape shape = new b2CircleShape(); shape.Position = b2Vec2.Zero; shape.Radius = 0.5f; body.CreateFixture(shape, 1.0f); bd.bullet = true; bd.position.Set(0.0f, 10.0f); body = m_world.CreateBody(bd); body.CreateFixture(shape, 1.0f); body.LinearVelocity = new b2Vec2(0.0f, -100.0f); } #endif }
public Pulleys() { float y = 16.0f; float L = 12.0f; float a = 1.0f; float b = 2.0f; b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape edge = new b2EdgeShape(); edge.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); //ground->CreateFixture(&shape, 0.0f); b2CircleShape circle = new b2CircleShape(); circle.Radius = 2.0f; circle.Position = new b2Vec2(-10.0f, y + b + L); ground.CreateFixture(circle, 0.0f); circle.Position = new b2Vec2(10.0f, y + b + L); ground.CreateFixture(circle, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(a, b); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; //bd.fixedRotation = true; bd.position.Set(-10.0f, y); b2Body body1 = m_world.CreateBody(bd); body1.CreateFixture(shape, 5.0f); bd.position.Set(10.0f, y); b2Body body2 = m_world.CreateBody(bd); body2.CreateFixture(shape, 5.0f); b2PulleyJointDef pulleyDef = new b2PulleyJointDef(); b2Vec2 anchor1 = new b2Vec2(-10.0f, y + b); b2Vec2 anchor2 = new b2Vec2(10.0f, y + b); b2Vec2 groundAnchor1 = new b2Vec2(-10.0f, y + b + L); b2Vec2 groundAnchor2 = new b2Vec2(10.0f, y + b + L); pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f); m_joint1 = (b2PulleyJoint) m_world.CreateJoint(pulleyDef); } }
//public const int e_columnCount = 1; //public const int e_rowCount = 1; public VerticalStack() { { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); shape.Set(new b2Vec2(20.0f, 0.0f), new b2Vec2(20.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); } float[] xs = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f}; for (int j = 0; j < e_columnCount; ++j) { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.5f, 0.5f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < e_rowCount; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; int n = j * e_rowCount + i; Debug.Assert(n < e_rowCount * e_columnCount); m_indices[n] = n; bd.userData = m_indices[n]; float x = 0.0f; //float32 x = RandomFloat(-0.02f, 0.02f); //float32 x = i % 2 == 0 ? -0.025f : 0.025f; bd.position.Set(xs[j] + x, 0.752f + 1.54f * i); b2Body body = m_world.CreateBody(bd); m_bodies[n] = body; body.CreateFixture(fd); } } m_bullet = null; }
void CreatePlatform() { // Define the dynamic body. var bodyDef = new b2BodyDef(); bodyDef.type = b2BodyType.b2_staticBody; //or you could use b2_staticBody bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO); var shape = new b2PolygonShape(); var num = 4; b2Vec2[] vertices = { new b2Vec2(-102.0f / Constants.PTM_RATIO, -49.5f / Constants.PTM_RATIO), new b2Vec2(-113.0f / Constants.PTM_RATIO, -81.5f / Constants.PTM_RATIO), new b2Vec2(113.0f / Constants.PTM_RATIO, -84.5f / Constants.PTM_RATIO), new b2Vec2(106.0f / Constants.PTM_RATIO, -47.5f / Constants.PTM_RATIO) }; shape.Set(vertices, num); // Define the dynamic body fixture. var fixtureDef = new b2FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; fixtureDef.restitution = 0.1f; CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName); //CONTINUING TO ADD BODY SHAPE.... // THIS IS THE Sling base.... //row 1, col 1 var num2 = 4; b2Vec2[] vertices2 = { new b2Vec2(41.0f / Constants.PTM_RATIO, -6.5f / Constants.PTM_RATIO), new b2Vec2(35.0f / Constants.PTM_RATIO, -57.5f / Constants.PTM_RATIO), new b2Vec2(57.0f / Constants.PTM_RATIO, -65.5f / Constants.PTM_RATIO), new b2Vec2(49.0f / Constants.PTM_RATIO, -7.5f / Constants.PTM_RATIO) }; shape.Set(vertices2, num2); fixtureDef.shape = shape; body.CreateFixture(fixtureDef); }
public override void Set(b2Shape other) { base.Set(other); if (other is b2PolygonShape) { b2PolygonShape other2 = other as b2PolygonShape; m_centroid.SetV(other2.m_centroid); m_vertexCount = other2.m_vertexCount; Reserve(m_vertexCount); for (int i = 0; i < m_vertexCount; i++) { m_vertices[i].SetV(other2.m_vertices[i]); m_normals[i].SetV(other2.m_normals[i]); } } }
public override void Step(Settings settings) { base.Step(settings); if (m_count < e_count) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 10.0f); b2Body body = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.125f, 0.125f); body.CreateFixture(shape, 1.0f); ++m_count; } }
private void CreateGround() { // Define the dynamic body. var bodyDef = new b2BodyDef(); bodyDef.type = b2BodyType.b2_staticBody; //or you could use b2_staticBody bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO); b2PolygonShape shape = new b2PolygonShape(); int num = 4; b2Vec2[] vertices = { new b2Vec2(-1220.0f / Constants.PTM_RATIO, 54.0f / Constants.PTM_RATIO), new b2Vec2(-1220.0f / Constants.PTM_RATIO, -52.0f / Constants.PTM_RATIO), new b2Vec2(1019.0f / Constants.PTM_RATIO, -52.0f / Constants.PTM_RATIO), new b2Vec2(1019.0f / Constants.PTM_RATIO, 54.0f / Constants.PTM_RATIO) }; shape.Set(vertices, num); // Define the dynamic body fixture. var fixtureDef = new b2FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 1.0f; fixtureDef.friction = 1.0f; fixtureDef.restitution = 0.1f; CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName); if (!TheLevel.SharedLevel.IS_RETINA) { //non retina adjustment sprite.ScaleX = 1.05f; } else { // retina adjustment sprite.ScaleX = 2.05f; sprite.ScaleY = 2.0f; } }
public Platform(List<CCPoint> platformWaypoints, int platformSpeed, Container gameContainer) { this.Texture = new CCTexture2D("platform"); this.Scale = SpriteScale; this.IsAntialiased = false; this.Position = platformWaypoints [0]; Waypoints = platformWaypoints; speed = platformSpeed; //umso geringer der speed umso schneller die platform CurrentWaypoint = 0; wayToMove = new CCSize (Waypoints [CurrentWaypoint + 1].X - Waypoints [CurrentWaypoint].X, Waypoints [CurrentWaypoint + 1].Y - Waypoints [CurrentWaypoint].Y); //box2d b2BodyDef platformDef = new b2BodyDef (); platformDef.type = b2BodyType.b2_kinematicBody; platformDef.position = new b2Vec2 (Waypoints[CurrentWaypoint].X / PhysicsHandler.pixelPerMeter, Waypoints[CurrentWaypoint].Y / PhysicsHandler.pixelPerMeter); platformBody = gameContainer.physicsHandler.gameWorld.CreateBody (platformDef); b2PolygonShape platformShape = new b2PolygonShape (); platformShape.SetAsBox ((float)this.ScaledContentSize.Width / PhysicsHandler.pixelPerMeter / 2, (float)this.ScaledContentSize.Height / PhysicsHandler.pixelPerMeter / 2); b2FixtureDef platformFixture = new b2FixtureDef (); platformFixture.shape = platformShape; platformFixture.density = 0.0f; //Dichte platformFixture.restitution = 0f; //Rückprall platformFixture.userData = WorldFixtureData.platform; platformBody.CreateFixture (platformFixture); // this.Position = new CCPoint (platformBody.Position.x * PhysicsHandler.pixelPerMeter, platformBody.Position.y * PhysicsHandler.pixelPerMeter); progressionX = wayToMove.Width/ (float)speed; progressionY = wayToMove.Height/(float)speed ; if (float.IsInfinity (progressionX)) progressionX = 0; if (float.IsInfinity (progressionY)) progressionY = 0; b2Vec2 Velocity = platformBody.LinearVelocity; Velocity.y = progressionY; Velocity.x = progressionX; platformBody.LinearVelocity = Velocity; }
public Prismatic() { b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(2.0f, 0.5f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-10.0f, 10.0f); bd.angle = 0.5f * b2Settings.b2_pi; bd.allowSleep = false; b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 5.0f); b2PrismaticJointDef pjd = new b2PrismaticJointDef(); // Bouncy limit b2Vec2 axis = new b2Vec2(2.0f, 1.0f); axis.Normalize(); pjd.Initialize(ground, body, new b2Vec2(0.0f, 0.0f), axis); // Non-bouncy limit //pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f)); pjd.motorSpeed = 10.0f; pjd.maxMotorForce = 10000.0f; pjd.enableMotor = true; pjd.lowerTranslation = 0.0f; pjd.upperTranslation = 20.0f; pjd.enableLimit = true; m_joint = (b2PrismaticJoint) m_world.CreateJoint(pjd); } }
public AddPair() { m_world.Gravity = new b2Vec2(0.0f, 0.0f); { b2CircleShape shape = new b2CircleShape(); shape.Position = b2Vec2.Zero; shape.Radius = 0.1f; float minX = -6.0f; float maxX = 0.0f; float minY = 4.0f; float maxY = 6.0f; for (int i = 0; i < 400; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = new b2Vec2(Rand.RandomFloat(minX, maxX), Rand.RandomFloat(minY, maxY)); b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 0.01f); } } { b2Shape shape = null; if (_DoCircle) { shape = new b2CircleShape(); ((b2CircleShape)shape).Radius = 1.5f; } else { shape = new b2PolygonShape(); ((b2PolygonShape)shape).SetAsBox(1.6f, 1.6f); } b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = new b2Vec2(-40.0f, 5.0f); bd.bullet = true; b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 1.0f); body.LinearVelocity = new b2Vec2(150.0f, 0.0f); } }
public OneSidedPlatform() { // Ground { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Platform { b2BodyDef bd = new b2BodyDef(); bd.position.Set(0.0f, 10.0f); b2Body body = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(3.0f, 0.5f); m_platform = body.CreateFixture(shape, 0.0f); m_bottom = 10.0f - 0.5f; m_top = 10.0f + 0.5f; } // Actor { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 12.0f); b2Body body = m_world.CreateBody(bd); m_radius = 0.5f; b2CircleShape shape = new b2CircleShape(); shape.Radius = m_radius; m_character = body.CreateFixture(shape, 20.0f); body.LinearVelocity = new b2Vec2(0.0f, -50.0f); m_state = State.e_unknown; } }
public Chain() { b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.6f, 0.125f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.2f; b2RevoluteJointDef jd = new b2RevoluteJointDef(); jd.CollideConnected = false; const float y = 25.0f; b2Body prevBody = ground; for (int i = 0; i < 30; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.5f + i, y); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); b2Vec2 anchor = new b2Vec2(i, y); jd.Initialize(prevBody, body, anchor); m_world.CreateJoint(jd); prevBody = body; } } }
public BulletTest() { { b2BodyDef bd = new b2BodyDef(); bd.position.Set(0.0f, 0.0f); b2Body body = m_world.CreateBody(bd); b2EdgeShape edge = new b2EdgeShape(); edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f)); body.CreateFixture(edge, 0.0f); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f); body.CreateFixture(shape, 0.0f); } { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 4.0f); b2PolygonShape box = new b2PolygonShape(); box.SetAsBox(2.0f, 0.1f); m_body = m_world.CreateBody(bd); m_body.CreateFixture(box, 1.0f); box.SetAsBox(0.25f, 0.25f); //m_x = RandomFloat(-1.0f, 1.0f); m_x = 0.20352793f; bd.position.Set(m_x, 10.0f); bd.bullet = true; m_bullet = m_world.CreateBody(bd); m_bullet.CreateFixture(box, 100.0f); m_bullet.LinearVelocity = new b2Vec2(0.0f, -50.0f); } }
public Pyramid() { { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { float a = 0.5f; b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(a, a); b2Vec2 x = new b2Vec2(-7.0f, 0.75f); b2Vec2 y; b2Vec2 deltaX = new b2Vec2(0.5625f, 1.25f); b2Vec2 deltaY = new b2Vec2(1.125f, 0.0f); for (int i = 0; i < e_count; ++i) { y = x; for (int j = i; j < e_count; ++j) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = y; b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 5.0f); y += deltaY; } x += deltaX; } } }
public Tumbler() { b2Body ground; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); } { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.allowSleep = false; bd.position.Set(0.0f, 10.0f); b2Body body = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.5f, 10.0f, new b2Vec2(10.0f, 0.0f), 0.0f); body.CreateFixture(shape, 5.0f); shape.SetAsBox(0.5f, 10.0f, new b2Vec2(-10.0f, 0.0f), 0.0f); body.CreateFixture(shape, 5.0f); shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, 10.0f), 0.0f); body.CreateFixture(shape, 5.0f); shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, -10.0f), 0.0f); body.CreateFixture(shape, 5.0f); b2RevoluteJointDef jd = new b2RevoluteJointDef(); jd.BodyA = ground; jd.BodyB = body; jd.localAnchorA.Set(0.0f, 10.0f); jd.localAnchorB.Set(0.0f, 0.0f); jd.referenceAngle = 0.0f; jd.motorSpeed = 0.05f * b2Settings.b2_pi; jd.maxMotorTorque = 1e8f; jd.enableMotor = true; m_joint = (b2RevoluteJoint) m_world.CreateJoint(jd); } m_count = 0; }
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); }
public ShapeEditing() { { b2BodyDef bd1 = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd1); b2EdgeShape shape1 = new b2EdgeShape(); shape1.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape1, 0.0f); } b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 10.0f); m_body = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(4.0f, 4.0f, new b2Vec2(0.0f, 0.0f), 0.0f); m_fixture1 = m_body.CreateFixture(shape, 10.0f); m_fixture2 = null; m_sensor = false; }
public void addNewSpriteAtPosition(CCPoint p) { CCLog.Log("Add sprite #{2} : {0} x {1}", p.X, p.Y, _batch.ChildrenCount + 1); //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is //just randomly picking one of the images int idx = (Random.Float_0_1() > .5 ? 0 : 1); int idy = (Random.Float_0_1() > .5 ? 0 : 1); var sprite = new CCPhysicsSprite(m_pSpriteTexture, new CCRect(32 * idx, 32 * idy, 32, 32)); _batch.AddChild(sprite, 0, kTagForPhysicsSprite); sprite.Position = new CCPoint(p.X, p.Y); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef def = b2BodyDef.Create(); def.position = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO); def.type = b2BodyType.b2_dynamicBody; b2Body body = _world.CreateBody(def); // Define another box shape for our dynamic body. var dynamicBox = new b2PolygonShape(); dynamicBox.SetAsBox(.5f, .5f); //These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fd = b2FixtureDef.Create(); fd.shape = dynamicBox; fd.density = 1f; fd.friction = 0.3f; b2Fixture fixture = body.CreateFixture(fd); sprite.PhysicsBody = body; //_world.SetContactListener(new Myb2Listener()); // _world.Dump(); }
/// Compute the collision manifold between two polygons. // Find edge normal of max separation on A - return if separating axis is found // Find edge normal of max separation on B - return if separation axis is found // Choose reference edge as min(minA, minB) // Find incident edge // Clip // The normal points from 1 to 2 public static void b2CollidePolygons(ref b2Manifold manifold, b2PolygonShape polyA, ref b2Transform xfA, b2PolygonShape polyB, ref b2Transform xfB) { manifold.pointCount = 0; float totalRadius = polyA.Radius + polyB.Radius; int edgeA = 0; float separationA = b2FindMaxSeparation(out edgeA, polyA, ref xfA, polyB, ref xfB); if (separationA > totalRadius) return; int edgeB = 0; float separationB = b2FindMaxSeparation(out edgeB, polyB, ref xfB, polyA, ref xfA); if (separationB > totalRadius) return; b2PolygonShape poly1; // reference polygon b2PolygonShape poly2; // incident polygon b2Transform xf1, xf2; int edge1; // reference edge byte flip; const float k_relativeTol = 0.98f; const float k_absoluteTol = 0.001f; if (separationB > k_relativeTol * separationA + k_absoluteTol) { poly1 = polyB; poly2 = polyA; xf1 = xfB; xf2 = xfA; edge1 = edgeB; manifold.type = b2ManifoldType.e_faceB; flip = 1; } else { poly1 = polyA; poly2 = polyB; xf1 = xfA; xf2 = xfB; edge1 = edgeA; manifold.type = b2ManifoldType.e_faceA; flip = 0; } b2ClipVertex[] incidentEdge = new b2ClipVertex[2]; b2FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2); int count1 = poly1.VertexCount; b2Vec2[] vertices1 = poly1.Vertices; int iv1 = edge1; int iv2 = edge1 + 1 < count1 ? edge1 + 1 : 0; b2Vec2 v11 = vertices1[iv1]; b2Vec2 v12 = vertices1[iv2]; b2Vec2 localTangent = v12 - v11; localTangent.Normalize(); b2Vec2 localNormal = localTangent.UnitCross(); // b2Math.b2Cross(localTangent, 1.0f); b2Vec2 planePoint = 0.5f * (v11 + v12); b2Vec2 tangent = b2Math.b2Mul(xf1.q, localTangent); b2Vec2 normal = tangent.UnitCross(); // b2Math.b2Cross(tangent, 1.0f); v11 = b2Math.b2Mul(xf1, v11); v12 = b2Math.b2Mul(xf1, v12); // Face offset. float frontOffset = b2Math.b2Dot(ref normal, ref v11); // Side offsets, extended by polytope skin thickness. float sideOffset1 = -b2Math.b2Dot(ref tangent, ref v11) + totalRadius; float sideOffset2 = b2Math.b2Dot(ref tangent, ref v12) + totalRadius; // Clip incident edge against extruded edge1 side edges. b2ClipVertex[] clipPoints1 = new b2ClipVertex[2]; b2ClipVertex[] clipPoints2 = new b2ClipVertex[2]; int np; // Clip to box side 1 np = b2ClipSegmentToLine(clipPoints1, incidentEdge, -tangent, sideOffset1, (byte)iv1); if (np < 2) return; // Clip to negative box side 1 np = b2ClipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2, (byte)iv2); if (np < 2) { return; } // Now clipPoints2 contains the clipped points. manifold.localNormal = localNormal; manifold.localPoint = planePoint; int pointCount = 0; for (int i = 0; i < b2Settings.b2_maxManifoldPoints; ++i) { float separation = b2Math.b2Dot(ref normal, ref clipPoints2[i].v) - frontOffset; if (separation <= totalRadius) { b2ManifoldPoint cp = manifold.points[pointCount]; cp.localPoint = b2Math.b2MulT(xf2, clipPoints2[i].v); cp.id = clipPoints2[i].id; if (flip != 0) { // Swap features b2ContactFeature cf = cp.id; cp.id.indexA = cf.indexB; cp.id.indexB = cf.indexA; cp.id.typeA = cf.typeB; cp.id.typeB = cf.typeA; } manifold.points[pointCount] = cp; ++pointCount; } } manifold.pointCount = pointCount; }
public override b2Shape Clone() { b2PolygonShape clone = new b2PolygonShape(this); return(clone); }
public PhysicalBarrel(StarlingGameSpriteWithBunkerTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); // hide in a barrel? //this.driverseat = new DriverSeat(); this.textures = textures; this.Context = Context; //for (int i = 0; i < 7; i++) //{ // this.KarmaInput0.Enqueue( // new KeySample() // ); //} visualshadow = new Image( textures.barrel1_shadow() ).AttachTo(Context.Content_layer2_shadows); visual = new Image( textures.barrel1() ).AttachTo(Context.Content_layer3_buildings); { //initialize body var bdef = new b2BodyDef(); bdef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bdef.linearDamping = 8.0; bdef.angularDamping = 8; bdef.angle = 0; this.body = Context.ground_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); fixdef.density = 1; fixdef.friction = 0.01; //fixdef.restitution = 0.4; //positively bouncy! var shape = new b2PolygonShape(); fixdef.shape = shape; shape.SetAsBox(1.6, 1); var fix = this.body.CreateFixture(fixdef); var fix_data = new Action<double>( jeep_forceA => { if (jeep_forceA < 1) return; if (oncollision != null) oncollision(this, jeep_forceA); } ); fix.SetUserData(fix_data); } { //initialize body var bdef = new b2BodyDef(); bdef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bdef.linearDamping = 8.0; bdef.angularDamping = 8; bdef.angle = 0; this.karmabody = Context.groundkarma_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); fixdef.density = 1; fixdef.friction = 0.01; //fixdef.restitution = 0.4; //positively bouncy! var shape = new b2PolygonShape(); fixdef.shape = shape; shape.SetAsBox(1.6, 1); this.karmabody.CreateFixture(fixdef); } Context.internalunits.Add(this); }
public EdgeShapes() { // Ground body { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); float x1 = -20.0f; float y1 = 2.0f * (float) Math.Cos(x1 / 10.0f * b2Settings.b2_pi); for (int i = 0; i < 80; ++i) { float x2 = x1 + 0.5f; float y2 = 2.0f * (float) Math.Cos(x2 / 10.0f * b2Settings.b2_pi); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(x1, y1), new b2Vec2(x2, y2)); ground.CreateFixture(shape, 0.0f); x1 = x2; y1 = y2; } } { b2Vec2[] vertices = new b2Vec2[3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(0.0f, 1.5f); m_polygons[0] = new b2PolygonShape(); m_polygons[0].Set(vertices, 3); } { b2Vec2[] vertices = new b2Vec2[3]; vertices[0].Set(-0.1f, 0.0f); vertices[1].Set(0.1f, 0.0f); vertices[2].Set(0.0f, 1.5f); m_polygons[1] = new b2PolygonShape(); m_polygons[1].Set(vertices, 3); } { float w = 1.0f; float b = w / (2.0f + b2Math.b2Sqrt(2.0f)); float s = b2Math.b2Sqrt(2.0f) * b; b2Vec2[] vertices = new b2Vec2[8]; vertices[0].Set(0.5f * s, 0.0f); vertices[1].Set(0.5f * w, b); vertices[2].Set(0.5f * w, b + s); vertices[3].Set(0.5f * s, w); vertices[4].Set(-0.5f * s, w); vertices[5].Set(-0.5f * w, b + s); vertices[6].Set(-0.5f * w, b); vertices[7].Set(-0.5f * s, 0.0f); m_polygons[2] = new b2PolygonShape(); m_polygons[2].Set(vertices, 8); } { m_polygons[3] = new b2PolygonShape(); m_polygons[3].SetAsBox(0.5f, 0.5f); } { m_circle.Radius = 0.5f; } m_bodyIndex = 0; m_angle = 0.0f; }
public virtual b2Shape Clone() { b2PolygonShape clone = new b2PolygonShape(this); return(clone); }
// Find the max separation between poly1 and poly2 using edge normals from poly1. public static float b2FindMaxSeparation(out int edgeIndex, b2PolygonShape poly1, ref b2Transform xf1, b2PolygonShape poly2, ref b2Transform xf2) { int count1 = poly1.VertexCount; b2Vec2[] normals1 = poly1.Normals; // Vector pointing from the centroid of poly1 to the centroid of poly2. b2Vec2 d = b2Math.b2Mul(xf2, poly2.Centroid) - b2Math.b2Mul(xf1, poly1.Centroid); b2Vec2 dLocal1 = b2Math.b2MulT(xf1.q, d); // Find edge normal on poly1 that has the largest projection onto d. int edge = 0; float maxDot = -b2Settings.b2_maxFloat; for (int i = 0; i < count1; ++i) { float dot = b2Math.b2Dot(normals1[i], dLocal1); if (dot > maxDot) { maxDot = dot; edge = i; } } // Get the separation for the edge normal. float s = b2EdgeSeparation(poly1, xf1, edge, poly2, xf2); // Check the separation for the previous edge normal. int prevEdge = edge - 1 >= 0 ? edge - 1 : count1 - 1; float sPrev = b2EdgeSeparation(poly1, xf1, prevEdge, poly2, xf2); // Check the separation for the next edge normal. int nextEdge = edge + 1 < count1 ? edge + 1 : 0; float sNext = b2EdgeSeparation(poly1, xf1, nextEdge, poly2, xf2); // Find the best edge and the search direction. int bestEdge; float bestSeparation; int increment; if (sPrev > s && sPrev > sNext) { increment = -1; bestEdge = prevEdge; bestSeparation = sPrev; } else if (sNext > s) { increment = 1; bestEdge = nextEdge; bestSeparation = sNext; } else { edgeIndex = edge; return s; } // Perform a local search for the best edge normal. for (; ; ) { if (increment == -1) edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1; else edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0; s = b2EdgeSeparation(poly1, xf1, edge, poly2, xf2); if (s > bestSeparation) { bestEdge = edge; bestSeparation = s; } else { break; } } edgeIndex = bestEdge; return bestSeparation; }
public static void b2FindIncidentEdge(b2ClipVertex[] c, b2PolygonShape poly1, b2Transform xf1, int edge1, b2PolygonShape poly2, b2Transform xf2) { b2Vec2[] normals1 = poly1.Normals; int count2 = poly2.VertexCount; b2Vec2[] vertices2 = poly2.Vertices; b2Vec2[] normals2 = poly2.Normals; // Get the normal of the reference edge in poly2's frame. b2Vec2 normal1 = b2Math.b2MulT(xf2.q, b2Math.b2Mul(xf1.q, normals1[edge1])); // Find the incident edge on poly2. int index = 0; float minDot = b2Settings.b2_maxFloat; for (int i = 0; i < count2; ++i) { float dot = b2Math.b2Dot(normal1, normals2[i]); if (dot < minDot) { minDot = dot; index = i; } } // Build the clip vertices for the incident edge. int i1 = index; int i2 = i1 + 1 < count2 ? i1 + 1 : 0; c[0].v = b2Math.b2Mul(xf2, vertices2[i1]); c[0].id.indexA = (byte)edge1; c[0].id.indexB = (byte)i1; c[0].id.typeA = b2ContactFeatureType.e_face; c[0].id.typeB = b2ContactFeatureType.e_vertex; c[1].v = b2Math.b2Mul(xf2, vertices2[i2]); c[1].id.indexA = (byte)edge1; c[1].id.indexB = (byte)i2; c[1].id.typeA = b2ContactFeatureType.e_face; c[1].id.typeB = b2ContactFeatureType.e_vertex; }
/// Compute the collision manifold between an edge and a circle. public static void b2CollideEdgeAndPolygon(ref b2Manifold manifold, b2EdgeShape edgeA, ref b2Transform xfA, b2PolygonShape polygonB, ref b2Transform xfB) { b2EPCollider b = new b2EPCollider(); b.Collide(ref manifold, edgeA, ref xfA, polygonB, ref xfB); }
public static float b2EdgeSeparation(b2PolygonShape poly1, b2Transform xf1, int edge1, b2PolygonShape poly2, b2Transform xf2) { b2Vec2[] vertices1 = poly1.Vertices; b2Vec2[] normals1 = poly1.Normals; int count2 = poly2.VertexCount; b2Vec2[] vertices2 = poly2.Vertices; // Convert normal from poly1's frame into poly2's frame. b2Vec2 normal1World = b2Math.b2Mul(xf1.q, normals1[edge1]); b2Vec2 normal1 = b2Math.b2MulT(xf2.q, normal1World); // Find support vertex on poly2 for -normal. int index = 0; float minDot = b2Settings.b2_maxFloat; for (int i = 0; i < count2; ++i) { float dot = b2Math.b2Dot(ref vertices2[i], ref normal1); if (dot < minDot) { minDot = dot; index = i; } } b2Vec2 v1 = b2Math.b2Mul(xf1, vertices1[edge1]); b2Vec2 v2 = b2Math.b2Mul(xf2, vertices2[index]); float separation = b2Math.b2Dot(v2 - v1, normal1World); return separation; }
/// <summary> /// ��һ���ڳ��������һ����̬��bird���� /// �ڶ�����bird�����ж��� /// ����������box-2d����������bird�����������з��� /// </summary> private void AddBird() { CCSprite bird = CCSprite.spriteWithFile("imgs/bird/bird_01"); bird.rotation = -15; // bird���ж���֡���� List<CCSpriteFrame> frames = new List<CCSpriteFrame>(); for (int i = 1; i < 3; i++) { // ֡��ͼ CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage("imgs/bird/bird_0" + i); // �������һ�������bug����������õĻ����ͻᲥ�Ų��������� texture.Name = (uint)i; var frame = CCSpriteFrame.frameWithTexture(texture, new CCRect(0, 0, texture.ContentSizeInPixels.width, texture.ContentSizeInPixels.height)); frames.Add(frame); } // ���ж��� CCAnimation marmotShowanimation = CCAnimation.animationWithFrames(frames, 0.1f); CCAnimate flyAction = CCAnimate.actionWithAnimation(marmotShowanimation, false); flyRepeatAction = CCRepeat.actionWithAction(flyAction, 2); flyRepeatAction.tag = 0; bird.runAction(flyRepeatAction); // �����������ж���һ��body��������λ�ã�����bird��֮��Ӧ b2BodyDef ballBodyDef = new b2BodyDef(); ballBodyDef.type = b2BodyType.b2_dynamicBody; ballBodyDef.position = new b2Vec2(AppDelegate.screenSize.width / PTM_RATIO / 2, (float)(AppDelegate.screenSize.height / PTM_RATIO)); ballBodyDef.userData = bird; birdBody = world.CreateBody(ballBodyDef); // Ϊbody������״��������һЩ�������� b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(bird.contentSize.width / 2 / PTM_RATIO, bird.contentSize.height / 2 / PTM_RATIO); b2FixtureDef fixtureDef = new b2FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 500.0f; fixtureDef.friction = 0.5f; birdBody.CreateFixture(fixtureDef); this.addChild(bird); }
/// Compute the collision manifold between a polygon and a circle. public static void b2CollidePolygonAndCircle(ref b2Manifold manifold, b2PolygonShape polygonA, ref b2Transform xfA, b2CircleShape circleB, ref b2Transform xfB) { manifold.pointCount = 0; // Compute circle position in the frame of the polygon. b2Vec2 c = b2Math.b2Mul(xfB, circleB.Position); b2Vec2 cLocal = b2Math.b2MulT(xfA, c); // Find the min separating edge. int normalIndex = 0; float separation = -b2Settings.b2_maxFloat; float radius = polygonA.Radius + circleB.Radius; int vertexCount = polygonA.VertexCount; b2Vec2[] vertices = polygonA.Vertices; b2Vec2[] normals = polygonA.Normals; for (int i = 0; i < vertexCount; ++i) { float s = b2Math.b2Dot(normals[i], cLocal - vertices[i]); if (s > radius) { // Early out. return; } if (s > separation) { separation = s; normalIndex = i; } } // Vertices that subtend the incident face. int vertIndex1 = normalIndex; int vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0; b2Vec2 v1 = vertices[vertIndex1]; b2Vec2 v2 = vertices[vertIndex2]; // If the center is inside the polygon ... if (separation < b2Settings.b2_epsilon) { manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = normals[normalIndex]; manifold.localPoint = 0.5f * (v1 + v2); manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; return; } // Compute barycentric coordinates float u1 = b2Math.b2Dot(cLocal - v1, v2 - v1); float u2 = b2Math.b2Dot(cLocal - v2, v1 - v2); if (u1 <= 0.0f) { if (b2Math.b2DistanceSquared(cLocal, v1) > radius * radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = cLocal - v1; manifold.localNormal.Normalize(); manifold.localPoint = v1; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; } else if (u2 <= 0.0f) { if (b2Math.b2DistanceSquared(cLocal, v2) > radius * radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = cLocal - v2; manifold.localNormal.Normalize(); manifold.localPoint = v2; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; } else { b2Vec2 faceCenter = 0.5f * (v1 + v2); separation = b2Math.b2Dot(cLocal - faceCenter, normals[vertIndex1]); if (separation > radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = normals[vertIndex1]; manifold.localPoint = faceCenter; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; } }
/// <summary> /// ��һ����ӵ��� /// �ڶ�������box-2d�������潫������Ϊ��̬������ӵ����������� /// </summary> private void AddGround() { ground = CCSprite.spriteWithFile("imgs/ground/ground"); ground.position = new CCPoint(ground.contentSize.width / 2, ground.contentSize.height / 2); b2BodyDef groundBodyDef = new b2BodyDef(); groundBodyDef.position = new b2Vec2(ground.contentSize.width / PTM_RATIO / 2, ground.contentSize.height / PTM_RATIO / 2); groundBodyDef.userData = ground; // ��������Ӱ�죬�����˶� groundBodyDef.type = b2BodyType.b2_staticBody; // ����ground���˶���ʹ���ٶ���bird�ķ����ٶ�һ�� var action1 = CCMoveTo.actionWithDuration(ground.contentSize.width / (4 * PTM_RATIO * flySpeed), new CCPoint(ground.contentSize.width / 4, ground.position.y)); var action2 = CCMoveTo.actionWithDuration(0, new CCPoint(ground.contentSize.width / 2, ground.position.y)); var action = CCSequence.actionOneTwo(action1, action2); var repeatAction = CCRepeatForever.actionWithAction(action); repeatAction.tag = 0; ground.runAction(repeatAction); b2Body groundBody = world.CreateBody(groundBodyDef); b2PolygonShape groundBox = new b2PolygonShape(); b2FixtureDef boxShapeDef = new b2FixtureDef(); boxShapeDef.shape = groundBox; groundBox.SetAsBox(ground.contentSize.width / PTM_RATIO / 2, ground.contentSize.height / PTM_RATIO / 2); groundBody.CreateFixture(boxShapeDef); this.addChild(ground); }