Javascript - Homing bullet -
i've been working on little project, , while can bullet home in on mouse, has strange behavior.
if bullet going left/right/up/down mouse, change direction , forth , continue fly away mouse instead of turning @ it. also, makes loops unnecessary, figure that's because i'm not keeping direction positive or negative (i wrong though).
http://codepen.io/evanward/pen/voegdo?editors=001
this code have updating singular bullet.
var target = app.mouse.subtract(app.bullet.pos), angle = target.angle(); if(angle < 0) { angle += math.pi * 2; } var delta = angle - app.bullet.dir; if(math.abs(delta) > 180) { if(delta < 0) { delta += 180; delta *= -1; } else { delta -= 180; delta *= -1; } } if(app.bullet.dir > math.pi * 2) { app.bullet.dir -= math.pi * 2; } app.bullet.dir += delta/10; app.bullet.pos.x += math.cos(app.bullet.dir) * app.bullet.vel; app.bullet.pos.y += math.sin(app.bullet.dir) * app.bullet.vel;
Comments
Post a Comment