private void ApplyWind(Vector2 position) { Vector2 min = Vector2.Subtract(position, new Vector2(0, 100)); Vector2 max = Vector2.Add(position, new Vector2(300, 100)); AABB aabb = new AABB(ref min, ref max); foreach (Body body in _physicsSimulator.BodyList) { if (aabb.Contains(body.Position)) { body.ApplyForce(new Vector2(400,0)); } } }
private void ApplyExplosion(Vector2 position) { Vector2 min = Vector2.Subtract(position, new Vector2(100, 100)); Vector2 max = Vector2.Add(position, new Vector2(100, 100)); AABB aabb = new AABB(ref min, ref max); foreach (Body body in _physicsSimulator.BodyList) { if (aabb.Contains(body.Position)) { Vector2 fv = body.Position; fv = Vector2.Subtract(fv, position); fv.Normalize(); fv = Vector2.Multiply(fv, 50000); body.ApplyForce(fv); } } }
// This is used in my editing application. /// <summary> /// Gets index of control point if point is inside it. /// </summary> /// <param name="point">Point to test against.</param> /// <returns>Index of control point or -1 if no intersection.</returns> public int PointInControlPoint(Vector2 point) { AABB controlPointAABB; foreach (Vector2 controlPoint in _controlPoints) { controlPointAABB = new AABB( new Vector2(controlPoint.X - (_controlPointSize/2), controlPoint.Y - (_controlPointSize/2)), new Vector2(controlPoint.X + (_controlPointSize/2), controlPoint.Y + (_controlPointSize/2))); if (controlPointAABB.Contains(ref point)) return _controlPoints.IndexOf(controlPoint); } return -1; }
/// <summary> /// tells you if drawing the texture will actually draw onscreen. /// </summary> /// <param name="tex"> /// the texture to check. /// </param> /// <param name="position"> /// the position of the texture's center. /// </param> /// <param name="origin"> /// a Vector2 equaling half of the texture's size. /// </param> /// <param name="rotation"> /// the rotation of the texture, in radians. /// </param> /// <returns> /// a bool indicating whether you should draw this texture. /// </returns> public bool ShouldDraw(Texture2D tex, Vector2 position, Vector2 origin, float rotation) { Matrix textureMatrix = Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3(position, 0)); Vector2 topLeft = Vector2.Transform(-origin, textureMatrix); Vector2 topRight = Vector2.Transform(new Vector2(origin.X, -origin.Y), textureMatrix); Vector2 botLeft = Vector2.Transform(new Vector2(-origin.X, origin.Y), textureMatrix); Vector2 botRight = Vector2.Transform(origin, textureMatrix); AABB texAABB = new AABB(); if (rotation >= 0 && rotation <= MathHelper.PiOver2) { Vector2 v1 = new Vector2(botLeft.X, topLeft.Y); Vector2 v2 = new Vector2(topRight.X, botRight.Y); texAABB = new AABB(ref v1, ref v2); } else if (rotation > MathHelper.PiOver2 && rotation <= MathHelper.Pi) { Vector2 v1 = new Vector2(botRight.X, botLeft.Y); Vector2 v2 = new Vector2(topLeft.X, topRight.Y); texAABB = new AABB(ref v1, ref v2); } else if (rotation > MathHelper.Pi && rotation <= (MathHelper.Pi + MathHelper.PiOver2)) { Vector2 v1 = new Vector2(botRight.X, topRight.Y); Vector2 v2 = new Vector2(botLeft.X, topLeft.Y); texAABB = new AABB(ref v1, ref v2); } else if (rotation > (MathHelper.Pi + MathHelper.PiOver2) && rotation <= MathHelper.TwoPi) { Vector2 v1 = new Vector2(topLeft.X, topRight.Y); Vector2 v2 = new Vector2(botRight.X, botLeft.Y); texAABB = new AABB(ref v1, ref v2); } Matrix simpleCameraMatrix = Matrix.Identity * Matrix.CreateTranslation(new Vector3(-_position, 0)) * Matrix.CreateRotationZ(_rotation) * Matrix.CreateTranslation(new Vector3(-_size / 2, 0)); Vector2 camOrigin = (_size * (1 / _zoom)); topLeft = Vector2.Transform(-camOrigin, simpleCameraMatrix); topRight = Vector2.Transform(new Vector2(camOrigin.X, -camOrigin.Y), simpleCameraMatrix); botLeft = Vector2.Transform(new Vector2(-camOrigin.X, camOrigin.Y), simpleCameraMatrix); botRight = Vector2.Transform(camOrigin, simpleCameraMatrix); AABB camAABB = new AABB(); if (rotation >= 0 && rotation <= MathHelper.PiOver2) { Vector2 v1 = new Vector2(botLeft.X, topLeft.Y); Vector2 v2 = new Vector2(topRight.X, botRight.Y); texAABB = new AABB(ref v1, ref v2); } else if (rotation > MathHelper.PiOver2 && rotation <= MathHelper.Pi) { Vector2 v1 = new Vector2(botRight.X, botLeft.Y); Vector2 v2 = new Vector2(topLeft.X, topRight.Y); texAABB = new AABB(ref v1, ref v2); } else if (rotation > MathHelper.Pi && rotation <= (MathHelper.Pi + MathHelper.PiOver2)) { Vector2 v1 = new Vector2(botRight.X, topRight.Y); Vector2 v2 = new Vector2(botLeft.X, topLeft.Y); texAABB = new AABB(ref v1, ref v2); } else if (rotation > (MathHelper.Pi + MathHelper.PiOver2) && rotation <= MathHelper.TwoPi) { Vector2 v1 = new Vector2(topLeft.X, topRight.Y); Vector2 v2 = new Vector2(botRight.X, botLeft.Y); texAABB = new AABB(ref v1, ref v2); } if (camAABB.Contains(texAABB.Min) || camAABB.Contains(texAABB.Max)) return true; return false; }
// This is used in my editing application. /// <summary> /// Gets index of control point if point is inside it. /// </summary> /// <param name="point">Point to test against.</param> /// <returns>Index of control point or -1 if no intersection.</returns> public int PointInControlPoint(Vector2 point) { AABB controlPointAABB; Vector2 temp1; Vector2 temp2; foreach (Vector2 controlPoint in _controlPoints) { temp1 = new Vector2(controlPoint.X - (_controlPointSize / 2), controlPoint.Y - (_controlPointSize / 2)); temp2 = new Vector2(controlPoint.X + (_controlPointSize / 2), controlPoint.Y + (_controlPointSize / 2)); controlPointAABB = new AABB(ref temp1, ref temp2); if (controlPointAABB.Contains(ref point)) return _controlPoints.IndexOf(controlPoint); } return -1; }