Libgdx x and y difference - java

In the picture below you can see that I have drawn a black circle into the screen and in the code I have tried to get the black circle to match the coordinates of the clear circle which is my physics body which is completely working.
My problem is when I move the clear circle around my map the black circle stays there in the corner. The black circle moves a little about a few pixels so it seems its mmoves around a bit but very scaled down and I don't know why. Thanks.
public void update(float dt){
handleInput(dt);
world.step(1 / 60f, 6, 2);
player.update(dt);
camX();
gameCam.update();
renderer.setView(gameCam);
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.begin();
game.batch.draw(ball,player.getX() , player.getY(), 70, 70);
game.batch.end();
}
public void camX(){
gameCam.position.x = gamePort.getWorldWidth() / 2;
if(player.b2Body.getPosition().x >= gamePort.getWorldWidth() / 2){
gameCam.position.x = player.b2Body.getPosition().x;
}
}
public class Character extends Sprite {
public World world;
public Body b2Body;
public float x;
public float y;
public Character(World world){
this.world = world;
defineMario();
}
public void update(float dt) {
setPosition(b2Body.getPosition().x,b2Body.getPosition().y );
}

You set the Batch's projection matrix to the UI's camera for UI drawing, but you never change it to the gameCamera's projection before drawing your game elements. Before the game.batch.begin(); line you need to add game.batch.setProjectionMatrix(gameCamera.combined);.
By the way, it doesn't make sense for Character to extend Sprite, since you don't use Character for drawing anything, only for tracking a position.

Please try this :
public class Character extends Sprite {
public World world;
public Body b2Body;
public float x;
public float y;
public Character(World world){
this.world = world;
defineMario();
}
public void update(float dt) {
//so the sprite and body will have the same center
setPosition(b2Body.getPosition().x-getWidth()/2, b2Body.getPosition().y- getHeight()/2 );
setCenterOrigin(); // to handle when body does a rotation
}
please, tell me if it works or not in comment
Good luck

Related

LibGdx update with deltatime is not precise

I'm new in creating game.
I have several boxes that move from top to bottom of screen and i need to make 1 unit distance between them.
Every thing works fine except the distance is not precise and i think the deltatime cause this problem
This is how i move the boxes :
objX -= deltatime * speed;
Update
time += deltatime :if(time >= 3.0f) spawn
Im using libgdx
You can schedule your task in this way :
Array<Sprite> sprites;
Texture texture;
SpriteBatch batch;
float speed=35;
#Override
public void create() {
sprites=new Array<Sprite>();
texture=new Texture("badlogic.jpg");
batch=new SpriteBatch();
Timer.instance().scheduleTask(new Timer.Task() {
#Override
public void run() {
spawn();
}
},1,2);
}
private void spawn(){
Sprite sprite=new Sprite(texture);
sprite.setSize(50,50);
sprite.setPosition(Gdx.graphics.getWidth()/2,0);
sprites.add(sprite);
}
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float delta=Gdx.graphics.getDeltaTime();
batch.begin();
for (Sprite sprite:sprites){
sprite.setY(sprite.getY()+delta*speed);
sprite.draw(batch);
}
batch.end();
}
After a lot of struggling i came up with an idea which is
instead of moving boxes and spawning them by deltatime , make boxes static and move camera over them!
So i just move camera here
public void render(float delta) {
posCameraDesired.y += 2000.0f * Gdx.graphics.getDeltaTime();
guiCam.position.lerp(posCameraDesired, 0.1f);
SemiRender();
}
and i have static Y that i'll update after each spawn
lastY = 10 + lastY + ((width / 4) / 2) * 3;
and some other logic to recycle them when they passed the viewport.
and every one is happy!

Java - LibGDX - Problems with the process of rendering a Tiled map

The problem
I cannot seem to be able to get Tiled maps to render properly. I am using LibGDX as a library for loading the map (Release 1.6.0).
Video demonstration
I have created a video to show you the actual problem and make things easier by skipping the whole process of explaining it. Here is a link to it.
The code I've used
protected Level level;
protected OrthogonalTiledMapRenderer mapRenderer;
protected OrthographicCamera camera;
protected TiledMap map;
protected MainGameLoop game;
protected SpriteBatch batch;
private BitmapFont font;
private int w, h;
public Level1(MainGameLoop game) {
this.game = game;
}
#Override
public void show() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
int CAMERA_WIDTH = 800;
int CAMERA_HEIGHT = 450 * (w / h);
camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
camera.setToOrtho(false);
camera.update();
map = new TmxMapLoader().load("maps/map1.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(map);
Gdx.input.setInputProcessor(this);
font = new BitmapFont();
font.setColor(Color.BLUE);
batch = new SpriteBatch();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
mapRenderer.setView(camera);
mapRenderer.render();
batch.begin();
font.draw(batch, "Camera zoom: " + camera.zoom, 40, 40);
batch.end();
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
#Override
public void dispose() {
mapRenderer.dispose();
map.dispose();
background.dispose();
Gdx.input.setInputProcessor(null);
}
#Override
public boolean scrolled(int amount) {
camera.zoom += amount;
camera.update();
return true;
}
// Here go the rest of the methods, such as pause, resume, hide, keyDown, keyUp, keyTyped, touchDown, touchUp, touchDragged & mouseMoved.
Solutions I've tried
I have tried using different numbers for the camera's x and y with no luck. I have also tried tranlating the camera to the proper position (hardcoded it), as well as using another map (different tilemap and dimensions) but that did not work either.
Conclusion
I can't seem to find a way to fix this problem. Any help is much appreciated. Thank you very much.
Quick introduction
Ok after a good while, I managed to solve this matter by hardcoding some stuff. But it works properly, so I am happy with it.
What I did to solve the problem
First of all, I found out the exact number I had to use to scale up my Tiled map, which is this number: 3.125f .
Then, instead of using pixels for the camera, I used my own units (something I should have done from the the first moment).
After doing those two things, I noticed that the map was zoomed in a lot. So, by using the scrolled method from the InputProcessor, I managed to find the exact number the map had to be "unzoomed".
I also found out that if I call the setToOrtho(false) method from the OrthographicCamera object, it zooms the map in 19 times for some weird reason. If that method does not get called, the map is zoomed in only 1 time
The code I am currently using
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
final float WIDTH = 8000;
final float HEIGHT = 4500;
final float num = 3.125f;
#Override
public void show() {
tiledMap = MapLoader.realm1_level1;
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, num);
camera = new OrthographicCamera(WIDTH, HEIGHT);
Gdx.input.setInputProcessor(this);
camera.zoom += 1f;
camera.update();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
}
// This method was just used for testing to see where
// the map was or should have been placed.
#Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.LEFT)
camera.translate(-32, 0);
if (keycode == Input.Keys.RIGHT)
camera.translate(32, 0);
if (keycode == Input.Keys.UP)
camera.translate(0, 32);
if (keycode == Input.Keys.DOWN)
camera.translate(0, -32);
if (keycode == Input.Keys.NUM_1)
tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
return true;
}
#Override
public void resize(int width, int height) {
camera.position.set(WIDTH, HEIGHT, 0);
camera.update();
}
#Override
public boolean scrolled(int amount) {
camera.zoom += amount;
camera.update();
return true;
}
#Override
public void dispose() {
tiledMap.dispose();
}
// And here go the rest of the methods that come from the
//Screen and the InputProcessor interfaces.
Notes
The numbers I used for the WIDTH and the HEIGHT variables work properly, strictly with tiled maps that their width is 80 tiles, and their height is 45 tiles.
If you are facing the same problem, you'll have to find the appropriate number to scale your map up/down, depending on the unit numbers you're using. Trial and error is your only guide. If we ignore StackOverflow of course.

