Sunday, 15 March 2015

javascript - Why won't my sprite/entity move in a straight line? -



javascript - Why won't my sprite/entity move in a straight line? -

my entity supposed move in straight line toward mouse. it's close, not quite there yet. here's working demo show mean.

and here's screenshot: reddish represents path mouse took. can see, entity not take same path.

relevant code:

entityplayer = ig.entity.extend({ movementspeed: 400, update: function() { this.parent(); this.move_toward_coord(ig.input.mouse.x, ig.input.mouse.y); }, move_toward_coord: function(x, y) { var distance_to_target_x = x - this.pos.x - this.size.x / 2; var distance_to_target_y = y - this.pos.y - this.size.y / 2; if(math.abs(distance_to_target_x) > 1 || math.abs(distance_to_target_y) > 1) { this.vel.x = (distance_to_target_x > 1 ? 1 : -1) * this.movementspeed * (math.abs(distance_to_target_x) / (math.abs(distance_to_target_x) + math.abs(distance_to_target_y))); this.vel.y = (distance_to_target_y > 1 ? 1 : -1) * this.movementspeed * (math.abs(distance_to_target_y) / (math.abs(distance_to_target_x) + math.abs(distance_to_target_y))); } else { this.vel.y = 0; this.vel.x = 0; } } });

i suspect there wrong move_to_coord method, after tweaking many hours, i'm still not sure is...

why doesn't ship travel in straight line?

ughh!! figured out literally seconds after posted this. sorry, bad. because of property called maxvel limiting speed on either x or y velocity, 1 more other. >.<

javascript math geometry game-engine impactjs

No comments:

Post a Comment