LibGdx RayCast box2d debug (visual on screen)

587 views Asked by At

Can someone help me?

I want see the raycast in the game screen (for debugging..).

What is the best way to achieve that? Please note that i use "box2d". and the way that i draw stuff to the screen is with animation.. So does it means that i need to create an "EdgeShape"? and then this is my debug line?

Please if there is any suggestions that you can give or ideas i really don't mind how to implement, all i want is a proper way to see the Raycast. I could not found a good way to draw the Raycast, i saw someone that use batch.draw(); - but i guess that it won't work for me, because that way that my game work's is with box2ds shapes and animation? is that right?

Thanks so much!

   world.rayCast(callback,     enemy.getBody().getPosition(), 
            new Vector2(enemy.getBody().getPosition().x-500, enemy.getBody().getPosition().y));
   }

   RayCastCallback callback = new RayCastCallback() {
      @Override
      public float reportRayFixture(Fixture fixture, Vector2 point,
            Vector2 normal, float fraction) {

         if(fixture.getBody().getUserData() == ModelType.PLAYER) {
            System.out.println("hey!");
            return 0;
         }
         return -1;
      }
1

There are 1 answers

0
adam On BEST ANSWER

I debug it now with ShapeRendrer:

shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.line(rayStart,rayEnd);
shapeRenderer.setColor(Color.RED);
shapeRenderer.end();

I just add it to the same class, where my enemy is.

And i also taking care of the points (the Vector2):

world.rayCast(rayCastCallback, p1, p2);

To be updated, as my enemy moves.