Jump to content

mesh position on scene


isigui
 Share

Recommended Posts

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.

post-5570-0-37580300-1385424799_thumb.pn

post-5570-0-69153400-1385425200_thumb.pn

Link to comment
Share on other sites

I make a precision: to check the collision, I use the

BABYLON.Mesh.prototype.intersects(ray, fastcheck) function

var 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 !

 

Link to comment
Share on other sites

Walls.getBoundingInfo().boundingBox.minimum is the bounding box's smallest value but expressed in the world of the object

minimumWorld 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);
    };
Link to comment
Share on other sites

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 !

 

post-5570-0-63819300-1385485088_thumb.pn

post-5570-0-31603200-1385485089.png

Link to comment
Share on other sites

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

I 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 :wacko:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);            }        });

post-5570-0-34073800-1385558739.png

post-5570-0-91763500-1385559097.png

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