Unable to see Box2DdebugRenderer's body outlines - libgdx

I am using box2ddebugrenderer temporarily as of now to render the bodies into the world but I am not able the see the mono-color outlines of bodies being created in the world upon running.
This is the code of my game's Screen:
public class GameScreen implements Screen {
static final int VIEWPORT_WIDTH = 20;
static final int VIEWPORT_HEIGHT = 13;
World world;
Body ground;
final float TIME_STEP = 1 / 300f;
float accumulator = 0f;
OrthographicCamera camera;
private Box2DDebugRenderer renderer;
#Override
public void render(float delta) {
//Clear the screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render(world, camera.combined);
accumulator += delta;
while (accumulator >= delta) {
world.step(TIME_STEP, 6, 2);
accumulator -= TIME_STEP;
}
}
#Override
public void show() {
world = createWorld();
ground = createGround(world);
renderer = new Box2DDebugRenderer();
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f);
camera.update();
}
public World createWorld() {
return new World(Constants.WORLD_GRAVITY, true);
}
public Body createGround(World world) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y));
Body body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2);
body.createFixture(shape, Constants.GROUND_DENSITY);
shape.dispose();
return body;
}
}

Need to define BodyType of physics body in BodyDef
BodyDef bodyDef=new BodyDef();
bodyDef.BodyType= BodyType.STATIC;
value of different constant should be appropriate so that body lie within the camera viewport like you have
Constants.GROUND_X,
Constants.GROUND_Y,
Constants.GROUND_WIDTH,
Constants.GROUND_HEIGHT
create small body and check, body outline is visible or not.
After this, still if you got nothing on screen draw simple Sprite using SpriteBatch.
One more condition
May be you are overriding render method of Game in your Child Game class and not calling parent render method.

Related

RayHandler doesn't goes with Box2D world

I have a difficult situation.
I'm making a 2D game and I'm on the step
of integrating box2Dlights part.
But when i'm rendering my RayHandler, I have a black screen.
Here is another image, where I don't render my lights. There is no lights now.
I need to have collision of Box2D and Box2Dlights mixed together.
Please note:
All my boxes are made in units, equal to 1, so that one room is 16 squares length.
All entire world is zoomed by setToOrtho((16 - 1.875f) / Gdx.graphics.getHeight()), where 16 is tile size.
If I scaled my tile size, I would like to have this stuff work.
All code is complicated so I send the only GameScreen and GameMap classes for now:
#Override
public void show() {
InstanceVars inst = InstanceVars.getInstance();
inst.world = new World(new Vector2(0, 0f), true);
inst.engine = new Engine();
inst.handler = new RayHandler(inst.world);
LevelCreator creator = new LevelCreator("level_def.cfg");
try {
gameWorld = creator.createNewLevel();
gameWorld.start();
} catch (FileCorruptedException e) {
e.printStackTrace();
}
}
#Override
public void render(float delta) {
gameWorld.render();
}
#Override
public void resize(int width, int height) {
gameWorld.resize(width, height);
}
#Override
public void dispose() {
gameWorld.dispose();
InstanceVars.getInstance().dispose();
}
for the GameScreen.java
and
private World physWorld = InstanceVars.getInstance().world;
private Box2DDebugRenderer physRender = new Box2DDebugRenderer();
private OrthographicCamera cam = new OrthographicCamera();
private RayHandler lights = new RayHandler(physWorld);
private PlayerController controls;
private float STD_ZOOM = ((float) CELL_SIZE + 16) / Gdx.graphics.getHeight();
private Body playerBody;
public GameMap() {
cam.zoom = STD_ZOOM;
}
public void start() {
GameObject player = (GameObject) getProperties().get("ent_player");
playerBody = player.getComponent(PhysicsComponent.class).getBody();
controls = new PlayerController();
Gdx.input.setInputProcessor(controls);
physWorld.setContactListener(new CollisionInteractor(InstanceVars.getInstance().engine));
PointLight light = new PointLight(lights, 1000, Color.GOLDENROD, 500, playerBody.getPosition().x, playerBody.getPosition().y);
}
public void render() {
updatePlayer();
physWorld.step(1/60f, 8, 2);
updateCamera();
physRender.render(physWorld, cam.combined);
// lights.updateAndRender();
}
private void updatePlayer() {
if (controls.up) playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, -controls.speed);
else playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, controls.down ? controls.speed : 0);
if (controls.down) playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, controls.speed);
else playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, controls.up ? -controls.speed : 0);
if (controls.left) playerBody.setLinearVelocity(-controls.speed, playerBody.getLinearVelocity().y);
else playerBody.setLinearVelocity(controls.right ? controls.speed : 0, playerBody.getLinearVelocity().y);
if (controls.right) playerBody.setLinearVelocity(controls.speed, playerBody.getLinearVelocity().y);
else playerBody.setLinearVelocity(controls.left ? -controls.speed : 0, playerBody.getLinearVelocity().y);
}
public void resize(float width, float height) {
STD_ZOOM = ((float) CELL_SIZE - 1.875f) / Gdx.graphics.getHeight();
cam.setToOrtho(true, width, height);
}
private void updateCamera() {
cam.position.set(playerBody.getPosition(), 0);
cam.update();
}
public void dispose() {
super.dispose();
physWorld.dispose();
physRender.dispose();
}
for GameMap.java.
If you need more details, don't be afraid to ask.
I cannot solve the problem on my own.

