Using orthogonal projection

If the projection is changed from “Perspective” to “Orthogonal,” the current code would not work for picking the turret position. After some digging around I find that if the code is changed to:

	var mouse_position: Vector2 = get_viewport().get_mouse_position()

	ray_cast_3d.global_position = project_ray_origin(mouse_position)
	ray_cast_3d.target_position = ray_cast_3d.global_position + project_local_ray_normal(mouse_position) * 30000.0
	
	ray_cast_3d.force_raycast_update()

then it works. However there’s still an offset along the z axis for some reason. This is more apparent if the multiplier is changed to 1000. Am I going about this the right way? Is there a way to get rid of the offset for good?

P.S. If you keep the current camera settings, later on in the course you’ll find that the trees near the edge of the screen will look distorted. This can be remedied by lowering the FOV or using Keep Width instead of Keep Height.

1 Like

@OboShape Any ideas on this one?

Not something ive ever played with using an ortho camera in this way. I’m going to have a look at this a little more tonight and get back.
I had a quick look, might have to change the raycast from location since we’re no longer using the positions from eye position to the screen/near clip plane and then projecting.
More using the click position on screen as the origin and casting forward.

I’ll get back in a bit once I have a test.

okie doke, hope this helps if you want an ortho camera.

its probably not the prettiest and more than likely theres other ways of doing it.

so what i had to do was move the raycast3d node, as thats the origin of the cast.

image

but heres a snippit of the code that i tested with an ortho camera.

this is in the ray_picker_script

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:

	# so i got the 2d position of mouse on canvas
	var mouse_position_2d = get_viewport().get_mouse_position()
	# converted that 2d mouse position, into 3d global space, ith a z depth of 0
	var mouse_position_3d = project_position (mouse_position_2d, 0)
	# move the raycast object to that position
	ray_cast_3d.global_position = mouse_position_3d
	# then cast from where the raycast3d object is, then along the normal
	ray_cast_3d.target_position = project_local_ray_normal(mouse_position_2d)


	ray_cast_3d.force_raycast_update()

hope that helps to get going.

DaZ

1 Like

Wow, that works as intended! I saw that diagram on the godot tutorials page too. The godot docs say project_ray_origin is useful for object intersection or picking, so I did not realize you are supposed to use “project_position(mouse_position_2d, 0)” instead of “project_ray_origin(mouse_position_2d).” Your solution helps a lot, thank you!

Oh one more thing… For those who want to try using orthogoal view with OboShape’s solution above, you may also need a ray length multiplier like so:

ray_cast_3d.target_position = project_local_ray_normal(mouse_position_2d) * 100.0
1 Like

Aye, that seemed a good image to nick to show, couldn’t get the docs snippet to work tho :frowning:

That’s my bad, sorry, think I cleaned up the code a little too much and forgot to put back in the multiplier value when I removed my local variable.

Glad it worked for you tho :+1:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms