Jump to content

Rotating a physics object [Oimojs]


Recommended Posts


I need to be able to rotate and move my mesh, which has a physics state body set, keeping its physical simulation.

It seems that when I set a physics state on the mesh, I'm unable to rotate it or move it directly.


I've created an example here: http://www.babylonjs-playground.com/#UMR7M#44


Invoking setPhysicsState prevents direct modification of rotation and position.


How can I achieve this? Should I keep a reference to the underlying physics body and modify it directly?


On my own code I even tried:

    mesh.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(      Math.radians(gameState.mesh.gamma),      Math.radians(gameState.mesh.beta),      Math.radians(gameState.mesh.alpha)    );    mesh.updatePhysicsBodyPosition();

But yet, had no luck with it: the mesh is stucked at its initial angle.


It is a week I'm playing with BabylonJS and it is outstanding what I have already achieved, keep up the great work!

Thank you in advance.



Link to comment
Share on other sites

yuh yuh yuh.  Is that what Ahiru has/does, DK?  *nod*  Yep, Sir Kosh of Delta taught me that when there's an issue with 'dispose'... it might be caused by the PREVIOUS playground scene... trying to "unload"... and failing.  I think that's what's happening in Ahiruville.

Link to comment
Share on other sites

Guys, is there an easier way to do this?  Can we streamline physics body positioning and rotating?  (wrap all the BS in something wonderful) ?  Thoughts?


I think this can be easier to use. Just thinking out loud - if there is a setPhysicsState function, why not set the physics body object returned from this function within this function call? something like this:

target.setPhysicsState(BABYLON.PhysicsEngine.SphereImpostor, {   mass: 1,   friction: 0.5,   restitution: 1});//target.physicsBody was just set in this function call//and then		var mq = target.rotationQuaternion;		// create quaternion to add		var q = BABYLON.Quaternion.RotationYawPitchRoll(.02, 0, 0);		target.rotationQuaternion = q.multiply(mq);		target.physicsBody.setQuaternion(target.rotationQuaternion);		target.physicsBody.sleeping = false;

Or, maybe, finding the common "body" components and create an interface for physicsEngineBody ?


//Edit after actually looking at the code :-)

The registeredMeshes array in the plugin is actually making the link between a mesh and its physics body object. Maybe use this together with a unified interface?

Link to comment
Share on other sites

Hi R... um... did you go to the url... http://www.html5gamedevs.com/topic/15005-players-movements-and-oimojs/#entry85074




In the demo line 24, target.body didn't exist... so I had to set it (with the returned 'body' from the setPhysicsState call.)

SO... mesh.body (what you are calling physicsBody) DID once exist... but I think it got changed some time ago.

What I would REALLY like to see... is mesh.physicsBody.rotation = new BABYLON.Vector3(1,2,3);  And it needs to be "live" so we can torture it from within render loops, too.  :)  Box is tumbling along just fine... playing proper physics... and boom, we change the imposter's rotation mid-tumble.  Instant tumble craziness!

(Like I have ANY idea what I'm talking about, here)  :)

Hmm, could applyImpulse on a sphere... pitch a curveball?  (small imposter z-rots as the sphere travels along)  hmm. 

Link to comment
Share on other sites

In the demo line 24, target.body didn't exist... so I had to set it (with the returned 'body' from the setPhysicsState call.)


Of course I went to the URL :-) And your sentence here is exactly my point - 

target.body never existed. thsi is a wonderful javascript hack that allows you to set parameters dynamically to any object. Making this variable (call it body or whatever) without the need of the call in line 24, as part of the framework, would be a solution that will simplify things (as it is then documented and part of the mesh class).

Now, about the rotation setting - this can be done nicely, but will have to require a setter to the rotationQuaternion (or something to check it each frame, which will be bad for performance).

The setter might look like this (combining both ideas, and in a very prototypical way):

function setQuaternion(quaternion) {  this.rotationQuaternion = quaternion;  if(this.physicsBody) {    this.physicsBody.setQuaternion(quaternion);  }}
Link to comment
Share on other sites

VERY cool, thanks R!  I like it.

btw, I tried to pitch a curveball... and it doesn't appear to be working.  I might be able to rotate the imposter mid-flight, but that's not changing trajectory at all.  I guess that's completely understandable.  I'm trying bend a force direction with a rotation...  pretty much a demented idea.  :)


Link to comment
Share on other sites

Thank you for the replies guys, it took me a while to figure out why Wingnut example was not working in my scenario.

Turns out that if I put mass: 0 for the Impostor (http://playground.babylonjs.com/#1VNAYW#9) the cube stops rotating.

Now since the mesh I'm using should be controlled by the end-user and I'd need it to have no mass, how could I workaround this?

Link to comment
Share on other sites

I expanded on Raanan's "test++" demo... to make sure that 0 mass imposters... can still do proper colliding (pinball flipper thought).  It smells like joez82 might be going-after moving a physics-active object...  with mouse moves or similar.  SO, I thought I would test some things.   I was in-for a surprise...


If I read this right, it appears that we can rotate that imposter until we are silly, but when it comes time for a collision, the physics engine takes over and sets the rotation as IT sees fit.  :)  Interesting!  We need to un-bind the mesh from the body.  Or... something.  Selective unbind and rebinding.  errr...  I'm scared.  :)  #12 too.

Link to comment
Share on other sites

At this point I elaborated on Wingnut's modified evolution of RaananW's sample and it seems that adding another target.updatePhysicsBodyPosition(); after setting the quaternion does the trick.


Yet, I am wondering wether going down this hacky road is a wise choice... maybe I could make my mesh non-physical and test for mesh intersections.

But to make collisions realistic it would be nice if there was a way to detect the point of contact of the collision, even if approximated.

Any advices on this?

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