/// <summary> /// Determines if a ray intersects the polygon, and at what position the ray first intersects the polygon. /// </summary> /// <param name="rayLine">The line segment representing the ray.</param> /// <param name="intersectionPoint">The first intersection of the ray and the polygon. Is NAN if no intersection.</param> /// <returns>Returns if the polygon was intersected by the ray.</returns> public bool RayIntersection(LineSegment rayLine, out Vector2 intersectionPoint) { intersectionPoint = new Vector2(float.NaN, float.NaN); //Low-level detection if (LineSegment.Distance(CenterPoint, rayLine.EndPoints[0]) <= rayLine.Length + MaximumRadius) { LineSegment[] sideSegments = SideSegments; float minDist = 0; for (int j = 0; j < sideSegments.Length; j++) { Vector2 potentialIntersectionPoint = new Vector2(); if (sideSegments[j].IntersectsSegment(rayLine, ref potentialIntersectionPoint)) { float distanceToRayOrigin = LineSegment.Distance(potentialIntersectionPoint, rayLine.EndPoints[0]); if (float.IsNaN(intersectionPoint.X) || distanceToRayOrigin < minDist) { minDist = distanceToRayOrigin; intersectionPoint = potentialIntersectionPoint; } } } } return(!float.IsNaN(intersectionPoint.X)); }
public PolygonPoint(Vector2 point, Polygon polygon) { initialRotation = polygon.TotalRotation; this.polygon = polygon; radius = LineSegment.Distance(point, polygon.CenterPoint); angle = (float)Math.Atan2(point.Y - polygon.CenterPoint.Y, point.X - polygon.CenterPoint.X); }
/// <summary> /// Creates a joint that applies forces to both bodies to keep the points at a specified distance. /// </summary> /// <param name="elasticity">Elasticity of the joint.</param> /// <param name="rb1">The first RigidBody.</param> /// <param name="p1">The point on the first RigidBody.</param> /// <param name="rb2">The second RigidBody.</param> /// <param name="p2">The point on the second RigidBody.</param> /// <param name="stretchLimit"></param> public ElasticJoint(float elasticity, RigidBody rb1, Vector2 p1, RigidBody rb2, Vector2 p2) { targetDistance = LineSegment.Distance(p1, p2); this.rb1 = rb1; this.rb2 = rb2; this.p1 = new PolygonPoint(p1, rb1.CollisionPolygon); this.p2 = new PolygonPoint(p2, rb2.CollisionPolygon); this.elasticity = elasticity; }
private float GetInertia(float mass) { //return mass * 150; //Sum vertices Vector2[] vertices = CollisionPolygon.Vertices; float massDivision = mass / vertices.Length; float inertialSum = 0; for (int i = 0; i < vertices.Length; i++) { inertialSum += massDivision * (float)Math.Pow(LineSegment.Distance(vertices[i], CollisionPolygon.CenterPoint), 2); } return(inertialSum / INERTIAL_DIVISOR); }
public void ControlUpdate(GameTime gameTime, MouseState mouseState, Vector2 scale, Vector2 translation) { currentMouseState = mouseState; //Apply transformations to position Vector2 relativeMousePosition = new Vector2((mouseState.Position.X + translation.X) * scale.X, (mouseState.Position.Y + translation.Y) * scale.Y); //Update control if (currentMouseState.LeftButton == ButtonState.Pressed) { if (prevMouseState.LeftButton == ButtonState.Released) { //Potentially add joint control T[] bodies = GetBodies(); for (int i = 0; i < bodies.Length; i++) { if (bodies[i].CollisionPolygon.MaximumRadius >= LineSegment.Distance(bodies[i].CollisionPolygon.CenterPoint, relativeMousePosition) && bodies[i].CollisionPolygon.ContainsPoint(relativeMousePosition)) { //Add joint control CreateJointControl(relativeMousePosition, bodies[i]); break; } } } else { //Uphold current joint control (if exists) if (interactionJoint != null) { UpdateJointControl(relativeMousePosition); } } } else { if (prevMouseState.LeftButton == ButtonState.Pressed) { //Remove joint control (if exists) RemoveJointControl(); } } Update(gameTime); prevMouseState = mouseState; }
public float GetHeight() { Line normalLine = new LineSegment(vertices[0], vertices[1]).NormalLine; return(LineSegment.Distance(normalLine.PointProjection(vertices[0]), normalLine.PointProjection(vertices[2]))); }
public float GetBase() { return(LineSegment.Distance(vertices[0], vertices[1])); }