Add mesh collision on obj model

I am trying to load obj file from server by using OBJ Loader script and adding it on an entity which is working fine but now I want to add mesh collision on that entity which is not working I have tried several ways to do that but still failed to get it working,
Here is a dummy project link where I am loading that model from server and attaching it on entity.
https://playcanvas.com/editor/scene/1107463

Have you tried applying the model created to the model parameter on the collision component? https://developer.playcanvas.com/en/api/pc.CollisionComponent.html#model

var objData = new OBJ.Mesh(objStr);

var options = {};
if (objData.vertexNormals) options.normals = objData.vertexNormals;
if (objData.textures) options.uvs = objData.textures;
if (objData.indices) options.indices = objData.indices;
if (objData.vertices && objData.vertexNormals && objData.textures && objData.indices) {
    var tangents = pc.calculateTangents(objData.vertices, objData.vertexNormals, objData.textures, objData.indices);
    options.tangents = tangents;
}

var mesh = pc.createMesh(this.app.graphicsDevice, objData.vertices, options);
var node = new pc.GraphNode();

// var material = this.material ? this.material.resource : new pc.StandardMaterial();
var material = new pc.StandardMaterial();
var meshInstance = new pc.MeshInstance(node, mesh, material);
console.log(meshInstance.aabb.halfExtents);
var model = new pc.Model();
model.graph = node;
model.meshInstances = [ meshInstance ];

// model.name = “assetModel”;

var entity = new pc.Entity();
entity.addComponent('model');
entity.model.model = model;
entity.setLocalScale(0.01,0.01,0.01);
entity.setPosition(0,0.5,0);
this.app.root.addChild(entity);

entity.addComponent('collision', {
    type: "mesh",
    model: model
});

this is how I am trying to add collision on the object, and its not working, other types of collision are working fine like box etc on it but I want the mesh collision for the project

I’ve had a quick play at this here: https://playcanvas.com/editor/scene/1107468

It looks related to the scale and positioning when using a mesh collision as at default scale and position, it seems to be fine.

1 Like

so is there a way where I can scale, rotate or move the entity from default state without effecting the mesh collision ?

You should be able to change the initial scale and position of the collision mesh as that’s how it works in the Editor. I’m not 100% what is going wrong here when doing it in code and will need investigation.

If anyone else has ideas, I’m all ears

Found the issue and have a workaround. Updated project here: https://playcanvas.com/editor/scene/1107468

We have to add the model component after the collision otherwise the scale is applied to the collision mesh twice (ie 0.01 * 0.01)

This is a bug and I will log it in the morning.

The code is now:

    var entity = new pc.Entity();
    entity.name = 'My Box';
    this.app.root.addChild(entity);
    
    entity.setLocalScale(0.01,0.01,0.01);
    entity.addComponent('collision', {
        type: "mesh",
        model: model
    });
    
    entity.addComponent('rigidbody', {
        type: pc.BODYTYPE_KINEMATIC 
    });
    
    entity.setPosition(0,0.5,0);
    
    entity.addComponent('model');
    entity.model.model = model;
1 Like

Looked into this some more. It looks like it isn’t a bug and more that we are doing it wrong :sweat_smile:

When using the model resource directly, we should be cloning it (https://developer.playcanvas.com/en/api/pc.Model.html#clone) to give to the components.

ie:

    var entity = new pc.Entity();
    entity.name = 'My Box';
    this.app.root.addChild(entity);
    
    entity.addComponent('model');
    entity.model.model = model.clone();

    entity.setLocalScale(0.01,0.01,0.01);
    entity.addComponent('collision', {
        type: "mesh",
        model: model.clone()
    });
    
    entity.addComponent('rigidbody', {
        type: pc.BODYTYPE_KINEMATIC 
    });
    
    entity.setPosition(0,0.5,0);
2 Likes

Thank you that worked perfectly fine.
Now I would like to know if there is a way I can use editor’s gizmo for moving the objects in playmode

@will was looking into this but hasn’t got it to a releasable state yet.

Yes! I hope to make more progress on this over this weekend.

3 Likes