+1 vote

I have a camera inside a sphere from which I cast a raycast to get the intersection point. I figured out that raycasting somehow only works if the origin point and the cast-to point are outside of the collision shape. Is there any way to cast it from inside a collision shape and still get the collision point?

Any help is appreciated!

in Engine by (13 points)

1 Answer

+1 vote

Try to set your CollisionObject's (the one that you want to ignore) collision layer different from the RayCast's collision masks.

by (148 points)

Thanks for your answer! I just realized that my question is probably not so clear, as I don't want to ignore the CollisionObject.

Here's my setup:
I have a camera at the point 0,0,0 which is moveable by the mouse. It's surrounded by a textured sphere (I am trying to copy the controls of games like Myst, where you have 360° pre rendered background and move through clicking "hotspots" on these backgrounds). I have one sphere where it's really important to get the exact point on the sphere where it is clicked, so I need to cast a raycast from inside the sphere (where the camera is) and I want to get the point where it hits the shell of the sphere (which is a mesh with a static object as a child which has a spheric collision shape). This doesn't work as the raycast from inside the sphere somehow ignores it. Sorry if it's still a little confusing, English is not my first language.

I put together a quick example, which might could help you.
Here you can see my scene hierarchy:

enter image description here

And I have the following script on my Camera:

extends Camera

var ray = RayCast.new()
func _ready():
    add_child(ray)
    ray.enabled = true

func _input(event):
    var distance_from_camera = far 
    if event is InputEventMouseButton:
            var from = project_ray_origin(event.position);
            var to = from + project_ray_normal(event.position) * distance_from_camera;
            ray.cast_to = to-global_transform.origin
            if (event is InputEventMouseButton):
                if (event.pressed == false and event.button_index == 1):
                    if ray.is_colliding():
                        var raypoint = ray.get_collision_point()
                        return print("raypoint:", raypoint)

Is this similar to your use case?

Thanks again! It's almost similar, but in my case the camera is not a child of the collision shape but is in another node that is higher on the hierarchy. I tried your code on my camera and again, got no results. Is it crucial that the camera is a child of the collision shape?

Ah and is your camera also "placed" on the inside of the collision shape? In my case, the camera has to be surrounded by the collision sphere and I think that is the problem. If I have any collision shapes that are in distance to the camera, it works with no problems.

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.