Jump to content

(Solved) Avoid FreeCamera Fly / takes Y.Rotation in consideration on move


BabyJS
 Share

Recommended Posts

I'm using a FreeCamera, gravity,etc... and when i look at the sky and press W to go forward i go up and can fly, the same if i look at the floor and press back.

I want that with WASD the camera moves without take the Y.rotation in consideration, because i still need to move the camera and rotate to look everywhere.

 

How can i achieve that? I tried with "Camera.CamFrontX(Distance)" , Camera.DirectionX(AxisX, AxisY, AxisZ) ,etc... to fix the Y.Rotation value but never got the desired behaviour, sometimes the Y is limited and i can't climb ramps or stairs, other the collisions are not detected or other weird stuff.

Link to comment
Share on other sites

The thing is create some kind of switch so:

Deactivated : The camera not take the Y.Rotation in consideration and you can only move in X/Z axis. But if you find a ramp or stairs you still can climb it.

Activated: The actual free camera, to be used as a "Jetpack behaviour" for the player on you can fly.

 

Any ideas how to achieve that?

Link to comment
Share on other sites

Hi Matriax.  Ahh, Deltakosh beat me to it.  :)  Oh well... here's more:

A little playground demo... where a "camMesh" box is created, and then the free camera is parented to the box, and then the box uses a handy little moveCamera() function, to get the job done.  In this demo, the camera (camMesh) is always moving forward... slowly.  (Notice .moveCamera() is called repeatedly from inside the renderLoop at line 63)

The reason it is able to climb the ramp... yet not climb the SIDES of the ramp... is based upon the size and elliptic shape... of camMesh.ellipsoid (a vector 3 that determines the scale of a mesh collider).

You can play with camMesh.ellipsoid values... while raising the ramp height.  If too much raising of ramp, camMesh will not be able to climb it anymore.  The edge gets higher than the center of its .ellipsoid size.  Remember the camMesh.ellipsoid is the RADIUS of the invisible collider, not diameter.

You can view the collision .ellipsoid on cameras and mesh... by creating a sphere of the same size, and parenting it to camMesh.  It might help if it is .visibility = .5;  (so you can see-through it.)

Gravity is also handled... by moveCamera() function, too.  Later, when you want to use jetpack cam, simply turn off scene gravity, and use moveCamera() again (with some keypress/gamepad eventListening to determine thrust directions).

The only reason camera.applyGravity is not used in this playground demo... is because moveCamera is doing that FOR us.  You can do camera moving and gravity work just fine... without using moveCamera() and camMesh.  You can control the camera itself.

Link to comment
Share on other sites

I'm already using gravity for the camera but i did it to be as a real life, not a constant gravity, so the camera fall every time more faster.

 

Also i had problems to climb stairs and go over some objects so i made two things:

1.- When you hit the floor the gravity comes to 0 and if you try to jump or whatever the gravity starts to increase from 0 to Up.(instead to be always 9.8).

2.-When the player try to go up, this means the camera ellipsoid try change to a higher Y(like climb a stairs), i add some impulse(substract some gravity).

 

With this i can add stairs of a big size and the player climb them so smoothly like if was a ramp in each step. No "earthquake" effect, without add some fake ramp mesh objects in the staris or whatever.

 

But the problem as i said is when you move, the camera takes the Y rotation in consideration to change his position so makes you can fly if you look the sky and go forward or if you look the floor and go back.

 

So, i need a way to using the FreeCamera avoid takes the Y.Rotation when camera moves. Also i tried toclear controls and use my own like "When W is pressed" > Camera.CamFrontX(1),etc...  that avoids the Y rotation and the camera only moves in the axis but always i get weird results, i'm bad in this axis-math stuff :S . Any example?

 

 

Link to comment
Share on other sites

http://www.babylonjs-playground.com/#1GL6RR#7

Here I changed the moveCamera function a bit.

Lines 62/63 ... slewX and slewZ can be thought-of as left/right throttle setting, and forward/backward throttle setting (perhaps -5 to +5 on each).  These are WORLD coordinates, so it doesn't matter how the camera is aimed.  (Y-rotation ignored).

Down in line 77, I changed variable name to "movement" because it is not camera-forwards anymore.  It is world-directions... so... I changed name.

Now you can look sideways, and still go over ramp... because camera/camMesh is always going to move along WORLD +/- X, and WORLD +/- Z... or both at same time.  Camera aiming direction (y-rotation) doesn't matter.... because of major changes to line 77.  Hope this helps.  Write back.  :)

Link to comment
Share on other sites

LOTS OF THANKS Deltakosh!!, worked! simple and perfect! . Also thanks Wingnut for that explanation and examples ;) .

In my case using the plugin of Babylon.JS for Construct 2 the execute javascript action is this:

"this.runtime.scenes[0].getCameraByName(""camera"").cameraDirection.y = 0;"

Well, other little thing fixed after some days of research.

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