[SOLVED][LibGDX] Rotate around a shape in a non-circular motion

I am trying to rotate these circles around the black shape. Currently, they are rotating in a circular motion, but I want them to travel from vertex to vertex.

Rotation code:

  //central is the black shape
         angle += delta * rotSpeed * direction;
        x = MathUtils.cos(angle) * central.radius + central.x;
        y = MathUtils.sin(angle) * central.radius + central.y;

Thank you guys very much for helping me!

Linearly interpolate between the vertices.

How can I get the vertice coords?

The black shape is being rendered like this:


        sr.begin(ShapeRenderer.ShapeType.Filled);
        sr.setColor(Color.BLACK);
        sr.circle(this.x, this.y, this.radius, segments);
        sr.setColor(Color.GREEN);
        sr.circle(this.x, this.y, this.radius / 4);
        sr.end();

You have to compute them yourself by inscribing the circle with a regular polygon. The vertices of that polygon, as libGDX draws them, are calculated in this method.
They once compute the angle in radians which is 2.0 * PI / segments, then compute the sine and cosine of that angle and then repeatedly apply a 2x2 matrix/vector multiplication to rotate the “running” vertex coordinates (cx, cy) around the circle. This avoids having to compute the sine/cosine of the increasing angle every iteration.

Alternately, you could calculate the tangent’s gradient of the circle at the center of each line, which would look something like this I think, but I haven’t tested this:

private float getSlope(int sides, float angle) {
	float b = angle - angle % (int) (2 * Math.PI / sides);
	return (float)(- Math.sin(b) / Math.cos(b));
}

Interpolation might not be necessary. You could do a procedural calculation by starting at the beginning and rotating at an angle relative to the circle. You would just need the distance of a side if it is linear.

Imagine moving around a 6gon in a circle. You have, on each vertex, an intersecting point on the path you take. You just need to rotate enough to hit that specific point and rotate from there.

:slight_smile:

I’m very happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this greatest doc.