Box2dlights textures isn't drawn

I am using box2dlights in my project and there seem to be an issue. When I actually use RayHandler in the program, only the light is drawn but no textures. And so when I remove RayHandler from the render loop it all works fine. I want the light to be drawn and then textures to be drawn above it. How do I reach that?
This is the code:
public class GameScreen implements Screen {
private Core core;
private SpriteBatch batch;
private Sprite spiderSprite;
private OrthographicCamera camera;
private RayHandler rayHandler;
private Box2DDebugRenderer debugRenderer;
private World world;
private Body spider, prop;
private PointLight spiderLight;
private Stage stage;
private boolean paused, lost, check;
private int pointsCount;
private float time;
public GameScreen(Core c) {
core = c;
stage = new Stage();
batch = new SpriteBatch();
camera = new OrthographicCamera();
camera.setToOrtho(false, core.SCREEN_WIDTH, core.SCREEN_HEIGHT);
batch.setProjectionMatrix(camera.combined);
debugRenderer = new Box2DDebugRenderer();
spiderSprite = core.chars.createSprite("spider");
world = new World(new Vector2(.0f, -10.0f), true);
BodyDef spiderDef = new BodyDef();
spiderDef.type = BodyDef.BodyType.DynamicBody;
spiderDef.position.set(core.SCREEN_WIDTH / 2, core.SCREEN_HEIGHT / 2);
spider = world.createBody(spiderDef);
CircleShape shape = new CircleShape();
shape.setRadius(core.SCREEN_WIDTH / 15);
spider.createFixture(shape, .1f);
BodyDef propDef = new BodyDef();
propDef.type = BodyDef.BodyType.StaticBody;
propDef.position.set(core.SCREEN_WIDTH / 2, core.SCREEN_HEIGHT);
prop = world.createBody(propDef);
CircleShape propShape = new CircleShape();
propShape.setRadius(0.1f);
prop.createFixture(propShape, 1.0f);
RopeJointDef rope = new RopeJointDef();
rope.bodyA = spider;
rope.bodyB = prop;
rope.maxLength = core.SCREEN_HEIGHT / 2;
world.createJoint(rope);
rayHandler = new RayHandler(world);
rayHandler.setAmbientLight(.5f);
spiderLight = new PointLight(rayHandler, 100, Color.WHITE, core.SCREEN_WIDTH, 100, 100);
spiderLight.attachToBody(spider);
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
stage.addAction(Actions.fadeIn(0.85f));
}
#Override
public void render(float delta) {
clearScreen();
batch.setProjectionMatrix(camera.combined);
batch.begin();
if (!lost && !paused) {
updateWorld();
updateSpider();
}
drawSpider();
stage.act();
stage.draw();
batch.end();
}
private void clearScreen() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
private void updateSpider() {
//spider.setLinearVelocity(Gdx.input.getAccelerometerX() > 0 ? 50.0f : -50.0f, 0.0f);
float angle = MathUtils.radiansToDegrees * MathUtils.atan2(core.SCREEN_HEIGHT - spider.getPosition().y, core.SCREEN_WIDTH / 2 - spider.getPosition().x) - 90;
spiderSprite.setPosition(spider.getPosition().x, spider.getPosition().y);
spiderSprite.setRotation(angle);
}
private void drawSpider() {
spiderSprite.draw(batch);
rayHandler.render();
}
private void updateWorld() {
world.step(1/60f, 6, 2);
rayHandler.setCombinedMatrix(camera);
rayHandler.update();
time += Gdx.graphics.getDeltaTime();
}
}
I also removed a redundant part of the code which is not connected with the issue.
Stage has it's own batch with it's own .begin() and .end(). You should call batch.end(); before calling stage.draw();
I found it out. RayHandler.render() must not be invoked between batch.begin() and batch.end().

