public static PayloadChain Connect(SpaceShip spaceShip, Payload payload, Vector2 payloadTarget) { var dynamicWorld = (DynamicEntityWorld)spaceShip.World; var world = dynamicWorld.PhysicsWorld; AABB spaceShipSize; spaceShip.Body.FixtureList.First().GetAABB(out spaceShipSize, 0); AABB payloadSize; payload.Body.FixtureList.First().GetAABB(out payloadSize, 0); var chainSize = new Vector2(1.0f); var chainSizeSingle = Math.Max(chainSize.X, chainSize.Y); var start = new Vector2(spaceShip.Position.X - spaceShipSize.Extents.Y, spaceShip.Position.Y); var length = payloadTarget.Length(); var targetVector = payloadTarget / length; var chainVector = targetVector * chainSizeSingle; start += chainVector; var chainCount = (int)Math.Ceiling(length / chainSizeSingle); var lastBody = spaceShip.Body; var chain = new PayloadChain(spaceShip.Name + "_Chain"); var elements = new List<ChainElement>(); for (var i = 0; i < chainCount; i++) { var chainBody = BodyFactory.CreateBody(world, start + i * chainVector); chainBody.BodyType = BodyType.Dynamic; var chainFixture = FixtureFactory.AttachRectangle(chainSize.X, chainSize.Y, 0.1f, Vector2.Zero, chainBody); chainFixture.CollidesWith = Category.Cat2; JointFactory.CreateRevoluteJoint(world, lastBody, chainBody, -chainVector / 2.0f); lastBody = chainBody; elements.Add(ChainElement.CreateFor(chain, elements.Count, dynamicWorld, chainBody)); } payload.Position = new Vector3(start + chainCount * chainVector + targetVector * 2.5f, 0.0f); JointFactory.CreateRevoluteJoint(world, lastBody, payload.Body, new Vector2(0.0f, -2.5f)); var ropeJoin = new RopeJoint(spaceShip.Body, payload.Body, new Vector2(0.0f, 3.0f), new Vector2(0.0f, -2.5f)); ropeJoin.CollideConnected = true; world.AddJoint(ropeJoin); return chain; }
public void LoadContent() { _spriteBatch = new SpriteBatch(_graphics); _world = new DynamicEntityWorld(_game.Services); var pp = _graphics.PresentationParameters; _debugView = new DebugViewXNA(_world.PhysicsWorld); _debugView.LoadContent(_graphics, _content); _camera = new DefaultCamera(null, new Viewport(0, 0, pp.BackBufferWidth, pp.BackBufferHeight)); _cameraNode = _world.Scene.Root.CreateChild("Camera"); _cameraNode.Attach(_camera); // Create the tile map. var terrainSize = new Vector2(4096.0f, 2048.0f); _terrain = new Terrain(terrainSize, 128.0f, 7) { DebugEnabled = false, Position = new Vector3(50.0f, -terrainSize.Y / 2.0f, 0.0f), TextureName = @"Textures\SpaceRock" }; _world.Add(_terrain); GenerateTunnels(new Vector2(50.0f, 0)); // Create the circle brush. _circleBrush = new CircleBrush(2.5f); _circleBrushNode = _world.Scene.CreateSceneNode(); _circleBrushNode.Attach(new CircleRenderable(2.5f, 64) { Color = Vector3.One }); // Dust cloud _dustCloud = _world.Scene.CreateSceneNode("DustCloud"); _dustCloud.Position = new Vector3(0.0f, 0.0f, -1.0f); _dustCloud.Attach(new DustCloud()); _world.Scene.Root.AddChild(_dustCloud); // Player _playerEntity = new SpaceShip(); _playerEntity.Attach(new PlayerController()); _playerEntity.Orientation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.ToRadians(90.0f)); _world.Add(_playerEntity); _payloadEntity = new Payload(); _payloadEntity.Orientation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.ToRadians(90.0f)); _world.Add(_payloadEntity); _payloadChain = PayloadChain.Connect(_playerEntity, _payloadEntity, Vector2.UnitX * -20.0f); }