# Move Sprite in constant speed on squircle

I'm creating a game and i need to move a sprite on squircle path.

https://en.wikipedia.org/wiki/Squircle


i created the path using the equation :

    angle -= Gdx.graphics.getDeltaTime()*speed ;
float x = (float) (R*Math.pow(Math.abs(MathUtils.cos(angle)),1/10f)*sgn(MathUtils.cos(angle)))+a;
float y = (float) (R*Math.pow(Math.abs(MathUtils.sin(angle)),1/10f)*sgn(MathUtils.sin(angle)))+b;
sprite.setPosition(x,y);


the result is :

My question is how to make the sprite move in a constant speed

Edit : i used the formula found on Wikipedia :

• Voted up for "squircle", lmao.
– Dan
Jun 1 '16 at 13:42

If you want to make your sprite move at a constant speed alond the squircle, you should transform it into a vectorial path, in order to calculate its linear speed instead of the angular speed as you did.

If you want to work with angular speed, there's no problem as it will be a good approssimation anyway.

Although you used the formula found on Wikipedia, the one you implemented is slightly different from it. Try using the following for your sprite position:

// a = horizontal axis; b = vertical axis
angle -= Gdx.graphics.getDeltaTime()*speed;
float x = Math.pow(Math.abs(MathUtils.cos(angle)),0.5f)*sgn(MathUtils.cos(angle))*a;
float y = Math.pow(Math.abs(MathUtils.sin(angle)),0.5f)*sgn(MathUtils.sin(angle))*b;
sprite.setPosition(x,y);


or, alternatively:

// R = max radius, equal to having a=b in the previous script
angle -= Gdx.graphics.getDeltaTime()*speed;
float x = Math.pow(Math.abs(MathUtils.cos(angle)),0.5f)*sgn(MathUtils.cos(angle))*R;
float y = Math.pow(Math.abs(MathUtils.sin(angle)),0.5f)*sgn(MathUtils.sin(angle))*R;
sprite.setPosition(x,y);


This snippet reflects the geometric definition in polar coordinates of the squircle, if the effect isn't what you expect you can change the speed value when increasing/decreasing the value of angle.

• thank you for your answer. what do you mean by you should transform it into a vectorial path, Jun 1 '16 at 15:37
• In general, by "path" it's meant a data structure consisting of points in the plane or space. To cut it out, you transform the starting geometric shape into a polygon, made up of points, from which you can draw many very tiny lines. From a graphical appearance it looks exactly the same to the original (no edgy borders), yet you can move your sprite from point A to point B by interpolating (lerp()) relative coordinates. Jun 1 '16 at 16:49