ssbarbee Posted November 26, 2015 Share Posted November 26, 2015 Hello everyone! I've recently started playing with BabylonJS and I have to say it's really a wonderful library to work with. The learning curve is really smooth but I'm having issues with the physics engine (mostly due to my ignorance, although I've googled a bit trying to find a solution). So the problem I am having is following :In the following playground http://www.babylonjs-playground.com/#2DQWXU#0 we can see a ball in the top left corner and a cube in the bottom right corner. The user can interact with the ball and by selecting it you can move it around the grid (without physics enabled ). The movement is a simple increasing/decreasing of the values for ball position until a certain clicked position is reached (the vector expressed by variables xreach, yreach, zreach). Now a strange thing happens when physics is enabled (just set the variable enablePhysics to true). The user cannot move the ball, it wont respond to the action and the ball and the cube start dancing and jittering a bit. I am curios what is happening? Should the movement of the ball happen by calling some physics function when physics engine is enabled? If so can you tell me which one? Can someone also review my render code (should i be calling render function for the cube also)? Is there something that I can improve overall? Thank you for reading my question(s). fenomas 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 27, 2015 Share Posted November 27, 2015 http://playground.babylonjs.com/#2DQWXU#3 (starting to come alive) Hi ssbarbee, welcome to the forum! I'm a bit short on time right now, but, there are two TYPES of collision systems available in BJS. First, there is a collision/intersect system built-into BJS. It most-often uses a function called .moveWithCollisions() to move objects and it checks for intersection between any two mesh. You can see it in action... here...http://playground.babylonjs.com/?9 and here... http://playground.babylonjs.com/?10 . It is a highly useful intersection system and runs much faster than using the third-party add-on Cannon or Oimo physics libraries. But it won't bounce and deflect things like a physics engine will. You chose to use the Cannon physics engine in your demo, and, generally speaking, physics engines use mesh.applyImpulse(forceDirectionAndMagnitude, contactPoint) (a little jet engine) to do mesh moving. Impulsing has no brakes, so it is difficult to STOP a ball that's been impulsed. Spheres have no significant surface area at the ground contact point - so friction has no effect. (Wingy gives a nod of thx to Fenomas.) Simulated wind-drag has been discussed, recently. One idea was to constantly multiply the physics imposter .angularVelocity by .999 - introducing a "drag factor".) But there is another way to move physics-active mesh (and stop them in the right place), and I used that in the #3 demo. (lines 422, 427, 439, and 444)... .updatePhysicsBodyPosition(). It will cause the mesh.body.body (sometimes called a physics imposter) to track-to the .position of the mesh. So, right after you did your moves, I added the .updatePhysicsBodyPosition() calls. I also removed the restitution on your ground to reduce/eliminate "jiggle". I also added some serious friction to ground and box... so that the box doesn't fall off the grid so easily. I also turned off your camera limiters, but you can turn them back-on as wanted. I probably adjusted a few more things, too. Anyway, now you can roll the ball into the box and the box will tip over and move around when the sphere hits it. It's not perfect, but maybe it helps. I'll discuss these systems more with you, when I have a bit more time. Others are nearby, too. You have an interesting coding style... I like it. More soon, good luck, and again, welcome! Party on! fenomas, jerome and Vousk-prod. 3 Quote Link to comment Share on other sites More sharing options...
fenomas Posted November 28, 2015 Share Posted November 28, 2015 Sweet demo wingy! To expand/pontificate a little, physics-based contents tend to work best if you treat objects as either being static (where you might set the position manually, but the physics engine treats it as a static immovable object) or dynamic (where you might apply forces or impulses, but you leave it up to the engine to decide the actual position). If you get into the middle ground where you want an object to move dynamically but you also want to force it into certain positions sometimes, things tend to get unstable. (I mean to be clear, manually setting an object's position always has the potential to make things blow up, but if it's necessary it's necessary.) So wingnut's demo is a perfect example of the former approach. The ball goes just where its told, being moved by the hand of god as it were. But if you also wanted the ball to bounce off things, or slow down when moving very heavy objects, or generally behave like an object with mass and velocity and so forth, then you'll probably need to give up control over its precise position, and switch to pushing it where you want it to go. Incidentally one very common way to do this is with springs - attaching a short, strong spring between the sphere and a desired location will move it precisely but in a physics-friendly way. I don't know how one does this in cannon - maybe a resident expert can chime in? Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 28, 2015 Share Posted November 28, 2015 Thanks for the kind words and info, Fenomas. Well said. It's actually ssbarbee's demo, though. I just added a few lines and did some minor tweaks. AND, I consider Fenomas to be one of the resident experts. He might not be an expert on Cannon or Oimo, but he's got some physics expertise from somewhere... and he's fixed my incorrect physics thinking on numerous occasions. I'm pretty glad he hangs around with us. fenomas 1 Quote Link to comment Share on other sites More sharing options...
RaananW Posted November 28, 2015 Share Posted November 28, 2015 Well! That's a nice thread Here is my take on it - Wingnut, fenomas, you are both right.Once physics is enabled, the mesh's tranformation (position, rotation) is fully controlled by the physics engine. If you just want to change the mesh's position, you could do that - http://www.babylonjs-playground.com/#2DQWXU#5try colliding against the box, you will see that it actually bumps into it and "reflects" back. is this what you wanted to achieve? to get the physics body, check lines 322 and 324. 322 - you can assign directly when creating it. You could also use line 324 everywhere in your code - this will return the mesh's physics body. Wingnut 1 Quote Link to comment Share on other sites More sharing options...
ssbarbee Posted November 28, 2015 Author Share Posted November 28, 2015 Thank you guys for the fast responses. Just a quick summary below as a future reference So from what I can tell updating the physics body can be done by keeping a reference of the physics body once we set the physics engine and then updating that object. This approach was recommended by RaananW and is summed in the following lines pB = scene.getPhysicsEngine().getPhysicsBodyOfMesh(mesh) and then somewhere below pB.position.x = someX instead of mesh.position.x = someX. Another way of doing the same thing is calling the function mesh.updatePhysicsBodyPosition() right after updating the mesh position ( mesh.position.x = someX; ) which was suggested by Wingnut. Both of the suggestions resolved my issue and helped me get a better grasp of using a physics engine in Babylon JS for which I am grateful. Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 29, 2015 Share Posted November 29, 2015 Yeah, I think you've got it, ss! I'm quite new at this, and have not had too many needs to "hand-of-God" (hog)-move physics-active mesh. I was rather surprised to see .position and .rotation on the rigidBody (physics body). I had to go look at my bad Oimo map and about 3/4 down... in the rigidBody class... there it was, #8... .position. Whooda thunkit? Raanan is who. Hand of God. Hog. HOG-movin' physics mesh... being a pig... grunting the impostor into positions and orientations... HOGgin' all the forces. I like it. HOG move! I think we just created a new term. "I just HOGGED the imposter into position." yeah! I LIKE IT! (I'm SO easily entertained.) Good thread... I learned a bunch of stuff... and we all got to meet ssbarbee, future physics mad scientist. Can we expect a Rube Goldberg machine by mid-summer 2016, ss? 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.