We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! Need help with PVectors. Here is my sketch, and you can see orange points and a red brush in it. The question is - how to make brush move these points (on mouse pressed), using PVectors. And I want this movement to be smooth - if the point is near the center of the brush, point moves 100%, and if the point is near the border of the brush, the movement power is fading.
void setup()
{
size(700,700);
}
void draw()
{
background(#75849D);
for(int i=10;i<width;i=i+40)
{
for(int j=10;j<height;j=j+40)
{
PVector tempPos = new PVector(i,j);
Point p1 = new Point(tempPos);
p1.display();
}
}
Brush br1 = new Brush();
br1.display();
}
class Point
{
PVector pos;
Point(PVector posIn)
{
pos = posIn;
}
void display()
{
noStroke();
fill(#DBA350);
ellipse(pos.x,pos.y,5,5);
}
}
class Brush
{
int size = 200;
void display()
{
noFill();
stroke(255,0,0);
ellipse(mouseX,mouseY,size,size);
ellipse(mouseX,mouseY,size/2,size/2);
ellipse(mouseX,mouseY,2,2);
}
}
Answers
First, you are creating your points every single draw loop. Create them in
setup()
, then loop through them indraw()
.Second, you in order to refer to a list of your points you need to save a list (an array, or an ArrayList) of your points! You are creating each point with the same name, then throwing those names away. See the
objects
andclass
demos and examples in the Processing Reference.Third, you say "if the point is near the center of the brush." How would you write that line, in a line of code? "if" "the point" "is near" "the center of the brush". Then were would you put that line?
jeremydouglass, thanks for help, I already did some successful steps. Of course, I need to initialize an ArrayList of class instances in setup, maybe a few weeks without programming makes me forgot something)) Now working on smooth movement, trying to use dist(), but got no result yet. I'll post my code later.
@djevazi -- glad to hear that you are making progress. Good luck with it!
Keep in mind that you can also use
map()
andlerp()
if you are trying to scale or interpolate sets of values.these two function I still don't understand, but I'll try to! thanx!
Look at the Interpolate Example and the Map Example to help you understand those functions.
@djeazi
Those functions are not difficult to understand and are very very helpful. I will strongly suggest to become familiar with them.
For example for map, imagine you want the user to input a number from 0 to 9 and you want your background color to be shades of gray (including black and white) based on theuser's input. If 0 then your background is black, if the input were 5 then it is a gray color right btw black and white. If 9 then your background is white. You do that with the following code:
Kf
Thanks for example, will try to use it in my work
Code update
@djevazi -- does "code update" mean that everything is now working for you?
@jeremydouglass -- I'm not quite happy about the points behaviour while moving (they running to the border of the brush too much), but that's a great start for experimenting and improving! I think that the task is solved, but if you have any suggestions about improving the code, please, feel free to post.