#Trying to move a ball using normalised camera data from Python

1 messages · Page 1 of 1 (latest)

river seal
#
var deserialisedJson = JsonSerializer.Deserialize<UDPValue>(packet.GetStringFromUtf8());
            if (deserialisedJson != null && deserialisedJson.landmarks != null && deserialisedJson.landmarks.Count > 0)
            {
                //GD.Print($"Width: {deserialisedJson.width}, Height: {deserialisedJson.height}, Hand Info: {deserialisedJson.hand_info}");
                // Map coordinates for each landmark  

                for (int i = 0; i < Math.Min(deserialisedJson.landmarks.Count, _balls.Count); i++)
                {
                    var landmark = deserialisedJson.landmarks[i];

                    if (landmark.TryGetValue("x", out var xObj) &&
                        landmark.TryGetValue("y", out var yObj) &&
                        landmark.TryGetValue("z", out var zObj))
                    {
                        // Convert JsonElement to float
                        float x = Convert.ToSingle(xObj);
                        float y = Convert.ToSingle(yObj);
                        float z = Convert.ToSingle(zObj);
                        GD.Print($"Landmark {i}: x={x}, y={y}, z={z}");

                        float scaledX = x * deserialisedJson.width;
                        float scaledY = y * deserialisedJson.height;
                        GD.Print($"Scaled Coordinates for Ball {i}: x={scaledX}, y={scaledY}");
                        Godot.Vector3 newPosition = _camera.ProjectPosition(new Godot.Vector2(scaledX, scaledY), z);
                        GD.Print($"New Position for Ball {i}: {newPosition}");
                        _balls[i].Position = newPosition;
                    }
                }
            }```
#

what happens is that Sphere only changes its Z, weirdly.

#

@olive vessel

olive vessel
#

something like

get_world_pos : Vector2 ( pos : Vector3, video_size : Vector2):
  var result = pos * video_size //scale to pixel coords
var vps = GetViewport().GetVisibleRect().Size
  result *= Math.minimum(vps.x / video_size.x, vps.y / video_size.y) // fit to screen size
result +=  + GetViewport().GetVisibleRect().Size / 2 // offset so it's centered on the active viewport
return result

should work

olive vessel
#

can I see the code?

#

are we just getting the way the project position function works wrong?

olive vessel
#

side tangent but in strongly typed languages it's generally recommended to use the type instead of var when defining a variable

#

I don't get it
I did the same stuff as you and I get a working result

#

I literally just set the input position to what yours printed and it went to the middle of the screen as expected

extends MeshInstance3D

@export var input_position : Vector3

func _process(delta: float) -> void:
    position = get_viewport().get_camera_3d().project_position(Vector2(input_position.x,input_position.y), input_position.z)
#

I'm absolutely bamboozled

river seal
#

honestly i am bummed as well 😄

olive vessel
#

maybe it just doesn't work the same in c#?

river seal
#

i have no idea

#

i'll go have my lunch, thanks for your help!

#

I feel tired 😄

olive vessel
#

aight enjoy

#

I'll have a little dig around and let you know if I find anything

river seal
#

Currently I can move the ball with camera movement

olive vessel
#

eyy nice

river seal
olive vessel
river seal