0 votes

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!

in Engine by (110 points)
edited by

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

thanks for the tip :)

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

1 Answer

0 votes

What you get in collisions array is a local position relative to the collider, the nav node, which is a child of camera. Since directstate.intersectray doesn't give a global position, I guess you have to add the nav.getglobaltransform().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.

by (701 points)

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

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

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.