libGDX creating a minimap of a tiledmap

I am developing a top-down 2D game with a TiledMap.
Currently i want to create a minimap of my TiledMap, but i´m really confused how to do this.
I´ve read something that i should create another camera, zoom out and render the map again, but how should i draw the zoomed out map in the upper right corner?
Am i overthinking this whole thing?
My Map:
public class LiquidMap {
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera = new OrthographicCamera();
private MiniMap miniMap;
public LiquidMap(String filePath) {
map = new TmxMapLoader().load(filePath);
renderer = new OrthogonalTiledMapRenderer(map, 1 / 32f);
camera.setToOrtho(false, 30, 20);
miniMap = new MiniMap(map);
}
public void update(float x, float y){
camera.position.x = x;
camera.position.y = y;
camera.update();
//renderer.setView(camera.combined, x - 10, y - 10, 20, 20);
renderer.setView(camera);
miniMap.update(x, y);
}
public void update(HostPlayer player){
this.update(player.position.x + (player.skin.getWidth()/2f)/32f, player.position.y + (player.skin.getHeight()/2f)/32f);
}
public void render(HostPlayer player){
renderer.render();
renderer.getBatch().begin();
renderer.getBatch().draw(player.skin, player.position.x, player.position.y, 1/32f * player.skin.getWidth(), 1/32f * player.skin.getHeight());
renderer.getBatch().end();
miniMap.render();
}
And my MiniMap:
public class MiniMap {
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera = new OrthographicCamera();
public MiniMap(TiledMap map) {
renderer = new OrthogonalTiledMapRenderer(map, 1 / 32f);
camera.setToOrtho(false, 30, 20);
camera.zoom = 10;
}
public void update(){
}
public void update(float x, float y){
//Pixventure.instance.gameScreen.getCamera()
camera.position.x = x;
camera.position.y = y;
camera.update();
renderer.setView(camera.combined, x-15, y-15, 30, 30);
//renderer.setView(camera);
}
public void render(){
renderer.render();
}
}
The whole situation is looking like this:
Minimap's camera position is updated by player position. Place your minimap at the corner and don't change his position by your update() method.

render to FrameBuffer not working in combination with ShapeRenderer in a custom Actor (libgdx)

I try to implement a custom Actor which displays contents of a FrameBuffer by extending Image. Inside I'd like to draw using a ShapeRenderer.
public class CustomActor extends Image {
private FrameBuffer frameBuffer = null;
private ShapeRenderer shapeRenderer = new ShapeRenderer();
private int width;
private int height;
public CustomActor() {
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
shapeRenderer.setAutoShapeType(true);
}
#Override
public void draw(Batch batch, float parentAlpha) {
if (frameBuffer == null) {
frameBuffer = new FrameBuffer(Pixmap.Format.RGB565, width / 2, height / 2, false);
TextureRegion textureRegion = new TextureRegion(frameBuffer.getColorBufferTexture());
textureRegion.flip(false, true);
setDrawable(new TextureRegionDrawable(textureRegion));
}
frameBuffer.begin();
// render content
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(0, 0, frameBuffer.getWidth() / 2, frameBuffer.getHeight() / 2);
shapeRenderer.end();
if (frameBuffer != null) {
frameBuffer.end();
}
super.draw(batch, parentAlpha);
}
}
When deleting the ShapeRenderer part I can see the green background in the scene. But as soon as I use the ShapeRenderer the content is black. There is no white rectangle visible.
Any ideas what I am doing wrong?
Just found out that using my own SpriteBatch (with begin() and end()) after the FrameBuffer end() it works. So the problem seems to have to do with the calling order of the FrameBuffers methods and the SpriteBatches methods. Ending the given actor batch before beginning the FrameBuffer and "re"-beginning it to draw the actor is also working (see comment above).

Resize drawing to match frame size

I've written an app that custom draws everything inside paint() based on fixed pixel positions. Then I disabled resize of the frame so its always visible.
However, now I would like to be able to resize it but I dont want to change my drawling code. I was hoping I could grab the 300x300 square of the Graphics g object and resize it to the JFrame current size after all of my drawling code, but I have no idea what I'm doing.
Here sample code. In this I want the 100x100 square to remain in the middle, proportionate to the resized JFrame:
package DrawAndScale;
import java.awt.Color;
import java.awt.Graphics;
public class DASFrame extends javax.swing.JFrame {
public DASFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setSize(300, 300);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DASFrame().setVisible(true);
}
});
}
#Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fill3DRect(100, 100, 100, 100, true);
}
}
Thanks.
Assuming you rename your method that paints for 300x300 as paint300, define a buffered image:
#Override public void paint(Graphics g) {
Image bufferImage = createImage(300, 300); // empty image
paint300(bufferImage.getGraphics()); // fill the image
g.drawImage(bufferImage, 0, 0, null); // send the image to graphics device
}
Above is when you want to draw at full size (300x300).
If your window is resized:
#Override public void paint(Graphics g) {
Image bufferImage = createImage(300, 300);
paint300(bufferImage.getGraphics());
int width = getWidth();
int height = getHeight();
CropImageFilter crop =
new CropImageFilter((300 - width)/2, (300 - height)/2 , width, height);
FilteredImageSource fis = new FilteredImageSource(bufferImage, crop);
Image croppedImage = createImage(fis);
g.drawImage(croppedImage, 0, 0, null);
}
The new classes are from from java.awt.image.*.
I didn't test this code. It's just to send you in the right direction.
if you want to painting Custom paint then look for JLabel or JPanel and including Icon/ImageIcon inside, simple example about that
import java.awt.*;
import javax.swing.*;
public class MainComponentPaint extends JFrame {
private static final long serialVersionUID = 1L;
public MainComponentPaint() {
setTitle("Customize Preffered Size Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void display() {
add(new CustomComponent());
pack();
setMinimumSize(getSize());
setPreferredSize(getSize());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
setVisible(true);
}
});
}
public static void main(String[] args) {
MainComponentPaint main = new MainComponentPaint();
main.display();
}
}
class CustomComponent extends JComponent {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
for (int i = 0; i < Math.max(w, h); i += 20) {
g.drawLine(i, 0, i, h);
g.drawLine(0, i, w, i);
}
}
}
Not an expert, but you could just scale the Graphics2D object (the passed Graphics is in fact a Graphics2D instance), where the x and y ratios are the ratios of the fixed size you chose to draw and the actual size of the frame.
See http://download.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#scale%28double,%20double%29
You could do this with some math.
public void paint(Graphics g){
int height = 100;
int width = 100;
int x = (this.getWidth() / 2) - (width / 2);
int y = (this.getHeight() / 2) - (height / 2);
g.setColor(Color.BLACK);
g.fill3DRect(x, y, width, height, true);
}
Or if you wanted to keep the width and height of the box with the same proportion, use int width = this.getWidth() / 3; and int height = this.getHeight() / 3.
The other option is to use Graphics2D.scale(), as JB pointed out, the passed Graphics object is actually a Graphics2D object.