Jump to content

Change position on blender imported Mesh.


Kilombo
 Share

Recommended Posts

Hello everyone,

 

I had no problem on importing a blender mesh into the scene. But I'm not beeing able to make it move.

I've created a class, so that you can easily instantiate one object in the scene. But since the Babylon.ImportMesh is a callback method, I'm not beeing able to access the object position in the loop method.

 

Help Plz :)

 

Thanks in advance, Kilombo.

Link to comment
Share on other sites

Hi Deltakosh,

 

Thanks for your answer. Although that doesn't solve my problem. I'm not understanding what you mean. That works but the object stays in the same place. The position is changed in the render loop (by receiving a scene.pick(event), that defines the position to where the object should translate. Problem is, I'm not beeing able to change the newMeshes[0].position from the renderloop, I'm only beeing able to access it in the first "draw" (when babylon first draws the object in the initial position).

I'm putting some of the code to see if it helps explaining the problem. There it goes:

 

//this is part of the unit class

 

unit.prototype.load3D = function(name,imageFileName){
    BABYLON.SceneLoader.ImportMesh(name, "Assets/babylonreadyfiles/", imageFileName, scene, function(newMeshes) {
        //unit.setShipPos(setup(newMeshes[0]));
        if (scene.isReady()){
        newMeshes[0].scaling.x = 0.2;
        newMeshes[0].scaling.y = 0.2;
        newMeshes[0].scaling.z = 0.2;
        
        //if (selfchecker === 0) {
        //selfchecker = 1;
        /*$.get("Communications/getUnit.php", function(data) {
            //console.log(data);
            var name = jQuery.parseJSON(data);
 
        });*/
        $.get("Communications/getPosition.php", function(data) {
            //console.log(data);
            var pos = jQuery.parseJSON(data);
            newMeshes[0].position.x = pos.posx;
            newMeshes[0].position.z = pos.posy;
            newMeshes[0].position.y = 0;
        });
        
        //};
    }
        unit.prototype.setShipPos(newMeshes[0].position.x,newMeshes[0].position.z);
        /*newMeshes[0].position.x = shipx;
        newMeshes[0].position.y = 0;
        newMeshes[0].position.z = shipz;    
        */
        
        });
};

 

//this is loaded on the function start

 

var scout1 = new unit();
scout1.load3D("transport","transport.babylon");
 
//this is part of the render loop
posix = scout1.getShipPosx();
        posiz = scout1.getShipPosz();
 
        dx = targetx - posix;
        dz = targetz - posiz;
 
        rot = Math.atan2(dx, dz); // or dz,dx, but modify rotation
        len = Math.sqrt(dx * dx + dz * dz);
        if (len === 0)
            len = 1;
        dx = dx / len;
        dz = dz / len;
 
        posfy = parseFloat(rot + Math.PI);
        posfx = parseFloat(scout1.getShipPosx() + dx);
        posfz = parseFloat(scout1.getShipPosz() + dz);
 
        shiproty = Math.round(posfy, 1);
        shipx = Math.round(posfx, 1);
        shipz = Math.round(posfz, 1);
        scout1.setShipPos(shipx,shipz);
        //console.log(box.position.y);
        //console.log(box.position.x);
        //console.log(box.position.z);
 
        if (counter === 20) {
 
            //if (check === false){
            //check = true;
            $.ajax({url: 'Communications/PositionUpdate.php', type: 'post',
                data: {'posx': shipx, 'posy': shipz},
                success: function(data) {
                }
            });
            $.get("Communications/CheckSystem.php", function(data2) {
                var pos = jQuery.parseJSON(data2);
                otherbox.position.x = pos.posx;
                otherbox.position.z = pos.posy;
                otherbox.position.y = 0;
                // check = false;
            });
 
 
            counter = 0;
        }
        counter++;
        scene.render();
    });
 
Thanks in advance mate :)
Link to comment
Share on other sites

Thanks, it didn't solved my problem, but kind of opened a path to solve it. OOP in javascript is a litle different from java and c++, I've got to get used to it (and most of the problem was with the class)  :)

Link to comment
Share on other sites

  • 2 months later...

Hi Zimbofly,

 

Since I work in OOP (objecto oriented programming) I had to do a work around.

In the example below you have the function "load3D" of the class "Unit", so, when I do "var temp = this;", that variable will receive everything that concerns to the class or object it self, so I can use them in the "BABYLON.SceneLoader" method.

By this, I can to whatever I want, you see that I do in the last lines "temp.setMesh(ship);", with this I'm passing the mesh it self by argument to another method of the classe called setMesh. On the setMesh I do all the stuff that I want.

 

If it doesn't work out for you, please post the code, I'll give it a try on helping you out. Hope I've been helpfull.

