help to fix script please [collision problem]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By swipis
:warning: Old Version Published before Godot 3 was released.

Hello guys
Four days I trying to fix this problem but no luck so far…

So here is the problem: to understand what I talking about please check this video

  1. Blue wired box is collision shape
  2. white sphere represents where ray hits collision box

When I start game and ship not moving everything looks fine, where mouse button is pressed there appears white sphere but when ship starts moving white sphere appears not in same point where mouse is clicked, just look at the video at first looks fine, later looks like collision box getting smaller, probably miss calculations. in case here is my project

I know someone said something wrong with local and global positioning, but I cant figured out what or where is mistake. Here is raycast and rotation code:

func cast():
   var mouse_pos = get_viewport().get_mouse_pos()
   ray_from = cam.project_ray_origin(mouse_pos)
   ray_to = ray_from + cam.project_ray_normal(mouse_pos) * ray_length

   var directState = PhysicsServer.space_get_direct_state(cam.get_world().get_space())
   var collisions = directState.intersect_ray(ray_from, ray_to, [self])

  if collisions.size() and !collisions.empty() and collisions["collider"].get_name() == "nav":
	col_pos = collisions.position
	var t = get_node("ship").get_transform()
	col = col_pos - t.origin
	#print("collided with: ", collisions["collider"].get_name(), " distance ", ship.get_rotation().distance_to(col), " ___ ")
	print("something went wrong --> NO COLLISIONS DETECTED!")
func shipRotation(delta):
   var t = get_node("ship").get_transform()
   var rotA = get_node("ship").get_rotation()
   var lookDir = col - t.origin
   var rotTrans = t.looking_at(lookDir, Vector3(0,1,0))
   var thisRot = Quat(t.basis).slerpni(rotTrans.basis, currentRotSp * delta)
   if currentRotSp > global.shipRotSp:
	currentRotSp = global.shipRotSp
   get_node("ship").set_transform(Transform(thisRot, t.origin))
   var rotB = get_node("ship").get_rotation()

   if rotA == rotB:
	enableRot = false
	col = null

enter image description here
Please help me to fix this problem, because can’t do anything till this problem exist.
Thank you!

I think that you will increase a chance that somebody will help you if you make your code more readable with proper formating. Read How to use Q&A and how to write underscore

lukas | 2016-04-12 14:38

thanks for the tip :slight_smile:

swipis | 2016-04-12 14:54

Did this ever get fixed? The project link leads to a 404…

ericdl | 2016-06-10 03:46

:bust_in_silhouette: Reply From: Gokudomatic2

What you get in collisions array is a local position relative to the collider, the nav node, which is a child of camera. Since direct_state.intersect_ray doesn’t give a global position, I guess you have to add the nav.get_global_transform().origin to the collisions.position in order to have the global position of the click event.


col_pos = collisions.position + collisions["collider"].get_global_transform().origin

And since we cannot compare apples with oranges, nor local positions with global positions, the ship transform must be get like

var t = get_node("ship").get_global_transform()

And then the distance should be closer to what you expect.

First thank you! I tried what you wrote, but getting errors:

Object went too far away (more than 1000… mts from origin))

swipis | 2016-04-13 11:11