I'm trying to generate a planet's shape from code.
I'm using Sprite for the image and CollisionPolygon2D for the collision.
But the Sprite and the CollisionPolygon2D don't exactly line up.
Shader code looks like that:
vec2 st = (UV-0.5);
float r = length(st)*2.;
float angle = atan(st.x, st.y)*1./(PI*2.);
float r_a = r;
r_a *= (1.+hills_1_a*sin((angle)*round(hills_1_d)*2.*PI));
r_a *= (1.+hills_2_a*sin((angle)*round(hills_2_d)*2.*PI));
r_a *= (1.+hills_3_a*sin((angle)*round(hills_3_d)*2.*PI));
COLOR.a = float(r_a < rad_thresh*radius);
hills_[1-3]_[a,d] are just float scalars.
For the CollisionPolygon2D the code looks like this:
var arr:PoolVector2Array = PoolVector2Array()
var angle:float = 0
var angle_delta = 2*PI/shape_points
while angle <= 2*PI:
var r = radius
r *= (1+hills_1_a*sin((angle)*round(hills_1_d)));
r *= (1+hills_2_a*sin((angle)*round(hills_2_d)));
r *= (1+hills_3_a*sin((angle)*round(hills_3_d)));
var p = Vector2.UP.rotated(angle) * r
angle += angle_delta
$CollisionPolygon2D.polygon = arr
hills_[1-3]_[a,d] variables are the same values as in the shader (they are passed as uniform in other part of the code).
shape_points is the number of points in the polygon (I use around 1000 but it doesn't affect the difference)
So as you can see the code is essentially the same. But as seen on the image above the values are off. I'll admit, it's pretty zoomed in. Here's how it looks zoomed out:
But since it's going to be a planet, I need it to be accurate when zoomed in.
What's the reason behind this? (I'm guessing cpu vs gpu rounding difference, but I hope I'm wrong)
Is there a way to fix this?
If not, how can I work around it? How can I get accurate procedurally generated planet that has same sprite and collision?
I'm okay with having to write a lot more code if needed, even modifying the engine/writing cpp modules.
Thanks in advance.