Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Make rope into a slingshot to launch a sphere
#8
(26-05-2021, 05:54 PM)josemendez Wrote: Imho this is a bad idea, as it disconnects the visuals from the actual outcome of launching. Will look weird at best.


It doesn't make much sense to get the position of the ball when it first touches the rope, imho. The user might be able to move the rope to a completely different position after that, and you'd get a really bad estimate of launch direction/strength.

I'd store the ball position in initialPosition every frame as long as rope and ball are in contact (just like you're currently doing). Then when the user releases the mouse button, wait for a frame and get a second ball position. Then your velocity estimate would be (secondPos-initialPos)/Time.fixedDeltaTime.

Thank you for your response Sonrisa

Quote:Imho this is a bad idea, as it disconnects the visuals from the actual outcome of launching. Will look weird at best.

I do that because the player is supposed to be able to decide the direction the ball will launch in.
The player can rotate the ball on its Y axis, and the force is applied on the ball's forward direction.

Quote:It doesn't make much sense to get the position of the ball when it first touches the rope, imho. The user might be able to move the rope to a completely different position after that, and you'd get a really bad estimate of launch direction/strength.

I'd store the ball position in initialPosition every frame as long as rope and ball are in contact (just like you're currently doing). Then when the user releases the mouse button, wait for a frame and get a second ball position. Then your velocity estimate would be (secondPos-initialPos)/Time.fixedDeltaTime.

I already do pretty much that except the waiting one frame.

Getting initialPosition:
Code:
var world = ObiColliderWorld.GetInstance();

// just iterate over all contacts in the current frame:
foreach (Oni.Contact contact in e.contacts)
{
    // if this one is an actual collision:
    if (contact.distance < 0.01)
    {
        ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
        if (col != null)
        {
            hookBallPosition = transform.position;
        }
    }
}

On mouse release:
Code:
private void OnMouseUp()
{
    StartCoroutine(Launch());
}

private IEnumerator Launch()
{
    // Wait 1 frame
    yield return null;

    // Get current ball position
    Vector3 secondPosition = transform.position;

    // Get launch vector
    Vector3 direction = hookBallPosition - secondPosition;

    // Launch ball in vector
    rigidBody.velocity = direction / Time.deltaTime;
}

The only issue now is the rope is too loose and stretchy.

It sometimes even gets stuck under the ground and won't come back up.
You can see it in this video: https://i.gyazo.com/68734514524df7e36177...30e6a0.mp4
Reply


Messages In This Thread
RE: Make rope into a slingshot to launch a sphere - by Brainiac - 27-05-2021, 02:57 PM