public Tank() { Size = new SFML.Window.Vector2f(32, 32); FillColor = Color.Black; initStats(); }
// IMPORTANT: a1 and a2 cannot be the same, e.g. a1--a2 is a true segment, not a point // b1/b2 may be the same (b1--b2 is a point) private static SFML.Window.Vector2f[] OneD_Intersection(SFML.Window.Vector2f a1, SFML.Window.Vector2f a2, SFML.Window.Vector2f b1, SFML.Window.Vector2f b2) { //float ua1 = 0.0f; // by definition //float ua2 = 1.0f; // by definition float ub1, ub2; float denomx = a2.X - a1.X; float denomy = a2.Y - a1.Y; if (Math.Abs(denomx) > Math.Abs(denomy)) { ub1 = (b1.X - a1.X) / denomx; ub2 = (b2.X - a1.X) / denomx; } else { ub1 = (b1.Y - a1.Y) / denomy; ub2 = (b2.Y - a1.Y) / denomy; } List <SFML.Window.Vector2f> ret = new List <SFML.Window.Vector2f>(); float[] interval = OverlapIntervals(ub1, ub2); foreach (float f in interval) { float x = a2.X * f + a1.X * (1.0f - f); float y = a2.Y * f + a1.Y * (1.0f - f); SFML.Window.Vector2f p = new SFML.Window.Vector2f(x, y); ret.Add(p); } return(ret.ToArray()); }
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 PointOnLine(SFML.Window.Vector2f p, SFML.Window.Vector2f a1, SFML.Window.Vector2f a2) { float dummyU = 0.0f; float d = DistFromSeg(p, a1, a2, MyEpsilon, ref dummyU); return(d < MyEpsilon); }
static public float DistanceBetweenTwoPoints(SFML.Window.Vector2f a, SFML.Window.Vector2f b) { float distanceX = b.X - a.X; float distanceY = b.Y - a.Y; return((float)Math.Sqrt((Double)(distanceX * distanceX) + (Double)(distanceY * distanceY))); }
public static bool BoundingBoxTest(SFML.Graphics.Sprite Object1, SFML.Graphics.Sprite Object2) { OrientedBoundingBox OBB1 = new OrientedBoundingBox(Object1); OrientedBoundingBox OBB2 = new OrientedBoundingBox(Object2); // 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 LineSegment(SFML.Window.Vector2f start, SFML.Window.Vector2f end) { Start = start; End = end; linePoints = new VertexArray(PrimitiveType.Lines); linePoints.Append(new Vertex(start)); linePoints.Append(new Vertex(end)); }
public AABBProjection(AABB start, SFML.Window.Vector2f movement, SFML.Graphics.Color color) { Start = start; End = new AABB(new SFML.Window.Vector2f(start.Position.X + movement.X, start.Position.Y + movement.Y), start.Extents.X, start.Extents.Y, Start.Color); this.color = color; InitializeSegments(); }
static void w_MouseMoved(object sender, SFML.Window.MouseMoveEventArgs e) { if (_point.HasValue) { SFML.Window.Vector2f newPoint = new SFML.Window.Vector2f(e.X, e.Y); SFML.Window.Vector2f delta = _point.Value - newPoint; SFML.Graphics.RenderWindow w = (SFML.Graphics.RenderWindow)sender; w.SetView(new SFML.Graphics.View(w.GetView().Center + delta, w.GetView().Size)); _point = newPoint; } }
public OrientedBoundingBox(SFML.Graphics.Sprite Object) // Calculate the four points of the OBB from a transformed (scaled, rotated...) sprite { Points = new SFML.Window.Vector2f[4]; SFML.Graphics.Transform trans = Object.Transform; SFML.Graphics.IntRect local = Object.TextureRect; Points[0] = trans.TransformPoint(0.0f, 0.0f); Points[1] = trans.TransformPoint(local.Width, 0.0f); Points[2] = trans.TransformPoint(local.Width, local.Height); Points[3] = trans.TransformPoint(0.0f, local.Height); }
public static bool CircleTest(SFML.Graphics.Sprite Object1, SFML.Graphics.Sprite 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)); }
public OrientedBoundingBox(SFML.Graphics.Shape Object) // Calculate the four points of the OBB from a transformed (scaled, rotated...) sprite { Points = new SFML.Window.Vector2f[4]; SFML.Graphics.Transform trans = Object.Transform; Points[0] = trans.TransformPoint(0.0f, 0.0f); Points[1] = trans.TransformPoint(Object.GetLocalBounds().Width, 0.0f); Points[2] = trans.TransformPoint(Object.GetLocalBounds().Width, Object.GetLocalBounds().Height); Points[3] = trans.TransformPoint(0.0f, Object.GetLocalBounds().Height); }
public LineSegment(SFML.Window.Vector2f start, SFML.Window.Vector2f end, SFML.Graphics.Color color) { Start = start; End = end; linePoints = new VertexArray(PrimitiveType.Lines); linePoints.Append(new Vertex(start) { Color = color }); linePoints.Append(new Vertex(end) { Color = color }); }
public void ProjectOntoAxis(SFML.Window.Vector2f Axis, ref float Min, ref float Max) // Project all four points of the OBB onto the given axis and return the dotproducts of the two outermost points { Min = (Points[0].X * Axis.X + Points[0].Y * Axis.Y); Max = Min; for (int j = 1; j < 4; j++) { float Projection = (Points[j].X * Axis.X + Points[j].Y * Axis.Y); if (Projection < Min) { Min = Projection; } if (Projection > Max) { Max = Projection; } } }
private static float DistFromSeg(SFML.Window.Vector2f p, SFML.Window.Vector2f q0, SFML.Window.Vector2f q1, float radius, ref float u) { // formula here: //http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html // where x0,y0 = p // x1,y1 = q0 // x2,y2 = q1 float dx21 = q1.X - q0.X; float dy21 = q1.Y - q0.Y; float dx10 = q0.X - p.X; float dy10 = q0.Y - p.Y; float segLength = (float)Math.Sqrt((double)dx21 * dx21 + dy21 * dy21); if (segLength < MyEpsilon) { throw new Exception("Expected line segment, not point."); } float num = Math.Abs(dx21 * dy10 - dx10 * dy21); float d = num / segLength; return(d); }
// this is the general case. Really really general public static SFML.Window.Vector2f[] Intersection(SFML.Window.Vector2f a1, SFML.Window.Vector2f a2, SFML.Window.Vector2f b1, SFML.Window.Vector2f b2) { if (a1.Equals(a2) && b1.Equals(b2)) { // both "segments" are points, return either point if (a1.Equals(b1)) { return new SFML.Window.Vector2f[] { a1 } } ; else // both "segments" are different points, return empty set { return new SFML.Window.Vector2f[] { } }; } else if (b1.Equals(b2)) // b is a point, a is a segment { if (PointOnLine(b1, a1, a2)) { return new SFML.Window.Vector2f[] { b1 } } ; else { return new SFML.Window.Vector2f[] { } }; } else if (a1.Equals(a2)) // a is a point, b is a segment { if (PointOnLine(a1, b1, b2)) { return new SFML.Window.Vector2f[] { a1 } } ; else { return new SFML.Window.Vector2f[] { } }; } // at this point we know both a and b are actual segments float ua_t = (b2.X - b1.X) * (a1.Y - b1.Y) - (b2.Y - b1.Y) * (a1.X - b1.X); float ub_t = (a2.X - a1.X) * (a1.Y - b1.Y) - (a2.Y - a1.Y) * (a1.X - b1.X); float u_b = (b2.Y - b1.Y) * (a2.X - a1.X) - (b2.X - b1.X) * (a2.Y - a1.Y); // Infinite lines intersect somewhere if (!(-MyEpsilon < u_b && u_b < MyEpsilon)) // e.g. u_b != 0.0 { float ua = ua_t / u_b; float ub = ub_t / u_b; if (0.0f <= ua && ua <= 1.0f && 0.0f <= ub && ub <= 1.0f) { // Intersection return(new SFML.Window.Vector2f[] { new SFML.Window.Vector2f(a1.X + ua * (a2.X - a1.X), a1.Y + ua * (a2.Y - a1.Y)) }); } else { // No Intersection return(new SFML.Window.Vector2f[] { }); } } else // lines (not just segments) are parallel or the same line { // Coincident // find the common overlapping section of the lines // first find the distance (squared) from one point (a1) to each point if ((-MyEpsilon < ua_t && ua_t < MyEpsilon) || (-MyEpsilon < ub_t && ub_t < MyEpsilon)) { if (a1.Equals(a2)) // danger! { return(OneD_Intersection(b1, b2, a1, a2)); } else // safe { return(OneD_Intersection(a1, a2, b1, b2)); } } else { // Parallel return(new SFML.Window.Vector2f[] { }); } } } #endregion }
private void DrawPreviewLighting(SFML.Graphics.RenderWindow rw) { //pack data in matrix as SFML doesn't support array uniforms SFML.Window.Vector2f[] positions = new SFML.Window.Vector2f[m_maxLights]; for (var i = 0; i < m_lights.Count; ++i) { positions[i] = m_lights[i].Position; } SFML.Graphics.Transform t = new SFML.Graphics.Transform( positions[0].X, positions[0].Y, positions[1].X, positions[1].Y, positions[2].X, positions[2].Y, positions[3].X, positions[3].Y, 0f); m_lightShaderTextured.SetParameter("u_lightColour", m_sunColour); m_lightShaderTextured.SetParameter("u_ambientColour", m_ambientColour); m_lightShaderTextured.SetParameter("u_lightPositions", t); m_lightShaderColoured.SetParameter("u_lightColour", m_sunColour); m_lightShaderColoured.SetParameter("u_ambientColour", m_ambientColour); m_lightShaderColoured.SetParameter("u_lightPositions", t); for (var i = 0; i < m_lights.Count; ++i) { string param = "u_pointColour" + i.ToString(); m_lightShaderTextured.SetParameter(param, m_lights[i].Colour); m_lightShaderColoured.SetParameter(param, m_lights[i].Colour); } SFML.Graphics.RenderStates states = SFML.Graphics.RenderStates.Default; foreach (var layer in m_previewLayers) { //if (layer != m_previewLayers[(int)Layer.Dynamic]) //{ // states.Shader = m_lightShaderTextured; //} foreach (var d in layer) { if (d.Texture == null) { m_lightShaderColoured.SetParameter("u_inverseWorldViewMatrix", d.InverseTransform); states.Shader = m_lightShaderColoured; } else { m_lightShaderTextured.SetParameter("u_texture", SFML.Graphics.Shader.CurrentTexture); m_lightShaderTextured.SetParameter("u_inverseWorldViewMatrix", d.InverseTransform); states.Shader = m_lightShaderTextured; } rw.Draw(d, states); } if (layer == m_previewLayers[(int)Layer.Background] && checkBoxSnap.Checked) { rw.Draw(m_grid); } } }
private static SFML.Window.Vector2f GetSpriteSize(SFML.Graphics.Sprite 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)); }
// IMPORTANT: a1 and a2 cannot be the same, e.g. a1--a2 is a true segment, not a point // b1/b2 may be the same (b1--b2 is a point) private static SFML.Window.Vector2f[] OneD_Intersection(SFML.Window.Vector2f a1, SFML.Window.Vector2f a2, SFML.Window.Vector2f b1, SFML.Window.Vector2f b2) { //float ua1 = 0.0f; // by definition //float ua2 = 1.0f; // by definition float ub1, ub2; float denomx = a2.X - a1.X; float denomy = a2.Y - a1.Y; if (Math.Abs(denomx) > Math.Abs(denomy)) { ub1 = (b1.X - a1.X) / denomx; ub2 = (b2.X - a1.X) / denomx; } else { ub1 = (b1.Y - a1.Y) / denomy; ub2 = (b2.Y - a1.Y) / denomy; } List<SFML.Window.Vector2f> ret = new List<SFML.Window.Vector2f>(); float[] interval = OverlapIntervals(ub1, ub2); foreach (float f in interval) { float x = a2.X * f + a1.X * (1.0f - f); float y = a2.Y * f + a1.Y * (1.0f - f); SFML.Window.Vector2f p = new SFML.Window.Vector2f(x, y); ret.Add(p); } return ret.ToArray(); }