Jump to content

Scale mesh during velocity


Recommended Posts

I'm trying to make my mesh smaller during its linear velocity movement. But unfortunately, as soon as I set the scaling, it stops the movement.

How can I scale down my mesh in size without interrupting the movement? Is there an elegant way without having to calculate positions myself?

See my example, where the movement stops after 1sec when trying to scale: http://www.babylonjs-playground.com/#5WEAFN

Link to comment
Share on other sites

Hi! :) For me.

The physics engine is dealing with rigid bodies that do not change in scale and shape. Changing the scale of an object will technically recreate the mesh impostor. The old impostor's details (like the angular and linear velocity) will have to be applied to the new impostor. It is possible, but not recommended as there are a lot of factors you will need to consider. Performance will also hurt a lot from that, as recreating the impostor over and over again is relatively "expensive".

Link to comment
Share on other sites

Hi kraftwer1.  Welcome aboard.  Hi everyone else, too.

Do you need smooth-scaling, krafty?  Must the scaling be gentle, or can it be in distinct steps?

Anyway, I had to experiment.  https://www.babylonjs-playground.com/#15AFCG#33

Currently, the small gray ibox and its impostor... is attached via joint1... to the green player box/impostor.  ibox does the moving, and green box is joint-dragged-along via forces passed-thru the joint.

You can use up/down arrow keys to move the ibox along z-axis.  (x-axis moving also works, but auto-scaling is not yet programmed for that axis).

You can also turn-on auto-velocity by enabling the code in lines 93-102.

The manual-moving is done in lines 116-136.

Right now, I am NOT copying the linear and angular velocity from previous impostor... to the new impostor that gets installed automatically after a physics-active shape... gets scaled.

Instead... I re-add the joint that connects ibox and green player - in lines 123 and 134.  It works... sort-of ok.

You can re-activate the oldImp->newImp velocities-copying... by activating lines 118-119, 121-122, 129-130, and 132-133. This makes things act a little differently for manual-moving.

Remember that when a physics-active mesh is scaled, our plugins (actually, our physicsImpostor wrapper-code) automatically disposes the old impostor, and adds a new one.  (it's called a forceUpdate, I believe.)

You can see that activity... in this area of our physicsImpostor wrapper:  https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/babylon.physicsImpostor.ts#L181

It is a feature, and is the only way to make a rigid body act any-what "sane" after a scaling.  It CAN be disabled via hacking the wrapper (over-riding some/all of our wrapper), but after that... troubles/challenges.  But still, it would be a cool adventure.

Your physicsBODY really needs to change from rigid, to dynamic.  A gruesome challenge.  No indication of allowed rigidBody scaling features.... is seen in either of the 3rd party physics engines.

But, if you want to "go deep" and "go native" (dive UNDER our physics plugins/wrappers)... you might be able to use dynamic/soft bodies.  I'm sure not qualified to talk about that stuff.  I just make goofy playgrounds.  :)  Speak of which... https://www.babylonjs-playground.com/#15AFCG#34

In THAT playground, the entire BJS physicsImpostor wrapper/class has been "hijacked" into the playground (for hacking fun).  Wow, huh?  Around line 213... perhaps adjust-to:

if (this.shape != player) { this.forceUpdate() }  // tweak as needed.  That would prevent a shape scaling... from disposing old impostor.  But that would be only the START of the needed hacking to convert from a CannonJS rigidBody... to a dynamicBody.  Greasy deep hacking.  :)

I hope this has been helpful, and I hope I didn't say any incorrect things.

Link to comment
Share on other sites

@Wingnut In a nutshell, your first experiment is to set a new scaling every 250ms, while adding the previous velocity to the newly created impostor?

I tried this in my project and reducing the 250ms to something like 30ms would make the scaling smooth and gentle enough for my purposes: http://www.babylonjs-playground.com/#5WEAFN#2

I guess putting it into requestAnimationFrame() and scale it every whichever frame would make it even smoother.

Link to comment
Share on other sites

Nice nice nice! 


Minor adjustment... pre-declared prevVelocity before the interval loop (line 57).

I'm not smart enough to know if that gains any garbage collection efficiency.  It MIGHT be ONLY when new whatever() is used repeatedly... that we need to worry about leakage.  :)

Oh yeah, I guess I modified some other things too, having fun.  :)

Ever see airbags used to tip-up a tipped-over semi-trailer;) 

We even have a slow dramatic camera pull-pan-ped.  (lines 63-65)

I wanted to see the scaled impostor... do something physical... making sure it stayed the correct size.  It does.  Cooooool.

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.

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.


  • Recently Browsing   0 members

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