public PhysicsActor(World _world, Vector2 _position, float _angle = 0.0f, bool _isStatic = false) { BodyDef bodydef = new BodyDef(); bodydef.Position = _position; bodydef.Angle = 0.0f; body = _world.CreateBody(bodydef); }
public InGameState() { //position the score display centered at the top ScoreDisplay.CharacterSize = 90; ScoreDisplay.DisplayedString = InGameState.WinCount0.ToString(); float leftSize = ScoreDisplay.GetLocalBounds().Width; ScoreDisplay.DisplayedString += " : "; float midSize = ScoreDisplay.GetLocalBounds().Width - leftSize; ScoreDisplay.DisplayedString += InGameState.WinCount1.ToString(); ScoreDisplay.Origin = new Vector2(leftSize + 0.5f * midSize, 0f); ScoreDisplay.Position = new Vector2(Constants.windowSizeX / 2 , Constants.windowSizeY / 14); ScoreDisplay.Color = new SFML.Graphics.Color(200, 255, 200); AABB aabb = new AABB(); aabb.LowerBound.Set(0.0f, 0.0f); aabb.UpperBound.Set(800, 600/*Constants.worldSizeX * Constants.screenRatio*/); physicsWorld = new World(aabb, new Vec2(0.0f, -9.81f), false); contactManager = Physics.ContactManager.g_contactManager; physicsWorld.SetContactListener(contactManager); // Set new Players and appending dekoHands ResetPlayers(); setDekoFlags(); //0xF0A58A4 groundPolygonAct = new Actors.PolygonActor(physicsWorld, new Vec2(0.0f, 15.0f), 0xFBA58A4, Actors.FunctionType.GradientNoise, 4); BackgroundBackSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundBack)); BackgroundBackSprite.Scale = new Vector2(Constants.windowSizeX / (float)BackgroundBackSprite.TextureRect.Width, Constants.windowSizeY / (float)BackgroundBackSprite.TextureRect.Height);//0.5F * Vector2.One; BackgroundFrontSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundFront)); BackgroundFrontSprite.Scale = BackgroundBackSprite.Scale; //left and right borders of the map BodyDef bodydef = new BodyDef(); bodydef.Position = new Vector2(0,0); bodydef.Angle = 0.0f; PolygonDef box = new PolygonDef(); box.SetAsBox(1f, Constants.worldSizeY); Body leftEdge = physicsWorld.CreateBody(bodydef); contactManager.addNonLethalShape(leftEdge.CreateShape(box)); bodydef.Position = new Vector2(Constants.worldSizeX-1, 0); Body rightEdge = physicsWorld.CreateBody(bodydef); contactManager.addNonLethalShape(rightEdge.CreateShape(box)); bodydef.Position = new Vector2(0, Constants.worldSizeY); box.SetAsBox(Constants.worldSizeX, 1f); Body topEdge = physicsWorld.CreateBody(bodydef); contactManager.addNonLethalShape(topEdge.CreateShape(box)); }
protected override void CreatePhysics(World world, float positionX, float positionY, float friction) { var bodyDef = new BodyDef(); bodyDef.Position.Set(positionX, positionY); this.body = world.CreateBody(bodyDef); var shapeDef = new PolygonDef(); shapeDef.SetAsBox(this.Width, this.Height); shapeDef.Density = this.Density; shapeDef.Friction = friction; shapeDef.Restitution = 0.3f; this.body.CreateShape(shapeDef); this.body.SetMassFromShapes(); }
protected override void CreatePhysics(World world, float positionX, float positionY, float friction) { var bodyDef = new BodyDef(); bodyDef.Position.Set(positionX, positionY); bodyDef.LinearDamping = 0.3f; bodyDef.AngularDamping = 0.6f; var circleDef = new CircleDef(); circleDef.Density = this.Density; circleDef.Radius = this.Radius * 0.5f; circleDef.Friction = friction; circleDef.Restitution = 0.3f; this.body = world.CreateBody(bodyDef); this.body.CreateShape(circleDef); this.body.SetMassFromShapes(); }
public static void CreateMound(World world, float x, float y, float friction, float restitution) { // ==== Mound ==== BodyDef moundBodyDef = new BodyDef(); moundBodyDef.Position.Set(x, y); // Mound body. Body moundBody = world.CreateBody(moundBodyDef); // Define shape. PolygonDef moundShapeDef = new PolygonDef(); moundShapeDef.VertexCount = 3; moundShapeDef.Vertices[0].Set(0.20f, 0f); moundShapeDef.Vertices[1].Set(0f, 0.025f); moundShapeDef.Vertices[2].Set(-0.20f, 0f); moundShapeDef.Friction = friction; moundShapeDef.Restitution = restitution; moundShapeDef.Filter.CategoryBits = 0x3; // Add shape to body. moundBody.CreateShape(moundShapeDef); }
public virtual Body CreateBody(BodyDef bodyDef) { return(world.CreateBody(bodyDef)); }
/* * @param _funcType Style of the top line that generated * @param _res Size of mapunits for one linear segment * */ public PolygonActor(World _world, Vector2 _position, uint _seed, FunctionType _funcType, int _res = 5) : base(_world, _position) { int lineCount = (int)Constants.worldSizeX / _res; //make sure to have an even number if (lineCount % 2 != 0) lineCount++; Vec2[] verts = new Vec2[(int)lineCount + 1 + 4]; vertexBuffer = new VertexArray(PrimitiveType.LinesStrip); Vector2 posScreen = _position.toScreenCoord(); //repeatable random sequenze Rand rnd = new Rand(_seed); //start and end have even ground verts[0] = new Vec2(0, 6); verts[1] = new Vec2(_res, 6); verts[2] = new Vec2(_res + _res, 6); verts[lineCount - 2] = new Vec2(_res * (lineCount - 2), 6); verts[lineCount-1] = new Vec2(_res * (lineCount-1), 6); verts[lineCount] = new Vec2(_res * lineCount, 6); vertexBuffer.Append(new Vertex(((Vector2)verts[0] + _position).toScreenCoord())); //create the function if (_funcType == FunctionType.Simple) { for (int i = 2; i <= lineCount; ++i) { //Vector2 pos = new Vec2(i * 5, 10 + Rand.IntValue(10)); Vector2 pos = new Vec2(i * _res, System.Math.Max((verts[i - 1].Y + (int)rnd.next(6) - 3), 0)); verts[i] = pos; } } else if(_funcType == FunctionType.GradientNoise) { for (int i = 2; i < lineCount-3;) { int nextGrad = i + 4; if (nextGrad < lineCount - 2) { verts[nextGrad] = new Vec2(nextGrad * _res, rnd.next((int)maxHeight)); } else nextGrad = lineCount - 2; //interpolate between float relativeA = verts[i].Y / maxHeight; float relativeB = verts[nextGrad].Y / maxHeight; for (int c = i + 1; c < nextGrad; ++c) { verts[c] = new Vec2(c * _res, maxHeight * interpolateCos(relativeA, relativeB, (float)(c - i) / 4)); } i = nextGrad; } } Array.Resize<Body>(ref triangleBodys, lineCount); PolygonDef triangleDef = new PolygonDef(); triangleDef.Density = 0.0f; triangleDef.Friction = 1.0f; triangleDef.VertexCount = 3; BodyDef bodydef = new BodyDef(); bodydef.Position = _position; bodydef.Angle = 0.0f; //convert to triangles for (int i = 0; i < lineCount; ++i) { //always 3 points of the function form a triangle triangleDef.Vertices[0] = verts[i]; triangleDef.Vertices[1] = verts[i] - new Vec2(0.0f, 50.0f); triangleDef.Vertices[2] = verts[i + 1];//.Y < verts[i+1].Y ? verts[i] : verts[i + 1] triangleBodys[i] = _world.CreateBody(bodydef); triangleBodys[i].CreateShape(triangleDef); vertexBuffer.Append(new Vertex(((Vector2)verts[i+1] + _position).toScreenCoord())); } }
/// <summary> /// Called to setup the arena boundaries and bind to all of the physics stuff. /// </summary> public virtual void Bind() { float width = dimensions.X, height = dimensions.Y; worldAABB = new AABB(); worldAABB.LowerBound.Set(-width / 2 - edgeTolerance, -edgeTolerance); worldAABB.UpperBound.Set(width / 2 + edgeTolerance, height + edgeTolerance); Vec2 gravity = new Vec2(this.gravity.X, this.gravity.Y); bool doSleep = true; world = new World(worldAABB, gravity, doSleep); world.SetContactFilter(new ContactFilter()); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, 0.0f); Body groundBody = world.CreateBody(groundBodyDef); // Bottom AddBoundaryBlock(groundBody, 0, -(boundaryThickness / 2), width + boundaryThickness * 2, boundaryThickness); // Top AddBoundaryBlock(groundBody, 0, height + boundaryThickness / 2, width + boundaryThickness * 2, boundaryThickness); // Left AddBoundaryBlock(groundBody, -(width / 2) - boundaryThickness / 2, height / 2, boundaryThickness, height + boundaryThickness * 2); // Right AddBoundaryBlock(groundBody, +(width / 2) + boundaryThickness / 2, height / 2, boundaryThickness, height + boundaryThickness * 2); DebugDraw draw = new OpenTKDebugDraw(); draw.Flags = DebugDraw.DrawFlags.Shape; if (Program.DebugDraw) { world.SetDebugDraw(draw); } world.SetContactListener(this); // Old code for reference /* * * _worldAABB = new AABB(); _worldAABB.LowerBound.Set(-30.0f, -20.0f); _worldAABB.UpperBound.Set(30.0f, 40.0f); Vec2 gravity = new Vec2(); gravity.Set(0.0f, -10.0f); bool doSleep = true; _world = new World(_worldAABB, gravity, doSleep); _world.SetContactFilter(new ContactFilter()); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, 0.0f); Body groundBody = _world.CreateBody(groundBodyDef); AddBlock(groundBody, 0, -5, 40, 10); AddBlock(groundBody, -20, 20, 1, 40); AddBlock(groundBody, 20, 20, 1, 40); AddBlock(groundBody, 0, 30, 40, 10); DebugDraw draw = new OpenTKDebugDraw(); draw.Flags = DebugDraw.DrawFlags.Shape; if (Program.DebugDraw) { _world.SetDebugDraw(draw); } _world.SetContactListener(this); */ }
// This is a simple example of building and running a simulation // using Box2DX. Here we create a large ground box and a small dynamic // box. static void Main(string[] args) { // Define the size of the world. Simulation will still work // if bodies reach the end of the world, but it will be slower. AABB worldAABB = new AABB(); worldAABB.LowerBound.Set(-100.0f); worldAABB.UpperBound.Set(100.0f); // Define the gravity vector. Vec2 gravity = new Vec2(0.0f, -10.0f); // Do we want to let bodies sleep? bool doSleep = true; // Construct a world object, which will hold and simulate the rigid bodies. World world = new World(worldAABB, gravity, doSleep); // Define the ground body. BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, -10.0f); // Call the body factory which creates the ground box shape. // The body is also added to the world. Body groundBody = world.CreateBody(groundBodyDef); // Define the ground box shape. PolygonDef groundShapeDef = new PolygonDef(); // The extents are the half-widths of the box. groundShapeDef.SetAsBox(50.0f, 10.0f); // Add the ground shape to the ground body. groundBody.CreateFixture(groundShapeDef); // Define the dynamic body. We set its position and call the body factory. BodyDef bodyDef = new BodyDef(); bodyDef.Position.Set(0.0f, 4.0f); Body body = world.CreateBody(bodyDef); // Define another box shape for our dynamic body. PolygonDef shapeDef = new PolygonDef(); shapeDef.SetAsBox(1.0f, 1.0f); // Set the box density to be non-zero, so it will be dynamic. shapeDef.Density = 1.0f; // Override the default friction. shapeDef.Friction = 0.3f; // Add the shape to the body. body.CreateFixture(shapeDef); // Now tell the dynamic body to compute it's mass properties base // on its shape. body.SetMassFromShapes(); // Prepare for simulation. Typically we use a time step of 1/60 of a // second (60Hz) and 10 iterations. This provides a high quality simulation // in most game scenarios. float timeStep = 1.0f / 60.0f; int velocityIterations = 8; int positionIterations = 1; // This is our little game loop. for (int i = 0; i < 100; ++i) { // Instruct the world to perform a single step of simulation. It is // generally best to keep the time step and iterations fixed. world.Step(timeStep, velocityIterations, positionIterations); // Now print the position and angle of the body. Vec2 position = body.GetPosition(); float angle = body.GetAngle(); Console.WriteLine("Step: {3} - X: {0}, Y: {1}, Angle: {2}", new object[]{position.X.ToString(),position.Y.ToString(),angle.ToString(), i.ToString()}); } // When the world destructor is called, all bodies and joints are freed. This can // create orphaned pointers, so be careful about your world management. Console.ReadLine(); }
public PlayerCharacter(World _world, Vector2 _position) : base(_world, _position) { playerIndex = playerCount++; color = PlayerCharacter.Colors[playerIndex]; accelerate = 0; rotate = 0; isDead = false; this.angVelocity = 0; //build the unicycle CircleDef circleDef = new CircleDef(); circleDef.Radius = 1; circleDef.Density = 1f; circleDef.Friction = 1.0f; circleDef.Restitution = 0.0f; circleDef.LocalPosition.Set(0, 0); wheel = body.CreateShape(circleDef); body.SetMassFromShapes(); body.SetUserData(this); // link body and this to register collisions in this //build the head and connect with the wheel BodyDef bodydef = new BodyDef(); bodydef.Position = _position + new Vector2(0.0f, 3.5f); bodydef.Angle = 0f; chest = _world.CreateBody(bodydef); //add the head circleDef.Density = 0.0001f; circleDef.Radius = 0.75f; circleDef.LocalPosition.Set(0, 3); head = chest.CreateShape(circleDef); rotationHead = _position; PolygonDef Boxdef = new PolygonDef(); Boxdef.SetAsBox(1, 1.5f); Boxdef.Density = 0.25f; Boxdef.Friction = 0.4f; Box2DX.Collision.Shape s2 = chest.CreateShape(Boxdef); chest.SetMassFromShapes(); chest.SetUserData(this); //Jointshit RevoluteJointDef jointDefKW = new RevoluteJointDef(); jointDefKW.Body2 = chest; jointDefKW.Body1 = body; jointDefKW.CollideConnected = false; jointDefKW.LocalAnchor2 = new Vector2(-0.0f, -3.8f); jointDefKW.LocalAnchor1 = new Vector2(0, 0); jointDefKW.EnableLimit = false; _world.CreateJoint(jointDefKW); // add visuals Texture wheelTexture = AssetManager.getTexture(AssetManager.TextureName.ShoopWheel); wheelSprite = new AnimatedSprite(wheelTexture, 1.0f, 1, (Vector2)wheelTexture.Size); wheelSprite.Scale = Constants.windowScaleFactor * new Vector2(0.2f, 0.2f); //(Vector2.One / (Vector2)wheelTexture.Size * 2F * circleDef.Radius).toScreenCoord() - Vector2.Zero.toScreenCoord();//new Vector2(0.08f, 0.08f); wheelSprite.Origin = ((Vector2)wheelSprite.spriteSize) / 2F; sheepSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.ShoopInfronUnicycle)); sheepSprite.Origin = ((Vector2)sheepSprite.Texture.Size) / 2F; sheepSprite.Scale = Constants.windowScaleFactor * new Vector2(_position.X > Constants.worldSizeX / 2F ? -0.2f : 0.2f, 0.2f); }
public Test() { Vec2 gravity = new Vec2(); gravity.Set(0.0f, -10.0f); bool doSleep = true; _world = new World(gravity, doSleep); _bomb = null; _textLine = 30; _mouseJoint = null; _pointCount = 0; _destructionListener.test = this; _world.SetDestructionListener(_destructionListener); _world.SetContactListener(this); _world.SetDebugDraw(_debugDraw); _bombSpawning = false; _stepCount = 0; BodyDef bodyDef = new BodyDef(); _groundBody = _world.CreateBody(bodyDef); }
/// <summary> /// Creates a box2D body based on the GameObject's current sprite. /// </summary> /// <param name="world">The physics world to create the body in.</param> public void SetupBodyFromSprite(World world) { BodyDef bDef = new BodyDef(); bDef.Position = position.ToWorldVector(); body2D = world.CreateBody(bDef); PolygonDef shapeDef = new PolygonDef(); shapeDef.SetAsBox((float)animations[animationIndex].Width / Game.pixelsToMeters, (float)animations[animationIndex].Height / Game.pixelsToMeters); shapeDef.Density = 1F; body2D.CreateFixture(shapeDef); body2D.SetMassFromShapes(); }
public Biped(World w, Vec2 position) { m_world = w; BipedDef def = new BipedDef(); BodyDef bd; // create body parts bd = def.LFootDef; bd.Position += position; LFoot = w.CreateBody(bd); LFoot.CreateShape(def.LFootPoly); LFoot.SetMassFromShapes(); bd = def.RFootDef; bd.Position += position; RFoot = w.CreateBody(bd); RFoot.CreateShape(def.RFootPoly); RFoot.SetMassFromShapes(); bd = def.LCalfDef; bd.Position += position; LCalf = w.CreateBody(bd); LCalf.CreateShape(def.LCalfPoly); LCalf.SetMassFromShapes(); bd = def.RCalfDef; bd.Position += position; RCalf = w.CreateBody(bd); RCalf.CreateShape(def.RCalfPoly); RCalf.SetMassFromShapes(); bd = def.LThighDef; bd.Position += position; LThigh = w.CreateBody(bd); LThigh.CreateShape(def.LThighPoly); LThigh.SetMassFromShapes(); bd = def.RThighDef; bd.Position += position; RThigh = w.CreateBody(bd); RThigh.CreateShape(def.RThighPoly); RThigh.SetMassFromShapes(); bd = def.PelvisDef; bd.Position += position; Pelvis = w.CreateBody(bd); Pelvis.CreateShape(def.PelvisPoly); Pelvis.SetMassFromShapes(); bd = def.PelvisDef; bd.Position += position; Stomach = w.CreateBody(bd); Stomach.CreateShape(def.StomachPoly); Stomach.SetMassFromShapes(); bd = def.ChestDef; bd.Position += position; Chest = w.CreateBody(bd); Chest.CreateShape(def.ChestPoly); Chest.SetMassFromShapes(); bd = def.NeckDef; bd.Position += position; Neck = w.CreateBody(bd); Neck.CreateShape(def.NeckPoly); Neck.SetMassFromShapes(); bd = def.HeadDef; bd.Position += position; Head = w.CreateBody(bd); Head.CreateShape(def.HeadCirc); Head.SetMassFromShapes(); bd = def.LUpperArmDef; bd.Position += position; LUpperArm = w.CreateBody(bd); LUpperArm.CreateShape(def.LUpperArmPoly); LUpperArm.SetMassFromShapes(); bd = def.RUpperArmDef; bd.Position += position; RUpperArm = w.CreateBody(bd); RUpperArm.CreateShape(def.RUpperArmPoly); RUpperArm.SetMassFromShapes(); bd = def.LForearmDef; bd.Position += position; LForearm = w.CreateBody(bd); LForearm.CreateShape(def.LForearmPoly); LForearm.SetMassFromShapes(); bd = def.RForearmDef; bd.Position += position; RForearm = w.CreateBody(bd); RForearm.CreateShape(def.RForearmPoly); RForearm.SetMassFromShapes(); bd = def.LHandDef; bd.Position += position; LHand = w.CreateBody(bd); LHand.CreateShape(def.LHandPoly); LHand.SetMassFromShapes(); bd = def.RHandDef; bd.Position += position; RHand = w.CreateBody(bd); RHand.CreateShape(def.RHandPoly); RHand.SetMassFromShapes(); // link body parts def.LAnkleDef.Body1 = LFoot; def.LAnkleDef.Body2 = LCalf; def.RAnkleDef.Body1 = RFoot; def.RAnkleDef.Body2 = RCalf; def.LKneeDef.Body1 = LCalf; def.LKneeDef.Body2 = LThigh; def.RKneeDef.Body1 = RCalf; def.RKneeDef.Body2 = RThigh; def.LHipDef.Body1 = LThigh; def.LHipDef.Body2 = Pelvis; def.RHipDef.Body1 = RThigh; def.RHipDef.Body2 = Pelvis; def.LowerAbsDef.Body1 = Pelvis; def.LowerAbsDef.Body2 = Stomach; def.UpperAbsDef.Body1 = Stomach; def.UpperAbsDef.Body2 = Chest; def.LowerNeckDef.Body1 = Chest; def.LowerNeckDef.Body2 = Neck; def.UpperNeckDef.Body1 = Chest; def.UpperNeckDef.Body2 = Head; def.LShoulderDef.Body1 = Chest; def.LShoulderDef.Body2 = LUpperArm; def.RShoulderDef.Body1 = Chest; def.RShoulderDef.Body2 = RUpperArm; def.LElbowDef.Body1 = LForearm; def.LElbowDef.Body2 = LUpperArm; def.RElbowDef.Body1 = RForearm; def.RElbowDef.Body2 = RUpperArm; def.LWristDef.Body1 = LHand; def.LWristDef.Body2 = LForearm; def.RWristDef.Body1 = RHand; def.RWristDef.Body2 = RForearm; // create joints LAnkle = (RevoluteJoint)w.CreateJoint(def.LAnkleDef); RAnkle = (RevoluteJoint)w.CreateJoint(def.RAnkleDef); LKnee = (RevoluteJoint)w.CreateJoint(def.LKneeDef); RKnee = (RevoluteJoint)w.CreateJoint(def.RKneeDef); LHip = (RevoluteJoint)w.CreateJoint(def.LHipDef); RHip = (RevoluteJoint)w.CreateJoint(def.RHipDef); LowerAbs = (RevoluteJoint)w.CreateJoint(def.LowerAbsDef); UpperAbs = (RevoluteJoint)w.CreateJoint(def.UpperAbsDef); LowerNeck = (RevoluteJoint)w.CreateJoint(def.LowerNeckDef); UpperNeck = (RevoluteJoint)w.CreateJoint(def.UpperNeckDef); LShoulder = (RevoluteJoint)w.CreateJoint(def.LShoulderDef); RShoulder = (RevoluteJoint)w.CreateJoint(def.RShoulderDef); LElbow = (RevoluteJoint)w.CreateJoint(def.LElbowDef); RElbow = (RevoluteJoint)w.CreateJoint(def.RElbowDef); LWrist = (RevoluteJoint)w.CreateJoint(def.LWristDef); RWrist = (RevoluteJoint)w.CreateJoint(def.RWristDef); }
/// <summary> /// Called after the application has been initialized. /// </summary> protected override void OnInitialized() { SetFileSourceFromManifestIfExists("UltravioletGame.Content.uvarc"); MouseInputBinding mib = new MouseInputBinding(this.Ultraviolet, MouseButton.Left); mouse = mib.Mouse; //initialize the firingBullet bool firingBullet = false; //Setup Box2D world worldAABB = new AABB(); worldAABB.Center.Set(0F, 0F); worldAABB.LowerBound.Set(-100F); worldAABB.UpperBound.Set(100F); physicsWorld = new World(worldAABB, new Vec2(0F, 9.8F), true); BodyDef physicsTestDef = new BodyDef(); physicsTestDef.Position.Set(0F, 0F); //physicsTestObject = new GameObject(); //physicsTestObject.body2D = physicsWorld.CreateBody(physicsTestDef); PolygonDef physicsTestShapeDef = new PolygonDef(); physicsTestShapeDef.SetAsBox(1F, 1F); physicsTestShapeDef.Density = 1F; //physicsTestObject.body2D.CreateFixture(physicsTestShapeDef); //physicsTestObject.body2D.SetMassFromShapes(); BodyDef groundDef = new BodyDef(); groundDef.Position.Set(0F, 3F); groundTestObject = new GameObject(); groundTestObject.body2D = physicsWorld.CreateBody(groundDef); PolygonDef groundShapeDef = new PolygonDef(); groundShapeDef.SetAsBox(1F, 1F); groundTestObject.body2D.CreateFixture(groundShapeDef); base.OnInitialized(); }