Get index of collided instanced static mesh

Hello,

this question was often here and some claim to have found a solution, but they haven’t. If an object collided with an instanced static mesh component, the Hit Item in the Hit Result should give the index of the collided instanced static mesh. But it doesn’t, it’s always -1. What am I making wrong?

Thank you,

Thilo

Hey ThiloN1987,

This seems to work for me.

Thank you for your answer. I want to decide what happens with the instanced static mesh in the OnComponentHit-Event of the instanced static mesh component (e.g. a wall consisting of many bricks), not in the OnComponentHit-Event of the other colliding object (e.g. a gun projectile). As I found out, it works if the other component (gun projectile) is a non-physics-object. If it’s simulating physics, the OnComponentHit-Event of the gun projectile has the correct HitItem, but in the OnComponentHit-Event of the wall the HitItem is still -1.

Look at this

It gives me -1 if I am hitting something that is not part of the instance static mesh.

Which is correct, btw.

It’s bad engine design, I guess. If both colliding actors would return the right index, what if both actors are instanced static meshes? The indices would be different. There should be two indices for both colliding instanced static meshes.
Now I have the projectile let the wall tell which of it’s brick it hit, because the wall doesn’t know it.

The whole point of (hierarchical) instanced static meshes is to be extremely light and performant, they’re not supposed to simulate physics. Their transforms sit in the GPU cache and the CPU does the physics (to simplify it by an order of magnitude!). Update instances’ transforms (even via a batch mode) the benefits are mostly lost at this point.

You can move the component that holds onto the instances free of charge, though, so to speak.


When you collide with the instance, as demonstrated above, get its transform by the provided index, remove and replace it with a physics actor - feed it the transform, give it an impulse bump in the correct direction.

Making games is about cutting corners and faking stuff a lot of time, especially as things grow in scope.

It’s bad engine design

We can do this at 120fps:

Not too shabby.

This was exactly what I wanted to do. A wall consists of many bricks, so for it an instanced static mesh could be used. Then a gun projectile hit’s a specific brick and the brick breaks. Get the index of the instanced static mesh of the brick, remove it, replace it by a physics actor at the same transform, let it fall down. The problem is that my wall should decide what to do with a brick which was hit by a gun projectile. So the OnComponentHit-event of the wall respectively of the instanced static mesh component should know the index of the instanced static which was hit by the projectile, not the projectile. But it’s like I said, the projectile knows the correct index, but the wall receives index -1. This is only true if the projectile is simulating physics. If it’s using projectile movement component, both have the right index.

Ah, I understand your problem. I had a similar problem before but with skeletal meshes. But it seems that hitresult mainly gives you information about the other object that you hit.

The problem is that my wall should
decide what to do with a brick which
was hit by a gun projectile.

But it’s like I said, the projectile
knows the correct index

What exactly is stopping you from propagating the data from the projectile to the wall actor?

I can solve this by making an (additional) line trace through the wall:

But this is not efficient as the projectile already knows the index of the instanced static mesh and perhaps I will need an additional trace channel for this…

Seems not to be very safe to me. I could just let the projectile tell the wall that it has hit it and ignore the OnComponentHit of the instanced static mesh component, but probably the wall needs the information in the HitResult of it’s instanced static mesh, too. So I probably need both informations. So if the OnComponentHit of the wall is fired, wait for the information the projectile tells it… How to do this? Also, what if another collision with the same projectile occures before the wall receives the information from the projectile? And what if multiple projectiles are hitting the wall and sending the information? It just seems not to be safe to me, I would expect difficult to resolve bugs with it as they have to do with asynchronously.