Problem with mouse should press and move cube inside of viewport like Blender or 3D Max

SourceSkyBoxerSourceSkyBoxer Posts: 11Member
edited October 19 in Programming

Hello everyone,

I am new for Godot Engine. Thank you for welcome! :) I am happy because I find easy Engine like our .

But I have problem with hard understanding code like mouse button_left should be pressed and be moved with world ( Spatial )
Because I am really excited to use Godot Engine for more new add-ons.

My Code: is MainApp.gd

extends Control

var viewport = null
var viewport_sprite = null
var world = null

func _ready():
viewport = get_node("Viewport")
viewport_sprite = get_node("ViewportSprite")
world = get_node("Viewport/World")
viewport.set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
viewport_sprite.texture = viewport.get_texture()

func _input(event):
if (event.type==InputEvent.MOUSE_BUTTON and event.pressed):
var local_pos = Vector2(10, 10) # local to Control/Node2D
event.position = get_viewport_transform() * (world.get_global_transform() * local_pos)
get_tree().input_event(event)

My root scene looks like:

Sorry my bad English. I hope you understand that.

// EDIT:
I have tried tried ....

extends Control
var viewport = null
var viewport_sprite = null
var world = null
func _ready():
viewport = get_node("Viewport")
viewport_sprite = get_node("ViewportSprite")
world = get_node("Viewport/World")
viewport.set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)

yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")

viewport_sprite.texture = viewport.get_texture()

func _input(event):
if Input.is_mouse_button_pressed(BUTTON_LEFT):
var pos = viewport.get_viewport().get_mouse_position()
world.translate_object_local (Vector3(pos.x, pos.y, 0))

No success :/

How do I get working pressing mousebutton with whole spatial moving...

Did you see red arrows like I said move with cube and sky if you rotate like any 3D programs with rotate, zoom and transform??

Best regards

