Jump to content

Rays and Collisions


primesoftware
 Share

Recommended Posts

Hello all! I am new around here, but not new to game dev. I was using the Godot engine for a bit, but wasn't happy with it so I switched to Babylon. I need a little help with collisions and ray casting though. I've combed through all the tutorials and can't seem to get this working. I am loading a scene imported from Blender. In Blender I made a simple Box where I want to cast a ray to when clicked on. Simple picking isn't going to work for me because I only want to perform an action when a small area of larger model is clicked. The Box is not visible, but is registered as a physics object. Clicking the invisible box does not work at all. I just end up hitting the visible model underneath the invisible box.

var canvas, engine, scene, camera;document.addEventListener("DOMContentLoaded", function () {    if (BABYLON.Engine.isSupported()) {        initScene();    }}, false);function initScene() {    canvas = document.getElementById("webGLCanvas");    engine = new BABYLON.Engine(canvas, true);        BABYLON.SceneLoader.Load("assets/models/", "Gameboard.babylon", engine, function (gameScene) {        gameScene.executeWhenReady(function () {            gameScene.enablePhysics(new BABYLON.Vector3(0, -10, 0), new BABYLON.OimoJSPlugin());            gameScene.collisionsEnabled = true;                        var light = new BABYLON.PointLight("light", new BABYLON.Vector3(0, 5, -5), gameScene);                        camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 4, -10), gameScene);            camera.setTarget(new BABYLON.Vector3(0, 0, 10));            camera.attachControl(canvas);                        var counterHitBox = gameScene.getMeshByName("CounterHitBox");            counterHitBox.checkCollisions = true;            counterHitBox.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, {mass: 0, friction: 0.5, restitution: 0.7});                        gameScene.onPointerDown = function (evt) {                var pickResult = gameScene.pick(gameScene.pointerX, gameScene.pointerY);                                alert(pickResult.pickedMesh.name);            };                        engine.runRenderLoop(function() {                gameScene.render();            });        });    }, function (progress) {        // To do: give progress feedback to user    });};
Link to comment
Share on other sites

Hello and welcome!

 

Physics and picking are not related as they do not use the same internal engine. The picking just need you to set mesh.isPickable = true

 

Example here:

http://www.babylonjs-playground.com/#UF7SS

 

Thank you for the welcome! I gave this a try, but it doesn't pick my invisible mesh. The mesh is invisible, but enabled. Does that make a difference? I saw someone else said use the pick function with a predicate. I gave that a try, but it was picking every mesh on the screen even if I didn't click on it.

 

Edit: So I figured it out using this thread (http://www.html5gamedevs.com/topic/7709-scenepick-a-mesh-that-is-enabled-but-not-visible/). The predicate function just says which meshes are valid, not which ones were picked. That was why I was getting an alert with all the meshes in my scene.

 

This code now works:

            gameScene.onPointerDown = function (evt) {                var pickResult = gameScene.pick(gameScene.pointerX, gameScene.pointerY, function(mesh) {                    return mesh.isPickable && mesh.isEnabled();                });                                alert(pickResult.pickedMesh.name);            };
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...