How to draw Grid lines with libgdx Shape renderer




From this video you will learn how to create a grid line for your android game development, using grid line you could easily track the position of object  with in the game world and by this technique you can get rid lot's of error that would possibly happened accross while developing your game , I hope you learn something about this video and dont forget like it, or share it with your friends and subscribe to my channel Thank you for watching
become an Adfly publisher ?:
https://bit.ly/2C3Mpio
https://join-adf.ly/16684457
VISIT: ihttps://ifindsolutionorg.blogspot.com/
fb page:
https://www.facebook.com/Pinoynetcorner/

Step 1.) Create a package and name it "grid"
Step 2.) Create a new Class inside package "grid" and name it "Grid" following the standard naming for a class in java.

Step 3.) Use the codes below and try if it works... and give me a feedback in the comment section!!!
              Watch the video above for your reference.




import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

public class Shape{
private static final float WORLD_HEIGHT=40f;
private static final float WORLD_WIDTH = 20f;
private OrthographicCamera camera;
private Viewport viewport;
private ShapeRenderer sr;
private boolean drawGrid = true;
private boolean drawCircle = true;
private boolean drawRectangle = true;
private boolean drawPoints = true;

public Shape(ShapeRenderer sr){
this.sr = sr;
}

public void create(){
camera = new OrthographicCamera();
viewport = new FitViewport(WORLD_HEIGHT,WORLD_WIDTH,camera);
sr = new ShapeRenderer();
}

public void render(){
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT|GL20.GL_DEPTH_BUFFER_BIT);
camera.update();
sr.setProjectionMatrix(camera.combined);
int width = (int)WORLD_WIDTH;
int height =(int)WORLD_HEIGHT;
if(drawGrid){
drawGrid();
}
}
private void drawGrid() {
sr.begin(ShapeRenderer.ShapeType.Line);
sr.setColor(Color.WHITE);
for(int x = (int) -WORLD_WIDTH;x<WORLD_HEIGHT;x++){
sr.line(-x, -WORLD_WIDTH,x, WORLD_WIDTH);
for(int y = (int) -WORLD_HEIGHT;y<WORLD_HEIGHT;y++){
sr.line(-WORLD_HEIGHT, y, WORLD_HEIGHT, y);
}
sr.setColor(Color.GREEN);
sr.line(0, -WORLD_WIDTH, 0, WORLD_HEIGHT);
sr.setColor(Color.RED);
sr.line(-WORLD_HEIGHT, y, WORLD_HEIGHT, y);
sr.end();
}

}

public void resize(int width, int height){
viewport.update(width, height);

}
public void dispose(){
sr.dispose();
}

}


Post a Comment

0 Comments