public override void Initialize(ConfigSection section) { base.Initialize(section); textureName = section["texture"]; circlePos = section["position"].AsVector2(); Radius = section["radius"]; var bodyDescription = new scBodyDescription(); bodyDescription.bodyType = scBodyType.Kinematic; bodyDescription.transform.position = circlePos; bodyDescription.inertiaScale = section["inertiaScale"]; bodyDescription.linearDamping = section["linearDamping"]; bodyDescription.angularDamping = section["angularDamping"]; bodyDescription.userData = this; var shape = new scCircleShape(); shape.radius = Radius; var bodyPartDescription = new scBodyPartDescription(); bodyPartDescription.restitution = section["restitution"]; bodyPartDescription.density = section["density"]; bodyPartDescription.shape = shape; bodyPartDescription.friction = section["friction"]; Body = Game.level.World.createBody(bodyDescription, bodyPartDescription); Body.owner = this; }
public static bool detectCircleEdge(scTransform circleTransform, scCircleShape circle, scTransform edgeTransform, scEdgeShape edge) { var transformedEdgeVertices = new List<Vector2>(); transformedEdgeVertices.Add(scTransformUtils.applyTransform(edgeTransform, edge.start)); transformedEdgeVertices.Add(scTransformUtils.applyTransform(edgeTransform, edge.end)); var transformedCircleCenter = scTransformUtils.applyTransform(circleTransform, circle.localPosition); //get nearest edge vertex to circle center var distanceToFirstVertex = transformedEdgeVertices[0] - transformedCircleCenter; var distanceToSecondVertex = transformedEdgeVertices[1] - transformedCircleCenter; var circleAxis = distanceToFirstVertex.Length() <= distanceToSecondVertex.Length() ? distanceToFirstVertex : distanceToSecondVertex; circleAxis.Normalize(); var edgeAxis = scCollisionHelpers.getNormal(transformedEdgeVertices[0], transformedEdgeVertices[1]); { var lineSegmentCircle = scCollisionHelpers.projectCircleOnAxis(transformedCircleCenter, circle.radius, circleAxis); var lineSegmentEdge = scCollisionHelpers.projectShapeOnAxis(transformedEdgeVertices, circleAxis); if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentCircle, lineSegmentEdge)) { return false; } } { var lineSegmentCircle = scCollisionHelpers.projectCircleOnAxis(transformedCircleCenter, circle.radius, edgeAxis); var lineSegmentEdge = scCollisionHelpers.projectShapeOnAxis(transformedEdgeVertices, edgeAxis); if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentCircle, lineSegmentEdge)) { return false; } } return true; }
public static bool detectCircleCircle(scTransform aTransform, scCircleShape aShape, scTransform bTransform, scCircleShape bShape) { var aCenter = aTransform.position + aShape.localPosition; var bCenter = bTransform.position + bShape.localPosition; var deltaVector = aCenter - bCenter; return deltaVector.Length() < aShape.radius + bShape.radius; }
public static bool detectCircleRectangle(scTransform circleTransform, scCircleShape circle, scTransform rectangleTransform, scRectangleShape rectangle) { var transformedRectangleVertices = new List<Vector2>(); foreach (var vertex in rectangle.vertices) { transformedRectangleVertices.Add(scTransformUtils.applyTransform(rectangleTransform, vertex)); } var axes = new List<Vector2>(); axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[0], transformedRectangleVertices[1])); axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[1], transformedRectangleVertices[2])); axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[2], transformedRectangleVertices[3])); axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[3], transformedRectangleVertices[0])); var transformedCircleCenter = scTransformUtils.applyTransform(circleTransform, circle.localPosition); //get nearest edge vertex to circle center var distancesToVertices = new List<Vector2>(); distancesToVertices.Add(transformedRectangleVertices[0] - transformedCircleCenter); distancesToVertices.Add(transformedRectangleVertices[1] - transformedCircleCenter); distancesToVertices.Add(transformedRectangleVertices[2] - transformedCircleCenter); distancesToVertices.Add(transformedRectangleVertices[3] - transformedCircleCenter); var circleAxis = distancesToVertices[0]; foreach (var distance in distancesToVertices) { if (distance.Length() < circleAxis.Length()) { circleAxis = distance; } } circleAxis.Normalize(); axes.Add(circleAxis); foreach (var axis in axes) { var lineSegmentRectangleA = scCollisionHelpers.projectCircleOnAxis(transformedCircleCenter, circle.radius, axis); var lineSegmentRectangleB = scCollisionHelpers.projectShapeOnAxis(transformedRectangleVertices, axis); if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentRectangleA, lineSegmentRectangleB)) { return false; } } return true; }
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; } }