[Java] LibGDX Isometric map - movement of an entity

Started by
3 comments, last by ben berizovsky 6 years, 8 months ago

Hello there, I am new to the forum and in game development in general!

I am currently building a simulator for some game, and I am having a few problems already, and I am using the libgdx framework.

 

I have an isometric map drawn, where each tile is a spot that only one entity can be at. 

An entity can either move left, right or just forward in a straight line.

 

Now this is what it looks at the moment:

 

8e24b32c523d6749271a38c48291a654.png

 

As you can see, my ship entity is located at 1,1.

I want to start with adding a function that moves the ship in a straight line, so I need to move this ship to 1,2.

 

So I have a Vector2 that represents the ship's position, I made a dummy one in the local class where it paints entities, and the map and it's set to isometric coordinates of 1,1:


r = new TextureRegion(texture, location.getX(), location.getY(), location.getWidth(), location.getHeight());
local = new Vector2(getIsometricX(1,1, r), getIsometricY(1,1, r));

 

"r" is my texture region, which is my sprite image off the spritesheet.

 

Now I have set a target position to 1,2:


target = new Vector2(getIsometricX(1,2, r), getIsometricY(1,2, r));

 

So now my question is, how can I make that ship move to the target position? If i add 1 to x and y every tick, it will just move too much to the right.

This is how I paint everything:

 


@Override
public void render() {
    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    // Render the map
    renderSeaBattle();

    // Render ships
    renderEntities();

    batch.end();
}

 

And the map painting:

 


private void renderSeaBattle() {

    // The map tiles
    GameTile[][] tiles = map.getTiles();

    for (int i = 0; i < tiles.length; i++) {
        for(int j = 0; j < tiles.length; j++) {

            GameTile tile = tiles[j];
            Texture texture = tile.getTexture();

            int x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) -texture.getWidth() / 2;
            int y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) -texture.getHeight() / 2;

            batch.draw(texture, x, y);

        }
    }
}

 

And the entities:


private void renderEntities() {
    batch.draw(r, local.x + location.getOffsetx(), local.y + location.getOffsety());
}

location is an instance that contains the offset position for that specific sprite, because not all sprites are the same size, so to center it on the tile, each one has set offsetX and Y.

And this is my coordinate conversion methods:


public int getIsometricX(int x, int y, TextureRegion region) {
    return (x * GameTile.TILE_WIDTH / 2) - (y * GameTile.TILE_WIDTH / 2) - (region.getRegionWidth() / 2);
}

public int getIsometricY(int x, int y, TextureRegion region) {
    return (x * GameTile.TILE_HEIGHT / 2) + (y * GameTile.TILE_HEIGHT / 2) - (region.getRegionHeight() / 2);
}

 

After I do the straight line, how can I create left/right movements in curves?

Thanks!

 

 

Advertisement

You have to treat this more or less like you'd do for an animation.

ie, you have to express the movement in some period of time. You know where the ship is, you know where it should go, you have to determine in how much time that trip would look pleasing, and then interpolate the position each frame until it reaches it.

Say that you're moving one cell per second, and you want to move two cells, so you'd need to interpolate the position of the ship between cell A to cell B for two seconds worth of game ticks.

This can get more complex if you say, want the ship to steer a bit if maneuvering, or you want the ship to start slow and accelerate, then stop gradually until it reaches the destination cell.

You could google around for "steering behavior".

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

6 minutes ago, TheChubu said:

You have to treat this more or less like you'd do for an animation.

ie, you have to express the movement in some period of time. You know where the ship is, you know where it should go, you have to determine in how much time that trip would look pleasing, and then interpolate the position each frame until it reaches it.

Say that you're moving one cell per second, and you want to move two cells, so you'd need to interpolate the position of the ship between cell A to cell B for two seconds worth of game ticks.

This can get more complex if you say, want the ship to steer a bit if maneuvering, or you want the ship to start slow and accelerate, then stop gradually until it reaches the destination cell.

You could google around for "steering behavior".

 

Hello there!, thank you for your reply.

I'd like to state that the movement speed is not something that a user can control. User clicks on a button "forward" and that moves the ship a block forward, with motion. 

Take a look at this video: 

 

I am building a simulator for the exact game.

Basically at the end of this project of mine, I want to approach movements like in this video. I have all sprite rotations packed in an orientations object, that I just can increment the index to go to the next rotation.

 

But I really want to start with the forward, it is very simple compared to left/right movements. Move a ship from local position to target position. 

 

This sounds easy, but I really have a problem seeing it in isometric map, while on a flat map you simply just increase X or Y only, but there you have to move a little in x, and a little in y every time.

 

So my question is, how exactly?

 

I am b

I noticed that if I do the movement simply y += 0.1f on screen coordinates, and convert to isometric, it works great!

This topic is closed to new replies.

Advertisement