Forum

[SOLVED] Problem with loading the object and translating it

14 February 2016 10:04 #7416
Hi everyone!

I was looking at the code of the dynamic loading example and tried to replicate the same behaviour. The problem I have is that the object would indeed load, but it will not translate to the position of the spawner. It always spawns in the middle of the scene…

This is the code of the app:
"use strict"

// register the application module
b4w.register("project_alpha", function(exports, require) {

// import modules used by the app
var m_app  = require("app");
var m_cfg  = require("config");
var m_data = require("data");
var m_ver  = require("version");
var m_scenes = require("scenes");
var m_phy    = require("physics");
var m_util = require("util");
var m_ctl   = require("controls");
var m_obj    = require("objects");
var m_trans  = require("transform");


// detect application mode
var DEBUG = (m_ver.type() === "DEBUG");

// automatically detect assets path
var APP_ASSETS_PATH = m_cfg.get_std_assets_path() + "project_alpha/";

var spawner_pos = new Float32Array(3);

/**
 * export the method to initialize the app (called at the bottom of this file)
 */
exports.init = function() {
    m_app.init({
        canvas_container_id: "main_canvas_container",
        callback: init_cb,
        show_fps: true,
        console_verbose: DEBUG,
        autoresize: false
    });
}

/**
 * callback executed when the app is initialized 
 */
function init_cb(canvas_elem, success) {

    if (!success) {
        console.log("b4w init failure");
        return;
    }

    load();
}

/**
 * load the scene data
 */
function load() {
    m_data.load(APP_ASSETS_PATH + "project_alpha.json", load_cb);
}

/**
 * callback executed when the scene is loaded
 */
function load_cb(data_id, success) {

    if (!success) {
        console.log("b4w load failure");
        return;
    }

    m_app.enable_controls();

    var spawner = m_scenes.get_object_by_name("spawner");
    m_trans.get_translation(spawner, spawner_pos);

    m_app.enable_camera_controls();
    m_data.load("monkey.json", loaded_cb, null, true, false);
    // place your code here

}

function loaded_cb(data_id) {

    var objs = m_scenes.get_all_objects("ALL", data_id);

    for (var i = 0; i < objs.length; i++) {
        var obj = objs[i];
		//console.log(obj);
        if (m_phy.has_physics(obj)) {
            m_phy.enable_simulation(obj);

            var obj_parent = m_obj.get_parent(obj);
            if (obj_parent && m_obj.is_armature(obj_parent)){
                // translate the parent (armature) of the animated object
                m_trans.set_translation_v(obj_parent, spawner_pos);
            }
            else{
                m_trans.set_translation_v(obj, spawner_pos);
            }
        }
        // show appended object
        if (m_obj.is_mesh(obj))
            m_scenes.show_object(obj);
    }
}


});

// import the app module and start the app by calling the init method
b4w.require("project_alpha").init();


The monkey.blend is a simple .blend file with Suzanne set to a rigid body. The main scene is a simple scene with an empty called 'spawner', a static plane and a rigid body cube.

No matter what I try, I cannot get the monkey to spawn at the 'spawner'

Thank you for your help and your time guys!
14 February 2016 17:21 #7419
Oh, thank god, I figured out what was wrong! In order to set the object translation you have to disable it's physics simulation first, then set the translation and then enable the physics again.

Thanks anyway, guys!
14 February 2016 17:27 #7420
Hi!

Glad you've resolved it. BTW welcome to our forum
Blend4Web Team
https://twitter.com/AlexKowel
14 February 2016 22:23 #7421
Thank you! You have a great thing going on here, keep up with the great work!
 
Please register or log in to leave a reply.