public static project ( Vector3 vector, Vector3 directionVector ) : Vector3 | ||
vector | Vector3 | |
directionVector | Vector3 | |
return | Vector3 |
public static Vector3 calculateVelocity(Vector3 position, Vector3 velocity) { Vector3 targetVelocity = velocity; Vector3 grappleVector = anchor - position; float sqrDistance = grappleVector.sqrMagnitude; // The player's velocity along the direction of the grapple line Vector3 projectedVelocity = Math.project(velocity, grappleVector); // Make sure the player is not moving away from the anchor if (Math.isAntiparallel(projectedVelocity, grappleVector) && sqrDistance >= sqrLength) { targetVelocity -= projectedVelocity; } if (reelMultiplier > 0) { Vector3 errantVelocity = velocity - projectedVelocity; targetVelocity -= errantVelocity; Vector3 reelVelocity = grappleVector.normalized * reelSpeed * reelMultiplier; if (Math.vector3LessThan(projectedVelocity, new Vector3(Mathf.Abs(reelVelocity.x), Mathf.Abs(reelVelocity.y), Mathf.Abs(reelVelocity.z))) || Math.isAntiparallel(projectedVelocity, reelVelocity)) { targetVelocity -= lastAddedVelocity; targetVelocity = reelVelocity; lastAddedVelocity = reelVelocity; } // Apply stabilizers if (reelMultiplier > 1) { errantVelocity /= stabilizer; } targetVelocity += errantVelocity; sqrLength = sqrDistance; } return(targetVelocity); }
internal bool fit(Polygon polygon) { var tightenedVertices = polygon.vertices; // OBB for (int index = 0; index < tightenedVertices.Count - 1; ++index) { var projectionLineStart = tightenedVertices[index]; var projectionLineEnd = tightenedVertices[index + 1]; // Don't cling to the edge to avoid concave polygons. var transNor = Vector3.Cross(Vector3.up, projectionLineEnd - projectionLineStart).normalized; projectionLineStart += transNor * Config.EPSILON_TRANSLATION_FOR_OBB; projectionLineEnd += transNor * Config.EPSILON_TRANSLATION_FOR_OBB; var projectionLine = projectionLineEnd - projectionLineStart; var sideCoefficients = tightenedVertices. Select(point => Math.sideOfLineForPoint(projectionLineStart, projectionLineEnd, point)); // Not all of the points are on the same side. if (!sideCoefficients.All(d => d >= 0f) && !sideCoefficients.All(d => d <= 0f)) { continue; } var results = tightenedVertices .Select(point => new { vertex = point, dotProduct = Vector3.Dot(projectionLine, point - projectionLineStart), distance = Math.distanceToLine(projectionLineStart, projectionLineEnd, point) }); var orderedResultsByProduct = results.OrderBy(item => item.dotProduct); var farthestResult = results.Aggregate((a, b) => a.distance > b.distance ? a : b); // Points on bounding box. var left = orderedResultsByProduct.First().vertex; var right = orderedResultsByProduct.Last().vertex; var top = farthestResult.vertex; // Corners of bounding box. var cornerBL = Math.project(projectionLineStart, projectionLineEnd, left); var cornerBR = Math.project(projectionLineStart, projectionLineEnd, right); // Current area of bounding box is larger than smallest one. var u = Vector3.Distance(cornerBL, cornerBR); var v = farthestResult.distance; var curArea = u * v; if (curArea >= Area) { continue; } var translation = -transNor * farthestResult.distance; var cornerTL = cornerBL + translation; var cornerTR = cornerBR + translation; corners = new List <Vector3> { cornerBL, cornerBR, cornerTR, cornerTL }; area = curArea; centre = (cornerTL + cornerBR) * .5f; this.u = u; this.v = v; if (u > v) { longEdgeDir = projectionLine.normalized; shortEdgeDir = transNor; } else { longEdgeDir = transNor; shortEdgeDir = projectionLine.normalized; } } return(corners.Count >= 4); }