Jump to content

Problem with rays and camera rotation


Yurifag
 Share

Recommended Posts

Hi there Babylon JS community,

I'm really new to Babylon JS and 3D game development. My goal is to create a simple, not too fancy first person shooter.

After I messed around with this library for a while I encountered some problems.

I'm using a FreeCamera which is hooked to WASD for movement and the mouse cursor is locked to the canvas to make it "FPS like".

My first problem is that I don't know how to get the point the camera is currently looking at as a vector to use rays for collision detection (to detect if you hit something). So I calculated it myself based on the camera's rotation, but the rays don't seem to hit the target on certain points. It's like an offset and I think it has to do with my calculation. I'm not really good at math and I don't see my mistake, sadly.

 

var rotationX = camera.rotation.x % (2*pi);	var rotationY = camera.rotation.y % (2*pi);if(rotationY < 0) {   rotationY = 2*pi + rotationY;	}var impulseY = -rotationX * (1 / (pi / 2));var impulseX = Math.sin(rotationY) - Math.abs(impulseY) * Math.sin(rotationY);var impulseZ = Math.cos(rotationY) - Math.abs(impulseY) * Math.cos(rotationY);var cameraDirection = new BABYLON.Vector3(impulseX, impulseY, impulseZ);var shot = new BABYLON.Ray(camera.position, cameraDirection.scale(100));var meshFound = scene.pickWithRay(shot);if(meshFound !== null && meshFound.pickedPoint != null) {   console.log("hit", meshFound.pickedMesh.name, meshFound.pickedPoint);}   
Here you can see an example of what I mean including the code above:

http://grzb.de/3D/test (wasd to move, left mouse button to "shoot" a ray, mesh information in console)

Press CTRL + U to view the full source code on my site.

My second problem is that the camera rotates "smoothly". It doesn't immediately stop when the mouse stops which of course isn't good for a FPS game.

If anyone could help me with these problems I'd be very thankful. I've spent a long time now trying to figure out what's wrongwrong.

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