Hello! I need to convert Vector2 to Vector3. Please, i need this so much. Thanks!

  • I think your best bet is to use this function called segment_intersects_sphere

    https://docs.godotengine.org/en/stable/classes/class_geometry.html#class-geometry-method-segment-intersects-sphere

    First you need 2 Vector3 3D positions for the line start and end. You have the mouse positions in screen space, but you want the world positions in the scene. You would use the function project_position with the depth (z) value is the distance to the plane your objects are on. In most cases this would be object.translation.z - camera.translation.z. If the camera or the object plane never move, then this can be a constant float value that you set on ready.

    https://docs.godotengine.org/en/stable/classes/class_camera.html#class-camera-method-project-position

    From there you call it twice, with each of the mouse positions (start and end) which give you the start and end points in 3D world space. From there you can use the segment intersects sphere function I mentioned above. You use the two 3D points to define the segment. Then you have a for loop which checks each of the fruit objects with a rough sphere to approximate their hit box. This should give you at least the first point of intersection, or maybe the exit point as well, you'll have to check what's in the array. You can also use segment_intersects_convex with a more complex collision shape, but this may be more complicated than needed, as the slice will be fast and does not need to be super accurate. You also know the angle of the slice (because you have the mouse points) se even just knowing the object is touching is enough to do a convincing slice. I hope that makes sense.

Converting a vector2 into a vector3 is easy.
A vector2 has two variables. x and y.
A vector3 has three, x, y and z.
you need.
vect3.x=vect2.x
vect3.y=vect2.y
vect3.z=0

The zeda gives depth.

    object translation is not accurate

    Please provide more details.

      The Vector2 should be cordinates. But thinking about it. I know y is up and down. Maybe you should use,
      vect3.z=vect2.x
      vect3.x=0
      vect3.y=vect2.y

      How far off is the location of the vector3 after convert?

        I think I have read, that the camera renders 2d with depth of 1.0 or -1.0, try those for z.
        Something to take note of, is 2d is orthographic, while 3d is perspective.

        It depends what they are doing, which they haven't explained. My assumption is that they are trying to project 3D positions into screen positions, but they haven't really stated it well so I can't post the specific code.

          That still doesn't explain anything. 2D and 3D are totally different spaces. You can project back and forth, but you can't convert directly from 2D to 3D or 3D to 2D without some context into the game or what you are doing. So I still don't understand.

            cybereality Ok, its again about fruit ninja. I noticed that start slice and end position isn't that accurate. that's why my Raycast3D was weird. To step forward i need to get exactly true 3D position of the 2D position.

            Okay, that makes sense. What you probably want is the slice to be a 3D plane (like a flat surface that is aligned with the camera so it looks like a line). Then you do your collision based on the plane and maybe simple sphere shapes for the fruit. This will give you a rough start and end of the slice, and then you can use closest point on the object to get that actual collision.

              So I understand that the 3D point you want is the collision point of the fruit. But why do you have a 2D point? Is that because the collision is 2D (like in screen space)? If that is the case, you'd have to project that onto the fruit, like with a ray cast. Or, if the fruit are on a flat plane (like all at Z = -10.0 or something) then use that for the Z. If they are at different Z values, but you know which specific object it is, then you can use the Z value of the object (which will be the center). It may not be the exact point, but it should be close enough for a slice and not noticeable to the user. But you have to be more specific about the problem you are having cause I'm just guessing here.

                cybereality I have 2D positions because, its the mouse positions (start and end). And yes, my fruits on one Z. And i need 3D position because originals are in 2D, and because of this i can't step forward in my game developement.

                I think your best bet is to use this function called segment_intersects_sphere

                https://docs.godotengine.org/en/stable/classes/class_geometry.html#class-geometry-method-segment-intersects-sphere

                First you need 2 Vector3 3D positions for the line start and end. You have the mouse positions in screen space, but you want the world positions in the scene. You would use the function project_position with the depth (z) value is the distance to the plane your objects are on. In most cases this would be object.translation.z - camera.translation.z. If the camera or the object plane never move, then this can be a constant float value that you set on ready.

                https://docs.godotengine.org/en/stable/classes/class_camera.html#class-camera-method-project-position

                From there you call it twice, with each of the mouse positions (start and end) which give you the start and end points in 3D world space. From there you can use the segment intersects sphere function I mentioned above. You use the two 3D points to define the segment. Then you have a for loop which checks each of the fruit objects with a rough sphere to approximate their hit box. This should give you at least the first point of intersection, or maybe the exit point as well, you'll have to check what's in the array. You can also use segment_intersects_convex with a more complex collision shape, but this may be more complicated than needed, as the slice will be fast and does not need to be super accurate. You also know the angle of the slice (because you have the mouse points) se even just knowing the object is touching is enough to do a convincing slice. I hope that makes sense.

                  Hramzhuk I think, its because Vector3 is in units and Vector2 is in pixels.

                  Yes and no, technically both vectors hold scalar values(floating point values such as 1.0, 2.5 etc.) but as you say the 2D situation is better at hiding the real source of problem here: floating point error.