More than 8 character facing directions?

Started by Nurz, Tue 05/09/2023 10:06:51

Previous topic - Next topic

Nurz

Hello. So, is there a way to create more character walking directions than 8? And would that impact the performance a lot?

In the views section in the editor, every new loop after 7 is generated with a "no special purpose" tag. Can I assign code to these somehow, as to create 8 more diagonal loops (e.g. in degrees such as 22,5 and 67,5) for a total of 16? Even better, are there plans to support this directly in the editor itself?

Khris

#1
It's not supported by AGS but it wouldn't impact the performance.

You can in theory code this: you would have to
1) make the actual player character invisible
2) track where they move
3) manually display a second character with the appropriate sprite in their place

2) means continuously reading their coordinates and determining the walking direction from that using inverse trig functions to figure out the loop, and also how far they moved to increment a walk cycle frame counter.

It's possible, and in high-res game it will probably work reasonably well.

Edit:

Put this in the main script of a new module and add a label called lblDebug to a persistent GUI.
This script tracks the movement and displays loop and frame. It has to be adjusted precisely to the walkcycle frames to look though. Again, only a high-res game will allow tracking on a scale that makes it reasonable to do this.

Code: ags
float prev_x, prev_y;

void on_event(EventType event, int data) {
  if (event == eEventEnterRoomBeforeFadein) {
    prev_x = IntToFloat(player.x);
    prev_y = IntToFloat(player.y);
  }
}

int loop, frame;
float total_dist;

int delay;

void repeatedly_execute_always() {
  delay++;
  if (delay < 4) return;
  delay = 0;
  float x = IntToFloat(player.x);
  float y = IntToFloat(player.y);
  float dx = x - prev_x;
  float dy = (y - prev_y) * 1.8; // perspective causes foreshortening, so we compensate
  float dist = Maths.Sqrt(dx * dx + dy * dy);
  if (dist == 0.0) {
    frame = 0;
    total_dist = 0.0;
  }
  else {
    float a = Maths.ArcTan2(dy, dx);
    loop = FloatToInt((a * 8.0 / Maths.Pi) + 7.5, eRoundNearest) % 16;
    total_dist += dist;
    frame = FloatToInt(total_dist / 7.0, eRoundDown) % 10 + 1; // 7 pixels per frame, 10 frames per loop
  }
  lblDebug.Text = String.Format("Loop: %d, Frame: %d, dx: %0.2f, dy: %0.2f", loop, frame, dx, dy);
  
  prev_x = x;
  prev_y = y;
}

Nurz

#2
Thank you, Khris. As I'm not so acquainted with writing code from scratch, you surely help a lot.

So, trying to translate your guidance into how I see things in AGS 4.0, I created a new script module (left it as NewScript) and a new GUI (as lblDebug), then I set the GUI's PopupStyle property to Always Shown (persistent). It throws a complition error "NewScript.asc(36): Error (line 36): '.Text' is not a public member of 'GUI'. Are you sure you spelt it correctly (remember, capital letters are important)?" Also, by adjusting it to the walkcycle frames, in which part of the code will I have to copy the frame number?

I'm throwing in a feature request in the editor forum too, but I'd like to do this by code as well. It surely helps to learn.

Snarky

I believe there is a drawback with @Khris's code, in that characters walking along a straight line can end up flickering, flipping back and forth between two angles, if the dy/dx ratio varies per step (as it can do due to integer rounding). Ideally we should have access to the list of waypoints calculated by the pathfinding algorithm, so we could know the angle of the path segment as a whole.

Crimson Wizard

Quote from: Nurz on Tue 05/09/2023 14:58:42It throws a complition error "NewScript.asc(36): Error (line 36): '.Text' is not a public member of 'GUI'. Are you sure you spelt it correctly (remember, capital letters are important)?"

You need to create a label on GUI, and assign a text to that label.

Quote from: Snarky on Tue 05/09/2023 15:34:30Ideally we should have access to the list of waypoints calculated by the pathfinding algorithm, so we could know the angle of the path segment as a whole.

There's a feature request for pathfinding API:
https://github.com/adventuregamestudio/ags/issues/1979

Nurz

Quote from: Crimson Wizard on Tue 05/09/2023 15:45:04You need to create a label on GUI, and assign a text to that label.

Got it, and it's now working. Appreciate it.

Khris

Quote from: Snarky on Tue 05/09/2023 15:34:30I believe there is a drawback with @Khris's code, in that characters walking along a straight line can end up flickering

Yeah, that's why I implemented the delay and emphasized that the resolution should be high. Without the delay this "algorithm" is almost useless.
Given how often people want to customize how walking works it would be a real boon if we had more low-level access to the pathfinding.
Years ago there was a FoA sequel in the works; they used vectors to define walkable areas; that way you could have interpolated lighting, scaling and areas on top of each other. It would be amazing for AGS 4 to support this.

Snarky

#7
The FOA2 engine is still available here, with screenshots of the editor. Yeah, I always loved that aspect of it, though it's essentially bolting a simple 3D level editor projected on top of a 2D background.


SMF spam blocked by CleanTalk