public void moveIntoViewBounds(scBody body) { var viewBoundsBoundingBox = scBoundingUtils.createFromBoundingVertices((Vector2)viewBounds.upperLeft, (Vector2)viewBounds.lowerRight); var boundingBox = body.calculateBoundingBox(); if (!viewBoundsBoundingBox.contains(boundingBox)) { if (boundingBox.leftBorder < viewBoundsBoundingBox.leftBorder) { body.transform.position.X = viewBoundsBoundingBox.leftBorder + boundingBox.halfExtents.X; } if (boundingBox.rightBorder > viewBoundsBoundingBox.rightBorder) { body.transform.position.X = viewBoundsBoundingBox.rightBorder - boundingBox.halfExtents.X; } if (boundingBox.upperBorder < viewBoundsBoundingBox.upperBorder) { body.transform.position.Y = viewBoundsBoundingBox.upperBorder + boundingBox.halfExtents.Y; } if (boundingBox.lowerBorder > viewBoundsBoundingBox.lowerBorder) { body.transform.position.Y = viewBoundsBoundingBox.lowerBorder - boundingBox.halfExtents.Y; } body.linearVelocity = Vector2.Zero; } }
public scBody createBody(scBodyDescription description, IList<scBodyPartDescription> bodyPartDescriptions) { var body = new scBody(); body.transform = description.transform; body.bodyType = description.bodyType; foreach (var bodyPartDescription in bodyPartDescriptions) { var bodyPart = new scBodyPart(); bodyPart.shape = bodyPartDescription.shape; bodyPart.userData = bodyPartDescription.userData; body.bodyParts.Add(bodyPart); } bodies.Add(body); if (bodyAdded != null) { bodyAdded(body); } return body; }
public void Initialize(ConfigOption option) { levelConfig = ConfigFile.FromFile(option.Value); { var physicsSection = levelConfig["Physics"]; PhysicsScale = physicsSection["scale"]; GroundFriction = physicsSection["groundFriction"]; World = new scPhysicsWorld(); World.game = game; physicsSection.IfOptionExists("gravity", o => World.gravity = o.AsVector2()); // physicsSection["gravity"].AsVector2(), physicsSection["doSleep"].AsBool() // World.ContinuousPhysics = physicsSection["continuousPhysics"].AsBool(); physicsWorldDebugRenderer = new scPhysicsWorldDebugRenderer(); physicsWorldDebugRenderer.world = World; } LevelGenerator = new LevelGenerator(this); // bodyList = LevelGenerator.generateLevel(); var viewport = game.GraphicsDevice.Viewport; InitializePlayers(); camera = new Camera2D(game); camera.Initialize(); camera.Position = levelConfig["Camera"]["startPos"].AsVector2(); camera.ViewBounds = levelConfig["Camera"]["viewBounds"].AsRectangle(); camera.MaxMoveSpeed = levelConfig["Camera"]["maxMoveSpeed"]; CreatePhysicalViewBounds(); var gameObjectsConfig = ConfigFile.FromFile(levelConfig.GlobalSection["objects"]); foreach (var section in gameObjectsConfig.Sections) { if (section.Value == gameObjectsConfig.GlobalSection) { // Skip the global section because it can not contain any useful info in this case. continue; } var go = GameObject.Create(game, section.Key, section.Value); GameObjects.Add(go); } Menu.InitializeFromConfigFile(levelConfig.GlobalSection["userInterface"].AsConfigFile()); levelConfig.IfSectionExists("Audio", section => { section.IfOptionExists("ambientCue", opt => AmbientMusicCue = opt); }); if (!levelConfig.Sections.ContainsKey("Debug")) { return; } var debugSection = levelConfig["Debug"]; debugSection.IfOptionExists("drawPhysics", opt => game.drawPhysics = opt.AsBool()); debugSection.IfOptionExists("drawVisualHelpers", opt => { if (opt.AsBool()) game.drawVisualHelpers.SetNormal(); else game.drawVisualHelpers.SetNone(); }); debugSection.IfOptionExists("drawDebugData", opt => { if (opt.AsBool()) game.drawDebugData.SetNormal(); else game.drawDebugData.SetNone(); }); { var circleshape = new scCircleShape(); circleshape.radius = 75; var bodyPartDescription = new scBodyPartDescription(); bodyPartDescription.shape = circleshape; var bodyDescription = new scBodyDescription(); bodyDescription.bodyType = scBodyType.Static; bodyDescription.transform.position = new Vector2(-150, 0); var bodyPartDescriptions = new List<scBodyPartDescription>(); bodyPartDescriptions.Add(bodyPartDescription); var body = World.createBody(bodyDescription, bodyPartDescriptions); } { var rectangleshape = scRectangleShape.fromLocalPositionAndHalfExtents(new Vector2(0, 0), new Vector2(75, 30)); var bodyPartDescription = new scBodyPartDescription(); bodyPartDescription.shape = rectangleshape; var bodyDescription = new scBodyDescription(); bodyDescription.bodyType = scBodyType.Static; bodyDescription.transform.position = new Vector2(50, 150); var bodyPartDescriptions = new List<scBodyPartDescription>(); bodyPartDescriptions.Add(bodyPartDescription); var body = World.createBody(bodyDescription, bodyPartDescriptions); } { var edgeshape = new scEdgeShape(); edgeshape.start = new Vector2(-20, -40); edgeshape.end = new Vector2(20, 40); var bodyPartDescription = new scBodyPartDescription(); bodyPartDescription.shape = edgeshape; var bodyDescription = new scBodyDescription(); bodyDescription.bodyType = scBodyType.Static; bodyDescription.transform.position = new Vector2(100, 50); var bodyPartDescriptions = new List<scBodyPartDescription>(); bodyPartDescriptions.Add(bodyPartDescription); var body = World.createBody(bodyDescription, bodyPartDescriptions); TESTEDGE = body; } { var rectangleshape = scRectangleShape.fromLocalPositionAndHalfExtents(new Vector2(0, 0), new Vector2(40, 40)); var bodyPartDescription = new scBodyPartDescription(); bodyPartDescription.shape = rectangleshape; var bodyDescription = new scBodyDescription(); bodyDescription.bodyType = scBodyType.Static; bodyDescription.transform.position = new Vector2(200, 50); var bodyPartDescriptions = new List<scBodyPartDescription>(); bodyPartDescriptions.Add(bodyPartDescription); var body = World.createBody(bodyDescription, bodyPartDescriptions); TESTRECTANGLE = body; } }
private void simulateMovement(scBody body, float dt) { var newTransform = body.transform; // TODO: apply linear damping body.linearVelocity += gravity * dt; newTransform.position += body.linearVelocity * dt; // TODO: apply rotation body.transform = newTransform; }
private void detectAndResolveIntersection(scBody body, float dt) { }
public bool removeBody(scBody body) { if (bodies.Remove(body)) { if (bodyRemoved != null) { bodyRemoved(body); } return true; } return false; }
scDebugData GetDebugDataForBody(scBody body) { if(bodyMap.ContainsKey(body)) { return bodyMap[body]; } return defaultDebugData; }
public scDebugData GetOrCreateDebugDataForBody(scBody body) { if(bodyMap.ContainsKey(body)) return bodyMap[body]; var debugData = new scDebugData(); bodyMap.Add(body, debugData); return debugData; }