Jump to content

How to find which cube face my object is hitting?


Recommended Posts

Question is about my half complete game. Here is my game: http://www.babylonjs-playground.com/#KBHU0#12

Use Arrow Keys to turn the cube. Game objective is to align the cube to catch the same color slab.

Question: When the slab hit the cube (function checkHit()), how do I decide if the color of slab and the that side of the cube is the same?

I am new to game development. Any suggestions on my code is very welcome.

Another question: Press Up (watch direction of turn), then press Left, now again Up - notice that direction is reversed. How to correct this (to keep the same direction)?





Link to comment
Share on other sites

Hello you should use scene.pickWithRay to decide which face should be selected: http://doc.babylonjs.com/classes/2.2/Scene#pickwithray-ray-predicate-fastcheck-rarr-pickinginfo-classes-2-2-pickinginfo-

For your second question you can use mesh.rotate (http://doc.babylonjs.com/overviews/How_Rotations_and_Translations_Work) with local system reference

Link to comment
Share on other sites

Than you for reply. However I am still lost.

Once I call pickWithRay, how I do I find out which side (face) of the mainCube it hit?

      var rayPick = new BABYLON.Ray(slabObj.slab.position, mainBox.position);
      var pickInfo = scene.pickWithRay(rayPick, function (item) {
        console.log("Found hit", item);
      console.log("PickInfo", pickInfo);

Callback function does find mainCube. But the pickInfo.hit is false!

Side question: Why the 2 callbacks are fired by pickWithRay? Is it for each side of the cube?

Check line 92 on




Link to comment
Share on other sites

pickwithray doesn't work with callbacks. See the doc here: http://doc.babylonjs.com/classes/2.2/Scene#pickwithray-ray-predicate-fastcheck-rarr-pickinginfo-classes-2-2-pickinginfo-

The second parameter is a function that should return 'true' if the mesh can be selected, or 'false' if it isn't.

That's why it's called twice in your example: you have 2 meshes.

Link to comment
Share on other sites

Hey kaushik,

The 2nd parameter needs to be a direction, not the position from the mainbox (which is BABYLON.Vector3(0, 0, 0)). http://doc.babylonjs.com/classes/2.2/Ray

var rayPick = new BABYLON.Ray(slabObj.slab.position, new BABYLON.Vector3(0, -1, 0));

back    BABYLON.Vector3(0, 0, -1).
down    BABYLON.Vector3(0, -1, 0).
forward    BABYLONVector3(0, .0, 1).
left    BABYLON.Vector3(-1, 0, 0).
right    BABYLON.Vector3(1, 0, 0).
up    BABYLON.Vector3(0, 1, 0).

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