blondegeek Posted June 20, 2018 Share Posted June 20, 2018 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! Quote Link to comment Share on other sites More sharing options...
Wingnut Posted June 20, 2018 Share Posted June 20, 2018 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! blondegeek and dbawel 2 Quote Link to comment Share on other sites More sharing options...
Sebavan Posted June 20, 2018 Share Posted June 20, 2018 Pinging also @RaananW who loves Physix Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted June 20, 2018 Share Posted June 20, 2018 @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... blondegeek 1 Quote Link to comment Share on other sites More sharing options...
RaananW Posted June 21, 2018 Share Posted June 21, 2018 @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. Wingnut 1 Quote Link to comment Share on other sites More sharing options...
blondegeek Posted June 22, 2018 Author Share Posted June 22, 2018 Thank you all for the replies! I'm just reading this now and will process over the weekend. Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.