unit.prototype.load3D = function(name,imageFileName,posx,posz,roty,meshid,type,octree){    var temp = this;    BABYLON.SceneLoader.ImportMesh(name, "Assets/babylonreadyfiles/", imageFileName, scenes[0], function(newMeshes) {        var ship = newMeshes[0].clone();                ship.scaling.x = constants.shipScale;        ship.scaling.y = constants.shipScale;        ship.scaling.z = constants.shipScale;                               ship.actionManager = new BABYLON.ActionManager(scenes[0]);                     if (selfchecker === 0) {        selfchecker = 1;        temp.setShipPos(posx,posz,roty);        //temp.loadParticlesEngine(newMeshes[0]);                        };         makeOverOut(ship,meshid,type);         octree.dynamicContent.push(ship);         octree.useOctreeForCollisions;         //newMeshes[0].checkCollisions = true;         temp.setMesh(ship);        });};
Link to comment
Share on other sites

Hi Kilombo - thanks so much for taking the time to help.
 
I'm just about grasping what you are saying with OOP - this is my code I've tried by copying your example...

 

var imageFileName = "Room3.babylon";
 
        unit.prototype.load3D = function(imageFileName){
        var temp = this;
        BABYLON.SceneLoader.ImportMesh("", "GalleryAssets/room/", imageFileName, scene, function (newMeshes) {
            var room = newMeshes[0].clone();
            room.scaling.x = 20;
            room.scaling.y = 20;
            room.scaling.z = 20;
            room.position = new BABYLON.Vector3(0, 50, 10);
 
            temp.setMesh(room);
 
            setup(newMeshes[0]);
            var bb1 = new BABYLON.Mesh.CreateBox("bb1", 1, scene);
            bb1.scaling = new BABYLON.Vector3(0.2, 17, 48);
            bb1.position = new BABYLON.Vector3(-7.7, 0, 20);
            bb1.visibility = 0;
            bb1.checkCollisions = true; 
        });
 
with this it tells me 'unit' is not defined - which i supose makes sense (I thought it might have been a babylon command) - now I'm not really sure how to progress?
Link to comment
Share on other sites

Here is my original code - that is bringing the mesh in, but not allowing me to change size or position...

 

var mesh = this;
        BABYLON.SceneLoader.ImportMesh("", "GalleryAssets/room/", "Room3.babylon", scene, function (newMeshes, particleSystems) {
            this.internalMesh = newMeshes[0];
            this.internalMesh.position.y = 20;
            this.internalMesh.scaling.x = 0.2;
            newMeshes[0].scaling.x = 0.2;
            newMeshes[0].scaling.y = 0.2;
            newMeshes[0].scaling.z = 0.2;
 
            //this.setMesh(newMeshes[0]);
 
            setup(newMeshes[0]);
            var bb1 = new BABYLON.Mesh.CreateBox("bb1", 1, scene);
            bb1.scaling = new BABYLON.Vector3(0.2, 17, 48);
            bb1.position = new BABYLON.Vector3(-7.7, 0, 20);
            bb1.visibility = 0;
            bb1.checkCollisions = true;
        });
 
i guess what you are saying is that variable 'mesh'  doesn't really contain anything, so this.internalMesh.scaling.x = 0.2; is unable to do anything?
Link to comment
Share on other sites

Don't use 'this.' in your callback function. 'this' is a keyword referring here to the callback function itself. You can learn more about this keyword here.

Try this : 

var internalMesh;        BABYLON.SceneLoader.ImportMesh("", "GalleryAssets/room/", "Room3.babylon", scene, function (newMeshes, particleSystems) {            // Your first imported mesh            internalMesh = newMeshes[0];            internalMesh.position.y = 20;            internalMesh.scaling.x = 0.2;            internalMesh.scaling.y = 0.2;            internalMesh.scaling.z = 0.2;                        setup(internalMesh);            var bb1 = new BABYLON.Mesh.CreateBox("bb1", 1, scene);            bb1.scaling = new BABYLON.Vector3(0.2, 17, 48);            bb1.position = new BABYLON.Vector3(-7.7, 0, 20);            bb1.visibility = 0;            bb1.checkCollisions = true;        });
Link to comment
Share on other sites

Hi temechon - many thanks!

 

your code is bringing in the mesh fine, but it's still not responding to any scaling or positioning...

 

var internalMesh;
        BABYLON.SceneLoader.ImportMesh("", "GalleryAssets/room/", "office_chair.babylon", scene, function (newMeshes, particleSystems) {
            // Your first imported mesh
            internalMesh = newMeshes[0];
 
            internalMesh.position.y = 20;
            internalMesh.scaling.x = 0.2;
            internalMesh.scaling.y = 0.2;
            internalMesh.scaling.z = 0.2;
 
            setup(internalMesh);
        });
 
function setup(mesh) {
            mesh.position.x = 10;
            mesh.position.y = 1;
            mesh.position.z = -10;
 
            mesh.scaling.x = 5;
            mesh.scaling.y = 5;
            mesh.scaling.z = 5;
        }

 

Any ideas?   help!!!

thanks

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