简体   繁体   中英

libGDX connect two actors with line

I have to create 2 images and connect them with line. When I move one actor obviously correct end of line must fallow my actor. I know, that I can use ShapeRenderer and redraw that line every time, but is this the best idea?

ShapeRenderer sr = new ShapeRenderer();
    sr.setColor(Color.RED);
    sr.setProjectionMatrix(viewport.getCamera().combined);

    sr.begin(ShapeRenderer.ShapeType.Filled);
    sr.rectLine(vertex1.getCenterX(), vertex1.getCenterY(),
            vertex2.getCenterX(), vertex2.getCenterY(), 10);
    sr.end();

I have already created two actors and manage drag event. Now I have to draw that line. It should looks like this

在此处输入图片说明

If the shaperender suits your style I don't see why that would be a bad idea?

If you draw the lines behind the actors you simply have to draw from the center of actor 1 to 2. Otherwise you have to calculate where to start. Something like this pseudo code:

//Subtract vectors and normalize to get direction
direction = origin1.sub(origin2);
direction.nor();

//Add the radius in the correct direction from the origin.
startPoint = origin2.add(direction * actorRadius);
endPoint = origin1.add(direction.rotate(180) * actorRadius);

Be careful of working with vectors like that. Chaining like above will change the original vector if you do not use vector.cpy() . So if you still need the original origins you have to do direction = origin1.cpy().sub(origin2); now direction holds a copy of the vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM