# Drawing a curved line

Discussion in 'Programming' started by Ido-f, May 29, 2019.

1. ### Ido-fMember

Joined:
Feb 19, 2018
Posts:
145
Let's say I want to draw a line from point A to point B, but instead of being a straight line I want it to follow some kind of a curve.
I want this line to be dynamic, both in the start & end points and in the curve parameters.

What is a good way of achieving this?

Edit: I remember seeing a unity streamer using a vertex buffer with tangent data for each vertex and somehow worked with that.
But even with that data on the vertices, how would I communicate the fragment shader what curve the fragment positions should be interpolated by, instead of producing the regular linear interpolation resulting in a straight line?

Last edited: May 29, 2019
2. ### VishnyaMember

Joined:
Jul 6, 2016
Posts:
65
You can use path to create curve line, or use splines

3. ### Ido-fMember

Joined:
Feb 19, 2018
Posts:
145
@Vishnya Do you have an informative link on what are splines?
I can't find anything in the manual or online, only on 'skeletal sprites' exported from other programs. Was that what you meant?

Also, If anyone know a more flexible solution where I'd write most of the code myself, maybe with a vertex buffer, I'd love to hear.

Joined:
Jun 24, 2016
Posts:
2,092
5. ### Ido-fMember

Joined:
Feb 19, 2018
Posts:
145
That's maybe a good source for maths, but I don't see anything in there about actually drawing it in Gamemaker. Am I missing something?

6. ### VishnyaMember

Joined:
Jul 6, 2016
Posts:
65
You should just find list of points and then draw lines using primitive or draw_line function.

7. ### Ido-fMember

Joined:
Feb 19, 2018
Posts:
145
That would give a bunch of straight lines, and require heavy cpu processing instead of fast graphics card calculations.

8. ### NeZversMember

Joined:
Mar 24, 2018
Posts:
320
Last edited: May 30, 2019
Ido-f likes this.
9. ### GMWolfaka fel666

Joined:
Jun 21, 2016
Posts:
3,518
Then just increase the number of lines untill it looks like a curve. It's fairly cheap.
That's how we do things.

Yeah, you *could" use a Bézier curve distance estimator and draw a curve line in a fragment shader using that, but that would most certainly be far more expensive, not to mention complicated.

10. ### Ido-fMember

Joined:
Feb 19, 2018
Posts:
145
@GMWolf Do you know if tessellation is possible in gamemaker?

As in, adding vertices in the vertex shader or adding vertices otherwise using the graphics card somehow?

11. ### GMWolfaka fel666

Joined:
Jun 21, 2016
Posts:
3,518
Nah you can't do that. Besides, if it's not gonna change there is not much point.
If you really want to reduce overhead just create a vertex buffer of your lines.
If you then want to move those points you could do it in the vertex shader. No CPU necessary at this point.

But really you should be fine doing it on the cpu, unless your entire game is made out of curved lines.

12. ### Ido-fMember

Joined:
Feb 19, 2018
Posts:
145
I guess it'd be easier with the line's width issue as well.
Oh well, I guess my graphics dreams can wait for later. Thanks for your input

13. ### GMWolfaka fel666

Joined:
Jun 21, 2016
Posts:
3,518
What is that dream? I'm sure it's feasible with what is available.
Sure it won't ever be as fast as a system using compute shaders and such. But we should be able to come up with a good solution.

14. ### ArmykMember

Joined:
Oct 31, 2018
Posts:
3
Hello! I would really want to create a bezier curve shader for game maker studio 2, but I dont know how to implement it.

15. ### Mikenobody importantGMC Elder

Joined:
Apr 12, 2016
Posts:
2,417
Why do you need to do a shader? Why not just generate a triangle strip? Or generate a path and render that? A shader for this is a little overkill isn't it?

16. ### ArmykMember

Joined:
Oct 31, 2018
Posts:
3
Hello again. How would you create a curved line and then draw it wide? You cannot draw path with width. An when you use a triangle strip, you can see a little pixels between the triangles

17. ### GMWolfaka fel666

Joined:
Jun 21, 2016
Posts:
3,518
Then you triangle strip isn't very... Good.
You want to make sure you "weld" the corners of your strips together.
One technique is the get the "normals" of each segment edge, add them together, normalize a d multiply by width. That gives you your welded vertex shared between both segments.

Code:
```/// Given points A, B, C, getting vertices V1 V2 at intersection B
Nab //normal of segment AB
Nbc //normal of segment BC
V1 = B + normalize( Na + Nb ) * Width.
V2 = B - normalize( Na + Nb ) * Width.
```

Joined:
Apr 12, 2016
Posts:
2,417