how to store mouse coordinate for later use? PVector or not.
in
Programming Questions
•
9 months ago
basically, I have an elipse that follows the mouse around. this math is no problem.
What I want to do is:
1. when ellipse comes within "x" amount of distance from mouse cursor;
a. stop following mouse
b.just take that specific mouse coordinates from that specifice frame
c.and move in that direction on a straight line ignoring actual current mouse location.
d. after another "x" amount of distance on path of line; return to following the mouse
I know how to make the ellipse move in a straight line and the logic to make it do so. but I cant figure out how to make it just keep going and ignore the current mouse location. so my math is wrong.
how can I get it to just keep going in a straight line using an old mouse position as a point of reference for the direction to move in? I cant figure out how to store a specific mouse coordinate without it always updating in the draw method.
** I have been learning a bit about PVectors and such so some of the code uses them. but its not a requirement
- PVector location,mouse;
float x,y,dx,dy;
float easing = 0.5;
void setup(){
size(500,500);
location = new PVector(width/2,height/2); //starting point
}- void draw(){
background(100);
distance();
render(); - //function that follows mouse
//updateA();
//if difference in distance is withing range do these
if(abs(dx) < 50){
if(abs(dy) < 50){
//updateC();
}//if
}//if
}//draw
void distance(){
//where is our mouse?
mouse = new PVector(mouseX,mouseY);
//distance and line between mouse and object
dx = mouse.x - location.x;
dy = mouse.y - location.y;
//visual representation of line between mouse and ellipse
stroke(255);
line(mouse.x,mouse.y,location.x,location.y);
}//distance-
void updateA(){
//follow the mouse
//convert to absolute value of dx/dy so these return true
//no matter where mouse is in relation to object. if
//values are negative the object stops moving
//if you dont do abs() you have to make two extra
//if statements for when dx/dy are less than 1
if(abs(dx) > 1){
location.x += dx*0.05;
}//if
if(abs(dy) > 1){
location.y += dy*0.05;
}//if
}//updateA- void updateC(){
// I have tried different things but how do I store a unique value for mouse.x and mouse.y with out it updating?
}//updateC- void render(){
//draw our 'enemy'
noFill();
stroke(0);
ellipse(location.x,location.y,10,10);
}//render
*__________________________________________________________________________________*
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
After working on this I have a rough and ugly BUT WORKING example. thanks to everyone!
- //Some PVectors to store our coordinates in for easy access
PVector location,mouse,distance;
//two booleans that are used to determine
//actions between mose and object
boolean isFar = true;
boolean isShort = false;
//floats to transfer data from PVectors and do math
float x,y,dx,dy,dx_Short,dy_Short;
float easing = 0.5;
void setup(){
size(500,500);
//our starting location for ellipse
location = new PVector(random(width/2),random(height/2));
}//setup- void draw(){
background(100);
trackMouse();//always track main mouse variable
render();//draw ellipse
tests();//where is mouse vs. object
//if mouse is more than 50px away
if(isFar){
distance();
updateA();
}//if
//if mouse is less than 50px away
if(isShort){
distanceShort();
}//if
}//draw - //this continually tracks and updates the mouse PVector
//so we can use that information in other methods.
void trackMouse(){
mouse = new PVector(mouseX,mouseY);
}//trackMouse - //do all our math to follow mouse
void distance(){
//distance and line between mouse and object
dx = mouse.x - location.x;//direction.x
dy = mouse.y - location.y;//direction.y
println("isFar");
println(dx + "dx");
println(dy + "dy");
}//distance - //saves a single mouse coordinate from a single frame
void distanceShort(){
//sets variables to last known values of dx, dy
//when mouse is within 50px
dx_Short = dx;
dy_Short = dy; - updateC();
println("isShort");
println(dx_Short + "dxSh");
println(dy_Short + "dySh");
}//distanceShort - //how close is the object to mouse
//change booleans appropriately
void tests(){
//are we 'x' distance away?
if(abs(mouse.x - location.x) > 50 && abs(mouse.y - location.y) > 50){
isShort = false;
isFar = true;
}//if
//did the mouse just move in a straight line up,down,left,right?
if(abs(mouse.x - location.x) >= 0 && abs(mouse.y - location.y) > 50){
isShort = false;
isFar = true;
}//if
if(abs(mouse.x - location.x) > 50 && abs(mouse.y - location.y) >= 0){
isShort = false;
isFar = true;
}//if
//are we within 'x' amount of distance?
if(abs(mouse.x - location.x) < 50 && abs(mouse.y - location.y) < 50){
isFar = false;
isShort = true;
}//if
}//tests - void updateA(){
//follow the mouse
//convert to absolute value of dx/dy so these return true
//no matter where mouse is in relation to object. if
//values are negative the object stops moving
//if you dont do abs() you have to make two extra
//if statements for when dx/dy are less than 1
if(abs(dx) > 1){
location.x += dx*0.05;
}//if
if(abs(dy) > 1){
location.y += dy*0.05;
}//if
}//updateA - void updateC(){
//captured the starting location of ellipse
//move 'x' distance from that location in
//direction of mouse in line
location.x += dx_Short * 0.1;
location.y += dy_Short * 0.1;
}//updateC - void render(){
//draw our 'enemy'
noFill();
stroke(0);
ellipse(location.x,location.y,10,10);
//draws line between object and mouse
stroke(255);
line(mouse.x,mouse.y,location.x,location.y);
}//render
1