public UnitPictureBox() { //frame = new RectangleShape(new SFML.Window.Vector2f(122, 122)); Scale = new SFML.Window.Vector2f(122, 122); Position = new SFML.Window.Vector2f(32, 640 - 124); FillColor = Color.Black; }
private static bool BoundingBoxTest(OrientedBoundingBox OBB1, OrientedBoundingBox OBB2) { // Create the four distinct axes that are perpendicular to the edges of the two rectangles SFML.Window.Vector2f[] Axes = new SFML.Window.Vector2f[4] { new SFML.Window.Vector2f(OBB1.Points[1].X - OBB1.Points[0].X, OBB1.Points[1].Y - OBB1.Points[0].Y), new SFML.Window.Vector2f(OBB1.Points[1].X - OBB1.Points[2].X, OBB1.Points[1].Y - OBB1.Points[2].Y), new SFML.Window.Vector2f(OBB2.Points[0].X - OBB2.Points[3].X, OBB2.Points[0].Y - OBB2.Points[3].Y), new SFML.Window.Vector2f(OBB2.Points[0].X - OBB2.Points[1].X, OBB2.Points[0].Y - OBB2.Points[1].Y) }; for (int i = 0; i < 4; i++) // For each axis... { float MinOBB1 = 0.0f, MaxOBB1 = 0.0f, MinOBB2 = 0.0f, MaxOBB2 = 0.0f; // ... project the points of both OBBs onto the axis ... OBB1.ProjectOntoAxis(Axes[i], ref MinOBB1, ref MaxOBB1); OBB2.ProjectOntoAxis(Axes[i], ref MinOBB2, ref MaxOBB2); // ... and check whether the outermost projected points of both OBBs overlap. // If this is not the case, the Seperating Axis Theorem states that there can be no collision between the rectangles if (!((MinOBB2 <= MaxOBB1) && (MaxOBB2 >= MinOBB1))) { return(false); } } return(true); }
public AABB(SFML.Window.Vector2f position, float width, float height, SFML.Graphics.Color color) { Position = position; Extents = new SFML.Window.Vector2f(width, height); this.color = color; Configure(); }
public Shot(SFML.Window.Vector2f pos, string sprite, int dmg, int fireRate) { _damage = dmg; _fireRate = fireRate; _sprite = new SFML.Graphics.Sprite(); _sprite.Texture = new SFML.Graphics.Texture(sprite, new SFML.Graphics.IntRect(10, 10, 5, 5)); _sprite.Position = new SFML.Window.Vector2f(pos.X + 15, pos.Y + 32); }
public AABB(SFML.Window.Vector2f position, Vector2f extents, Color color) { Position = position; Extents = extents; this.color = color; Configure(); }
public AABB(SFML.Window.Vector2f position, Vector2f extents) { Position = position; Extents = extents; this.color = Color.White; Configure(); }
public AABB(SFML.Window.Vector2f position, float width, float height) { Position = position; Extents = new SFML.Window.Vector2f(width, height); this.color = Color.White; Configure(); }
private void Configure() { Center = new SFML.Window.Vector2f(position.X + (Extents.X / 2), position.Y + (Extents.Y / 2)); Sides = new LineSegment[4]; Sides[(int)AABBSide.enTop] = new LineSegment(Position, new SFML.Window.Vector2f(Position.X + Extents.X, Position.Y), color); Sides[(int)AABBSide.enRight] = new LineSegment(Sides[(int)AABBSide.enTop].End, new SFML.Window.Vector2f(Position.X + Extents.X, Position.Y + Extents.Y), color); Sides[(int)AABBSide.enBottom] = new LineSegment(Sides[(int)AABBSide.enRight].End, new SFML.Window.Vector2f(Position.X, Position.Y + Extents.Y), color); Sides[(int)AABBSide.enLeft] = new LineSegment(Sides[(int)AABBSide.enBottom].End, Position, color); }
public static bool CircleTest(SFML.Graphics.Sprite Object1, SFML.Graphics.Shape Object2) { SFML.Window.Vector2f Obj1Size = GetSpriteSize(Object1); SFML.Window.Vector2f Obj2Size = GetSpriteSize(Object2); float Radius1 = (Obj1Size.X + Obj1Size.Y) / 4.0f; float Radius2 = (Obj2Size.X + Obj2Size.Y) / 4.0f; SFML.Window.Vector2f Distance = GetSpriteCenter(Object1) - GetSpriteCenter(Object2); return(Distance.X * Distance.X + Distance.Y * Distance.Y <= (Radius1 + Radius2) * (Radius1 + Radius2)); }
void DragSelectionboxStartCallback() { if (!bDragging) { bDragging = true; //Define the start rect selectionRect = new FloatRect(0, 0, 0, 0); //Set the start location initMouseRectPos = Form1.mouseWorldPos; selectionBox.Position = initMouseRectPos; } }
public void AABBProjectionTestC() { float currentFrame = GetNextTestTime(); Vector2f velocity1 = new SFML.Window.Vector2f(300.0f, 300.0f); Vector2f velocity2 = new SFML.Window.Vector2f(400.0f, -300.0f); Vector2f startPosition1 = new SFML.Window.Vector2f(150.0f, 100.0f) + (velocity1 * currentFrame); Vector2f startPosition2 = new SFML.Window.Vector2f(175.0f, 350.0f) + (velocity2 * currentFrame); velocity1 = velocity1 - (velocity1 * currentFrame); velocity2 = velocity2 - (velocity2 * currentFrame); CollisionObject box1 = new CollisionObject(startPosition1, new Vector2f(50.0f, 50.0f), Color.Blue); CollisionObject box2 = new CollisionObject(startPosition2, new Vector2f(50.0f, 50.0f), Color.Magenta); box1.Velocity = velocity1; box2.Velocity = velocity2; CollisionResults results = CollisionManager.TestCollisions(box1, box2); window.Draw(results); }
private static SFML.Window.Vector2f GetSpriteSize(SFML.Graphics.Shape Object) { SFML.Graphics.IntRect OriginalSize = Object.TextureRect; SFML.Window.Vector2f Scale = Object.Scale; return(new SFML.Window.Vector2f(OriginalSize.Width * Scale.X, OriginalSize.Height * Scale.Y)); }
static public CollisionResults TestCollisions(CollisionObject object1, CollisionObject object2) { CollisionResults results = new CollisionResults() { Object1 = object1, Object2 = object2 }; //Are they already colliding? if (object1.BoundingBox.Overlaps(object2.BoundingBox)) { results.Type = CollisionType.enAbsolute; return(results); } //If both items are stationary, then there's no reason for projections. Return if ((object1.Velocity.X == 0.0f && object1.Velocity.Y == 0.0f) && (object2.Velocity.X == 0.0f && object2.Velocity.Y == 0.0f)) { return(results); } AABBProjection object1Projection = new AABBProjection(object1.BoundingBox, object1.Velocity); AABBProjection object2Projection = new AABBProjection(object2.BoundingBox, object2.Velocity); results.Object1Projection = object1Projection; results.Object2Projection = object2Projection; List <AABBProjection.AABBProjectionCollisionResult> projectionResults = null; bool collisions = (object1Projection.CollidesWith(object2Projection, out projectionResults) && ((object1.Velocity.X != 0.0f || object1.Velocity.Y != 0.0f) && (object2.Velocity.X != 0.0f || object2.Velocity.Y != 0.0f))); if (collisions) { List <AABBProjection.AABBProjectionCollisionResult> sorted = projectionResults.OrderBy(x => x.Length).ToList(); AABBProjection.AABBProjectionCollisionResult shortestResult = sorted[0]; AABBProjection.AABBProjectionSegmentEnum[] adjacentSegments = AABBProjection.GetAdjacentSegments(shortestResult.LocalSide); List <AABBProjection.AABBProjectionCollisionResult> adjacentSegmentResults = sorted.Where(x => x.OtherSide == shortestResult.OtherSide && adjacentSegments.Contains(x.LocalSide)).OrderBy(x => x.Length).ToList(); if (adjacentSegmentResults.Count > 0) { float shortestLength = adjacentSegmentResults[0].Length; //There could be multiples if we hit a corner adjacentSegmentResults = adjacentSegmentResults.Where(x => x.Length == shortestLength).ToList(); //Verify collision by projecting the AABB's to this point-in-time in their movement. float totalMovementDistance = Helpers.DistanceBetweenTwoPoints(object1.BoundingBox.Position, object1Projection.End.Position); //This is when the collision *MIGHT* happen for THIS collision object float pointInTime = shortestLength / totalMovementDistance; SFML.Window.Vector2f object1Position = object1.BoundingBox.Position + (object1.Velocity * pointInTime); AABB localCollisionResultProjection = new AABB(object1Position, object1.BoundingBox.Extents, SFML.Graphics.Color.Green); //Now, see where the OTHER collision object will be at this point in time. SFML.Window.Vector2f otherPosition = object2.BoundingBox.Position + (object2.Velocity * pointInTime); AABB otherCollisionResultProjection = new AABB(otherPosition, object2.BoundingBox.Extents, SFML.Graphics.Color.Blue); results.Object1CollisionAABB = localCollisionResultProjection; results.Object2CollisionAABB = otherCollisionResultProjection; results.CollisionTime = pointInTime; //Do they overlap at the projection result? if (!localCollisionResultProjection.Overlaps(otherCollisionResultProjection)) { return(results); } foreach (AABBProjection.AABBProjectionCollisionResult collisionResult in adjacentSegmentResults) { AABB.AABBSide collisionSide = AABBProjection.GetSideFromProjectionSegments(shortestResult.LocalSide, collisionResult.LocalSide); AABB.AABBSide otherCollisionSide = AABB.GetOppositeSide(collisionSide); results.Object1CollisionAABB.Sides[(int)collisionSide].SetColor(SFML.Graphics.Color.Red); results.Object2CollisionAABB.Sides[(int)otherCollisionSide].SetColor(SFML.Graphics.Color.Red); results.Sides[collisionSide] = otherCollisionSide; } results.Type = CollisionType.enPrediction; } } else { List <ProjectionsSideCollisionInfo> collisionInfo = null; AABBProjection checkedObjectProjection = null; if (object1.Velocity.X != 0.0f || object1.Velocity.Y != 0.0f) { //Test projection1 against projection2's End's segments TestProjectionAgainstAABB(object1Projection, object2Projection.End, out collisionInfo); checkedObjectProjection = object1Projection; } else if (object2.Velocity.X != 0.0f || object2.Velocity.Y != 0.0f) { //Test projection2 against projection1's End segments TestProjectionAgainstAABB(object2Projection, object1Projection.End, out collisionInfo); checkedObjectProjection = object2Projection; } if (collisionInfo != null) { if (collisionInfo.Count > 0) { collisionInfo = collisionInfo.OrderBy(x => x.Length).ToList(); ProjectionsSideCollisionInfo closestInfo = collisionInfo[0]; float shortestLength = closestInfo.Length; //Verify collision by projecting the AABB's to this point-in-time in their movement. float totalMovementDistance = Helpers.DistanceBetweenTwoPoints(checkedObjectProjection.Start.Position, checkedObjectProjection.End.Position); //This is when the collision *MIGHT* happen for THIS collision object float pointInTime = shortestLength / totalMovementDistance; SFML.Window.Vector2f object1Position = object1.BoundingBox.Position + (object1.Velocity * pointInTime); AABB localCollisionResultProjection = new AABB(object1Position, object1.BoundingBox.Extents, SFML.Graphics.Color.Green); //Now, see where the OTHER collision object will be at this point in time. SFML.Window.Vector2f otherPosition = object2.BoundingBox.Position + (object2.Velocity * pointInTime); AABB otherCollisionResultProjection = new AABB(otherPosition, object2.BoundingBox.Extents, SFML.Graphics.Color.Blue); //Do they overlap at the projection result? //if (!localCollisionResultProjection.Overlaps(otherCollisionResultProjection)) //return results; results.Object1CollisionAABB = localCollisionResultProjection; results.Object2CollisionAABB = otherCollisionResultProjection; AABB.AABBSide object1Side = AABB.GetOppositeSide(closestInfo.Side); results.Object1CollisionAABB.Sides[(int)object1Side].SetColor(SFML.Graphics.Color.Red); results.Object2CollisionAABB.Sides[(int)closestInfo.Side].SetColor(SFML.Graphics.Color.Red); results.Type = CollisionType.enPrediction; results.Sides[object1Side] = closestInfo.Side; results.CollisionTime = pointInTime; } } } if (results.CollisionTime == (1.0f / 0.0f)) { int i = 0; } return(results); }
public void AABBProjectionTestE() { float currentFrame = GetNextTestTime(); Vector2f velocity1 = new SFML.Window.Vector2f(0.0f, -249.0f); Vector2f velocity2 = new SFML.Window.Vector2f(1.0f, -1.0f); Vector2f startPosition1 = new SFML.Window.Vector2f(300.0f, 550.0f) + (velocity1 * currentFrame); Vector2f startPosition2 = new SFML.Window.Vector2f(340.0f, 300.0f) + (velocity2 * currentFrame); velocity1 = velocity1 - (velocity1 * currentFrame); velocity2 = velocity2 - (velocity2 * currentFrame); CollisionObject box1 = new CollisionObject(startPosition1, new Vector2f(50.0f, 50.0f), Color.Blue); CollisionObject box2 = new CollisionObject(startPosition2, new Vector2f(50.0f, 50.0f), Color.Magenta); box1.Velocity = velocity1; box2.Velocity = velocity2; CollisionResults results = CollisionManager.TestCollisions(box1, box2); window.Draw(results); }
private void Form1_Load(object sender, EventArgs e) { //Intialize config data InitConfigData(); //Intialize viewport data _grid = new EditorGrid(); Console.WriteLine(); EditorDebug _debug = new EditorDebug(new SFML.Graphics.Font(Directory.GetCurrentDirectory() + "/EditorResources/pixelmix.ttf")); Stopwatch deltaClock = new Stopwatch(); TimeSpan _deltaTime = new TimeSpan(); Stopwatch clock = new Stopwatch(); float frames = 0; float fps = 0; clock.Start(); while (Visible) { Application.DoEvents(); RENDER_WINDOW.DispatchEvents(); RENDER_WINDOW.Clear(viewportBackgroundColor); //Calculate deltaTime _deltaTime = deltaClock.Elapsed; deltaClock.Restart(); Editor.deltaTime = _deltaTime.TotalSeconds; ///////////////////// /// PROCESS INPUT ///////////////////// EditorInput.Update(); ///////////////////// /// UPDATE ///////////////////// Editor.SCENE.Update(); //Update the camera VIEWPORT_CAMERA_VIEW.Update(); ///////////////////// /// DRAW ///////////////////// RENDER_WINDOW.SetView(VIEWPORT_CAMERA_VIEW); //Get debug info mouseScreenPos = Mouse.GetPosition(RENDER_WINDOW); mouseWorldPos = RENDER_WINDOW.MapPixelToCoords(mouseScreenPos); //Draw the editor grid RENDER_WINDOW.Draw(_grid); //Do scene drawing and update Editor.SCENE.Draw(RENDER_WINDOW, RenderStates.Default); VIEWPORT_CAMERA_VIEW.Draw(RENDER_WINDOW, RenderStates.Default); /////////////////// /// DRAW UI CAMERA /////////////////// RENDER_WINDOW.SetView(VIEWPORT_UI_VIEW); //Update ui camera VIEWPORT_UI_VIEW.Size = new Vector2f(RENDER_SURFACE.Width, RENDER_SURFACE.Height); VIEWPORT_UI_VIEW.Center = new Vector2f(RENDER_SURFACE.Width / 2, RENDER_SURFACE.Height / 2); //Draw debug information _debug.SetDebugInfo(0, "FPS: " + fps.ToString()); _debug.SetDebugInfo(1, "Mouse Screen Position: " + mouseScreenPos); _debug.SetDebugInfo(2, "Mouse World Position: " + mouseWorldPos); _debug.SetDebugInfo(3, "Scene Entity Count: " + EditorScene.SCENE_ACTORS.Count); RENDER_WINDOW.Draw(_debug); RENDER_WINDOW.Display(); frames++; //Calculate FPS if (clock.Elapsed.Seconds > 0) { fps = frames / clock.Elapsed.Seconds; frames = 0; clock.Restart(); } } }