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 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; } }
public static bool detectRectangleEdge(scTransform rectangleTransform, scRectangleShape rectangle, scTransform edgeTransform, scEdgeShape edge) { var transformedRectangleVertices = new List<Vector2>(); foreach (var vertex in rectangle.vertices) { transformedRectangleVertices.Add(scTransformUtils.applyTransform(rectangleTransform, vertex)); } var edgeStart = scTransformUtils.applyTransform(edgeTransform, edge.start); var edgeEnd = scTransformUtils.applyTransform(edgeTransform, edge.end); var transformedEdgeVertices = new List<Vector2>(); transformedEdgeVertices.Add(edgeStart); transformedEdgeVertices.Add(edgeEnd); var axes = new List<Vector2>(); axes.Add(scCollisionHelpers.getNormal(edgeStart, edgeEnd)); 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])); foreach (var axis in axes) { var lineSegmentRectangle = scCollisionHelpers.projectShapeOnAxis(transformedRectangleVertices, axis); var lineSegmentEdge = scCollisionHelpers.projectShapeOnAxis(transformedEdgeVertices, axis); if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentRectangle, lineSegmentEdge)) { return false; } } return true; }
public static bool detectEdgeEdge(scTransform aTransform, scEdgeShape aShape, scTransform bTransform, scEdgeShape bShape) { return false; }