Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CanvasItem.draw_polyline draws incorrect, distorted geometry #16466

Closed
Chaosus opened this issue Feb 7, 2018 · 15 comments
Closed

CanvasItem.draw_polyline draws incorrect, distorted geometry #16466

Chaosus opened this issue Feb 7, 2018 · 15 comments

Comments

@Chaosus
Copy link
Member

Chaosus commented Feb 7, 2018

Godot version:
cbdd410

Issue description:
image

the problem is inside

Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5;

Minimal reproduction project:
PolylineBug.zip

@Chaosus Chaosus changed the title CanvasItem.draw_polyline draw incorrect, distorted geometry CanvasItem.draw_polyline draws incorrect, distorted geometry Feb 7, 2018
@Zylann
Copy link
Contributor

Zylann commented Feb 7, 2018

It doesn't look like a bug, when you think about it. You would need to expand those corners so that the segment has the same thickness, however it can lead to infinitely sized turns in some cases.

It looks like draw_polyline is not using the algorithm used by the Line2D node, which is able to render this kind of line better IMO

@ghost ghost added this to the 3.1 milestone Feb 7, 2018
@simonpuchert
Copy link
Contributor

This is a fundamental problem of the idea used in the function ("replace every point by two points").

If there is a very small angle at some point, the replacement points either extend far into outer space or the lines attached to this corner are very thin. However, you can use three points (one for the inner corner, two for the outer almost-180°-"bend") to reduce these problems to sane levels.

After that, there might be some adjustments to make, depending on whether you want to keep the directions (in which case the original vertices might get cut off) or the points (in which case you have to move the above construction "outwards", changing the direction of the lines).

It's similar to planning a racetrack using the points as guidance for the straight segments vs. fitting a racetrack through the given points (or, Bézier curves vs. splines; in the sense that Bézier curves need not hit the intermediate points).

@Chaosus
Copy link
Member Author

Chaosus commented Apr 12, 2018

Yeah other method for smooth lines should be used.

@Chaosus Chaosus closed this as completed Apr 12, 2018
@akien-mga akien-mga added archived and removed bug labels Apr 12, 2018
@Zylann
Copy link
Contributor

Zylann commented Apr 12, 2018

@Chaosus what do you mean? There is no fix planned? Should users just use Line2D to have better results?

@Chaosus
Copy link
Member Author

Chaosus commented Apr 12, 2018

Yeah, I suspect Line2D is better choise

@Zylann
Copy link
Contributor

Zylann commented Apr 12, 2018

But it's not if you want custom draw (note that the Line2D node actually uses a LineBuilder, which can also be used as implementation for draw_polyline, not sure why it wasn't done)

@Chaosus
Copy link
Member Author

Chaosus commented Apr 12, 2018

Zylann, got it, should I reopen this ?

@Chaosus Chaosus reopened this Apr 12, 2018
@Zylann
Copy link
Contributor

Zylann commented Apr 12, 2018

Yeah, although it's possible to use LineBuilder with some predefined params to get a better looking line, I wonder why it wasn't used in the first place (draw_polyline was added long after Line2D)

@akien-mga akien-mga added bug and removed archived labels Apr 12, 2018
@reduz
Copy link
Member

reduz commented Sep 6, 2018

May be improved in 3.2

@reduz reduz modified the milestones: 3.1, 3.2 Sep 6, 2018
@Spartan322
Copy link
Contributor

Spartan322 commented Dec 3, 2019

Redirected from #34073 being archived as a duplicate.

Godot version:
Mono 3.2 Beta 2

OS/device including version:
Manjaro Arch Linux "Juhraya" 18.1.2

Issue description:
draw_polyline draws the start and end points wider then the rest of the points.

This is using the draw_polyline_color function but it results in the same shape
image

Steps to reproduce:

  • Add a _draw override to a CanvasItem
  • Add draw_polyline([Vector2(), Vector2(0, 100), Vector2(100, 100), Vector2(100, 0), Vector2(-25, 0)], Color.white, 50, false)
  • Have the node draw either in editor or in game

Minimal reproduction project:
polyline_issue_project.zip

@Xrayez
Copy link
Contributor

Xrayez commented Nov 16, 2020

Wanted to open a bug report but seeing this, so attaching my own reproduction project.

GLES2, GLES3, Godot 3.2.4 beta.

draw_polyline vs draw_line, width 3.0

Screenshot 2020-11-16 112005

draw_polyline vs draw_line, width 20.0

Screenshot 2020-11-16 112049

So, currently draw_polyline is not useful to use in most cases for width > 1.0 (I'd expect it to work like draw_line at least), and I think draw_line is kind of expected to draw rectangles with width > 5.0, so indeed Line2D is superior out of them all, unfortunately I just cannot allow myself to use Line2D as a dependency of other similar class for line drawing.

Minimal reproduction project:

extends Node2D

export(float, 0.1, 20.0, 0.1) var width = 5.0 setget set_width

func set_width(p_width):
	width = p_width
	update()

var points = [Vector2(100, 100), Vector2(100, 200), Vector2(200, 200), Vector2(300, 100)]

func _draw():
	draw_polyline(points, Color.orange, width)

	draw_set_transform(Vector2(300, 0), 0, Vector2.ONE)

	for i in points.size() - 1:
		var p1 = points[i]
		var p2 = points[i + 1]
		draw_line(p1, p2, Color.cornflower, width)

draw_polyline_malformed.zip

Also, I have a little feature request: make draw_polyline (or similar) also draw closed lines, as needed by #34923.

@Calinou Calinou modified the milestones: 4.0, 4.x Feb 20, 2023
@Calinou
Copy link
Member

Calinou commented Feb 20, 2023

This is fixed as of 4.0.rc2, closing.

image

Updated MRP: draw_polyline_malformed_1.zip

@Calinou Calinou closed this as completed Feb 20, 2023
@Calinou Calinou modified the milestones: 4.x, 4.0 Feb 20, 2023
@A404M
Copy link

A404M commented Dec 27, 2023

The bug still exists
Screenshot

@ComputerErika
Copy link

ComputerErika commented Jan 3, 2024

Yea this bug definitely still exists, Im using Godot 4.2.1-1

What I was doing:
I was playing around with drawing lines, and did an old thing I did way back with Python turtle, where I draw lines with points at random heights from evenly spaced points along the x axis

Anyways heres the screenshot, its really noticeable to me
image

And also heres the code I was using for the screenshot (truncated to only the affecting code)

var window = Vector2i(1280, 720)
var points: PackedVector2Array = []

func _ready():
	for i in range(64+1):
		points.append(Vector2(i*20, randi_range(0,720)))

func _draw():
	if points.size() >= 2:
		draw_polyline(points, Color.BLUE, 3, true)

Edit: fixed code embed
Edit2: moved edit comments out of the code section aaaaaaa
Edit3: finally added code highlighting

@Calinou Calinou removed this from the 4.0 milestone Jan 3, 2024
@enetheru
Copy link
Contributor

I'm observing the same thing as @ComputerErika using 4.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests