Jump to content

Parametric PhysicsJoint? How to change length of SpringJoint with Slider.


blondegeek
 Share

Recommended Posts

Howdy!

Is it possible to dynamically change the length of a SpringJoint, for example with a Slider?

Here's my attempt. The code is a mishmash of different code examples (highlighting selected meshes, dragging selected meshes, etc.) but the relevant part is where slider.onValueChangedObservable is called and {length: slider.value, ...} when the SpringJoint is created. Looks like springJoing.jointData.length can't be set to a new value -- so how do I change it?

https://github.com/blondegeek/babylon_connected_springs_and_masses/blob/master/click_sphere.html

Thanks!

Link to comment
Share on other sites

Hiya BG, welcome to the forum.

Here ya go...

http://playground.babylonjs.com/#0N1XI9#2

I turned off a few things (temporarily) and adjusted a few things.

Line 7 - just backed the camera out a bit,
Lines 27/28 - slightly adjusted the way physics is enabled.  It has evolved over the years.  :)
Line 56 - added a toFixed to shorten-up the numbers a bit.
Line 59 - I think jointData is used ONLY to CREATE the joint, but it is not used to do "live updates".  For live updating, I put the slider value into springJoing.physicsJoint.restLength which is a "live" value.  This is sometimes called "going native"... making adjustments to the ACTUAL Cannon/Oimo physics objects/formulas, instead of communicating through the BJS physics "plugins" (helper wrappers).  See http://schteppe.github.io/cannon.js/docs/classes/Spring.html   (restLength)  (a "native" parameter) 
Line 68 - made sure jointData had an INITIAL length (of 8, same as initial slider value).

Seems to work.  If you have questions, let's hear 'em.  I am no expert, but I sometimes get lucky and land a good solution.   As you can tell, this spring is an upward-aiming spring, and not a "dangler".  :)   But the lower sphere has mass, in a gravity-less world... so it DOES move when the spring pushes against it.  Good physics fun!

Reminder:  JointData is used as a helper to CREATE a joint, but not a place to change values real-time, and expect the ACTUAL joint to "listen-for" those changes.  Party on!

Link to comment
Share on other sites

@Wingnut there are the makings of a cellphone game there!

If you time it right and slide the slider all the way to minimum, then to max then back to minimum  and are able to time it right to maximize momentum the balls switch place and the spring reverses...

Kinda cool, in a I hate math kinda way...

Link to comment
Share on other sites

@Wingnut provided a wonderful example, and a good explanation regarding the object that needs to be altered.

The physics joint are synchronized with their corresponding physics objects from the physics engine, but such data as the axis, length, stiffness etc', are not synced. So, in this case, the best would be to do exactly what the playground does.

It is of course possible to implement a getter and a setter for the legth value. you can create an issue for me if you want that.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...