2014-02-17 3 views
8

libGDX를 사용하여 새총 같은 것을 만들려고합니다.LibGDX 그릴 선

내 코드 것은

if (Gdx.input.isTouched()) { 

     ShapeRenderer sr = new ShapeRenderer(); 
     sr.setColor(Color.BLACK); 
     sr.setProjectionMatrix(camera.combined); 

     sr.begin(ShapeType.Line); 
     sr.line(player.getLeft().x, player.getLeft().y, 
       Global.game_touch_position.x, Global.game_touch_position.y); 
     sr.line(player.getRight().x, player.getRight().y, 
       Global.game_touch_position.x, Global.game_touch_position.y); 
     sr.end(); 

    } 

내가 출력 enter image description here 이 끔찍한 외모, 나는 내 안드로이드 휴대 전화에서 디버깅 할 경우 로그 캣가 메시지

02-17 18:55:27.371: D/dalvikvm(7440): GC_CONCURRENT freed 1884K, 40% free 8287K/13635K, paused 15ms+2ms, total 40ms 

에 의해 스팸되어있을 것이다 이렇게 그리고 lags, 화면을 건드릴 때 30 fps처럼, 그리고 그렇지 않을 때 60 ...

나는 또한 lin 약간의 두께가있는 선이 있기 때문에 선이 더 커지면 더 두껍게 만들어야합니다.

libgdx에 간단한 줄을 그리는 가장 좋은 방법은 무엇입니까? 나는 아마 어떤 대답을 찾을 수 없을 것이다. 나는 선의 한 점에서 다른 선으로 원을 그릴 것이다. 이것은 괜찮아 보일 것이지만, 새총처럼 보이지 않을 것이다 ...

어떤 도움 ?

+3

프레임 렌더링마다 'ShapeRenderer'의 인스턴스를 생성하기 때문에 스팸이있는 것 같습니다. 생성자에서 대신 한 번 생성 해보십시오. – desertkun

+0

그러나 어떻게 생각으로 선을 그려야합니까? –

+0

일반적으로 게시물 당 하나의 질문 만 시도하십시오. 그래서 그 주위에 설계되었습니다. 어쨌든 선 두께는 http://stackoverflow.com/questions/16680908/libgdx-gl10-gllinewidth?rq=1을 참조하십시오. –

답변

11

난 그냥 도우미 또는 유틸 클래스에서이 같은 것을 갖고 싶어합니다. 나는 디버깅을하고 디버깅과 시각화에 사용한다.

private static ShapeRenderer debugRenderer = new ShapeRenderer(); 

    public static void DrawDebugLine(Vector2 start, Vector2 end, int lineWidth, Color color, Matrix4 projectionMatrix) 
    { 
     Gdx.gl.glLineWidth(lineWidth); 
     debugRenderer.setProjectionMatrix(projectionMatrix); 
     debugRenderer.begin(ShapeRenderer.ShapeType.Line); 
     debugRenderer.setColor(color); 
     debugRenderer.line(start, end); 
     debugRenderer.end(); 
     Gdx.gl.glLineWidth(1); 
    } 

    public static void DrawDebugLine(Vector2 start, Vector2 end, Matrix4 projectionMatrix) 
    { 
     Gdx.gl.glLineWidth(2); 
     debugRenderer.setProjectionMatrix(projectionMatrix); 
     debugRenderer.begin(ShapeRenderer.ShapeType.Line); 
     debugRenderer.setColor(Color.WHITE); 
     debugRenderer.line(start, end); 
     debugRenderer.end(); 
     Gdx.gl.glLineWidth(1); 
    } 

이제 어디에서나 선을 그릴 수있어 원하는 투영 매트릭스를 쉽게 얻을 수 있습니다.

HelperClass.DrawDebugLine(new Vector2(0,0), new Vector2(100,100), camera.combined); 

SpriteBatch 시작과 끝 외부에서 그립니다. 그리고 각 프레임마다 많은 선을 만들고 싶다면 ShapeRenderer를 별도의 정적 메서드로 시작하고 끝내는 것이 더 낫습니다. 또는 필요에 따라 공개하고 직접 처리하는 것이 좋습니다.

분명히 더 많은 모양이나 과부하에 대해 더 많은 방법을 만들 수 있습니다.

3

Gdx.gl10.glLineWidth(width_in_pixels)을 호출하여 선 두께를 설정할 수 있습니다.

+0

더보기 좋게하려면 어떻게해야합니까? –

+0

@Paul 대신 텍스처를 그릴 수 있습니다. 예를 들어, 크기가 [1px] x [16px]'인 라인 텍스처를'[line_length] x [16px]'로 늘리고'Math.atan2'를 사용하여 begin과 ent 점에 맞게 회전시킵니다. . 나는 나중에 약간의 코드를 제공 할 수있다. – desertkun

+0

휴대 전화에서 회선이 두꺼운 이유는 무엇입니까? 폭 10 –

1

ShapeRenderer에는 지정된 두께의 선을 그리는 rectLine()이라는 메서드가 있습니다. 그것은 정확하게 당신이 당신의 선 두께 질문으로 찾고있는 것입니다. 또한 sr.begin하는 (ShapeType.Filled를) sr.begin (ShapeType.Line)를 변경

if (Gdx.input.isTouched()) { 

    ShapeRenderer sr = new ShapeRenderer(); 
    sr.setColor(Color.BLACK); 
    sr.setProjectionMatrix(camera.combined); 

    sr.begin(ShapeType.Filled); 
    sr.rectLine(player.getLeft().x, player.getLeft().y, 
      Global.game_touch_position.x, Global.game_touch_position.y, desired_thickness); 
    sr.rectLine(player.getRight().x, player.getRight().y, 
      Global.game_touch_position.x, Global.game_touch_position.y, desired_thickness); 
    sr.end(); 

} 
+0

하하, 이전 질문! 또한 render 메소드의 shaperenderer가 매우 나쁩니다. –

+0

예 ... 렌더 메소드의 새 객체를 만드는 것이 게임에서 가장 좋은 방법은 아닙니다. 그리고 저는 너무 흥분하여 실제로 SO에 대한 질문에 답할 수있는 방법을 정확히 알고 있었기 때문에 저항 할 수 없었습니다.질문이 10 개월 된 경우에도 ... –

+0

나는 그 형제를 기억합니다! :) –