Jump to content

Apply custom force to Physics body


schteppe
 Share

Recommended Posts

Hello,

I was trying to reply to this question on StackOverflow, and realized Babylon.js may be missing a couple of important methods in the CannonJS plugin... But I'm not sure as I don't really use Babylon :P

The question is how to apply a custom force to a cannon body. This is usually done by running body.applyForce() inside the postStep event in cannon.js. So I was wondering, how do I do those things in Babylon.js ?

  1. How to listen to the physics postStep event from cannon (or other physics plugins)?
  2. How to apply a force? (applyImpulse is not the same thing)

Here's a simple Unity example using both of these things in a Unity context.

Thanks,

Stefan

Link to comment
Share on other sites

Hello!  Wow, it's Schteppe!  (drool)  You are a super-hero around here.  Be careful, you could get mauled by fans, here.  :)

Thanks for all the physics fun you have given us, so far, Stefan!  Nice work!  We are totally honored by your visit and concern for our CannonJS plugin.

https://github.com/BabylonJS/Babylon.js/tree/master/src/Physics ]

I don't think many/any of us have used body.addForce.  The physicsBody is returned from a BJS mesh.setPhysicsState() call.  Here's some demo code:

http://www.babylonjs-playground.com/#OJVVA#7

Line 28 gets the body.  Line 37 is continuously reporting the .angularVelocity of the 10th sphere... to console.

I hope this helps.  Others may reply, too.  Deltakosh, Temechon, Raanan... those are the CannonJS superstars around here, imho.  (and certainly YOU, too!)

Good luck with your question answering, and thanks for doing that!  Stick around if you can, and/or visit often.

Link to comment
Share on other sites

Yeah, it's me haha. I'm glad that I could help so many people with my work. I'll try to hang out here more often, though my time is quite limited.

I hope I'm in the right place, no? I saw that there's no Github issues for BJS?

I was able to post an answer to that question, but the solution wasn't the best I think. Since there's no way to catch the postStep event except from the Cannon.js world itself, I had to clutter the BJS code with a too much Cannon code. Anyway, solved.

I noticed that BJS uses variable time stepping, which is really bad for any game. Some times when I reload or do anything that hogs the JS thread for a few milliseconds I get unexpected behavior, hehe.

The easiest solution for this is to always pass the same value to step() here: https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/babylon.physicsEngine.ts#L45. I'd recommend a fixed time step of 1 / 60.

The less easy solution is to pass the elapsed time to cannon.js as well as a fixed time step and max sub steps, here: https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/Plugins/babylon.cannonJSPlugin.ts#L34

You would instead of this:

this._world.step(delta);

do this:

var fixedTimeStep = 1/60;
var maxSubSteps = 10;
this._world.step(fixedTimeStep, delta, maxSubSteps);

...and you would probably need to implement a fixedStep event, so the users can hook up to it and apply forces inside the physics loop, just like I did in the StackOverflow answer. You should also use body.interpolatedPosition and interpolatedQuaternion instead of .position and .quaternion for rendering.

I hope this is useful for someone :)

Link to comment
Share on other sites

Hey @schteppe,

Thanks a lot! Nice to have you around :)

For now the only way of actually using this kind of functionality in Cannon.js is to get the world object (if needed) and the physics body of the mesh and program it on your own. I added those functions in 2.3 exactly for that reason. But! This is just a temporary solution, as I am trying to integrate most of cannon's (and other engines') missing methods into Babylon.js, including the many types of hinges and links, SPH and maybe the vehicle body, which i always find fun to use :). I integrated one event (the "collide" event) so far, and will continue adding others in 2.4. 

Thanks for the tip about the interpolated transformation and the step variables! Makes a lot of sense and can explain a lot of small issues. I'll make sure to integrate it in the cannon plugin.

Link to comment
Share on other sites

Raanan recently was able to BJS-activate CannonJS meshImposter, and physics on heightMaps, and I just LOVE IT.  I think our whole community loves it.  We have tried fun experiments with basic mini-putt golf, bowl-o-balls, and of course... physics with fractals.  This is the zone of fun CannonJS experiments.

Stefan, I can't tell you how many times BJS users have wanted to drive your rigidVehicle on Babylon heightMaps.  Granted, your cool rigidVehicle demo is excellent, but we still need a version for BJS.  We have quite a few vehicle maniacs around here, and they want more jumps, more horsepower, more lighting, more land, some rivers, some mud, ice, snow, you know... ALL the things boys love in a rigidVehicle sandbox.  :D  I'm even hoping to do some road grading, snow plowing, and luge runs, someday... thanks to you.

