0 votes

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.

- 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.

Godot version 3.2.2 Stable
in Engine by (35 points)

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.

1 Answer

+1 vote

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()

by (4,982 points)


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 worldtomap() 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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.