Basically, When you click the mouse, I want the bullet to move in that direction. When you move the mouse, the bullets direction will never change.
So that would be gun aiming, more or less.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Bullet { float velocityX, velocityY, positionX, positionY; }
private void directBulletToPoint(Bullet b, float x, float y, float speed) { float dx = x - b.positionX; float dy = y - b.positionY; float len = Math.sqrt(dx*dx+dy*dy); b.velocityX = dx / len * speed; b.velocityY = dy / len * speed; } |
Simple vector stuff:
Get displacement vector
Normalize
Multiply by speed
Programmed in post, may contain errors.
Also somebody correct me if I screwed something up.