javascript - CraftyJS how to calculate vector between 2 sprites? -


i noob in game programming , not @ maths, trying write 1945 style shooting game, been far in bottle neck cannot figure out how make enemy aim @ player.

lets have enemy sprite , player sprite, how find out angle , path? sounds calculating vector between 2 points, have been reading documentation , particularly link http://craftyjs.com/api/crafty-math-vector2d.html

i cannot figure out how it, have tried following

var enemyv = crafty.math.vector2d(enemy.x, enemy.y); var playerv = crafty.math.vector2d(player.x, player.y); var angle = enemyv.angleto(playerv); 

the value of angle between -3 3, doesn't seem right angles @ all.

i hope has craftyjs experience can me out here.

angleto function returns radian value, running give actual angle degreex crafty.math.radtodeg(radianvalue)

to aim player , make bullet travel @ direction difference between 2 points bullet.x - player.x'bullet.y - player.y' apply incremental rate such below (

bullet.x_diff = (target.x - bullet.x)*0.02; bullet.y_diff = (target.y - bullet.y)*0.02; 

then inside enterframe loop:

this.x += this.x_diff; this.y += this.y_diff; 

once idea, should normalize diff dividing distance between points.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -