Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by SnakePlissken

So I am having a very weird problem while working on getting an entity to move to a certain pixel destination. What happens is when I click the entity tile that my main "Knight" entity wants to move to I find the tiles selected pos.x and the knight pos.x and increase the vel on the knight until it reaches the selected pos.x

Now the trouble I run into is the fact that vel.x will give decimal numbers. So my first thought was why not just update it so it becomes an integer before the if statement to see if it is at its destination.

Well that did not work and would stop my 'knight'. Then here is the strange part. While debugging in chrome I found out that the code I am using is working correctly. So it works while debugging but not while live.

Here is code everything being done at the destination entity tile.

update: function() {
            var currentXFloor;
            var currentYFloor;
            
            var CurrentXCeiling;
            var currentYCeiling;
            
            var PositionToInt;
    
            currentXFloor = this.pos.x;
            currentYFloor = this.pos.y;
            
            currentXCeiling = currentXFloor + 25;
            currentYCeiling = currentYFloor + 25;
            
            
            if((ig.input.pressed('click')) && (ig.input.mouse.y <= currentYCeiling && ig.input.mouse.y >= currentYFloor ) &&
               (ig.input.mouse.x <= currentXCeiling && ig.input.mouse.x >= currentXFloor) )
            {
                ig.global.SelectedXPos = this.pos.x;
                ig.global.SelectedYPos = this.pos.y;
                
                alert(ig.global.SelectedXPos + ', ' + ig.global.SelectedYPos);
                
                if (ig.global.SelectedKnight.pos.x > ig.global.SelectedXPos)
                {
                    ig.global.SelectedKnight.vel.x = -25;
                }
                else if (ig.global.SelectedKnight.pos.x < ig.global.SelectedXPos)
                {
                    ig.global.SelectedKnight.vel.x = 25;
                }
                
                ig.global.SelectedKnight.currentAnim = ig.global.SelectedKnight.anims.move;
                
                this.kill();
            }
            
            ig.global.SelectedKnight.pos.x = ig.global.SelectedKnight.pos.x.toInt();

            if(ig.global.SelectedKnight.pos.x == ig.global.SelectedXPos)
            {
                ig.global.SelectedKnight.vel.x = 0;
                ig.global.SelectedKnight.currentAnim = ig.global.SelectedKnight.anims.idle;
            }
}

1 decade ago by Arantor

Well... an entity's vel.x is multiplied by the per-frame tick counter to get the per-frame distance; vel of 25 means 25 pixels per second, which is where you get the floating point value from.

Now, what I'd imagine is happening is that it actually ends up rounding in such a way that it ends up overshooting the destination point, because the velocity is applied more often and with a greater chance of rounding errors creeping in. During debug, less frames are actually run (in almost every case; the debugging process adds an overhead)

I'm also not entirely clear why it's being done with an entity, and specifically what benefit there is to spawning an entity when a click is occurring, just to have a check routine when you're not using that entity for anything else (if it were being used for collision purposes, I could understand it, but it doesn't seem like it) - and to be honest, this is logic that applies to the player and probably should go in the player entity in the first place...

1 decade ago by SnakePlissken

Thanks for the reply Arantor the entities are being spawned on click to show the user where the "Knight" entity can move on the tile map. You were right in that the player logic should remain in on the player and that helped solved some problems while killing off the Entities to show where the "Knight" could move It would not run the rest of the update logic due to there not being an entity there to run it.

Would rounding errors really be caused in the matter of 25 pixels? Also I am using toInt and not the round method.

So should I put some Logic into my rounding to prevent it form going past the Selected Entities X and Y?

1 decade ago by Arantor

25 pixels? Certainly. Let's say it's 50 FPS to keep the maths simple.

25 pps = 0.5 pixels per frame assuming perfect rendering and no lag. When you're lagging due to debug, you're still doing 0.5 pixels per frame but you're not doing every frame, meaning that you stand a better chance of actually ending up where you're supposed to be.

Also, I'd be very wary of the behaviour of toInt(). You can't guarantee it will work how you necessarily think it will work - even after examining the definition of toInt() in Impact.js where it extends the Number prototype, I cannot tell you reliably how it will behave. If you have a floating point number, binary-or'd with 0, how is that going to react? Will it truncate the floating point, will it round, will it push-to-ceiling?
Page 1 of 1
« first « previous next › last »