Search code examples
javalibgdxscene2dbox2dlights

Attaching box2dLight to scene2d.Actor


As a part of my game I am moving a car on Stage and trying to put headlights on the same. I am setting actions on the CAR so that position update of CAR will be handled by scene2d.actions.Actions. This is the code I am using to set lights:

rayHandler.setCombinedMatrix(stage.getCamera().combined);
rayHandler.useCustomViewport(viewport.getScreenX(), viewport.getScreenY(),
                viewport.getScreenWidth(), viewport.getScreenHeight());
coneLight = new ConeLight(rayHandler, 50, Color.YELLOW, 150, 400, 150,0,45);

But the issue with this is, it requires the positions at which light has to be positioned, Now to get the position of headlight in a car is tricky (Or I am missing something which the whole world knows) since car can be heading in any direction, Is there any way to attach light to an actor? or should we always compute the position of light using

something like actor.getX() + cosDeg(actor.direction())*length;

for example see image enter image description here


Solution

  • getting location of sprite's vertexes solved the issue

    import static com.badlogic.gdx.graphics.g2d.Batch.X2;
    import static com.badlogic.gdx.graphics.g2d.Batch.X3;
    import static com.badlogic.gdx.graphics.g2d.Batch.Y2;
    import static com.badlogic.gdx.graphics.g2d.Batch.Y3;
    
    ... #somewhere in render method.
    coneLight.setPosition(sprite.getVectors()[X2], sprite.getVectors()[Y2]);