If you have vector
missile and another vector
target then to make missile move towards the target, you need:
1. substract missile FROM target (and thus you will get so called "direction" vector pointing from missile towards target.
2. normalize direction vector ( so it only keeps direction, but not distance to target. As we'll need in the next step set up our own approach speed)
3. multiply direction vector by desired speed (scalar); thus we will get "stepTowardsTarget" vector
4. now you can add stepTowardsTarget to missile vector to move missile closer to target.
And here's link to github:
- /**
- * This sketch shows how to use vectors to implement
- * target following.
- *
- */
- float speed = 0.6; // 5px
- PVector missile = new PVector();
- PVector target = new PVector();
- void setup(){
- size(400,400);
- }
- void draw(){
-
- fadeScreen(5);
-
- target.set(mouseX, mouseY); // to make it more fun, target is at mouse coords
-
-
-
- PVector direction = PVector.sub(target, missile);
- direction.normalize();
-
- PVector stepTowardsTarget = PVector.mult(direction, speed);
-
- missile.add(stepTowardsTarget);
-
-
- drawCircle(target);
-
- drawRect(missile);
-
- }
- void drawCircle(PVector v){
- fill(255);
- ellipse(v.x, v.y, 20, 20);
- }
- void drawRect(PVector v){
- fill(#FF0000);
- pushMatrix();
- translate(v.x, v.y);
- rectMode(CENTER);
- rotate(map(v.x, 0, width, 0, TWO_PI ));
- rect(0, 0, 30,30);
- popMatrix();
- }
- void fadeScreen(int val){
- fill(0, val);
- rectMode(CORNER);
- rect(0,0, width, height);
- }