Plotting an infinite, random curve in Defold?

Hello,

I was wondering if it would be possible to generate a spline given some random set of nodes that will fit on the screen, which I could then continue to graph as a curve with the highlighted nodes on the screen. I am new to Defold and Lua, so I’d appreciate some ideas on how to integrate something like this in Defold. Thanks!

Well, start breaking down your problem:

  1. You need a way to show the control points on the screen. Use a factory to spawn game objects for this.
  2. You need to make the control points draggable. You can do this by adding a physics body to your control points and casting a ray in your on_input to check which point you grabbed.
  3. Then, based on the control points, you need to calculate the intermediate points defining your spline. Wikipedia math might help here.
  4. Finally you need to display the intermediate points somehow. Game objects spawned from a factory are your friend here as well. If you want to do line segments, you’ll also have to rotate and scale each line segment to connect with the next one. The math.atan2() function is your friend here for getting the correct rotation angle.
2 Likes