Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

tower defense bullet following enemy with range javascript

//Please note, that this is not my script, it was made by a guy from StackOverflow named "gnarf"
//Thanks

// this is a "constant"  - representing 10px motion per "time unit"
var bulletSpeed = 10; 
// calculate the vector from our center to their center
var enemyVec = vec_sub(targetSprite.getCenter(), originSprite.getCenter());
// measure the "distance" the bullet will travel
var dist = vec_mag(enemyVec);
// adjust for target position based on the amount of "time units" to travel "dist"
// and the targets speed vector
enemyVec = vec_add(enemyVec, vec_mul(targetSprite.getSpeed(), dist/bulletSpeed));
// calculate trajectory of bullet
var bulletTrajectory = vec_mul(vec_normal(enemyVec), bulletSpeed);
// assign values
bulletSprite.speedX = bulletTrajectory.x;  
bulletSprite.speedY = bulletTrajectory.y;  

// functions used in the above example:

// getCenter and getSpeed return "vectors"
sprite.prototype.getCenter = function() { 
  return {
    x: this.x+(this.width/2), 
    y: this.y+(this.height/2) 
  }; 
};

sprite.prototype.getSpeed = function() { 
  return {
    x: this.speedX, 
    y: this.speedY 
  }; 
};

function vec_mag(vec) { // get the magnitude of the vector
  return Math.sqrt( vec.x * vec.x + vec.y * vec.y); 
 }
function vec_sub(a,b) { // subtract two vectors
  return { x: a.x-b.x, y: a.y-b.y };
}
function vec_add(a,b) { // add two vectors
  return { x: a.x + b.x, y: a.y + b.y };
}
function vec_mul(a,c) { // multiply a vector by a scalar
  return { x: a.x * c, y: a.y * c };
}
function vec_div(a,c) { // divide == multiply by 1/c
  return vec_mul(a, 1.0/c);
}
function vec_normal(a) { // normalize vector
  return vec_div(a, vec_mag(a)); 
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: object mapper pretty write as string 
Javascript :: material ui css supports 
Javascript :: node load testing-basic 
Javascript :: trie node pseudocode 
Javascript :: firebase is there a way to rename a document 
Javascript :: javascript reduce array of objects group by property 
Javascript :: for await range javascript 
Javascript :: markdown config 
Javascript :: vue2-editor save image 
Javascript :: module scope javascript 
Javascript :: vscode decrease window tab 
Javascript :: nodejs s3 read 
Javascript :: Timestamp Format: 23.12.2015 08:34:50 
Javascript :: nodejs cors 
Javascript :: hide show jquery 
Javascript :: what is runtime in javascript 
Javascript :: how to parse arguments to a promise in javascript 
Javascript :: javascript capitalize every word in a string 
Javascript :: javascript set default button form 
Javascript :: "Perform native operation by javascript in Android" 
Javascript :: jquery show div class 
Javascript :: How to handle protected routes in React plus redirect user to original URL being visited 
Javascript :: nextjs update ui when data is updated 
Javascript :: &nbsp replace javascript 
Javascript :: dict equivalent js 
Javascript :: re-resizable react example 
Javascript :: react with two components render empty 
Javascript :: converting jsObject to JSON 
Javascript :: how to iterate through linked list javascript 
Javascript :: disable submit button until form is fully validated 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =