Collision for animated objects

Hello everyone.
Is it possible to change the collision component along with the object’s animation?
I have a door object with animation and I have placed a car behind the door.
I have placed the camera in front of the door.
When I use raycast and the door is closed, I get raycast result as Door, but when the door is open and then I use raycast, I still get the raycast result as Door. Is there a way that the raycast hits the car when the door is open?
door door1

Thank you

Hi @maheshnaik,

So one trick that you could use is to add a collision entity as child to the animation bone node that is responsible for the rotation animation.

That way the collision entity will rotate together with animation. Though note if you are using rigid bodies, that will work with a kinematic body only.

@Leonidas

I didn’t get this.


Is it like this?

Project link: https://playcanvas.com/project/756060/overview/move-camera

Yes, you can attach entities to the bones of a skinned animation (search the forums on how to do that, there are a lot of posts), and those entities will rotate automatically. It can be tricky to get the right rotation if the bones aren’t set to move/rotate on a single axis, as I see here:

Otherwise, you can mimic the same behavior with the tween library, and try to rotate your collision entity in code, when the animation starts. To match the movement of the door. Check here on how to use the PlayCanvas tween lib:

https://developer.playcanvas.com/en/tutorials/tweening/

@Leonidas Thank you for your reply.
We have a different problem now. Actually the door is one of the mesh instances of the entire model and only door has the animation. So is it possible to add collision component to one of the mesh instances?
If possible, any link would be really helpfull.
Thank you.

No, that’s not possible, but you can create a separate entity with no model to act as collider.

2 Likes

Okay. Thank you for your suggestion.

For now, I did this way. It works!

@Leonidas
In the above project, you can see that when I imported the house model, I can’t access its hierarchy in the editor. Is there a way to access it programmatically?
I have imported the same model in https://threejs.org/editor/ and I can see the hierarchy and access each part. Is that possible in playcanvas?

Hi @maheshnaik,

You can access the different components of your model in code by diving into entity.model.meshInstances. It contains an array of the different mesh objects in the model.

Yes, that isn’t supported in editor right now. There is work being done currently by the PlayCanvas team to enable it.

@eproasim Thank you for your reply.
But I want to add a collision component to one part only(for example, only to the door of the entire house model).
Currently, I’m trying using this.entity.findByName() to find the graph node.

@Leonidas Is it possible to do it using the script.

var graphNode = this.entity.findByName('SM_TableChairs');
    
    this.entity.addComponent('collision', {
        type: 'mesh',
        asset: graphNode
    });

Currently, I’m trying the above method. But while raycasting, the result is still undefined.Is this the proper way or is there any other way to do it?
Thank you

Graphnodes can’t have components. You could create a new Entity with the collision and add it as a child of the graphNode bearing in mind that it will be affected by the transformation of the hierarchy.

Or alternatively, you can have a separate Entity with the collision and set the position and rotation of it to be the same as the graph node every frame.

There was a demo someone did that I can’t find where they attached physics object to a sword attack animation :thinking: @leonidas, do you know the one I’m thinking of? I think it was by a Japanese developer

Ah, found it! https://playcanvas.com/project/443589/overview/animation-with-collision

3 Likes

@yaustar Thank you. I will go through this project.

I’m not adding a component to graphNode instead I’m finding a graphNode and trying to add it as a collision component to the entity.

Like this. Is this possible?

Hi @maheshnaik, no that’s not possible. The collision component when set to mesh accepts a model asset, not an entity or graph node.

@Leonidas Thank you for your reply.
Now I have added a separate entity as a child to act as a collider and used tween to match the door’s animation as you suggested above. It seems to work fine now.

2 Likes