Comments

  • SourceSkyBoxerSourceSkyBoxer Posts: 11Member

    No answers!? Sorry I wait longer.... I have problem with ViewportContainer and ViewportSprite I already tried and I am broken - why does documentation not help me - if who know like so close to gizmo but I want Mouse Button if Left is rotate with mesh with world and right means move/translate and middle button or wheel means zoom in / out.

    I have tried and I have copy from Godot TextureMaker's Preview and it seems not work for me. I have copied to my test project.

    I want know how does it work?
    * Move / Translate with Mouse Button Right
    * Rotate with Mouse Button Left
    * Zoom with Mouse Middle Button or Wheel
    I have tried

    func _input(event)

    With if else statements
    And It seems not work? Is it buggy or what do I miss script?

    Is it possible for Godot 3.0 / 3.1 when it works fine

  • TwistedTwiglegTwistedTwigleg Posts: 858Admin

    I have not tried using Viewports with ViewportContainer/ViewportSprite nodes, so I may cannot say for sure on what is causing the problem, but here is what I would suggest changing your code to:

    extends Control
    var viewport = null
    var viewport_sprite = null
    var world = null
    
    # Modes: 1 = translate, 2 = rotate and zoom
    var mode = null
    const CAMERA_MOVE_SPEED = 5;
    const CAMERA_ROTATE_SPEED = 2;
    const CAMERA_ZOOM_SPEED = 2;
    
    func _ready():
        viewport = get_node("Viewport")
        viewport_sprite = get_node("ViewportSprite")
        world = get_node("Viewport/World")
        viewport.set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
    
        yield(get_tree(), "idle_frame")
        yield(get_tree(), "idle_frame")
    
        viewport_sprite.texture = viewport.get_texture()
    
    func _input(event):
        if event is InputEventMouseButton:
            if event.scancode == BUTTON_LEFT and event.pressed == true:
                mode = 1
            elif event.scancode == BUTTON_RIGHT and event.pressed == true:
                mode = 2
            else:
                mode = null
        elif event is InputEventMouseMotion:
            var camera = world.get_node(“Camera”)
            if (mode == 1):
                camera.translate(Vector3(event.relative.x, event.relative.y, 0) *  CAMERA_MOVE_SPEED);
            elif (mode == 2):
                camera.rotate_y(event.relative.x * CAMERA_ROTATE_SPEED);
                camera.translate(Vector3(0, 0, event.relative.y * CAMERA_ZOOM_SPEED))
    

    Hopefully that will have the desired effect. I have not tried the code so it may not work, but at least that may give you an idea on how to handle what you are trying to achieve. The only thing I am struggling to remember is how the translate function works with rotated nodes.

    I would highly suggest looking through the Input Events section of the documentation, the using 3D transforms section, and/or taking a look at the demos from the Godot demo repository for examples on how to use Viewports and InputEvents.

    Hopefully this helps :smile:


    @SourceSkyBoxer said:
    No answers!? Sorry I wait longer....

    It might be because everyone is busy right now, and/or no one knows how to help provide an answer right now. Activity on the forums comes and goes, so sometimes it just requires being patient :smile:

    If you have not already, I would maybe post your question on the Godot QA and/or the Godot Facebook/Reddit/Discord, as you may be able to get some answers there too.

  • SourceSkyBoxerSourceSkyBoxer Posts: 11Member
    edited October 20

    @TwistedTwigleg thanks for answer me! But I have got error since run into Godot Engine Game. And It throws error:

    PS Why do you use scancode? But I am using in Godot 3.1 Alpha

    I have tried replace this:

        if Input.is_mouse_button_pressed(BUTTON_LEFT) and event.pressed == true:
            mode = 1
        elif Input.is_mouse_button_pressed(BUTTON_RIGHT) and event.pressed == true:
            mode = 2
        else:
            mode = null
    

    And It looks like hiding mesh cube :( I want "press and move" or "press and rotate" or "press zoom" like Substance Designer's previewer

    You're right but - I AM SORRY - I am never supoorting for Facebook, Reddit and Discord because It is bad for me. Sorry I don't want sign up to Facebook, Reddit and Discord because they are very dangerous to me. But Facebook hurts me and Reddit forces me if I am new and I wait longer than 4 months they are bad to me. And Discord made by Unity 3D Team has thrown me from Discord. PLEASE LEAVE BAD CONNECTION! - I wish you have good connection with Twitter, Google+ because I am very dangerous to 3 bad connections. Sorry for bad ways with dangerous websites.

    Please respect me! I hate Facebook, Discord and Reddit. I never support for dangerous websites. Sorry for shocking! I want tell you because I am holding and ignoring to dangerous websites. Please make sure to connect to Google+ and Twitter!

    PS: Please do not share my threads / posts to Facebook, Reddit or Discord! Thank you!

    // EDIT - It works fine but I need release if Mouse Button doesn't press than motions should stay or be busy.

    I have resolved:

    extends Spatial

    var mode = null
    const CAMERA_MOVE_SPEED = 0.01;
    const CAMERA_ROTATE_SPEED = 0.01;
    const CAMERA_ZOOM_SPEED = 0.01;

    Called when the node enters the scene tree for the first time.

    func _ready():
    pass # Replace with function body.

    func _input(event):
    if event is InputEventMouseButton:
    if event.button_index == BUTTON_LEFT and event.pressed:
    mode = 1
    elif event.button_index == BUTTON_RIGHT and event.pressed:
    mode = 2
    elif event.button_index == BUTTON_MIDDLE and event.pressed:
    mode = 3
    elif event is InputEventMouseMotion:
    var camera = get_viewport().get_node("Camera")
    if (mode == 1):
    camera.translate(Vector3(event.relative.x, event.relative.y, 0) * CAMERA_MOVE_SPEED);
    elif (mode == 2):
    camera.rotate_y(event.relative.x * CAMERA_ROTATE_SPEED);
    elif (mode == 3):
    camera.translate(Vector3(0, 0, event.relative.y * CAMERA_ZOOM_SPEED))

    But rotate of Camera seems wrong. It just middle point of spatial should rotate like Substance Designer - If you have used Substance Designer and you have tried to play Preview of 3D view example cube you move, rotate and zoom if you press mouse button and if you release ( unpress ) moving, rotation or zooming will stop....

    For rotate I have resolved yeah it is from Spatial's rotate because It looks like 3D view from Substance Designer :smiley:

    Wow I am happy because I have resolved it.

    But rotate_x and rotate_y looks weird. I show you like that? Example from Substance Designer

  • TwistedTwiglegTwistedTwigleg Posts: 858Admin

    @SourceSkyBoxer said:
    @TwistedTwigleg thanks for answer me! But I have got error since run into Godot Engine Game. And It throws error:

    PS Why do you use scancode? But I am using in Godot 3.1 Alpha

    I have tried replace this:

        if Input.is_mouse_button_pressed(BUTTON_LEFT) and event.pressed == true:
            mode = 1
        elif Input.is_mouse_button_pressed(BUTTON_RIGHT) and event.pressed == true:
            mode = 2
        else:
            mode = null
    

    And It looks like hiding mesh cube :( I want "press and move" or "press and rotate" or "press zoom" like Substance Designer's previewer

    The reason I was using scancode is because I was writing the code from memory, and I couldn't quite remember :lol:
    As for moving the mesh around like in substance painter, I would need to watch some videos or something to remember how the setup works.

    Right now I am super busy with making a game for ProcJam, but once the jam is over I'll try to see if I can help (if I do not forget. I'll try to remember though).

    You're right but - I AM SORRY - I am never supoorting for Facebook, Reddit and Discord because It is bad for me. Sorry I don't want sign up to Facebook, Reddit and Discord because they are very dangerous to me. But Facebook hurts me and Reddit forces me if I am new and I wait longer than 4 months they are bad to me. And Discord made by Unity 3D Team has thrown me from Discord. PLEASE LEAVE BAD CONNECTION! - I wish you have good connection with Twitter, Google+ because I am very dangerous to 3 bad connections. Sorry for bad ways with dangerous websites.

    Please respect me! I hate Facebook, Discord and Reddit. I never support for dangerous websites. Sorry for shocking! I want tell you because I am holding and ignoring to dangerous websites. Please make sure to connect to Google+ and Twitter!

    PS: Please do not share my threads / posts to Facebook, Reddit or Discord! Thank you!

    No problem! I was not trying to pressure you or anything. I was only making a suggestion because there are some other users there who may be able to help. That said, there is nothing wrong with not posting there and I was by no means trying to persuade you to post there. If you do not want to post on other social media sites, that is 100% fine with me.

    (As for sharing threads/posts, I have no plans to do so)

    // EDIT - It works fine but I need release if Mouse Button doesn't press than motions should stay or be busy.

    I have resolved:

    extends Spatial

    var mode = null
    const CAMERA_MOVE_SPEED = 0.01;
    const CAMERA_ROTATE_SPEED = 0.01;
    const CAMERA_ZOOM_SPEED = 0.01;

    Called when the node enters the scene tree for the first time.

    func _ready():
    pass # Replace with function body.

    func _input(event):
    if event is InputEventMouseButton:
    if event.button_index == BUTTON_LEFT and event.pressed:
    mode = 1
    elif event.button_index == BUTTON_RIGHT and event.pressed:
    mode = 2
    elif event.button_index == BUTTON_MIDDLE and event.pressed:
    mode = 3
    elif event is InputEventMouseMotion:
    var camera = get_viewport().get_node("Camera")
    if (mode == 1):
    camera.translate(Vector3(event.relative.x, event.relative.y, 0) * CAMERA_MOVE_SPEED);
    elif (mode == 2):
    camera.rotate_y(event.relative.x * CAMERA_ROTATE_SPEED);
    elif (mode == 3):
    camera.translate(Vector3(0, 0, event.relative.y * CAMERA_ZOOM_SPEED))

    But rotate of Camera seems wrong. It just middle point of spatial should rotate like Substance Designer - If you have used Substance Designer and you have tried to play Preview of 3D view example cube you move, rotate and zoom if you press mouse button and if you release ( unpress ) moving, rotation or zooming will stop....

    For rotate I have resolved yeah it is from Spatial's rotate because It looks like 3D view from Substance Designer :smiley:

    Wow I am happy because I have resolved it.

    But rotate_x and rotate_y looks weird. I show you like that? Example from Substance Designer

    I'm not sure why rotate_x and rotate_y looks weird. Once ProcJam is over, I'll take a look and see if I can recreate something similar.

    As for the release, after this line of code:

    elif event.button_index == BUTTON_MIDDLE and event.pressed:
        mode = 3
    

    Add this:

    elif event.pressed == false:
        mode = null;
    

    And then when you release the mouse, it should stop moving/rotating/scaling. :smile:

  • SourceSkyBoxerSourceSkyBoxer Posts: 11Member

    Thanks for answer but you are quickly. I want show that - I am sorry - I am very busy from yesterday since problem with obs-studio under Ubuntu 18.04 - Now obs-studio works fine because it seems like crazy error message "Failed to initialize GPU..."

    Now I would like to show you about rotate problem example with Substance Designer
    Check video

    Thanks!

  • TwistedTwiglegTwistedTwigleg Posts: 858Admin

    @SourceSkyBoxer said:
    Thanks for answer but you are quickly. I want show that - I am sorry - I am very busy from yesterday since problem with obs-studio under Ubuntu 18.04 - Now obs-studio works fine because it seems like crazy error message "Failed to initialize GPU..."

    Now I would like to show you about rotate problem example with Substance Designer
    Check video

    Thanks!

    Cool. I'll take a look once I have some more spare time, which should be sometime after ProcJam ends.
    Thanks :smile:

  • TwistedTwiglegTwistedTwigleg Posts: 858Admin

    Hey @SourceSkyBoxer, I didn't forget about this post! Now that ProcJam is over, I had some spare time to spend and so I made an example project that should do what you are looking for.

    The example project that does everything shown in the video. The only thing that is slightly different in the example project is how rotation works, but I couldn't find a way to quite replicate the exact functionality shown in the video.

    I have linked the project below. Feel free to use it how you see fit :smile:

  • SourceSkyBoxerSourceSkyBoxer Posts: 11Member

    Hello my best, I am sorry for late because I open and help my German Godot Community :) Because I speak German :)

    Thanks for download But I understand now. Great job! Thanks!

  • TwistedTwiglegTwistedTwigleg Posts: 858Admin

    @SourceSkyBoxer said:
    Hello my best, I am sorry for late because I open and help my German Godot Community :) Because I speak German :)

    Thanks for download But I understand now. Great job! Thanks!

    No worries, and best of luck with your German Godot community :smile:

  • SourceSkyBoxerSourceSkyBoxer Posts: 11Member

    Oh thanks for understanding :)

Sign In or Register to comment.