Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create collision mesh using accessors #6405

Closed
LeXXik opened this issue May 21, 2024 · 0 comments · Fixed by #6630
Closed

Cannot create collision mesh using accessors #6405

LeXXik opened this issue May 21, 2024 · 0 comments · Fixed by #6630

Comments

@LeXXik
Copy link
Contributor

LeXXik commented May 21, 2024

The engine fails to create the collision mesh, when component's property accessors are used. This works:

this.entity.addComponent('collision', {
    type: 'mesh',
    renderAsset: this.app.root.findByName('cube').render.asset
});

This doesn't:

this.entity.addComponent('collision')
this.entity.collision.type = 'mesh';
this.entity.collision.renderAsset = this.app.root.findByName('cube').render.asset;

Repro:
https://playcanvas.com/project/1218921/overview/collision-mesh-issue

The issue is because base collision system implementation creates an empty model before initialization:

beforeInitialize(component, data) {
data.shape = null;
data.model = new Model();
data.model.graph = new GraphNode();
}

Then when we try to assign a render asset, it first looks into model, which exists, but has no mesh instances and as a result fails to create a collision mesh:

createPhysicalShape(entity, data) {
if (typeof Ammo === 'undefined') return undefined;
if (data.model || data.render) {
const shape = new Ammo.btCompoundShape();
const entityTransform = entity.getWorldTransform();
const scale = entityTransform.getScale();
if (data.model) {
const meshInstances = data.model.meshInstances;
for (let i = 0; i < meshInstances.length; i++) {
this.createAmmoMesh(meshInstances[i].mesh, meshInstances[i].node, shape, null, data.checkVertexDuplicates);
}
const vec = new Ammo.btVector3(scale.x, scale.y, scale.z);
shape.setLocalScaling(vec);
Ammo.destroy(vec);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant