Smooth bezier path
in
Programming Questions
•
1 year ago
How to avoid acute angles and make smooth bezier path?
I need it for smooth character movement.
- int startTime;
- float durationS = 3;
- float durationMS = durationS*1000;
- float segDist = 150;
- PVector[] coords = new PVector[4];
- void setup()
- {
- size(1280, 720);
- frameRate(30);
- smooth();
- background(0);
- for (int i = 1; i < 4; ++i) {
- coords[i] = new PVector(random(width), random(height));
- }
- for (int i = 1; i < 4; ++i) {
- nextPoint();
- }
- startTime = millis();
- }
- void nextPoint()
- {
- coords[0] = coords[3];
- coords[1] = coords[2];
- // reflect about coords[0]
- coords[1].x = coords[0].x - (coords[1].x-coords[0].x);
- coords[1].y = coords[0].y - (coords[1].y-coords[0].y);
- do {
- coords[3] = new PVector(random(width), random(height));
- }
- while (dist (coords[3].x, coords[3].y, width/2, height/2) > width/3);
- float a = random(TWO_PI);
- coords[2] = new PVector(coords[3].x+cos(a)*segDist, coords[3].y+sin(a)*segDist);
- }
- void draw()
- {
- float r = (millis() - startTime)/durationMS;
- while (r > 1.0) {
- r -= 1.0;
- nextPoint();
- startTime += durationMS;
- }
- float x = bezierPoint(coords[0].x, coords[1].x, coords[2].x, coords[3].x, r);
- float y = bezierPoint(coords[0].y, coords[1].y, coords[2].y, coords[3].y, r);
- float tx = bezierTangent(coords[0].x, coords[1].x, coords[2].x, coords[3].x, r);
- float ty = bezierTangent(coords[0].y, coords[1].y, coords[2].y, coords[3].y, r);
- float a = atan2(ty, tx);
- strokeWeight(3);
- stroke(255);
- point(x,y);
- }
1