1
\$\begingroup\$

As you can see on the image, I have a p1 and p2 objects with (x,y) coordinates which I know the values, and I know radius of all these circle objects.

However, I want to calculate new position x,y which would be p3 center point. Basically, as you can see it's p2 position + radius.

I am doing this for java game which is based on libgdx. I would appreciate any math or java language directions/examples.

enter image description here

\$\endgroup\$
3
  • 2
    \$\begingroup\$ This looks like just Cosine Law to work out the angle at the tip of the triangle P1P2P3, and then the Unit Circle to get the coordinates of the point at that angle. Each of these topics is covered extensively in online math and geometry tutorials. How have you tried to solve this problem, given your research of these resources to date? What new gamedev-specific insight do you need here? \$\endgroup\$
    – DMGregory
    Commented May 17, 2022 at 19:46
  • \$\begingroup\$ Thank you I will have a look at this. \$\endgroup\$
    – Kapparino
    Commented May 17, 2022 at 19:54
  • \$\begingroup\$ @Kapparino You could also take a look at gamedev.stackexchange.com/questions/84115/… \$\endgroup\$
    – Pateman
    Commented May 18, 2022 at 11:01

1 Answer 1

2
\$\begingroup\$

For circle p2 and p3 to be close enough to eachother to touch, we know that the distance between them must be 2 * rs (where rs is the radius of the small circles).

If we then construct a triangle from the points p1, p2 and p3, we can divide that triagle in the middle and know that the short side of the triangle is equal to rs

enter image description here

Using the sine rule we can then figure out the angle at the center of the big circle as we have a triangle where we know two of the sides (r and rs) and one angle (the 90 degree angle we get from splitting the triangle in half).

This angle times two is the angle between p2 and p3

angle = 2.0 * asin(rs / r)

Since we can get the angle of a Vector2 in libGDX it's then easy to find the angle of p2 and then add the angle calculated above, as shown in this example where the green circle is moved around and the blue position is calculated using the above method:

enter image description here

Full source code for a libGDX example:

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;

public class SandboxGame extends Game {
    OrthographicCamera camera;
    ShapeRenderer shapeRenderer;

    @Override
    public void create () {
        float aspectRatio = (float)Gdx.graphics.getHeight()/(float)Gdx.graphics.getWidth();
        camera = new OrthographicCamera(10.0f, 10.0f * aspectRatio);
        camera.position.set(0, 0, 0);
        shapeRenderer = new ShapeRenderer();
    }

    float b;
    @Override
    public void render() {
        b += 32.0f * Gdx.graphics.getDeltaTime();
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        shapeRenderer.setProjectionMatrix(camera.combined);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);

        float r = 1.0f;
        float rs = 0.4f;
        Vector2 p1 = new Vector2(1, 0.5f);
        Vector2 p2 = new Vector2(r, 0).rotateDeg(b).add(p1);
        float angleToP2 = new Vector2(p2).sub(p1).angleRad();
        float angle = 2.0f * MathUtils.asin(rs / r);
        Vector2 p3 = new Vector2(r, 0).rotateRad(angleToP2 + angle).add(p1);


        shapeRenderer.setColor(Color.RED);
        shapeRenderer.circle(p1.x, p1.y, r, 32);
        shapeRenderer.setColor(Color.GREEN);
        shapeRenderer.circle(p2.x, p2.y, rs, 32);
        shapeRenderer.setColor(Color.BLUE);
        shapeRenderer.circle(p3.x, p3.y, rs, 32);
        shapeRenderer.end();
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thank you for detail description. \$\endgroup\$
    – Kapparino
    Commented May 20, 2022 at 10:19
  • 1
    \$\begingroup\$ Not a problem @Kapparino, I hope it helped. \$\endgroup\$
    – bornander
    Commented May 20, 2022 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .