[SOLVED] Adding mesh collider's model error (from web)

I am loading models from the web and now require a collision on it. However, when I try to set the model in the collision component, it gives me this error:

launch.js:7872 Error loading model: {myUrl} [TypeError: Cannot read property 'length' of undefined]

and Cannot read property 'resources' of undefined which points to the same function called by loadFromUrl (asset.resources[0];).

this.app.assets.loadFromUrl(url, "model", function (err, asset) {
    console.log(err, asset);
    var entity = new pc.Entity();
    entity.addComponent("model");
    entity.model.model = asset.resources[0];
    var meshInstances = entity.model.meshInstances;
    for (var i = 0; i < meshInstances.length; ++i) {
        var mesh = meshInstances[i];
        mesh.material = self.modelMaterial.resource;
    }
    
    //add collision
    var collider = entity.addComponent('collision', {
        type: 'mesh',
        model: entity.model.model
    });

    self.app.root.addChild(entity);
}

am I overlooking something here? I don’t see why it would call this function again when I’m setting the model to something that’s already loaded?

Hi @ivodidutch,

Not sure why it’s not working, it should work from a first look.

Could you post a sample project that reproduces the issue?

I can not… because it just works in a new project. I guess I’ll go debug a bit more then :stuck_out_tongue:

1 Like

I have figured it out. I had imported Ammo library, as i thought you always had to import it. I removed the folder and tada, it works. :expressionless:

1 Like

Alright, it is still an issue, sorry. So this error only occurs when you load something using an url. Here is a project that loads a cube.obj: https://playcanvas.com/project/672760/overview/collision-error Code is based off this raycast tutorial https://playcanvas.com/project/410547/overview/entity-picking-using-physics.
Simply toggle

var collider = entity.addComponent('collision', {
    type: 'mesh',
    model: entity.model.model
});

in raycast.js to see the error.

Here’s what I saw:

A:

       if(err) {
            console.log(err);
            return;
        }

B:

        var collider = entity.addComponent('collision', {
            type: 'mesh',
            asset: entity.model.model
        });

I’ve changed the property to ‘asset’ from ‘model’.
Don’t know if this helps too much

2 Likes

:man_facepalming: it was B. Thank you :heart:

Maybe the docs should be updated on that part, since they provide both properties (asset/model) on the collision component but only one is used when instantiating it.

1 Like