Isometric Movement

Started by Vincent, Fri 29/04/2022 12:08:19

Previous topic - Next topic

Vincent

Good day to all Agser,
For a testing purpose I was trying to code a really simple isometric movement, as it showed in this video:

https://streamable.com/9vr9qn

By now I've got a really basic approach of it by simply moving the x and y of the player: (sorry for the embarrassing code below)

Spoiler

Code: ags

function repeatedly_execute()
{
  if (IsKeyPressed(eKeyW)) //up
  {
    if (!IsKeyPressed(eKeyS) && !IsKeyPressed(eKeyA) && !IsKeyPressed(eKeyD))
    {
      if (GetWalkableAreaAtRoom(player.x+2, player.y-1) == 1)
      {
        player.y -= 1;
        player.x += 2;
        player.Loop = 3;
      }
    }
  }

  if (IsKeyPressed(eKeyS)) //down
  {
    if (!IsKeyPressed(eKeyW) && !IsKeyPressed(eKeyA) && !IsKeyPressed(eKeyD))
    {
      if (GetWalkableAreaAtRoom(player.x-2, player.y+1) == 1)
      {
        player.y += 1;
        player.x -= 2;
        player.Loop = 0;
      }
    }
  }
}

[close]

While this seems to be looking barely 'good' I notice that by doing so the sprite doesn't move in a streight line, which looks a little weird. I'd like to ask those who have more experience if is it possible to make such a movement without coding a grid or a complex pathfinding? Also with this approach I can't find a proper way to implement the built-in command 'Walk' cause when I do that the walk animation animate so fast. Do you have any suggestions for the matter?

eri0o

Would changing the coordinate system be acceptable? Essentially, have a dummy character move in a regular top down view of the walkable area and then project the point to the isometric space and move the character there.

How are you making the room, is it just a regular AGS room with background?

Vincent

Thanks eri0o, well I guess everything is acceptable while having something similar to the video, but I can't quite understand what you mean by your idea of the dummy character... which seems interesting. Btw yes, I'd like to have regular ags backgrounds so having something simple avoiding to code a grid or something complex

Khris

#3
Here's basic iso walking inside walkable areas:

Code: ags
Point* up, left;
eKeyCode keyUp, keyDown, keyLeft, keyRight;

int isoFactor = 2; // slope: 1/2
bool disableNSEW = true; // diagonal walking only?

void game_start() {
  up = new Point();
  left = new Point(); //                                            U   R
  up.x = isoFactor; // set to -isoFactor for alt. iso directions:     X
  up.y = -1; //                                                     L   D 
  left.x = up.y * isoFactor;
  left.y = -up.x / isoFactor;
  // set keys
  keyUp = eKeyW;
  keyLeft = eKeyA;
  keyDown = eKeyS;
  keyRight = eKeyD;
}

int currentDir; // 0 = not walking

void repeatedly_execute_always() {
  int lr = IsKeyPressed(keyRight) - IsKeyPressed(keyLeft); // -1, 0 or 1
  int ud = IsKeyPressed(keyDown) - IsKeyPressed(keyUp);    // same
  int newDir = ud * 3 + lr;
  
  if (disableNSEW && lr && ud) newDir = currentDir;
 
  if (newDir == 0) player.StopMoving();
  else if (newDir != currentDir) {
    Point* vec = new Point();
    if (lr) { vec.x -= lr * left.x; vec.y -= lr * left.y; }
    if (ud) { vec.x -= ud * up.x;   vec.y -= ud * up.y;   }
    if (lr * ud) player.ChangeView(VIEWNORMAL);
    else {
      if (vec.y < 0) player.ChangeView(VIEWUP);
      if (vec.y > 0) player.ChangeView(VIEWDOWN);
    }
    player.WalkStraight(player.x + vec.x * 5000, player.y + vec.y * 5000, eNoBlock);
  }
  currentDir = newDir;
}

eri0o

Vincent, now I understand you doesn't intend to use pathfinding. Does your character has 8 directions? Have you tried my controlz module? If the character has 8 dir, using controlz module should give a somewhat alright walking around.

Vincent

@Khris Wow this is just amazing! I didn't even think it was possible to do such a movement without having a grid at all. Also you managed to write this code even in a very short time which I would never have been able to write it to be honest. Thanks a lot, I have tested the code and it works very well. The only small thing now I have to try to get the walking loops displayed correctly as in this picture:



@eri0o thanks for your suggestions, my intention was to have a very simple isometric movement without coding a grid or a pathfinding at all, so basically have something very simple (but even looking at khris code this wasnt so simple at all..) btw the character have only 4 directions, however I never heared about the controlz module before, I might have a look at it just in case;)

eri0o

Controlz is this module: https://www.adventuregamestudio.co.uk/forums/index.php?topic=57427.0

It will only work if your character has 8 dir views though.

And I didn't meant coding the pathfinding, I meant conceptually, in your game, does the player control the character using a mouse that clicks and the character walks following a path it figures by itself, or does the player uses a keyboard or something else to control each movement of the character directly, without relying on it finding a path.

Crimson Wizard

#7
Quote from: Vincent on Fri 29/04/2022 12:50:13but I can't quite understand what you mean by your idea of the dummy character... which seems interesting.

Trying to explain, the basic idea of this is two have two "worlds":
1. an imaginary (not shown on screen) top-down world, where you do all the movement calculation, pathfinding, etc, and get the positions of all objects;
2. a visible isometric world, where you do no calculations but arrange objects and characters.

Between these worlds there's a coordinate conversion. So you calculate movement and new positions for all objects in the first top-down world, where it is easy to do; then pass each object's coordinate through some function that converts them into isometric coordinates. Then use these final isometric coordinates to place objects in AGS room.

Khris

#8
I added two lines to my code near the end where I change the player's view depending on the direction.
You need to set up two views for the player, VIEWUP and VIEWDOWN. Then put the walking animation for up-left and up-right in the left/right loops of VIEWUP, and the walking animations for down-left and down-right in the left and right loops for VIEWDOWN.
(For eight-directional movement you would need the original view, too, and more code changes.)

Vincent

#9
@eri0o Thanks a lot for sharing your module, I will take it right now, it might come handly in the future. Conceptually speaking the player should use a keyboard to control the character without relying on it finding a path at all.

@Crimson Wizard Thanks a lot Crimson for clarification, I think I've got what eri0o meant to say now. While my intention was to have something very simple, yesterday I was watching this video on yt which remind me of what you wrote about the coordinate conversion so far.

@Khris I have implemented everything as you said and it's all working perfectly fine, thank you very much again. I am quite unsure how many coffees I should donate, but a whole coffee shop will be fine soon or later!;)

SMF spam blocked by CleanTalk