Jump to content

Move mesh with Physics without apply impulse


The Snow Irbix
 Share

Recommended Posts

Hello guys !

For few days I tried to move a box with physics collisions enable.

I did a playground to resume it : http://www.babylonjs-playground.com/#LKCKO#1

I read all other topic about this, I tried to refresh the physics box position, use translate and rotate, use moveWithCollisions but nothings seems work.

I also tried to move my box with apply impulse at the center of the box but it always failed due to the precision I think.

And applyImpulse is less suitable to move a box (future character).

I need help 

 

// FRENCH

 

Salut tout le monde !

Depuis quelques jours j'essaye de déplacer une simple boite avec les collisions entre objets.

voir : http://www.babylonjs-playground.com/#LKCKO#1

J'ai lu tout les autres topics qui parlait de ça, et j'ai testé les "solutions" à chaque fois (updatePhysicsBodyPosition, translate, rotate, moveWithCollisions) mais rien ne semble marcher.

J'ai aussi essayé avec les impulsions physiques (applyimpulse) au milieu de la boite (getBoundingInfo().boundingBox.center) mais celà ne marchait pas non plus. (La boite partait en rotation en appliquant plusieurs translations différentes).

Et apply impulse est moins adaptés pour controler les déplacements des personnages.

Bref, j'ai besoin d'aide !

Link to comment
Share on other sites

Tu n’est pas obliger d'utiliser la physique.

 

tu a une fonction "moveWithCollisions" qui consommera moins et seras plus simple.

meshPlayer = ? ton personnagevar forward = new BABYLON.Vector3(parseFloat(Math.sin(parseFloat(meshPlayer.rotation.y))) / 8, -0.15, parseFloat(Math.cos(parseFloat(meshPlayer.rotation.y))) / 8);meshPlayer.moveWithCollisions(forward);
Link to comment
Share on other sites

Hi irbix!  Fun demo.

 

The main thing you forgot to do... is:

 

scene.enablePhysics();

 

http://www.babylonjs-playground.com/#LKCKO#2

 

Now it's working better.  :)

 

Also, I setPhysicsState on the ground, too, and made it a bit bigger, and made your basic shapes a bit smaller.

 

Also, you had no 'restitution' (bounciness) factor set in any of your setPhysicsState settings.  I added some.

 

Lastly, I usually set my object rotations and positions BEFORE I use setPhysicsState on an object.  After an object has its setPhysicsState set.. it is a bit more difficult to rotate and translate.  Generally speaking, applyImpulse is the only way to apply movement force...  where a PHYSICS collision can happen.

 

Here's a version where I use gentle continuous impulse to roll the sphere into the box.

 

http://www.babylonjs-playground.com/#LKCKO#3

 

When moving a character with keypresses, it is not uncommon for functions to repeatedly run over and over while the key is held down.  Tiny amounts of repeated applyImpulse work fine for moving characters and having physics engines react.  *shrug*  I'm no pro, though.  :)  I used the animate function to repeatedly applyImpulse to the sphere.

 

Continuous small applyImpulsings in the animation loop... opens thoughts for many games, such as Sumu Wrestling (two boxes pushing on each other with random-generated force values), and... the old mechanical horse racing games.  (good betting/drinking games)  ;)

 

"Ladies and Gentlemen!  Tonight, 7 boxes will race across a plane with random amounts of energy force!  PLACE YOUR BETS, because the next race is about to begin!"

 

Then some idiot pours oil on the racetrack and all the boxes lose traction.  But they also lose friction!!!  HOILy Horses, Batman!  You won't want to miss the next...  Racing Boxes! (a product of IrbixCo)  :)

Link to comment
Share on other sites

thank you guys, I tried to use move with collisions and translate but it doesn't work, see http://www.babylonjs-playground.com/#LKCKO#4

box.refreshBoundingInfo();box.applyImpulse(new BABYLON.Vector3(-0.1276, 0, 0), box.getBoundingInfo().boundingBox.center); // work//box.translate(new BABYLON.Vector3(-0.5, 0, 0), 0.1, BABYLON.Space.WORLD); // Doesn't work : no collision//box.moveWithCollisions(-1, -0.15, 0); // Error : Uncaught TypeError: undefined is not a function 
moveWithCollisions give me the same result in local than translate : no collision

