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

Pathfinding and Animation? #102

Closed Pattentrick closed 11 years ago

Pattentrick commented 11 years ago

Hi there,

now back to my favorite topic: pathfinding! I switched to impact++ r6dev, because r6 fixed a bug of mine mentioned in Issue#99. Now i think i have found another bug related to animations and pathfinding.

When my player is moving down in a straight line, for a very short moment the „down-animation“ gets interrupted by my „moveX-animation“, causing the „down-animation“ to start all over again.

Switching on debugmode reveals that the path points for a split second to the right or left. And at this moment my „moveX-animation“ shows up. The strange part is that this only occurs on a down movement. Animations in all other directions are working as expected.

I made a video about this behavior:

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

I also made a demo (i am using impact 1.23 there):

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

And here is my player class in case i messed up something:

ig.module(
    'game.entities.player'
)
.requires(
    'plusplus.abstractities.player',
    'plusplus.core.config',
    'plusplus.helpers.utils'
)
.defines(function () {

    var _c  = ig.CONFIG;
    var _ut = ig.utils;

    ig.EntityPlayer = ig.global.EntityPlayer = ig.Player.extend({

        size: {
            x: 8,
            y: 8
        },

        offset: {
            x: 3,
            y: 20
        },

        animSheet: new ig.AnimationSheet( _c.PATH_TO_MEDIA + 'player.png', 14, 30 ),

        animInit: 'idleDown',

        animSettings: {
            idleX: {
                frameTime: 1,
                sequence: [0]
            },
            idleUp: {
                frameTime: 1,
                sequence: [12]
            },
            idleDown: {
                frameTime: 1,
                sequence: [6]
            },
            moveX: {
                frameTime: 0.10,
                sequence: [0,1,2,3,4,5]
            },
            moveUp: {
                frameTime: 0.10,
                sequence: [12,13,14,15,16,17]
            },
            moveDown: {
                frameTime: 0.10,
                sequence: [6,7,8,9,10,11]
            }
        },

        /**
         * handles what to do on mouse
         * input by player
         */
        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
                });

            }

        }

    });

});

Another thing i noticed is that the animInit is not working on my player. Although i declared that the player should start with the „idleDown-animation“, it starts with the „idleX-animation“.

And how can i disable the flash (small fade in/fade out) of my player when it spawns?

As allways any help is highly appreciated! Thanks!

collinhover commented 11 years ago

Easy answers first:

As for the main issue, it looks like as your character is walking the path it is making slight adjustments to the left and right. To make a movement in any direction, a character calls the "move" + direction method, and these set the facing.x/y property accordingly. I changed the pathfinding recently to move from the corners of an entity, instead of always from the center, to improve the accuracy of the pathfinding. It looks like this change also made the facing property less stable... maybe. It is also possible that the facing is changing slightly every time a new path is found. I think it may be the latter option, but I need to test it.

A possible easy fix on your end would be to override all the "move" + direction methods to threshold the facing change. In other words, an entity can change their facing direction if (a) they currently are not moving yet this.moving === false, or (b) they must have been moving in that direction for a set number of frames.

Pattentrick commented 11 years ago

@collinhover Thank you for your answer and explaining me how the temporaryInvulnerabilityAlpha and updateCurrentAnim works! For now i will try to override the move methods to work around my main issue.

Pattentrick commented 11 years ago

I modified the "move" + direction methods to fix the facing issue. However the animation on moving down, when using pathfinding, is still broken.

It seems like that the moveLeft and moveRight methods get called repeatedly on moving down. After those calls my „idleDown-animation“ gets executed everytime, and my „moveDown-animation“ gets interrupted again.

So on moving down, my player shows his „moveDown-animation“ for a split second, then for few moments my „idleDown-animation“, after that my „moveDown-animation“ again ... and so forth.

I made a short video to showcase this behavior

http://www.youtube.com/watch?v=ACddVSCrKWg

If you look closely, sometimes the player seems to „float“. The „float moments“ are actually my „idleDown-animation“. I also updatet the demo code:

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

Fun fact: This bug only occurs on moving down! Moving in all other directions works as expected, although the "move" + direction methods are getting called there too, when walking on a straight line.

collinhover commented 11 years ago

Okay, so it looks like there are actually 2 issues:

  1. moveLeft/Right are being called occasionally, causing short flickering of animation to left and right.
  2. The player is injecting the idle animation for short periods while moving.

Is this a good summary?

Pattentrick commented 11 years ago

A good summary indeed! What confuses me the most about this issue, is that it just seems to affect the down movement.

collinhover commented 11 years ago

Small update: I've got some rather large changes internally that I need to double check for stability, then I'll dive into this fix.

collinhover commented 11 years ago

I noticed in your player you override the moveToLeft/Right methods to only allow a facing change when the character is already moving. What exactly did this fix for you?

Pattentrick commented 11 years ago

This fixed the strange facing behavior of the player on moving down, which you can see on this video:

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

collinhover commented 11 years ago

Oops, this was a silly bug. A minor change to fix some issues with entities sticking to slopes in side scrolling mode was causing the movingY property to be set to false in most cases of top-down mode. The move animation requires that the character be moving, and that property is just a combination of movingX || movingY. However, neither was true, so the idle animation was playing.

I'll be pushing this shortly and I'll include your fix to disable facing change unless the character is moving. There will be a lot of changes in this push, but I dropped the latest version into your demo and it seemed to work without changing anything. On a side note, you might consider adding a small trap to your camera (see ig.Camera.boundsTrap). Pathfinding left and right in top down mode seems to have very slight vertical shifts, which I'm looking into now.

collinhover commented 11 years ago

I noticed in your user config you have the pathfinding delay set to 0, which means your characters are going to try to find a new path every frame, and this is rather expensive. However, the pathfinding delay is behaving incorrectly, in that if the character has no path it should not delay finding a new path, so I'll fix that and include it with the upcoming push.

collinhover commented 11 years ago

You can remove PATHFINDING_DELAY:0 from your user config, as the pathfinding delay is now reset when the character starts moving to another entity or point. This means the character will find a path and start moving instantly instead of having random delays.

As mentioned above, you can remove your moveTo + direction overrides, I've included those changes in the the library (thanks for trying them).

Pattentrick commented 11 years ago

Sounds great, can't wait to get my hands on the fixed version! Thank you for your effort on this one and fixing all the issues. And thank you for including my changes in the library. That makes me actually very proud ;-)

I do like impact++ very much, keep up the good work!

collinhover commented 11 years ago

Everything should be much smoother and working as expected.