beuleal Posted March 8, 2015 Share Posted March 8, 2015 Hey guys, im using coding tip 8 to develop a path of my enimie, so, i dislike how the sprite moves like: If the game have a long line, the sprite goes fast! and if the line is shot the sprites moves slowly... It should be oposite! It isnt? Plot function: plot: function () { var ix = 0; //var x = 1 / game.width; x = 0.0003; // x = 0; var k=0; for (var i = 0; i <= 1; i += x) { var px = this.math.catmullRomInterpolation(this.points.x, i); pxi = Math.floor(px); var py = this.math.catmullRomInterpolation(this.points.y, i); pyi = Math.floor(py); if((this.points.x[k] < (pxi +2) && this.points.x[k] > (pxi - 2)) && (this.points.y[k] < (pyi +2) && this.points.y[k] > (pyi - 2)) ) { console.log("HIT! x: " + pxi + " y: " + pyi + " dist: " + dist[k]); k++; } // if(k == dist.length-1){ // x = 2/dist[k]; // } // else // { // x = 2/dist[0]; // } var node = { x: px, y: py, angle: 0 }; if (ix > 0) { node.angle = this.math.angleBetweenPoints(this.path[ix - 1], node); } this.path.push(node); ix++; this.bmd.rect(px, py, 15, 15, 'rgba(255, 255, 255, 1)'); } for (var p = 0; p < this.points.x.length; p++) { this.bmd.rect(this.points.x[p]-3, this.points.y[p]-3, 10, 10, 'rgba(255, 0, 0, 1)'); } }, Link to comment Share on other sites More sharing options...
Tom Atom Posted March 9, 2015 Share Posted March 9, 2015 This problem is because regardless of the curve length between two points you are interpolating from 0 to 1. You are interpolating always the same time, which means that in case of short distance your moving object will move slow (it will take for example 1 second from point A to B, regardless of the length). The speed is not even constant within single node of curve - look at pixels density on first picture in Coding Tips 8. If you want to move your object at constatn speed then you can: - precalculate the curve: calculate the curve and then calculate path along it with constant distances - save the new points and move your object along it, - do it on the fly: after each step along the curve calculate distance from last position and sum it until you get distance that you need for one frame. Desired change in position should be greater or equal than any step length you get from curve calculation. Link to comment Share on other sites More sharing options...
beuleal Posted March 9, 2015 Author Share Posted March 9, 2015 I fixed it! The sprites moves point by point, so after first distance A to B, i recalculate the x value... Changed "for loop" to while loop; Link to comment Share on other sites More sharing options...
Recommended Posts