Animate player sprite movement with pathfinding

Get help using Construct 2

Post » Sat Oct 01, 2011 6:09 pm

I am trying to use the pathfinding behavior, and am having problems trying to get the player sprite animation working. I think it is fairly simple, I'm just ignorant of how to get it to work.

Here is the capx I've been working on: http://dl.dropbox.com/u/38038537/TestPathfinding.capx

Here are the steps I think should happen:

1. Initialize pathfinding
2. Generate pathfinding based on a mouse click
3. Move player sprite to target location along path

If you have reference to a demo I've missed, or can just tell me what I am doing wrong, I would greatly appreciate it.

Please let me know if you need any more information.

Thanks!Wastrel2011-10-01 18:12:59
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Sat Oct 01, 2011 7:01 pm

There isn't yet a reference to how to move a sprite accordingly to a path because, truth to be told, I haven't yet implemented it in a way I'm satisfied with.

Anyway, you have the correct logic idea. The "flaw" in your capx is that you make all the movements through the path in a single tick (as you are using a "Repeat loop").

So far, I've followed the same idea, just letting the movement itself handled by an 8direction behavior.
I simulate right, left, up or down arrows pressure by checking the actual distance between the sprite and the PathFinder.getCtXPathList (and getCtYPathList).

Pseudo-code:
If Sprite is on right of NextCellInPathList => Simulate left key.
If Sprite is on left of NextCellInPathList => Simulate right key.
Etc...

As I said, I yet have to implement a satisfying way of incrementing (in your case) indexPath when the sprite is rightly positionned on the next cell of the pathlist.

Instead of going through emulation of the 8dir behavior, you might as well calculate yourself the distance and angle to go (through system math expressions) and apply some bullet behavior, or even move it in events each tick.

That's the tricky part where I don't have a releasable solution yet. Let me know if you can come up with something.Kyatric2011-10-01 19:02:10
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Sun Oct 02, 2011 4:04 pm

It seems to me that you would want to keep the path-finding and movement behaviors separate. You would use the path-finding behavior to generate a path list, then use a movement behavior to execute the path list.

Looking at the bullet behavior, for instance, behind the scenes, it is generating its own linear path, then following that path every tick.

What we would need is a modified movement behavior which could accept a path list as a parameter. That path list could be from any source, whether it be from the path-finding behavior, or a more linear function.

I am working on building something using events, and i will post the capx once I have something working. Iam still trying to wrap my head around the JavaScript SDK and building plug-ins, so I will have to do it the hard way for now.Wastrel2011-10-02 16:06:13
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Mon Oct 03, 2011 9:17 pm

http://dl.dropbox.com/u/36472942/construct/forumhelp/Automated%20movement.capx
I just found again this example of an automated movement I had made.
It could be quite quick to adapt it so that target is the NextCell position and that it gets automaticly updated.

I don't want to include automated movement within the pathfinder as the movement the user wants will depend from a project to another. The pathfinder provides a path, it's up to the user to walk it the way he like best. (consider people wanting grid movement, those who will want a smooth rotating-like vehicle movement, those who will want something more floating, etc...
Hard to make everybody happy with a single automated behavior)
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Tue Oct 04, 2011 1:14 am

I will check out the capx when I get home.

I totally agree that the path finding and movement should be completely separate behaviors. I'm trying to come up with a movement pseudo-behavior (i.e. all event driven) that will accept an array of positions, but I think I have a basic mis-undertstanding about how the event sheet is processed.

Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Thu Oct 06, 2011 6:07 am

Made Path Array to store points whenever you click. The textbox shows you the first 4 points, but you can have as many as you want.... couldn't get sprite to follow them though.

https://www.dropbox.com/s/wla75aaygef4dht/PathStuff.capxScottP2011-10-06 06:08:23
B
20
S
9
G
2
Posts: 15
Reputation: 2,594

Post » Fri Oct 07, 2011 6:25 pm

@ScottP: The link isn't working for me.
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Tue Oct 11, 2011 9:46 pm

Sorry! It's my first time sharing from Dropbox. Let's try again...

http://db.tt/5Z4p9vww
B
20
S
9
G
2
Posts: 15
Reputation: 2,594

Post » Wed Oct 12, 2011 3:08 pm

@ScottP: thanks for the example

I was finally able to find time to work on the mouse-click movement using Kyatric's path-finding plug-in.

Here is the example capx:

http://dl.dropbox.com/u/38038537/TestPathfinding-00.capx

There are still some issues I need to resolve:

- I need to refine the setting of the player sprite angle towards the target.
- I need to refine the final movements of the player sprite to the target. The final position of the sprite always seem to be a bit off of the actual target coordinates.
- It appears that when setting the path-finding obstacle, it only recognizes the final obstacle set in the event sheet. I think I may not be setting the obstacle list correctly. In the example, you can see that the only obstacle recognized is the horizontal wall. The player sprite will walk right through the vertical wall and the column obstacles.

Overall, it seems to work pretty well. I am open to any suggestions for making it more elegant or precise.

Also, I am still getting up to speed on JavaScript and the SDK, but I am wondering if this type of movement using a path list would translate well to a plug-in, or if it is better handled through events.

Thanks!Wastrel2011-10-12 15:09:11
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Wed Oct 12, 2011 4:06 pm

[QUOTE=Wastrel]There are still some issues I need to resolve:
- I need to refine the setting of the player sprite angle towards the target.
- I need to refine the final movements of the player sprite to the target. The final position of the sprite always seem to be a bit off of the actual target coordinates.
[/quote]
This is because the pathfinding is made as a grid like in the example with S and D. When you drop the Destination icon wherever on a cell, it is automaticly repositionned to its center. That's because the center of the cell IS the target, its X and Y position, its center.
The pathfinder will locate the X and Y of the target object and will check in what cell this position actually is (so if your target object is put in between 4 cells at the same time, having bits of it being present in 4 cells, only the cell which contains the actual X and Y coordinates of the object will be set as destination of the pathfinder.)
Same principle goes for the Source object. It's X and Y defines the starting cell.
I may be unclear, I'll document it more in the next version of the pathfinder.

[QUOTE=Wastrel]- It appears that when setting the path-finding obstacle, it only recognizes the final obstacle set in the event sheet. I think I may not be setting the obstacle list correctly. In the example, you can see that the only obstacle recognized is the horizontal wall. The player sprite will walk right through the vertical wall and the column obstacles.[/quote]
This is possibly a bug in the behavior. I was thinking about it lately, did not run any test, I'll look more into it.

A workaround that could work for now would be to have a single object "Wall", set as obstacle.
Its animation would contain different frames, holding the several textures (WallHorz, WallVert and WallColumn).
On startup you assign the right frame to the object (you might "mark" the instances with an instance variable and display the correct frame for the setted value on start of layout).

Also be sure to keep consistency in the size of the objects. Your player is 80 pixel of height, be sure that your obstacles are also 80X80px.
For the obstacle, it's not taking only the X,Y position into account, but the bounding box.
Meaning that on the path calculation, if any bit of the bounding box of the obstacle is colliding/overlapsing a cell, the cell will be marked as unwalkable.
I'll document on that too.

If you don't mind, I'll use this example of yours (and work on it for tests and improvements) for the future releases of the behavior.

I'll also try to give the option to choose between bounding box collision and polygon collision mask, even if it won't change a lot of things. The slightest bit of obstacle being on a cell will still mark the full cell as unwalkable.

[QUOTE=Wastrel]Overall, it seems to work pretty well. I am open to any suggestions for making it more elegant or precise.

Also, I am still getting up to speed on JavaScript and the SDK, but I am wondering if this type of movement using a path list would translate well to a plug-in, or if it is better handled through events.

Thanks![/QUOTE]

Having an automated movement, moving the sprite from grid cells to grid cells might be handy as a behavior. Associating both the PF and this should give a "fully" fonctionnal automated PF behavior in the end.
I had sources at a time that were a first implementation of this, but it got lost somewhere in my backups.
I'll check on those too, but I will focus first on the PF as we obviously can handle/workaround the movement via events for now as demonstrated through this topic.

Thanks for the working on the behavior.Kyatric2011-10-12 16:07:27
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Next

Return to How do I....?

Who is online

Users browsing this forum: Matthew de and 27 guests