private Vector set_velocity() { Vector velocity = new Vector(); switch (CHARAKTER.direction) { case 0: velocity = new Vector(-CHARAKTER.speed.X, 0); break; case 1: velocity = new Vector(0, CHARAKTER.speed.Y); break; case 2: velocity = new Vector(0, -CHARAKTER.speed.Y); break; case 3: velocity = new Vector(CHARAKTER.speed.X, 0); break; case 4: velocity = new Vector(-CHARAKTER.speed.X, CHARAKTER.speed.Y); break; case 5: velocity = new Vector(CHARAKTER.speed.X, CHARAKTER.speed.Y); break; case 6: velocity = new Vector(-CHARAKTER.speed.X, -CHARAKTER.speed.Y); break; case 7: velocity = new Vector(CHARAKTER.speed.X, -CHARAKTER.speed.Y); break; } return velocity; }
private void charakter_collision(ref Vector playerTranslation) { PolygonCollisionResult result; Objcount = 0; int DmodPosY = (int)CHARAKTER.sp.Y+1; int DmodPosX = (int)CHARAKTER.sp.X - 1; int maxX = 3; int maxY = 3; if (DmodPosX >= RESOURCES.detailmapsize - 1) maxX = 0; if (DmodPosY >= RESOURCES.detailmapsize - 1) maxY = 0; int DmaxPosX = DmodPosX + maxX; int DmaxPosY = DmodPosY + maxY; if (DmodPosY < 0) DmodPosY = 0; if (DmodPosX < 0) DmodPosX = 0; int collisionsCount = 0; for (int y = DmodPosY; y < DmaxPosY; y++) { for (int x = DmodPosX; x < DmaxPosX; x++) { if (RESOURCES.detailmap[y, x].detailmap != -1 && RESOURCES.detailmap[y, x].detailmap != -2) { RESOURCES.detailmap[y, x].destinationRect.X = PositionX + 128 * x; RESOURCES.detailmap[y, x].destinationRect.Y = (PositionY + 97 * y) - RESOURCES.detailmap[y, x].destinationRect.Height; if (CHARAKTER.Destbox.IntersectsWith(RESOURCES.detailmap[y, x].destinationRect) == true) { Objcount++; RESOURCES.detailmap[y, x].THITBOX(RESOURCES.detailmap[y, x].destinationRect.X + playerTranslation.X, RESOURCES.detailmap[y, x].destinationRect.Y + playerTranslation.Y); if (RESOURCES.detailmap[y, x].HitBox.Points[0].Y != RESOURCES.detailmap[y, x].HitBox.Points[1].Y) //KEINE NORMALE BOUNDING BOX { result = ENGINE.MATHENGINE.PolygonCollision(CHARAKTER.HitBox, RESOURCES.detailmap[y, x].DESTHitBox, playerTranslation); if (ENGINE.MATHENGINE.behind(new PointF(CHARAKTER.HitBox.Points[0].X, CHARAKTER.HitBox.Points[0].Y), new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[0].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[0].Y), new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[1].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[1].Y)) == true) { CHARAKTER.Zebene = RESOURCES.detailmap[y, x].zebene - 0.001f; RESOURCES.detailmap[y, x].color = Color.FromArgb(200, 255, 255, 255); } else { CHARAKTER.Zebene = (CHARAKTER.HitBox.Points[0].Y + 100) / 1000f; RESOURCES.detailmap[y, x].color = Color.White; } if (result.WillIntersect) { collisionsCount++; if (collisionsCount <= 1) { playerTranslation = -result.MinimumTranslationVector + playerTranslation; CHARAKTER.caminar = false; } else { playerTranslation = new Vector(0, 0); CHARAKTER.caminar = false; } } } else //BEI NORMALER BOUNDING BOX !!! { /////////////////////////////////////////////// if (CHARAKTER.HitBox.Points[0].Y < RESOURCES.detailmap[y, x].DESTHitBox.Points[0].Y) { //dahinter //RESOURCES.detailmap[y, x].zebene = CHARAKTER.Zebene + (RESOURCES.detailmap[y, x].DESTHitBox.Points[0].Y+100) / 100f; RESOURCES.detailmap[y, x].color = Color.FromArgb(200, 255, 255, 255); } else { //davor //RESOURCES.detailmap[y, x].zebene = CHARAKTER.Zebene - Objcount / 20f; RESOURCES.detailmap[y, x].color = Color.White; } //////////////////////////////////////////////// if (CHARAKTER.HitBox.intersect(RESOURCES.detailmap[y, x].DESTHitBox.Points) == true) { playerTranslation = new Vector(0, 0); CHARAKTER.caminar = false; } } if (DEBUG) { line.draw_line(new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[0].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[0].Y), new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[1].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[1].Y)); line.draw_line(new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[1].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[1].Y), new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[2].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[2].Y)); line.draw_line(new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[2].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[2].Y), new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[3].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[3].Y)); line.draw_line(new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[3].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[3].Y), new PointF(RESOURCES.detailmap[y, x].DESTHitBox.Points[0].X, RESOURCES.detailmap[y, x].DESTHitBox.Points[0].Y)); } } else { CHARAKTER.Zebene = (CHARAKTER.HitBox.Points[0].Y + 100) / 1000f; RESOURCES.detailmap[y, x].color = Color.White; } } } } }
public void Offset(Vector v) { Offset(v.X, v.Y); }
public void Offset(float x, float y) { for (int i = 0; i < points.Count; i++) { Vector p = points[i]; points[i] = new Vector(p.X + x, p.Y + y); } }
// Calculate the projection of a polygon on an axis and returns it as a [min, max] interval public void ProjectPolygon(Vector axis, Polygon polygon, ref float min, ref float max) { // To project a point on an axis use the dot product float d = axis.DotProduct(polygon.Points[0]); min = d; max = d; for (int i = 0; i < polygon.Points.Count; i++) { d = polygon.Points[i].DotProduct(axis); if (d < min) { min = d; } else { if (d > max) { max = d; } } } }
// Check if polygon A is going to collide with polygon B for the given velocity public PolygonCollisionResult PolygonCollision(Polygon polygonA, Polygon polygonB, Vector velocity) { PolygonCollisionResult result = new PolygonCollisionResult(); result.Intersect = false; result.WillIntersect = true; int edgeCountA = polygonA.Edges.Count; int edgeCountB = polygonB.Edges.Count; float minIntervalDistance = float.PositiveInfinity; Vector translationAxis = new Vector(); Vector edge; // Loop through all the edges of both polygons for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) { if (edgeIndex < edgeCountA) { edge = polygonA.Edges[edgeIndex]; } else { edge = polygonB.Edges[edgeIndex - edgeCountA]; } // ===== 1. Find if the polygons are currently intersecting ===== // Find the axis perpendicular to the current edge Vector axis = new Vector(-edge.Y, edge.X); axis.Normalize(); // Find the projection of the polygon on the current axis float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; ProjectPolygon(axis, polygonA, ref minA, ref maxA); ProjectPolygon(axis, polygonB, ref minB, ref maxB); // Check if the polygon projections are currentlty intersecting //if (IntervalDistance(minA, maxA, minB, maxB) > 0) result.Intersect = false; // ===== 2. Now find if the polygons *will* intersect ===== // Project the velocity on the current axis float velocityProjection = axis.DotProduct(velocity); // Get the projection of polygon A during the movement if (velocityProjection < 0) { minA += velocityProjection; } else { maxA += velocityProjection; } // Do the same test as above for the new projection float intervalDistance = IntervalDistance(minA, maxA, minB, maxB); if (intervalDistance > 0) result.WillIntersect = false; // If the polygons are not intersecting and won't intersect, exit the loop if (!result.Intersect && !result.WillIntersect) break; // Check if the current interval distance is the minimum one. If so store // the interval distance and the current distance. // This will be used to calculate the minimum translation vector intervalDistance = Math.Abs(intervalDistance); if (intervalDistance < minIntervalDistance) { minIntervalDistance = intervalDistance; translationAxis = axis; Vector d = polygonA.Center - polygonB.Center; if (d.DotProduct(translationAxis) < 0) translationAxis = -translationAxis; } } // The minimum translation vector can be used to push the polygons appart. // First moves the polygons by their velocity // then move polygonA by MinimumTranslationVector. if (result.WillIntersect) result.MinimumTranslationVector = translationAxis * minIntervalDistance; return result; }
public bool Equals(Vector v) { return X == v.X && Y == v.Y; }
public float DotProduct(Vector vector) { return this.X * vector.X + this.Y * vector.Y; }
public float DistanceTo(Vector vector) { return (float)Math.Sqrt(Math.Pow(vector.X - this.X, 2) + Math.Pow(vector.Y - this.Y, 2)); }