The idea is to draw trace of moving Actor and if some other Actor intersect it, some function would be called.

I've been searching for solution to this problem in Intersector class, but it seems to me that the only way to do what I want is to implement my polyline as a Polygon and find intersections, but its totally inelegant. I'm sure there should be some smart way to do this.

Essentially you're looking for a Collision algorithm.

What you described is the "simplest" method of doing this (keeping track of all actor's and checking the intersection of the Polygons during each meaningful game tick.) There are a number of methods in their Maths library that can handle this for convex polygons.

Another option is to use an actual Physics library like Box2d to handle collisions and provide the collision methods; this has the bonus of simplifying a lot of logic on the side of smaller/faster actor movements.

up vote 0 down vote accepted

Well, the best way I could solve this problem is to create a Group of Actors, that are Sprites with Polygon bounds.

So CollidablePolyline class takes array of floats as vertices (just like Polyline class) and then draw textured polyline on stage.

class CollidablePolyline(vertices: Array<Float>): Group() {

    init {
        for (i in 0 until vertices.size-2 step 2) {
            val startPoint = Vector2(vertices[i], vertices[i+1])
            val endPoint = Vector2(vertices[i+2], vertices[i+3])
            addActor(CollidableLine(startPoint, endPoint))
        }
    }
}

class CollidableLine(p0: Vector2, p1: Vector2): Actor() {

    private var thickness = 15f

    private var pixmap = Pixmap(1, 1, Pixmap.Format.RGBA8888)
    private var texture = Texture(pixmap)
    private var sprite = Sprite(texture)
    private var bounds = Polygon()

    init {
        val distance = p0.dst(p1)
        setSize(distance, thickness)
        setOrigin(0f, height / 2f)
        setPosition(p0.x, p0.y - height / 2)
        val w = p1.x - p0.x
        val h = p1.y - p0.y
        rotation = MathUtils.radiansToDegrees * MathUtils.atan2(h, w)
        pixmap.setColor(0f, 0f, 0f, 1f)
        pixmap.fill()
        texture.draw(pixmap, 0, 0)
        sprite.setSize(width, height)
        sprite.setOrigin(originX, originY)
        sprite.rotation = rotation
        sprite.setOriginBasedPosition(p0.x, p0.y)
        bounds.vertices = floatArrayOf(p0.x, p0.y - height / 2, p0.x, p0.y + height / 2, p1.x, p1.y + height / 2, p1.x, p1.y - height / 2)
        bounds.setOrigin(originX, originY)
        bounds.rotation = rotation
    }

    override fun draw(batch: Batch?, parentAlpha: Float) {
        sprite.draw(batch)
    }
}

So now you can use bounds to detect intersection of each line in the polyline.

Moreover, CollidableLine class allows to change thickness and color (just change color of a Pixmap and texture.draw(pixmap) after that) of line.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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