isigui Posted November 26, 2013 Share Posted November 26, 2013 Hello,I have a simple blue box that I put in my scene and set the position this way: var box = BABYLON.Mesh.CreateBox("wall1", 10, that.Scene, false); box.position.z = 10; box.material = that.MaterialManager.GetBlueMaterial();I also have a character (the dude ...) on the scene and a BABYLON.Ray object which has:- for origin: the head of my character- for direction: BABYLON.vector3(0,-1,0). I want to check collision between my box and the Ray.When the dude collides with the box, the box appears red. Actually, the ray find a collision when the dude is between the (0,0,-5) and (0,0,5), I thought It should have been between z position (0,0,5) and (0,0,15). What Am I doing wrong ? Thanks,Guillaume. Quote Link to comment Share on other sites More sharing options...
isigui Posted November 26, 2013 Author Share Posted November 26, 2013 I make a precision: to check the collision, I use theBABYLON.Mesh.prototype.intersects(ray, fastcheck) functionvar distanceFromWall = Walls[i].intersects(ray, true);It seems that the property Walls.getBoundingInfo().boundingBox.minimum.z still persists to -5 value. Is it possible to have more infos about boundingBox.minimum/maximum property and minimumWorld/maximumWorld property ? One last question, when I change the position value of a mesh like mesh.position = new BABYLON.Vector3(x,y,z), I actually change its position in the world, isn't it ? Thanks for your answers ! Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 26, 2013 Share Posted November 26, 2013 Walls.getBoundingInfo().boundingBox.minimum is the bounding box's smallest value but expressed in the world of the objectminimumWorld is the same value but into the world If you want to use mesh.intersects(ray) you have to transform your ray into object's world:var inv = BABYLON.Matrix.Identity(); world.invertToRef(inv); BABYLON.Ray.Transform = function (ray, matrix) { var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix); var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix); return new BABYLON.Ray(newOrigin, newDirection); }; isigui 1 Quote Link to comment Share on other sites More sharing options...
isigui Posted November 26, 2013 Author Share Posted November 26, 2013 It works !!!thanks a lot ! for (var i = 0; i < that.SceneManager.Walls.length; i++) { var inv = BABYLON.Matrix.Identity(); that.SceneManager.Walls[i]._worldMatrix.invertToRef(inv); that.GroundRay = BABYLON.Ray.Transform(that.GroundRay, inv); var distanceFromWall = that.SceneManager.Walls[i].intersects(that.GroundRay); if(distanceFromWall.hit === true) { that.SceneManager.Walls[i].material = that.SceneManager.MaterialManager.GetRedMaterial(); } else { that.SceneManager.Walls[i].material = that.SceneManager.MaterialManager.GetBlueMaterial(); }}Just two more questions on this subject:1) If i want to use the same ray for all the "walls", I will have to convert the ray into the wall's world I want to check intersection with each time, is it a good solution ?2) I didn't find BABYLON.Ray.Transform(Ray,Matrix) into the BABYLON, will it be added ? Thanks again ! Temechon 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 26, 2013 Share Posted November 26, 2013 1/ It works:)2/ It will be available in 1.7 (But for you I made a preview^^) Quote Link to comment Share on other sites More sharing options...
isigui Posted November 26, 2013 Author Share Posted November 26, 2013 2) It's very nice of you Quote Link to comment Share on other sites More sharing options...
isigui Posted November 27, 2013 Author Share Posted November 27, 2013 Aie, just need some more help. In the example mentionned below, I used full built-in mesh from BABYLON.js, the box. This is ok, when I express the ray position in the mesh world, it works fine.Now I import a mesh from Blender, And try the same way to detect collisions with the Ray. But it is not working.After I express the ray in the "blender mesh" world, my ray got the direction u(0,-0.4,0), which I don't understand (express in the "global world", my ray direction is(0,-1,0)). I read that mesh from blender are y/z inverted compared to babylon.jsI feel this has something to do with my problem but I need a little push to understand and to solve it. Sorry again for certainly basics question Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted November 27, 2013 Share Posted November 27, 2013 It hould not have a difference. COuld you force a world matrix computation after loading your mesh ? (mesh.computeWorldMatrix()) Quote Link to comment Share on other sites More sharing options...
isigui Posted November 27, 2013 Author Share Posted November 27, 2013 I did this: BABYLON.SceneLoader.ImportMesh("Cube", "Scenes/Wall/", "Wall.babylon", scene, function (newMeshes, particleSystems, skeletons) { var wallMesh = newMeshes[0]; wallMesh.scaling.x = 5; //Removing the scaling makes it work wallMesh.position.z = 40; wallMesh.position.x = 20; wallMesh.computeWorldMatrix(); that.Walls.push(wallMesh);}); I noticed that when I remove the scaling, it works ... What Am I doing wrong again ... Quote Link to comment Share on other sites More sharing options...
Temechon Posted November 27, 2013 Share Posted November 27, 2013 Scaling does not update the bounding box info. Try to call wallMesh.refreshBoundingInfo() Quote Link to comment Share on other sites More sharing options...
isigui Posted November 27, 2013 Author Share Posted November 27, 2013 Ok, thanks for the information Temechon, I did it , apparently it seems to work when I don't touch to the y value of the scaling property See, code 1 (with result screenshot 1) - do what I expected (mesh become red when I pass under)BABYLON.SceneLoader.ImportMesh("Cube", "Scenes/Wall/", "Wall.babylon", scene, function (newMeshes, particleSystems, skeletons) { var wallMesh = newMeshes[0]; wallMesh.scaling.z = 2; wallMesh.scaling.x = 10; wallMesh.computeWorldMatrix(); wallMesh.refreshBoundingInfo(); that.Walls.push(wallMesh); for (var i = 0; i < 10; i++) { var w = wallMesh.clone("wallMesh" + i); w.id = "Wall" + i; w.position.z += (i + 1) * 4; w.position.y += (i + 1) * 3; that.Walls.push(w); } });and code 2 with (result screenshot 2) - don't do what I expect BABYLON.SceneLoader.ImportMesh("Cube", "Scenes/Wall/", "Wall.babylon", scene, function (newMeshes, particleSystems, skeletons) { var wallMesh = newMeshes[0]; //Change scaling.z to scaling.y wallMesh.scaling.y = 2; wallMesh.scaling.x = 10; wallMesh.computeWorldMatrix(); wallMesh.refreshBoundingInfo(); that.Walls.push(wallMesh); for (var i = 0; i < 10; i++) { var w = wallMesh.clone("wallMesh" + i); w.id = "Wall" + i; w.position.z += (i + 1) * 4; w.position.y += (i + 1) * 3; that.Walls.push(w); } }); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.