In a 3D world, I’ve a hockey puck-like physique.
I’ve made a mechanic the place I can launch the puck with a slingshot mechanism.
However the level of contact is decided with a raycast and the “sling draw” may be in any arbitrary (XZ airplane locked for simplicity) course.
Seen within the picture:
- BLUE circle = puck
- BLACK circles = factors of contact (drive factors) may be anyplace on the sting
- RED line = drive vector of 1 potential draw, that’s straight aligned with heart, therefore shouldn’t be deflected (in sport this precision wouldn’t be potential, so there would all the time be some deflection)
- GREEN line = drive vector of one other potential draw, that’s off heart, that needs to be deflected
- BLUE dashed line = regular from heart of mass
At the moment I am making an attempt to compute it utilizing:
// Can draw max as much as 2 items
var springMaxDistance = 2f;
// Drive utilized per items drawn
var springStrength = 5f;
var springVector = forcePoint - drawPoint;
// Decide the vector from drive level to mass
var massVector = forcePoint - centerOfMass;
// Derive how a lot of massVector is parallel to springVector
var massParallelVector = Vector3.Challenge(massVector, springVector);
// Derive the rest as perpendicularity
var massPerpendicularVector = massVector - massParallelVector;
// Restrict the utmost spring distance
var springDistance = Mathf.Min(springVector.magnitude, springMaxDistance); // Prevents winding greater than allowed
// Compute the drive that needs to be utilized
var springForce = springStrength * springDistance;
// Derive how a lot of most drive is to be utilized
var forceRatio = springForce / (springStrength * springMaxDistance);
// In addition to how offset (perpendicular) the shot is from it is heart
var offsetRatio = massPerpendicularVector.magnitude / massVector.magnitude;
// Decide how a lot to deflect
var deflectionMagnitude = offsetRatio * forceRatio;
// Generate the deflection vector based mostly (for 0 perpendicularity, this might be nothing - straight shot)
var deflectionVector = massPerpendicularVector.normalized * deflectionMagnitude;
// Combine
var launchVector = (forceVector - deflectionVector).normalized * springForce;
And, to additionally add any torques vital, I am making use of the drive as such:
rigibody.AddForceAtPosition(launchVector, startPoint, ForceMode.Impulse);
It really works as anticipated for straight pictures, like:
But I do not really feel like that is bodily correct although. For some angles, this produces bizarre vectors – like:
IDK if for such an excessive angle, the produced launchVector is sensible. I really feel it needs to be sharper.
I’ve been googling for hours and can’t discover the correct key phrases to search out the correct physics formulation for this.
I cobbled this strategy up with LLM.