LibGDX Bouncing ball

I want to create an infinite bouncing ball. For now I'm just trying to make the bouncing on Y (up & down).
This is my GameWorld class, you can see there is a method collides to detect the collision but How to make that "circle" go up?
public class GameWorld {
private Circle rond;
private Rectangle rect;
public GameWorld(int midPointY) {
rond = new Circle(100, midPointY - 5, 5);
rect = new Rectangle(0, 200, Gdx.graphics.getWidth(), 5);
}
public void update(float delta) {
if(collides(rond)){
}else
rond.y++;
}
public boolean collides(Circle rond) {
if (rect.y < rond.y + (rond.radius)*2) {
return (Intersector.overlaps(rond, rect));
}
return false;
}
public Circle getRond() {
return rond;
}
public Rectangle getRect() {
return rect;
}
}
Of course, I have another class GameRenderer that rendere these two objects
Typically with any kind of physics you want to store the speed of an object as well as its position. That way, each time though your update loop you just have to change the y position by the y speed. When the collision occurs, all you need to do is calculate the new yspeed and update rond.
public void update(float delta) {
if(collides(rond)){
rond.yspeed = -rond.yspeed;
}else{
rond.y+=rond.yspeed*delta;
}
}

libgdx - moving an object

So, I'm farely new to programming this is my first game, and I have been working on it for a while, for one of my classes and I'm creating a pong game just changed it up a bit and I'm having trouble moving my paddle from the left to the center everytime I change the the percentage of the width of the screen to move it for some reason it elevates the paddle by all almost the same. Also everything is controlled by the width for some bizarre reason its almost like the field.height has no effect on moving it? I'm stuck right here and I'm so close to finishing it I'm trying to lower the paddle to the bottom center? any help would really be appreciated and hopefully this makes sense.
here is the field
#Override
public void create () {
field.set(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
fieldLeft = field.x;
fieldRight = field.x + field.width;
fieldBottom = field.y;
fieldTop = field.y + field.height;
and here is the paddle movement
private void resetRectangle(){
paddle1.move(field.x + (field.width * .5f, 0);
paddle2.move(0,0);
}
and the class
public class Paddle extends GameShare{
private ShapeRenderer paddleRenderer;
protected Paddle() {
super(100, 32);
}
public void paddleCreate(){
paddleRenderer = new ShapeRenderer();
}
public void paddleRender(float dt){
paddleRenderer.begin(ShapeType.Filled);
drawPaddle(dt);
paddleRenderer.end();
}
private void drawPaddle(float dt) {
paddleRenderer.rect(this.getX(), this.getX(), this.getWidth(), this.getHeight());
paddleRenderer.rect(this.getX(), this.getX(), this.getWidth(), this.getHeight());
}

Setting the applcation to use "y-down" in LibGDX

http://badlogicgames.com/forum/viewtopic.php?f=11&t=2447
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html
There's hundreds of other links I could show you that I've looked at, but it's just not worth it because they all say the same thing.
public class InGame implements Screen {
SpriteBatch batch;
GameWorld world;
OrthographicCamera camera;
#Override
public void show() {
batch = new SpriteBatch();
world = new GameWorld();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
world.render(batch, delta);
batch.end();
}
}
What am I doing wrong? WHY is my world still being rendered with the 0,0 being at the bottom right. The math behind this while trying to work on my Tile-System is driving me absolutely insane.
World->Render
public void render(SpriteBatch batch, float delta) {
for(int xAxis = 0; xAxis < worldColumns; xAxis++) {
for(int yAxis = 0; yAxis < worldRows; yAxis++) {
tiles[xAxis][yAxis].render(batch, delta);
}
}
}
WorldTile->Render
public void render(SpriteBatch batch, float delta) {
myShape.begin(ShapeType.Filled);
myShape.setColor(tileColor);
myShape.rect(pos.getX(), pos.getY(), TILE_WIDTH, TILE_HEIGHT);
myShape.end();
}
The "pos" is the Position(x, y) that was passed in the World class.
If you are drawing a Sprite or TextureRegion using your SpriteBatch the code should work fine. However, you pass your SpriteBatch 'batch' all the way down to WorldTile.render and never use it?! Instead you use myShape which I assume is a ShapeRenderer. You need to set the projection matrix for the ShapeRenderer as well otherwise it will draw 'upside-down'.
Try calling myShape.setProjectionMatrix(camera.combined); before you use your myShape.
Its probably best to declare and initialise myShape in your InGame class, usemyShape.setProjectionMatrix(camera.combined);, and then pass myShape down to tiles.render() as you did with your SpriteBatch.
Hope this helps.

Categories