Rotate Physics object with mouse and freecam input

Hello!

I am making a custom first person controller, and I am wondering it it is possible to rotate a object with the camera on the y axis(so like the object is the child), but the object doesn’t when the camera rotates on the x axis.

Thanks for any help!

SOLVE EDIT: The answer was an event listener that tracked mouse movement. I had it track mouse movement in the x axis and rotated the object on the y axis.

hey!
Do you mean billboarding? (but only on Y? which is supported by babylonjs :))

What is

EDIT: unanimously voted best use of a quote :upside_down_face:

Look it up, G! C’mon!

Oh, docs search for billboarding returns zero hits? Ahh. :slight_smile:

It’s actually billboardMode… but that didn’t help the docs search much… except for returning API link for AbstractMesh, the grand-daddy of all mesh.

Perhaps a playground search for billboardMode?

That object that you want to rotate… is it physics-active? (rotationQuaternion)

If not, maybe…

scene.onBeforeRenderObservable.add(function(){
    object.rotation.y = camera.rotation.y;
};

Worth a test. If object is beneath camera, I don’t know how well object.billboardMode = 2; …will work.

Hmmmmmmm.:thinking::thinking::thinking::thinking:

I’ll try stuff and see how goes it.

So that’s billboard mode. That could be useful sometime in the future.

Activated by-default… on sprites and particles. There’s also a mesh.lookAt(something) laying around here… but I think it needs to be put in renderLoop instead of set-once like billboardMode.

You could go deep. Physics? Could a freecam have a .rotationQuaternion.y, by chance? Do you think you could force that value into object.physicsImpostor.physicsBody.quaternion.y? Its a long reach, and Quats HATE being modified, and often like having a NEW QUAT installed instead of editing an already existing one. Can you “insert” a quaternion.y without blowing up the x, z, and w values of the quat?

Bet not. :slight_smile: It’s a daring attempt… and its best to have medical crews standing-by. :slight_smile:

I tried a function which tracks the movement of the mouse, but didn’t work.
it went something like this:
function updatePosition(e) { x += e.movementX; y += e.movementY; rotate(sphere, new BABYLON.Vector3(0, x, 0), rotpower); }

Ahhh, you used a .rotate()… nice. That’s a quat rotate, I believe. Yuh, yuh, yuh. It’s never easy forcing-around physics objects (without impulses or setForce or other sets). PhysicsBody is the boss.

Still didn’t work. I couldn’t call it, as it would freeze everything.

Have you noticed that I copy-pasted the rotate function from your q & e rotate system? I replace the y value with the mouse movementX.:blush:

Ohhh, that’s OUR .rotate() that we coded-up in our chat-session ahh.

There’s another .rotate in BJS core… which I believe rotates mesh.rotationQuaternion… but… it will still likely toast a physicsBody’s quat.

You should just constantly do:

object.physicsImpostor.physicsBody.quaternion = camera.rotationQuaternion in the renderLoop. Should blow up REAL nice. :slight_smile: Constantly copying a BJS quat into a Cannon quat… gruesome. POOM! (sorry) heh.

I should have named ours… um… physicsRotate or something like that.

1 Like

Yeah, I could do that. The one focus of mine right now is having the camera rotate the sphere. any ideas?

That didn’t work. gave infinity fps.

How well do you have camera.inputs studied?

https://www.babylonjs-playground.com/#IFN77Q#32

Again, pointerLock turned off (neener)… and umm… lines 3 - 155 is a copy of BJS FreeCameraMouseInput, which is currently alive and well in your camera.inputs array.

Watch console to see line 153 reporting-in, proving that the scene is using YOUR mouseInput stuff, and not the one in BJS core. (a hijack).

Hacking that code… into doing someImpostor.setAngularVelocities instead of camera turns… is going to be greasy. But, you COULD get famous if you pull it off. hmm. Gruesome idea… but… you ARE seeing the camera mouse-input code right there in your playground/project. Learn how it works like the back of your hand… and you could own it… and make it do as you wish. Hard grind, I suspect.

Fill it full of console.log(“valueName:”, value) reports, and then start using the camera… watch the console… learn that _onMouseMove() section well.

Thanks! I’ll try my best.

https://doc.babylonjs.com/how_to/customizing_camera_inputs#with-javascript

You might wish to edit your thread title… to “Rotate Physics Object with Mouse FreeCam.input”

Something like that. Maybe add more details to your first post, too.

Do you see how/why deltakosh gave you a less-than-optimal solution… because of lack of details about what you meant?

Do your best. It’s a strange balancing act between lack of details, and TMI, isn’t it? I fight it daily, personally speaking, as you likely know. :slight_smile:

1 Like

A lot of times I don’t talk much, but on the right topic, I talk too much.

1 Like

Givo… I just thought of something.

Our Scene object has been loaded-up with some useful “observables”.

https://doc.babylonjs.com/api/classes/babylon.scene#onpointerobservable

OnPointerObservable is a universal “catch it all” observer… and your handler function would need to “sniff” to see what happened… turn, drag, possibly swipe/gestures, mousewheel, etc.

There’s also specific-action observables… like onPointerMove, down, up, pick, etc.

If I were you, I would leave FreeCameraMouseInput alone, for now. Keep it default.

Instead… write a bunch of custom scene-based pointer-event handlers… using those observers.

Start with little handlers… that simply console.log(“mouse moved”) and try to determine WHEN a user is dragging on-canvas. Actually, you’re a pointer-lock junky, so maybe you don’t need to listen-for drags… only pointer-moves.

We also have scene.actionManager to help us determine when user is leftbutton/rightbutton dragging or wheeling. (geez, does ANYONE do right-button dragging, these days?)

Anyway, with custom observers/handlers… set on these handy BJS SceneObject observables… you SHOULD be able to get all the power you need… WITHOUT having to deep-hack the camera’s mouseInput code.

Try that, first. While testing, or at other times, you may wish to NOT attach the camera to the canvas. Observers might quit working, then, though. Maybe scene.activeCamera = null;? I dunno… I’m just tying to think of ways to keep the camera frozen… and still listen for scene pointer observables.

Using scene observables… is a much better solution than the input hacking. I think I might have lead you astray by suggesting it. If you need help adding some basic observer handlers to the scene object, just holler. It’s simple, though.

scene.onPointerObservable.add(function(d,s){
     console.log("mouse did something!", d, s);
}

d and s are a couple of automatically-arriving objects that might have useful info on them.

There’s no results when searching Scene API for ‘mouse’ or ‘wheel’… just pointer stuff. No right-button or middle-button observables, either… but we can get them from scene.actionManager, if needed.

Give it a sniff, if you please. I think these scene-observer methods are much wiser than hacking that freeCamMouseInput crap.

Some keyboard stuff, seen, too. hmm. peace-out!

I’ve been thinking too. A cube isn’t the best thing for a hitbox because of the corners. (go against a wall, and not be able to turn) What if we making a a cylinder diameter 1. height 1, and call it good. then we could move it bas on the CAMERA’s y rotation, as a cylinder is uniform all the way around. The problem with that is we would have to combine the cylinder’s x an z rotation and the camera’s y. just something that might make it hard but could work.

I used an event listener that tracked mouse movement. Worked like a charm. thanks for trying to help @Deltakosh!

Note to self:

  1. Have a repro to my issue in the playground
  2. More details
  3. No TMI
2 Likes