playMaker

Author Topic: [SOLVED] Smooth Limit Rigidbody Distance  (Read 618 times)

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
[SOLVED] Smooth Limit Rigidbody Distance
« on: May 22, 2022, 09:24:50 AM »
Hello!

I'm using the DragRigidBody PlayMaker action for a bunch of mouse drag interactions in my current project but I'm having trouble thinking of a way to limit how far the player can drag certain objects from there original position.

I've tried using joints to limit the distance but found that they interfere with the rigid body when released since I'm controlling its position in that state. This also causes visual hitches since the rigid body is animating at the same time the joint is pulling it back.

What I would like to achieve is something similar to the Unity Scroll Rect which sets a soft/elastic limit on how far it can be scrolled. With that idea in mind, I tried getting the distance of my rigid body and increasing the spring value as it got further away to slow it down. This seems to have some effect but with a couple drawbacks: First of all, the object becomes immobile at its limit where it should be able to freely move around within the limits. Secondly, I'm stumped on how to go about detecting if the player begins dragging back towards the origin. In this situation, the spring should go back to normal rather than slowly decrease along with the distance. It's feeling more like the object gets stuck in molasses rather than is tied to a soft limit.

If anyone has a suggestion on how I can calculate this kind of thing, it would be greatly appreciated :)
« Last Edit: May 24, 2022, 10:14:55 AM by acornbringer »

Twood

  • Junior Playmaker
  • **
  • Posts: 76
Re: Smooth Limit Rigidbody Distance
« Reply #1 on: May 22, 2022, 01:26:39 PM »
Personally, because I couldn't write a thesis on how to program physics, I would take a simple approach to avoid bugs.  ;D

Just something like for the dragged object, you could track the absolute distance it has moved since dragging started. If it goes over the dragging limit, then disable the drag action for a moment (so you're not fighting.) Then disable physics and just tween it back towards the original position in a way that's smooth. Something like that. For something more complex with dragging stuff around I'd probably see if there's anything on the asset store.

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Smooth Limit Rigidbody Distance
« Reply #2 on: May 23, 2022, 08:02:29 AM »
[...] you could track the absolute distance it has moved since dragging started. If it goes over the dragging limit, then disable the drag action for a moment (so you're not fighting.) Then disable physics and just tween it back towards the original position in a way that's smooth. [..]

Same here. I'm still learning all about these things. It's pretty much set up now as you explained but I'm not sure about the way it feels for the control to be removed like that. It's not so satisfying or easy to understand and since it's a physics driven experience, I'm trying to get everything to be tactile.

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Smooth Limit Rigidbody Distance
« Reply #3 on: May 24, 2022, 10:12:55 AM »
Hey again!

I came up with a solution that seems to be working pretty well so far. Well I have two now but one is for a hard limit and the other will be smooth like I was after. Simply put, I'm using a Spring Joint for the smooth solution since it resolves all the math I was probably breaking my head over to think of. Let me explain both for posterity.

Hard Limit: Get the vector 3 position of the object and use that for a "Vector3 Clamp Magnitude" action. Then set the position to the the same vector 3 after the clamp. It works fine but there's something odd to me about setting the position to its self every frame when the position is already being controlled by the "Drag Rigid Body" action. If there's a better way to clamp position, I'd be happy to learn!

Soft Limit: Attach a Spring Joint to the parent object and on MOUSE DOWN, connect the draggable rigid body as its Connected Body. On MOUSE UP, set the Connected Body to none so the forces don't interfere with any animation there might be.

The Spring Joint solves this for me but I'm still pretty curious to learn how I might calculate the same kind of behavior myself for a bit more flexibility. Sometime I'll look into how the limits work on these joints in case I might glean some insight but for now, I have it working alright.

Thanks!