11.6 C
New York
Friday, March 14, 2025

unity – Making a slingshot impressed launch mechanism to hit arbitrary level on edge not simply fling straight by means of middle of mass


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 set with a raycast and the “sling draw” may be in any arbitrary (XZ aircraft locked for simplicity) course.

Seen within the picture:

  • BLUE circle = puck
  • BLACK circles = factors of contact (power factors) may be anyplace on the sting
  • RED line = power vector of 1 potential draw, that’s straight aligned with middle, therefore shouldn’t be deflected (in sport this precision wouldn’t be potential, so there would at all times be some deflection)
  • GREEN line = power vector of one other potential draw, that’s off middle, that needs to be deflected
  • BLUE dashed line = regular from middle of mass

At the moment I am attempting to compute it utilizing:

// Can draw max as much as 2 models
var springMaxDistance = 2f;

// Pressure utilized per models drawn
var springStrength = 5f; 

var springVector = forcePoint - drawPoint;
// Decide the vector from power level to mass
var massVector = forcePoint - centerOfMass;
// Derive how a lot of massVector is parallel to springVector
var massParallelVector = Vector3.Venture(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 power that needs to be utilized
var springForce = springStrength * springDistance;

// Derive how a lot of most power is to be utilized
var forceRatio = springForce / (springStrength * springMaxDistance);
// In addition to how offset (perpendicular) the shot is from it is middle
var offsetRatio = massPerpendicularVector.magnitude / massVector.magnitude;

// Decide how a lot to deflect
var deflectionMagnitude = offsetRatio * forceRatio;
// Generate the deflection vector primarily based (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 power as such:

rigibody.AddForceAtPosition(launchVector, startPoint, ForceMode.Impulse);

It really works as anticipated for straight pictures, like:
vectors for straight shots


But I do not really feel like that is bodily correct although. For some angles, this produces bizarre vectors – like:
vector for extreme angle

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 method up with LLM.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles