collinhover / impactplusplus

Impact++ is a collection of additions to ImpactJS with full featured physics, dynamic lighting, UI, abilities, and more.
http://collinhover.github.com/impactplusplus
MIT License
276 stars 59 forks source link

Can't get pathfinding to work? #99

Closed Pattentrick closed 10 years ago

Pattentrick commented 10 years ago

Hi there,

i have some basic understanding problems with pathfinding in impact++ and cant get it to work properly. I have a player entity that inherits from the player abstractities, which inherits from the character abstractities.

If i click anywhere in the level, the player should move to exact this position. Like in point and click adventures or in rts games. By the way, this is a top down game with an empty level and without any obstacles and other entities right now. I have tried it this way on my player entity:

handleInput : function(){

            if ( ig.input.pressed('click') ) {

                // get path
                this.path = ig.pathfinding.getPathToPoint(
                    ig.game.getPlayer(),
                    ig.input.mouse.x + ig.game.screen.x,
                    ig.input.mouse.y + ig.game.screen.y
                );

                // define settings
                var settings = {
                    simple : false,
                    unsafe : true,
                    avoidUnderground : false,
                    searchDistance : 200,
                    avoidEntities : false
                };

                // call move method
                this.moveToPath( this.path, settings );

            }
}

However this will move the player by 3-4 pixel and stops immediately. It seems that the pathfinding stops after a few frames. So i started debugging the character.js abstractities to get a better understanding why my approach fails.

At line 1346 the moveToPath method gets called without the settings parameter. In this case the method will use the settings stored in this.movingToSettings as defined at line 1262:

settings = settings || this.movingToSettings;.

But the movingToSettings property is undefined and i cant figure out where the property is set. So i added the movingToSettings property to my player entity, with the settings mentioned in the code above – still doesnt work.

Long story short: I am stuck on this, i think i misunderstand the code, and can't get pathfinding to work. Any help is highly appreciated!

Many thanks in advance! Pattentrick

Offtopic 1: I think i have found a typo in the pathfinding.js. At line 2521 a variable is declared with the name tileHieight instead of tileHeight.

Offtopic 2: Is this the right place for support questions like this? Or should i post at the impact forum in the future?

collinhover commented 10 years ago

@Pattentrick you're doing things the hard way :) let Impact++ do it for you:

handleInput: function() {

    if ( ig.input.pressed('click') ) {

        this.moveTo( {
            x: ig.input.mouse.x + ig.game.screen.x,
            y: ig.input.mouse.y + ig.game.screen.y
        } );

    }

}

One thing to note, there is a small bug in the ig.Player abstract class that is currently interrupting the player pathfinding. I'll push a fix for it later today (dev branch), but if you need it immediately just change: L332 of ig.Player if using master branch or L295 of ig.Player if using dev branch to if (!this.movingTo) { and it should work as expected.

collinhover commented 10 years ago

p.s. This is a fine place to ask questions. I'm more likely to see it here than on the ImpactJS forums. Thanks for finding the typo!

Pattentrick commented 10 years ago

You sir, are a genius. Thank you collin! Just calling the moveTo method is indeed much easier :D

Now it works like expected, but unfortunately there is now this strange bug. Every 3-4 clicks the pathfinding fails and the player starts moving uncontrolled back and forth. Like he needs to pee and can't find the toilet.

I made a video about this behavior.

https://www.youtube.com/watch?v=D_N36MPHeX8

As you can see, i have no other logic inside the player entity. Do i need to set some extra properties on the player entity to get this to work? Do i need to modify the config-user.js in a certain way?

By the way, this is an empty level with just a collison, an entity and a background layer. Is an extra pathfinding layer needed for proper pathfinding?

I dont take your support on this for granted, thank you for your hard work on this!

racingcow commented 10 years ago

...and the player starts moving uncontrolled back and forth. Like he needs to pee and can't find the toilet.

Best description of a behavior ever. This made me lol. :smile:

collinhover commented 10 years ago

I noticed similar behavior on one of my enemies this morning, caused by two lines in the character's moveToPath method that were moved incorrectly. I'll push that fix to the dev branch soonish. That said, I think it is not the same issue if your level is empty. Can you turn on debugging and turn on the A* show path and take a screen shot or video when the issue happens?

Pattentrick commented 10 years ago

@racingcow Good to hear that the description made you laugh ;-)

@collinhover

I uploaded the requested video here:

http://youtu.be/XjUPv-2d7sk

I have absolutely no clue whats going on there ...

collinhover commented 10 years ago

That is strange. It appears that the atNode check isn't passing. I'll look at the methods again.

collinhover commented 10 years ago

Currently I can't reproduce this behavior in the default sidescrolling mode, but that doesn't help much since you're in top down mode. For reference, I'm using the following in my player:

handleInput: function () {

    if ( ig.input.released('tap') ) {

        this.moveTo( {
            x: ig.input.mouse.x + ig.game.screen.x,
            y: ig.input.mouse.y + ig.game.screen.y
        } );

    }

}

I'd prefer to try to fix this using your current code, if you can throw together a demo. Otherwise, I'll work on putting something together.

Pattentrick commented 10 years ago

I made a demo on github which shows the bug:

https://github.com/Pattentrick/pathfinding-bug-demo

I used Impact version 1.23. Thank you again for your support on this one!

collinhover commented 10 years ago

@Pattentrick don't think I'll be able to get to it today, but I should have time tomorrow or Tuesday at the latest. Thanks for creating the demo!

Pattentrick commented 10 years ago

@collinhover Ok, thanks!

collinhover commented 10 years ago

@Pattentrick had a second to take a quick look and it appears that you're using r5 release. I'm going to try the demo later today, then I'll switch it to r6dev and see if it makes a difference. r6dev has a bunch of pathfinding fixes and enhancements, so hopefully that'll fix it. More soon!

Pattentrick commented 10 years ago

@collinhover great! I am looking forward to it. I hope r6dev fixes this strange behavior. thanks!

collinhover commented 10 years ago

@Pattentrick yep, updating to the latest dev branch fixes the issue. On a related note: your character may do a rubber band perpendicular to the path direction if your character's speed is very high and your friction is low.

(edit: don't forget to save your original user config before updating)

Pattentrick commented 10 years ago

@collinhover i switched to r6dev. works like a charm now :D