How do I keep the box from rotating when the mouse is not being dragged?
in
Programming Questions
•
8 months ago
So i only want the box to rotate when the mouse is pressed down and I can't seem to figure out how to do it. Here's my code:
// current location
float locX;
float locY;
// target location
float targetX;
float targetY;
// starting position
float startX;
float startY;
// speed
float speed = 0.2;
// animation duration
float duration;
// animation start time
float startTime;
boolean bIsAnimating;
void setup() {
size( 500, 500 );
// set it to position
locX = width/2.0;
locY = height/2.0;
// make its target position equal to it's current position by default - no moving
targetX = locX;
targetY = locY;
startX = locX;
startY = locY;
bIsAnimating = false;
}
void draw() {
background(255);
{
float angle = atan2( mouseY - height/2, mouseX - width/2 );
translate(locX,locY);
rotate( angle );
fill(0);
rectMode(CENTER);
rect(0, 0, 50, 50);
}
if ( bIsAnimating )
{
float timePassed = (millis() - startTime)/1000.0f;
if ( timePassed < duration )
{
// use Penner easing equation to interpolate to desired end position
// if only "easeInQuad" - square starts off slow and moves fast
locX = easeInOutQuad(timePassed, startX, targetX-startX, duration);
locY = easeInOutQuad(timePassed, startY, targetY-startY, duration);
}
else
{
// we're done
PApplet.println( "done animating: " + millis() );
locX = targetX;
locY = targetY;
bIsAnimating = false;
}
}
// noStroke();
// fill( 0);
// rectMode( CENTER );
//
// rect( locX, locY, 50, 50 );
}
void mouseDragged() {
// new target
targetX = mouseX;
targetY = mouseY;
duration = 0.1; // take 2 seconds
startX = locX;
startY = locY;
// get the animation's start time
// millis() returns how many milliseconds have passed since the application started running
startTime = millis();
PApplet.println( "startTime: " + startTime );
bIsAnimating = true;
}
// Rober Penner's easing equations
public float easeInQuad(float t, float b, float c, float d)
{
return c*(t/=d)*t + b;
}
public float easeOutQuad(float t, float b, float c, float d)
{
return -c * (t/=d)*(t-2) + b;
}
public float easeInOutQuad(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}
1