I think our local experts can help us get our Cannon rigidVehicle working, eventually.  But, Stefan, if you want to help us with that, THAT WOULD BE EXCELLENT!  I would sure be willing to donate some USD to your doughnut fund, if you would accept such.  http://www.babylonjs-playground.com/#1RKZXB would be a good starting place.  :)

If not, no problems, we'll get a rigidVehicle happening in BJS, soon.  Thanks either way.  As for general tips on how best to nicely integrate your engine... we are QUITE glad to hear anything and everything you have to say about it.

Personally, I'm just tickled pink and seriously star-struck that you're even visiting us.  Wow!  Too cool!  And feel free to tell us about you, too, and what you are working-on or playing-with.  This is a very social forum, and we would be glad to build you a cabin on a lake and provide you with a rigidVehicle boat to ride around in.  (I think I saw a video of you taking a boat ride, once, so I know you like doing such.)  :)

[I feel like I'm getting to talk with Elvis Presley!  Sigh.]  

Btw, I like your answer to that planetary gravity question, Schteppe.  I learned a ton from it. 

I bet that user wants the gravity to fall-off when the body is further from the planet.  I bet they want to do stable orbiting, and maybe "slingshot" a spacecraft.  They are going to try doing moon shots... launching bodies from one gravity source (Earth) and enter into stable orbits around a different gravity source (the moon).  I bet.  :)  How the heck...?  Failed launch, rocket hits mountain on Earth... heightMap#1.  Failed orbital entry, orbiter hits mountain on moon... heightMap#2.  Hmm.  (nevermind me, I'm a dreamer)

 

Link to comment
Share on other sites

Thanks a lot guys! Sounds like you have good plans for the physics (just don't add the SPH, it's probably too experimental at this point). Exposing the CANNON.Body was probably a very good move initially.

I don't really like the collide event... I added beginContact and endContact recently, you should probably use those :)

You @Wingnut make me blush haha !

A vehicle helper/class in BJS would be cool, yeah, I agree. It's a shortcut that takes you from a simple car mesh to an almost fully fledged racing game (lol). In Goo Create I made a script that you can simply put on an entity with a rigid body component. I think it works quite well, both game play wise and workflow wise. You put the script and can also add mesh entities for each wheel to be animated, and then you're done. Not sure if it's any help to you guys, but I can at least provide a link: Create Scene with the Vehicle Script, Live Demo.

Link to comment
Share on other sites

Oh man, that live demo is SWEET!  Just piles of DAT.gui knobs to play-with!  And yeah, that drives and "feels" nice.  (drooooool)

There was a game around called Stuntcar Racer... had a nice physics feel.  In-car view, elevated flat-shaded 3D track with steep banked corners and big jumps.  The tires on the car were sort of like sticky gum, and the track surface felt like hot Formica.  The tires never "skidded" on the track... too much friction, but the car could "dance" off the track.

When you got the speed just right... into a tight bank... tires well-stuck to the track... the frame of the car would start gently cracking due to torque/inertia.  The frame would also crack if you misjudge a jump speed and land rough.  If abused too much, the car would break in half, and that was the end of THAT race.  Too fun.  CannonJS can handle that, easy.  ;)

https://www.youtube.com/watch?v=7hpdj4mAxxM

This guy/gal is a pretty good driver... never saw him/her leave the track and chow dirt.

See the frame-cracking indicator along the top?  FUN! 

Racing on a meshImposter!  What else could a guy ask-for in life?  :)

So, yeah, thanks for that link.  Are we free to "borrow" pieces of your code from the live demo?  Yes?  Thanks!   heh.

You should get to know how very wonderful the BabylonJS framework IS, and if a guy from Sweden wanted to bust into the game world by creating Stuntcar Racer for BabylonJS/CannonJS, well, heck, you could not ask for a better dev platform.  You're already pretty famous, but, there's always a new audience to conquer, right?  Who could make a webGL version of Stuntcar Racer... better than Schteppe?

And Luge Run... is just a few changes from Stuntcar Racer.  The principle is the same, except we're racing on the insides of tubes/half-pipes.  FUN!  Accident-inducing!  [Wingnut super-glues Schteppe's feet to the floor of the forum, so he'll stay forever.]  :)

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...