The Snow Irbix Posted December 7, 2014 Share Posted December 7, 2014 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#1I 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#1J'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 ! Quote Link to comment Share on other sites More sharing options...
Dad72 Posted December 7, 2014 Share Posted December 7, 2014 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); The Snow Irbix 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted December 7, 2014 Share Posted December 7, 2014 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) Quote Link to comment Share on other sites More sharing options...
The Snow Irbix Posted December 7, 2014 Author Share Posted December 7, 2014 thank you guys, I tried to use move with collisions and translate but it doesn't work, see http://www.babylonjs-playground.com/#LKCKO#4box.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 collisionAnd apply impulse with KeyBoard Event rotate the box sometimes :http://www.babylonjs-playground.com/#1JSG4F Quote Link to comment Share on other sites More sharing options...
Wingnut Posted December 7, 2014 Share Posted December 7, 2014 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. The Snow Irbix 1 Quote Link to comment Share on other sites More sharing options...
Stephen Andrews Posted December 7, 2014 Share Posted December 7, 2014 I'm still trying to work this out too. I need my player character to collide with standard physics objects, but also move controlably. Quote Link to comment Share on other sites More sharing options...
Temechon Posted December 8, 2014 Share Posted December 8, 2014 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);} The Snow Irbix 1 Quote Link to comment Share on other sites More sharing options...
The Snow Irbix Posted December 8, 2014 Author Share Posted December 8, 2014 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 collisionYou 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 ? Quote Link to comment Share on other sites More sharing options...
The Snow Irbix Posted December 9, 2014 Author Share Posted December 9, 2014 I resolve my problem, thank you all Quote Link to comment Share on other sites More sharing options...
Dad72 Posted December 10, 2014 Share Posted December 10, 2014 I have a demo for you iribix http://www.castorengine.com/babylon/moveCharacter/ The Snow Irbix and Wingnut 2 Quote Link to comment Share on other sites More sharing options...
The Snow Irbix Posted December 10, 2014 Author Share Posted December 10, 2014 Thank you very much ! It's exactly what I want to do ! Quote Link to comment Share on other sites More sharing options...
Kilombo Posted April 21, 2015 Share Posted April 21, 2015 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.. Quote Link to comment Share on other sites More sharing options...
Temechon Posted April 21, 2015 Share Posted April 21, 2015 Yes, the variable S is the force, and the vector3 is the force direction.I don"'t know about zero gravity, can you have a test playground for this ? Quote Link to comment Share on other sites More sharing options...
fenomas Posted April 22, 2015 Share Posted April 22, 2015 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. 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.