Missile with wobble
back
Source
/* @pjs preload="rocket.png"; */
PImage rocket;
float SPEED = 2.5;
float TURN_RATE = 0.125;
float WOBBLE_LIMIT = 0.25;
float WOBBLE_SPEED = 0.75;
// Create a variable called wobble that tweens back and forth between
// -WOBBLE_LIMIT and +WOBBLE_LIMIT forever
float wobble = WOBBLE_LIMIT;
float wobble_direction = 1;
float rocket_x;
float rocket_y;
float velocity_x;
float velocity_y;
float rotation;
// Setup the example
void setup(){
size(640, 480);
imageMode(CENTER);
// Load images
rocket = loadImage("rocket.png");
}
// The draw() method is called every frame
void draw(){
background(#4488cc);
// Calculate the angle from the missile to the mouse cursor game.input.x
// and game.input.y are the mouse position; substitute with whatever
// target coordinates you need.
float targetRotation = atan2(mouseY - rocket_y, mouseX - rocket_x);
// Add our "wobble" factor to the targetRotation to make the missile wobble
targetRotation += wobble;
wobble = lerp(wobble, WOBBLE_LIMIT * wobble_direction, WOBBLE_SPEED);
if(wobble == WOBBLE_LIMIT * wobble_direction)
wobble_direction *= -1;
// Gradually (this.TURN_RATE) aim the missile towards the target angle
if(rotation != targetRotation){
// Calculate difference between the current angle and targetAngle
float delta = targetRotation - rotation;
// Keep it in range from -180 to 180 to make the most efficient turns.
if(delta > PI) delta -= TWO_PI;
if(delta < -PI) delta += TWO_PI;
if(delta > 0){
// Turn clockwise
rotation += TURN_RATE;
}else{
// Turn counter-clockwise
rotation -= TURN_RATE;
}
// Just set angle to target angle if they are close
if(abs(delta) < radians(TURN_RATE)){
rotation = targetRotation;
}
}
// Calculate velocity vector based on rotation and this.SPEED
velocity_x = cos(rotation) * SPEED;
velocity_y = sin(rotation) * SPEED;
rocket_x += velocity_x;
rocket_y += velocity_y;
pushMatrix();
translate(rocket_x, rocket_y);
rotate(rotation);
image(rocket, 0, 0);
popMatrix();
}