follow the path
in
Programming Questions
•
11 months ago
Hello all,
I wish see my shape follow a specific path, this one is an ArrayList of points.
My problem is than my shape stop the move to differents coordinates points and I don't understand why, I search, but every time is very differents, depending of the coordinate. Some time just after the first point, some time after three round....
If any one have an idea, I take it now !
Thx
Stan
I wish see my shape follow a specific path, this one is an ArrayList of points.
My problem is than my shape stop the move to differents coordinates points and I don't understand why, I search, but every time is very differents, depending of the coordinate. Some time just after the first point, some time after three round....
If any one have an idea, I take it now !
Thx
Stan
- Follow fllw ;
float speed = 0.5 ;
PVector position = new PVector(-1, -1 ) ; // to initialize the position
void setup()
{
size(400,400) ; background(255) ;
fllw = new Follow() ;
fllw.Path() ; // add point to the path example
//fllw.readPointsToThePath() ; // add point from file.txt
}
void draw()
{
background(255) ;
fllw.path() ; // draw the path
position = fllw.follow(position, speed) ; // feed back the position from the class Follow
translate(position.x, position.y) ; // update the position of the Shape
shape1() ;
}
void shape1()
{
noFill() ;
ellipse(0,0, 50, 50 ) ;
}
void mousePressed()
{
// fllw.addPointToThePath() ;
} - //////////////////////////////////
- class Follow
{
// list of the keypoint, use super_class Path
ArrayList<Path> listP ;
// distance between the keypoint and the position of the translation shape
float distObjectFromStartKeyPoint = 0 ;
float distBetweenTwoKeyPoints = 0 ;
// a & b are points to calculate the direction and position of the translation to give at the shape
PVector a, b ;
// calcul the gap between a & b
PVector c = new PVector (0,0,0) ;
// speed ratio to adjust the speed xy according to position target
PVector r = new PVector (0,0,0) ;
//keypoint
PVector pos ;
//keypoint example
PVector pos1, pos2, pos3, pos4, pos5, pos6 ;
// find a good keypoint in the ArrayList
int n = 0 ;
int m = 1 ;
Follow() {
listP = new ArrayList<Path>() ;
}
PVector follow(PVector position, float speed)
{
if (listP.size() > 1 ) {
// find a good keypoint in the list
Path pthA = (Path) listP.get(n) ;
Path pthB = (Path) listP.get(m) ;
// update the position a & b from the list of keypoint
a = pthA.pos ;
b = pthB.pos ;
// give a starting position of the path, must use the same xyz than initialize PVector, here it's -1, -1
if (position.x == -1 && position.y == -1 ) { position.x = a.x ; position.y = a.y ; }
// distance between the keypoint a & b and the position of the translation shape
distBetweenTwoKeyPoints = PVector.dist(a, b ) ;
distObjectFromStartKeyPoint = PVector.dist(position, a) ;
//update the position
if ( distObjectFromStartKeyPoint < distBetweenTwoKeyPoints ) {
// calcul speed ratio
float cx = a.x -b.x ;
float cy = a.y -b.y ;
// final calcul ratio
r.x = cx /cy ;
r.y = cy /cx ;
if(abs(r.x) > abs(r.y) ) { r.x = 1.0 ; r.y = abs(r.y) ; } else { r.x = abs(r.x) ; r.y = 1.0 ; }
// Give the good direction to the translation
if ( cx == 0 ) {
position.x += 0 ;
if (a.y - b.y < 0 ) position.y += speed ; else position.y -= speed ;
}
if ( cy == 0 ) {
if (a.x - b.x < 0 ) position.x += speed ; else position.x -= speed ;
position.y += 0 ;
}
if ( cx != 0 && cy != 0 ) {
if (a.x - b.x < 0 ) position.x += (speed *r.x) ; else position.x -= (speed *r.x) ;
if (a.y - b.y < 0 ) position.y += (speed *r.y) ; else position.y -= (speed *r.y) ;
}
println ("cx: " + cx + " cy: " + cy) ;
// println("rapport: " +abs(r.x) + " / " + abs(r.y) ) ;
// println("n:"+ n + " m:" + m + " size:" +listP.size() ) ;
println("a:" + a.x + "/"+ a.y + " b:" + b.x + "/" + b.y ) ;
}
//change to the next keypoint
if ( a.x < b.x && position.x >= b.x ) { n += 1 ; m += 1 ; }
if ( a.x > b.x && position.x <= b.x ) { n += 1 ; m += 1 ; }
// to close the loop
if ( m >= listP.size() ) { n = listP.size() -1 ; m = 0 ; }
if ( n >= listP.size() ) { n = 0 ; m = 1 ; }
} else {
position.x = width /2 ;
position.y = height /2 ;
}
////////////////
return position ;
/////////////////
}
// to draw the path
void path ()
{
for (int i=0 ; i < listP.size() ; i++) {
Path pth = (Path) listP.get(i) ; // we must indicate wich one in the list or use the method above "for ( Path pth : listP )"
fill(255,25,25) ;
text(i + "///" + pth.pos.x + "/" + pth.pos.y , pth.pos.x, pth.pos.y ) ;
// text(pth.pos.x + "/" + pth.pos.y , pth.pos.x, pth.pos.y ) ;
pth.keyPoint() ;
}
}
// add point to the path from the mousePressed
void addPointToThePath()
{
pos = new PVector (mouseX, mouseY ) ;
Path pth = new Path (pos) ;
listP.add(pth) ;
}
// to read the coordinates of path from file.txt
void readPointsToThePath()
{
listP.clear() ;
String[] path = loadStrings("path.txt");
for ( int i = 0 ; i < path.length ; i++) {
String[] pt = path[i].split(",");
pos = new PVector (float(pt[0]), float(pt[1])) ;
Path pth = new Path (pos) ;
listP.add(pth) ;
// pointCount++;
}
}
// void for the example keypoint
void Path()
{
/*
float ox = width /2.0 ;
float oy = height/2.0 ;
pos1 = new PVector( ox,oy ) ;
pos2 = new PVector( ox -100, oy -50 ) ;
*/
pos3 = new PVector(100,10) ;
pos4 = new PVector(90,145) ;
pos5 = new PVector(175,170) ;
pos6 = new PVector(185,120) ;
// Path pth1 = new Path (pos1) ;
// Path pth2 = new Path (pos2) ;
Path pth3 = new Path (pos3) ;
Path pth4 = new Path (pos4) ;
Path pth5 = new Path (pos5) ;
Path pth6 = new Path (pos6) ;
// listP.add(pth1) ;
// listP.add(pth2) ;
listP.add(pth3) ;
listP.add(pth4) ;
listP.add(pth5) ;
listP.add(pth6) ;
}
} - // super class use to create an ArrayList in the class Follow
class Path
{
PVector pos ;
Path(PVector pos_ ) {
pos = pos_ ;
}
void keyPoint ()
{
strokeWeight(5) ;
point (pos.x, pos.y ) ;
}
//RETURN position of the keypoint
PVector pos() {
return pos ;
}
}
1