How to detect collisions with a specific mesh inside a GridMap using Raycast

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ObsidianWhisper

I’m working on a project similar to XCom: UFO Defense. My current problem is that I can’t get the specific mesh the mouse selects when using;

func _physics_process(delta):
#get current physics state
var space_state = get_world().direct_space_state

#get current mouse position in the viewport
var mouse_position = get_viewport().get_mouse_position()

#set the ray origin
RayOrigin = project_ray_origin(mouse_position)

#set the ray end point
RayEnd = RayOrigin + project_ray_normal(mouse_position) * 2000

#get the ray hit
var intersection = space_state.intersect_ray(RayOrigin, RayEnd)

if not intersection.empty(): #if there is a proper ray hit get it's position
	mouse_position_3d = intersection.position

What returns is the GridMap object. I know I could use the coordinates provided to guess the cell, however my tree is split into separate GridMaps.

TacticalMap

  • CharacterGrid
    • TestChar
  • WallMap
  • GroundMap
  • PasThroughMap
  • HopMap
  • ObstacleMap
  • Camera
    • RayCast

The Ray is also only returning the GroundMap, even when it should be intersecting meshes other than a ground mesh first. I.E. Trees and walls.

My intention is to prevent the player from clicking on a wall or obstacle and causing the character to move. The other important thing is that when a character shoots, I want to detect the intersecting mesh properly so it can potentially destroy obstacles. At the moment I can’t even get the proper GridMap detected.

All currently used mesh instances have static bodies with collision shapes.
I’ve noticed the ray will detect the GroundMap even when clicking on on a cell that doesn’t have a mesh in it.

Is the Y position of where you’re casting from what you think it should be?

I haven’t worked much in 3D but a naive approach would be to cast multiple rays with different collisions layers then determine your target based on the smallest distance of those rays.

timothybrentwood | 2021-05-04 21:16

:bust_in_silhouette: Reply From: Wakatta

Simply put what you’re trying todo is not possible and if it was would be extremely resource heavy.

The correct approach would be to get the world_to_map() coordinate of your collision then get the related cell using GridMap’s get_cell_item() then compare that using GridMap’s mesh_library.get_item_name()

Unfortunate.

I took your advice and created a cumbersome check system I still haven’t tested out (not at that phase yet). The problem was that the walls the ray would collide with were placed between cells. Meaning the world_to_map() cell might be empty or the wrong wall.

I don’t know enough about 3d programming, but I’m surprised the individual meshs don’t have ids when used on the gridmap. If I simply used the gridmap as a point reference and instanced all of the meshes, I’d at least get unique object ids. But I suppose it would be far more resource intensive than using the gridmap.

Thank you for your help.

ObsidianWhisper | 2021-05-26 19:48