Isometric rectangle projection

boruokboruok Posts: 67Member
edited November 24 in 2D

Hi! anyone now how properly set transformed positions? I know how to draw rectangle but if i use transform to convert cartesian to isometric mouse position gain offset. Just copy paste code and see what it does. Mouse position are far away from top of rectangle.

extends Node2D


var start_position : Vector2
var rect : Rect2

var pressed := false


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton:
        if event.pressed:
            pressed = true
            start_position = get_global_mouse_position()
        else:
            # reset variables
            pressed = false
            start_position = Vector2(0, 0)
            rect = Rect2(0, 0, 0, 0)
            update()

    if event is InputEventMouseMotion:
        if pressed:
            var mouse := get_global_mouse_position()
            rect.position = Vector2(
                min(start_position.x, mouse.x),
                min(start_position.y, mouse.y))
            rect.size = Vector2(
                abs(start_position.x - mouse.x),
                abs(start_position.y - mouse.y))
            update()


func _draw() -> void:
    # transform projection to isometric
    draw_set_transform_matrix(Transform2D(Vector2(1, .5), Vector2(-1, .5), Vector2(0, 0)))
    draw_rect(rect, Color.red, false, 2)

Comments

  • boruokboruok Posts: 67Member

    let me answer to my question :)

    const ISO_TRANSFORM := Transform2D(Vector2(1, .5), Vector2(-1, .5), Vector2(0, 0))
    mouse_position = ISO_TRANSFORM.affine_inverse().xform(get_global_mouse_position())
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file