Is it possible to detect collisions directly from a GridMap object?
For instance, what if I have a block mesh with a StaticBody3dD and relative CollisionShape3D in my GridMap and a Projectile scene that has an Area3D and its CollisionShape3D.
I know I could use the Projectile signals to detect the collisions with the GridMap, but is it possible to do it using the GridMap in some way?
(this is to avoid to detect the GridMap on every scene that can interact with the block, in this example it is a Projectile, but there are more elements and doing it from the GridMap would be simpler)
Also, the body returned by the signal functions is the whole GridMap, so to change the block I’d have to calculate the coordinates every time, is ther a better way to do it?
If I understand it correctly, you have a gridmap with multiple block meshes that can interact with other objects in your game. You don’t want to implement the same collision code in each block, but rather keep it to the gridmap so you only need to consider the code in the gridmap.
You’re connecting the on_body_entered signal from the block to the gridmap, but when you want to respond to the signal, you lose track of the position of the collision? Is there a way you can .bind() the position of the block and send it to the gridmap and handle it there?
Yes sorry if I was not too clear in expressing the problem, the first part you are describing is correct.
This is what I am doing right now:
I have a Projectile scene with an area3D.
I connected the Projectile’s body_entered signal to a script.
When _on_body_entered() is called I check if the collision object is the grid_map, I check the coordinates of the Projectile and I update the relative block on the GridMap.
Now, let’s suppose there is even a LaserBeam scene that can interact with the block. I’d have to replicate the Projectile behaviour, connecting the signal, calculating the coordinates and so on.
It would be easier to detect the collisions directly from the GridMap’s blocks.
Let’s immagine the blocks are not parts of the GridMap, but they are a scene: Block.
I could use a signal from the Block scene to detect collisions with everything without the need to replicate the code on every scene that can interact with the Block.
Is it possible to replicate a similar beahaviour for the GridMap’s elements?
Then there is the extra question, right now to calculate which block to update on the GridMap I have to pass the projectile position at the time of impact, don’t know if there is a smarter way to do it, but this is not a big problem.
Hope it is clearer, sorry again for my bad english
From what I understand you want to turn the collision logic around, so that the block listens for incoming projectiles instead of the projectiles detecting when it hits something. In this case, you’d put an Area3d on the block scene and add a collisionshape to the projectile. You’d then have the block respond to the collisionshape entering it’s area. If you want the GridMap to do something too, you can forward the signal from the block to the gridmap
If you are concerned with re-implementing the collision behaviour for each projectile, you could make a new scene called projectile_attack or something. This is a very simple scene that implements the collision behaviour you have used in your bullet scene. It can have it’s own area3d or it can respond to the signal from the projectile. You would then add an instance of this scene as a child to each projectile, and now you would have the same behaviour across all projectiles that have this scene as a child.
You can also have one projectile scene and change the sprite to be a bullet or a laser depending on what you need. You would also need to change the speed and damage in each instance. I am using this strategy myself
Thanks! Yes I am starting to implement the first solution, I was coming to the same conclusion.
I’m going to instance an area3D on top of every block and use it to detect the collisions and update the GridMap.