4
\$\begingroup\$

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 :

Picture

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

Edit : i used the formula found on Wikipedia :

Formula

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Voted up for "squircle", lmao. \$\endgroup\$
    – Dan
    Jun 1 '16 at 13:42
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ thank you for your answer. what do you mean by you should transform it into a vectorial path, \$\endgroup\$
    – un famous
    Jun 1 '16 at 15:37
  • \$\begingroup\$ 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. \$\endgroup\$
    – liggiorgio
    Jun 1 '16 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.