And apply impulse with KeyBoard Event rotate the box sometimes   :( :

http://www.babylonjs-playground.com/#1JSG4F

Link to comment
Share on other sites

Ahh Irbix... you are running into a situation that is not well documented around these parts.  I'm no expert, but there are two types of collisions.  One is a PHYSICS collision, calculated by an add-on physics library such as Oimo or Cannon.  This collision uses "imposters".

 

The collisions that Dad72 is speaking-of... is mesh ellipsoid and mesh boundingBox collisions.  In that system, which IS built-in to BJS, the "imposters" don't come along when a mesh is moved or rotated.  In order to get a PHYSICS ENGINE collision, the imposter must be moving WITH the mesh.

 

When you did scene.enablePhysics (enable add-on physics engine), the "world" changes (at least for objects with setPhysicsState).  It becomes a world of applyImpulsings and imposter collisions.  Try to keep these two worlds separated in your mind.  They don't play well together.  :)

 

Physics colliding is slow, as you might have noticed.  Lots of calculations are needed to get that box to dance around on the rubber floor like it does.

 

Babylon built-in collision of boundingBoxes and ellipsoids, checkable with checkCollsions and intersectsMesh, etc... is much faster... but you will need to manually make the box dance around on the rubber floor.  The framework collision system will not do "true physics" (force, mass, friction, restitution, etc) like the physics engine will.

 

I might have worded that badly, and/or I could be wrong.  But that's how I see it.  It will take some experimenting to see WHAT types of positioning and rotating is available with physics engine enabled, and what is available with physics engine disabled.  Keep experimenting, good luck.

Link to comment
Share on other sites

Hello there, 

 

I think I will create an article about this in my website.

 

But basically, what you have to do is something like this : 

var body = box.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, {mass:1, friction:0.001, restitution:1.5});if (this.mvtDirection[0] != 0) {      box.applyImpulse(new BABYLON.Vector3(0,0,s), box.position);}if (this.mvtDirection[1] != 0) {      box.applyImpulse(new BABYLON.Vector3(0,0,-s), box.position);}if (this.mvtDirection[2] != 0) {      box.applyImpulse(new BABYLON.Vector3(-s,0,0), box.position);}if (this.mvtDirection[3] != 0) {      box.applyImpulse(new BABYLON.Vector3(s,0,0), box.position);}
Link to comment
Share on other sites

Thank's Wingnut and Temechon ! So I decided to use physics collision for all objects except my character. I would like to use moveWithCollision but it doesn't work in babylon js playground.. why ?

 

4 - Object vs. object collision

You can also do the same thing with a mesh by playing with mesh.ellipsoid property andmesh.moveWithCollisions(velocity) function. This function will try to move the mesh according to given velocity and will check if there is no collision between current mesh and all meshes with checkCollisions activated.

You can also use mesh.ellipsoidOffset to move the ellipsoid on the mesh (By default the ellipsoid is centered on the mesh)

How use it ? Can I have an example please ?

Link to comment
Share on other sites

  • 4 months later...

 

Hello there, 

 

I think I will create an article about this in my website.

 

But basically, what you have to do is something like this : 

var body = box.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, {mass:1, friction:0.001, restitution:1.5});if (this.mvtDirection[0] != 0) {      box.applyImpulse(new BABYLON.Vector3(0,0,s), box.position);}if (this.mvtDirection[1] != 0) {      box.applyImpulse(new BABYLON.Vector3(0,0,-s), box.position);}if (this.mvtDirection[2] != 0) {      box.applyImpulse(new BABYLON.Vector3(-s,0,0), box.position);}if (this.mvtDirection[3] != 0) {      box.applyImpulse(new BABYLON.Vector3(s,0,0), box.position);}

I'm sorry for bringing this topic alive again. But.... Temechon I need to ask you something about your post. The "S" value is the value of the force applied in the axe right ? So... by this I understand that for my "point and click" game I need to do a function that calculates the force needed for the ship to go from the origin to the target right?

Btw... If I apply zero to the gravity, shouldn't i be able to apply mass ? Without everything falling down to the infinite ?

 

Thanks..

Link to comment
Share on other sites

Kilombo - if you want to move a character to a given point you'll probably want a spring force - that is, you apply a force towards the target point, proportional to the distance. If the force is too strong it will shoot past the target and bounce back, but if you tune the damping and force you can probably get it to move smoothly.

 

More generally though, moving characters controlled by physics is a pretty nontrivial problem, and every game does it a little differently. Some games directly set the character's velocity, others apply forces, etc. In my case I apply forces, but I also turn off the character's friction when a key is pressed. But it really changes with each game's desired feel.

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